|
| 1 | +package frontend |
| 2 | + |
| 3 | +// Copyright (c) Microsoft Corporation. |
| 4 | +// Licensed under the Apache License 2.0. |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/golang/mock/gomock" |
| 14 | + "github.com/openshift/hive/apis/hiveinternal/v1alpha1" |
| 15 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 16 | + |
| 17 | + "github.com/Azure/ARO-RP/pkg/api" |
| 18 | + "github.com/Azure/ARO-RP/pkg/metrics/noop" |
| 19 | + mock_env "github.com/Azure/ARO-RP/pkg/util/mocks/env" |
| 20 | + mock_hive "github.com/Azure/ARO-RP/pkg/util/mocks/hive" |
| 21 | +) |
| 22 | + |
| 23 | +func TestGetAdminHiveSyncsetResources(t *testing.T) { |
| 24 | + fakeNamespace := "aro-00000000-0000-0000-0000-000000000000" |
| 25 | + ctx := context.Background() |
| 26 | + clusterSyncsetTest := &v1alpha1.ClusterSync{ |
| 27 | + ObjectMeta: metav1.ObjectMeta{ |
| 28 | + Name: "clustersync1", |
| 29 | + Namespace: fakeNamespace, |
| 30 | + }, |
| 31 | + } |
| 32 | + |
| 33 | + type test struct { |
| 34 | + name string |
| 35 | + namespace string |
| 36 | + hiveEnabled bool |
| 37 | + mocks func(*test, *mock_hive.MockClusterManager) |
| 38 | + wantStatusCode int |
| 39 | + wantResponse []byte |
| 40 | + wantError string |
| 41 | + } |
| 42 | + |
| 43 | + for _, tt := range []*test{ |
| 44 | + { |
| 45 | + name: "Cluster SyncSets must be namespaced", |
| 46 | + namespace: "", |
| 47 | + hiveEnabled: true, |
| 48 | + mocks: func(tt *test, s *mock_hive.MockClusterManager) {}, |
| 49 | + wantStatusCode: http.StatusInternalServerError, |
| 50 | + wantError: "500: InternalServerError: : hive is not enabled", //"404: NotFound: : hive is not enabled", |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "List ClusterSync resources successfully", |
| 54 | + namespace: "hive", |
| 55 | + wantError: "", |
| 56 | + mocks: func(tt *test, s *mock_hive.MockClusterManager) { |
| 57 | + s.EXPECT(). |
| 58 | + GetSyncSetResources(gomock.Any(), gomock.Any()). |
| 59 | + Return(&clusterSyncsetTest, nil).Times(1) |
| 60 | + }, |
| 61 | + wantStatusCode: http.StatusOK, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "Hive is not enabled", |
| 65 | + namespace: fakeNamespace, |
| 66 | + mocks: nil, |
| 67 | + hiveEnabled: false, |
| 68 | + wantStatusCode: http.StatusInternalServerError, |
| 69 | + wantError: "500: InternalServerError: : hive is not enabled", |
| 70 | + }, |
| 71 | + } { |
| 72 | + t.Run(tt.name, func(t *testing.T) { |
| 73 | + ti := newTestInfra(t).WithOpenShiftClusters().WithSubscriptions() |
| 74 | + defer ti.done() |
| 75 | + |
| 76 | + _env := ti.env.(*mock_env.MockInterface) |
| 77 | + var f *frontend |
| 78 | + var err error |
| 79 | + if tt.hiveEnabled { |
| 80 | + s := mock_hive.NewMockClusterManager(ti.controller) //NewMockSyncSetResourceManager(ti.controller) |
| 81 | + tt.mocks(tt, s) |
| 82 | + f, err = NewFrontend(ctx, ti.audit, ti.log, _env, ti.dbGroup, api.APIs, &noop.Noop{}, &noop.Noop{}, nil, nil, nil, nil, nil, nil) |
| 83 | + } else { |
| 84 | + f, err = NewFrontend(ctx, ti.audit, ti.log, _env, ti.dbGroup, api.APIs, &noop.Noop{}, &noop.Noop{}, nil, nil, nil, nil, nil, nil) |
| 85 | + } |
| 86 | + if err != nil { |
| 87 | + t.Fatal(err) |
| 88 | + } |
| 89 | + |
| 90 | + clusterSyncSet, err := f._getAdminHiveSyncsetResources(ctx, tt.namespace) |
| 91 | + cloudErr, isCloudErr := err.(*api.CloudError) |
| 92 | + if tt.wantError != "" && isCloudErr && cloudErr != nil { |
| 93 | + if tt.wantError != cloudErr.Error() { |
| 94 | + fmt.Println("first condition") |
| 95 | + fmt.Println(tt.wantError) |
| 96 | + fmt.Println(cloudErr) |
| 97 | + t.Fatalf("got %q but wanted %q", cloudErr.Error(), tt.wantError) |
| 98 | + } |
| 99 | + if tt.wantStatusCode != 0 && tt.wantStatusCode != cloudErr.StatusCode { |
| 100 | + fmt.Println("second condition") |
| 101 | + fmt.Println(tt.wantError) |
| 102 | + fmt.Println(cloudErr) |
| 103 | + t.Fatalf("got %q but wanted %q", cloudErr.Error(), tt.wantError) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if !strings.EqualFold(string(clusterSyncSet), string(tt.wantResponse)) { |
| 108 | + t.Fatalf("got %q and expected %q", clusterSyncSet, tt.wantResponse) |
| 109 | + } |
| 110 | + }) |
| 111 | + } |
| 112 | +} |
0 commit comments