|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os/exec" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/aerogear/mobile-cli/pkg/apis/servicecatalog/v1beta1" |
| 10 | +) |
| 11 | + |
| 12 | +const integrationTestPath = "createIntegrationTestData/" |
| 13 | + |
| 14 | +func TestCreateIntegration(t *testing.T) { |
| 15 | + fhSyncServer := &ProvisionServiceParams{ |
| 16 | + Name: "fh-sync-server", |
| 17 | + Namespace: fmt.Sprintf("--namespace=%s", *namespace), |
| 18 | + Params: []string{ |
| 19 | + "-p MONGODB_USER_NAME=fhsync", |
| 20 | + "-p MONGODB_USER_PASSWORD=fhsyncpass", |
| 21 | + "-p MONGODB_ADMIN_PASSWORD=pass", |
| 22 | + }, |
| 23 | + } |
| 24 | + |
| 25 | + keycloak := &ProvisionServiceParams{ |
| 26 | + Name: "keycloak", |
| 27 | + Namespace: fmt.Sprintf("--namespace=%s", *namespace), |
| 28 | + Params: []string{ |
| 29 | + "-p ADMIN_NAME=admin", |
| 30 | + "-p ADMIN_PASSWORD=pass", |
| 31 | + }, |
| 32 | + } |
| 33 | + |
| 34 | + // create fh-sync-server service instance |
| 35 | + createInstance(t, fhSyncServer) |
| 36 | + fhSyncID := getInstanceID(t, fhSyncServer) |
| 37 | + |
| 38 | + // create keycloak service instance |
| 39 | + createInstance(t, keycloak) |
| 40 | + keycloakID := getInstanceID(t, keycloak) |
| 41 | + |
| 42 | + tests := []struct { |
| 43 | + name string |
| 44 | + fhSyncID string |
| 45 | + keycloakID string |
| 46 | + fixture string |
| 47 | + validate func(t *testing.T, sb *v1beta1.ServiceBinding) |
| 48 | + }{ |
| 49 | + { |
| 50 | + name: "missing/incorrect arguments", |
| 51 | + fhSyncID: "", |
| 52 | + keycloakID: "", |
| 53 | + fixture: "missing-args.golden", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "create integration returns ready status", |
| 57 | + fhSyncID: fhSyncID, |
| 58 | + keycloakID: keycloakID, |
| 59 | + validate: func(t *testing.T, sb *v1beta1.ServiceBinding) { |
| 60 | + expectedType := "Ready" |
| 61 | + expectedStatus := "True" |
| 62 | + |
| 63 | + if actualType := string(sb.Status.Conditions[0].Type); actualType != expectedType { |
| 64 | + t.Fatalf("Expected condition type to be '%s' but got '%s'", expectedType, actualType) |
| 65 | + } |
| 66 | + |
| 67 | + if actualStatus := string(sb.Status.Conditions[0].Status); actualStatus != expectedStatus { |
| 68 | + t.Fatalf("Expected condition status to be '%s' but got '%s'", expectedStatus, actualStatus) |
| 69 | + } |
| 70 | + }, |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + for _, test := range tests { |
| 75 | + t.Run(test.name, func(t *testing.T) { |
| 76 | + // create service binding |
| 77 | + args := []string{"create", "integration", test.fhSyncID, test.keycloakID, "--namespace=" + *namespace} |
| 78 | + cmd := exec.Command(*executable, args...) |
| 79 | + output, err := cmd.CombinedOutput() |
| 80 | + if err != nil { |
| 81 | + t.Fatalf("Failed to create binding: %v", err) |
| 82 | + } |
| 83 | + if *update { |
| 84 | + WriteSnapshot(t, integrationTestPath+test.fixture, output) |
| 85 | + } |
| 86 | + |
| 87 | + if test.fixture != "" { |
| 88 | + actual := string(output) |
| 89 | + expected := LoadSnapshot(t, integrationTestPath+test.fixture) |
| 90 | + if actual != expected { |
| 91 | + t.Fatalf("actual = \n%s, expected = \n%s", actual, expected) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + sb := getBinding(t) |
| 96 | + if test.validate != nil { |
| 97 | + test.validate(t, sb) |
| 98 | + } |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func createInstance(t *testing.T, si *ProvisionServiceParams) { |
| 104 | + args := []string{"create", "serviceinstance", si.Name, si.Namespace} |
| 105 | + args = append(args, si.Params...) |
| 106 | + cmd := exec.Command(*executable, args...) |
| 107 | + |
| 108 | + output, err := cmd.CombinedOutput() |
| 109 | + if err != nil { |
| 110 | + t.Fatalf("Failed to create service instance %s: %v", si.Name, err) |
| 111 | + } |
| 112 | + |
| 113 | + fmt.Println(string(output)) |
| 114 | +} |
| 115 | + |
| 116 | +func getInstanceID(t *testing.T, si *ProvisionServiceParams) (id string) { |
| 117 | + args := []string{"get", "serviceinstances", si.Name, si.Namespace, "-o=json"} |
| 118 | + cmd := exec.Command(*executable, args...) |
| 119 | + |
| 120 | + output, err := cmd.CombinedOutput() |
| 121 | + if err != nil { |
| 122 | + t.Fatalf("Failed to get instances for %s: %v", si.Name, err) |
| 123 | + } |
| 124 | + |
| 125 | + siList := &v1beta1.ServiceInstanceList{} |
| 126 | + if err = json.Unmarshal(output, siList); err != nil { |
| 127 | + t.Fatal("Unexpected error unmarshalling service instance list:", err) |
| 128 | + } |
| 129 | + |
| 130 | + return siList.Items[0].ObjectMeta.Name |
| 131 | +} |
| 132 | + |
| 133 | +func getBinding(t *testing.T) *v1beta1.ServiceBinding { |
| 134 | + args := []string{"get", "integrations", "--namespace=" + *namespace, "-o=json"} |
| 135 | + cmd := exec.Command(*executable, args...) |
| 136 | + |
| 137 | + output, err := cmd.CombinedOutput() |
| 138 | + if err != nil { |
| 139 | + t.Fatal("Failed to get integrations:", err) |
| 140 | + } |
| 141 | + |
| 142 | + sbList := &v1beta1.ServiceBindingList{} |
| 143 | + if err = json.Unmarshal(output, sbList); err != nil { |
| 144 | + t.Fatal("Unexpected error unmarshalling service bindings list:", err) |
| 145 | + } |
| 146 | + |
| 147 | + return &sbList.Items[0] |
| 148 | +} |
0 commit comments