|
| 1 | +package cluster |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + mock_hive "github.com/Azure/ARO-RP/pkg/util/mocks/hive" |
| 10 | + mock_metrics "github.com/Azure/ARO-RP/pkg/util/mocks/metrics" |
| 11 | + "github.com/golang/mock/gomock" |
| 12 | + hivev1alpha1 "github.com/openshift/hive/apis/hiveinternal/v1alpha1" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/client-go/kubernetes/scheme" |
| 16 | +) |
| 17 | + |
| 18 | +func init() { |
| 19 | + // Register the ClusterSync type with the scheme |
| 20 | + hivev1alpha1.AddToScheme(scheme.Scheme) |
| 21 | +} |
| 22 | + |
| 23 | +func TestEmitSyncSetStatus(t *testing.T) { |
| 24 | + for _, tt := range []struct { |
| 25 | + name string |
| 26 | + clusterSync *hivev1alpha1.ClusterSync |
| 27 | + getClusterSyncErr error |
| 28 | + expectedError error |
| 29 | + expectedGauges []struct { |
| 30 | + name string |
| 31 | + value int64 |
| 32 | + labels map[string]string |
| 33 | + } |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "SyncSets and SelectorSyncSets have elements", |
| 37 | + clusterSync: &hivev1alpha1.ClusterSync{ |
| 38 | + Status: hivev1alpha1.ClusterSyncStatus{ |
| 39 | + SyncSets: []hivev1alpha1.SyncStatus{ |
| 40 | + { |
| 41 | + Name: "syncset1", |
| 42 | + Result: "Success", |
| 43 | + FirstSuccessTime: &metav1.Time{Time: time.Now()}, |
| 44 | + LastTransitionTime: metav1.Time{Time: time.Now()}, |
| 45 | + FailureMessage: "", |
| 46 | + }, |
| 47 | + }, |
| 48 | + SelectorSyncSets: []hivev1alpha1.SyncStatus{ |
| 49 | + { |
| 50 | + Name: "selectorsyncset1", |
| 51 | + Result: "Success", |
| 52 | + FirstSuccessTime: &metav1.Time{Time: time.Now()}, |
| 53 | + LastTransitionTime: metav1.Time{Time: time.Now()}, |
| 54 | + FailureMessage: "", |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + expectedError: nil, |
| 60 | + expectedGauges: []struct { |
| 61 | + name string |
| 62 | + value int64 |
| 63 | + labels map[string]string |
| 64 | + }{ |
| 65 | + { |
| 66 | + name: "hive.syncsets", |
| 67 | + value: 1, |
| 68 | + labels: map[string]string{ |
| 69 | + "name": "syncset1", |
| 70 | + "result": "Success", |
| 71 | + "firstSuccessTime": time.Now().Format(time.RFC3339), |
| 72 | + "lastTransitionTime": time.Now().Format(time.RFC3339), |
| 73 | + "failureMessage": "", |
| 74 | + }, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "hive.selectorsyncsets", |
| 78 | + value: 1, |
| 79 | + labels: map[string]string{ |
| 80 | + "name": "selectorsyncset1", |
| 81 | + "result": "Success", |
| 82 | + "firstSuccessTime": time.Now().Format(time.RFC3339), |
| 83 | + "lastTransitionTime": time.Now().Format(time.RFC3339), |
| 84 | + "failureMessage": "", |
| 85 | + }, |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "hive.clustersync", |
| 89 | + value: 1, |
| 90 | + labels: map[string]string{ |
| 91 | + "name": "selectorsyncset1", |
| 92 | + "result": "Success", |
| 93 | + "firstSuccessTime": time.Now().Format(time.RFC3339), |
| 94 | + "lastTransitionTime": time.Now().Format(time.RFC3339), |
| 95 | + "failureMessage": "", |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "SyncSets and SelectorSyncSets are nil", |
| 102 | + clusterSync: &hivev1alpha1.ClusterSync{ |
| 103 | + Status: hivev1alpha1.ClusterSyncStatus{ |
| 104 | + SyncSets: nil, |
| 105 | + SelectorSyncSets: nil, |
| 106 | + }, |
| 107 | + }, |
| 108 | + expectedError: nil, |
| 109 | + expectedGauges: []struct { |
| 110 | + name string |
| 111 | + value int64 |
| 112 | + labels map[string]string |
| 113 | + }{ |
| 114 | + { |
| 115 | + name: "hive.syncsets", |
| 116 | + value: 0, |
| 117 | + labels: map[string]string{}, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "hive.selectorsyncsets", |
| 121 | + value: 0, |
| 122 | + labels: map[string]string{}, |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "hive.clustersync", |
| 126 | + value: 0, |
| 127 | + labels: map[string]string{}, |
| 128 | + }, |
| 129 | + }, |
| 130 | + }, |
| 131 | + { |
| 132 | + name: "GetClusterSyncforClusterDeployment returns error", |
| 133 | + getClusterSyncErr: errors.New("some error"), |
| 134 | + expectedError: nil, |
| 135 | + expectedGauges: []struct { |
| 136 | + name string |
| 137 | + value int64 |
| 138 | + labels map[string]string |
| 139 | + }{}, |
| 140 | + }, |
| 141 | + } { |
| 142 | + t.Run(tt.name, func(t *testing.T) { |
| 143 | + ctrl := gomock.NewController(t) |
| 144 | + defer ctrl.Finish() |
| 145 | + |
| 146 | + mockHiveClusterManager := mock_hive.NewMockClusterManager(ctrl) |
| 147 | + m := mock_metrics.NewMockEmitter(ctrl) |
| 148 | + mockMonitor := &Monitor{ |
| 149 | + hiveClusterManager: mockHiveClusterManager, |
| 150 | + m: m, |
| 151 | + } |
| 152 | + |
| 153 | + ctx := context.Background() |
| 154 | + |
| 155 | + mockHiveClusterManager.EXPECT().GetSyncSetResources(ctx, mockMonitor.doc).Return(tt.clusterSync, tt.getClusterSyncErr).AnyTimes() |
| 156 | + |
| 157 | + for _, gauge := range tt.expectedGauges { |
| 158 | + m.EXPECT().EmitGauge(gauge.name, gauge.value, gauge.labels).Times(1) |
| 159 | + } |
| 160 | + |
| 161 | + err := mockMonitor.emitSyncSetStatus(ctx) |
| 162 | + assert.Equal(t, tt.expectedError, err) |
| 163 | + }) |
| 164 | + } |
| 165 | +} |
0 commit comments