Browse code

Merge pull request #82 from mfojtik/go_fixes

Merged by openshift-bot

OpenShift Bot authored on 2014/09/12 23:24:12
Showing 2 changed files
... ...
@@ -26,7 +26,7 @@ func Apply(data []byte, storage clientapi.ClientMappings) (errs errors.ErrorList
26 26
 	}
27 27
 
28 28
 	for _, item := range conf.Items {
29
-		kind, itemId, parseErrs := parseKindAndId(item)
29
+		kind, itemID, parseErrs := parseKindAndID(item)
30 30
 		if len(parseErrs) != 0 {
31 31
 			errs = append(errs, parseErrs...)
32 32
 			continue
... ...
@@ -34,17 +34,17 @@ func Apply(data []byte, storage clientapi.ClientMappings) (errs errors.ErrorList
34 34
 
35 35
 		client, path := getClientAndPath(kind, storage)
36 36
 		if client == nil {
37
-			errs = append(errs, fmt.Errorf("The resource %s is not a known type - unable to create %s", kind, itemId))
37
+			errs = append(errs, fmt.Errorf("The resource %s is not a known type - unable to create %s", kind, itemID))
38 38
 			continue
39 39
 		}
40 40
 
41 41
 		// Serialize the single Config item back into JSON
42
-		itemJson, _ := json.Marshal(item)
42
+		itemJSON, _ := json.Marshal(item)
43 43
 
44
-		request := client.Verb("POST").Path(path).Body(itemJson)
44
+		request := client.Verb("POST").Path(path).Body(itemJSON)
45 45
 		_, err := request.Do().Get()
46 46
 		if err != nil {
47
-			errs = append(errs, fmt.Errorf("[%s#%s] Failed to create: %v", kind, itemId, err))
47
+			errs = append(errs, fmt.Errorf("[%s#%s] Failed to create: %v", kind, itemID, err))
48 48
 		}
49 49
 	}
50 50
 
... ...
@@ -55,16 +55,16 @@ func Apply(data []byte, storage clientapi.ClientMappings) (errs errors.ErrorList
55 55
 // kind.
56 56
 func getClientAndPath(kind string, mappings clientapi.ClientMappings) (client clientapi.RESTClient, path string) {
57 57
 	for k, m := range mappings {
58
-		if k == kind {
58
+		if m.Kind == kind {
59 59
 			return m.Client, k
60 60
 		}
61 61
 	}
62 62
 	return
63 63
 }
64 64
 
65
-// parseKindAndId extracts the 'kind' and 'id' fields from the Config item JSON
65
+// parseKindAndID extracts the 'kind' and 'id' fields from the Config item JSON
66 66
 // and report errors if these fields are missing.
67
-func parseKindAndId(item interface{}) (kind, id string, errs errors.ErrorList) {
67
+func parseKindAndID(item interface{}) (kind, id string, errs errors.ErrorList) {
68 68
 	itemMap := item.(map[string]interface{})
69 69
 
70 70
 	kind, ok := itemMap["kind"].(string)
... ...
@@ -83,6 +83,6 @@ func parseKindAndId(item interface{}) (kind, id string, errs errors.ErrorList) {
83 83
 // reportError provides a human-readable error message that include the Config
84 84
 // item JSON representation.
85 85
 func reportError(item interface{}, message string) error {
86
-	itemJson, _ := json.Marshal(item)
87
-	return fmt.Errorf(message+": %s", string(itemJson))
86
+	itemJSON, _ := json.Marshal(item)
87
+	return fmt.Errorf(message+": %s", string(itemJSON))
88 88
 }
... ...
@@ -17,23 +17,19 @@ func TestParseKindAndItem(t *testing.T) {
17 17
 		t.Errorf("Failed to parse Config: %v", err)
18 18
 	}
19 19
 
20
-	kind, itemId, err := parseKindAndId(conf.Items[0])
20
+	kind, itemID, err := parseKindAndID(conf.Items[0])
21 21
 	if len(err) != 0 {
22 22
 		t.Errorf("Failed to parse kind and id from the Config item: %v", err)
23 23
 	}
24 24
 
25
-	if kind != "Service" && itemId != "frontend" {
26
-		t.Errorf("Invalid kind and id, should be Service and frontend: %s, %s", kind, itemId)
25
+	if kind != "Service" && itemID != "frontend" {
26
+		t.Errorf("Invalid kind and id, should be Service and frontend: %s, %s", kind, itemID)
27 27
 	}
28 28
 }
29 29
 
30 30
 func TestApply(t *testing.T) {
31
-	invalidData := []byte(`{"items": [ { "foo": "bar" } ]}`)
32
-	invalidConf := configJSON{}
33
-	if err := json.Unmarshal(invalidData, &invalidConf); err != nil {
34
-		t.Errorf("Failed to parse Config: %v", err)
35
-	}
36 31
 	clients := clientapi.ClientMappings{}
32
+	invalidData := []byte(`{"items": [ { "foo": "bar" } ]}`)
37 33
 	errs := Apply(invalidData, clients)
38 34
 	if len(errs) == 0 {
39 35
 		t.Errorf("Expected missing kind field for Config item, got %v", errs)
... ...
@@ -44,22 +40,30 @@ func TestApply(t *testing.T) {
44 44
 	}
45 45
 }
46 46
 
47
+func TestGetClientAndPath(t *testing.T) {
48
+	kubeClient, _ := kubeclient.New("127.0.0.1", nil)
49
+	testClientMappings := clientapi.ClientMappings{
50
+		"pods":     {"Pod", kubeClient.RESTClient},
51
+		"services": {"Service", kubeClient.RESTClient},
52
+	}
53
+	client, path := getClientAndPath("Service", testClientMappings)
54
+	if client != kubeClient.RESTClient {
55
+		t.Errorf("Failed to get client for Service")
56
+	}
57
+	if path != "services" {
58
+		t.Errorf("Failed to get path for Service")
59
+	}
60
+}
61
+
47 62
 func ExampleApply() {
48 63
 	kubeClient, _ := kubeclient.New("127.0.0.1", nil)
49
-	clients := clientapi.ClientMappings{
50
-		"pods": {
51
-			Kind:   "Pod",
52
-			Client: kubeClient.RESTClient,
53
-		},
54
-		"services": {
55
-			Kind:   "Service",
56
-			Client: kubeClient.RESTClient,
57
-		},
64
+	testClientMappings := clientapi.ClientMappings{
65
+		"pods":     {"Pod", kubeClient.RESTClient},
66
+		"services": {"Service", kubeClient.RESTClient},
58 67
 	}
59 68
 	data, _ := ioutil.ReadFile("../../examples/guestbook/config.json")
60
-	errs := Apply(data, clients)
69
+	errs := Apply(data, testClientMappings)
61 70
 	fmt.Println(errs)
62 71
 	// Output:
63
-	// [The resource Service is not a known type - unable to create frontend The resource Service is not a known type - unable to create redismaster The resource Service is not a known type - unable to create redisslave The resource Pod is not a known type - unable to create redis-master-2 The resource ReplicationController is not a known type - unable to create frontendController The resource ReplicationController is not a known type - unable to create redisSlaveController]
64
-	//
72
+	// [[Service#frontend] Failed to create: Post http://127.0.0.1/api/v1beta1/services: dial tcp 127.0.0.1:80: connection refused [Service#redismaster] Failed to create: Post http://127.0.0.1/api/v1beta1/services: dial tcp 127.0.0.1:80: connection refused [Service#redisslave] Failed to create: Post http://127.0.0.1/api/v1beta1/services: dial tcp 127.0.0.1:80: connection refused [Pod#redis-master-2] Failed to create: Post http://127.0.0.1/api/v1beta1/pods: dial tcp 127.0.0.1:80: connection refused The resource ReplicationController is not a known type - unable to create frontendController The resource ReplicationController is not a known type - unable to create redisSlaveController]
65 73
 }