|
| 1 | +package ceohelpers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + configv1 "github.com/openshift/api/config/v1" |
| 8 | + operatorv1 "github.com/openshift/api/operator/v1" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/openshift/cluster-etcd-operator/pkg/testutils" |
| 12 | + "github.com/openshift/cluster-etcd-operator/pkg/tnf/pkg/etcd" |
| 13 | +) |
| 14 | + |
| 15 | +func TestIsExternalEtcdCluster(t *testing.T) { |
| 16 | + tests := []struct { |
| 17 | + name string |
| 18 | + topology configv1.TopologyMode |
| 19 | + expectedResult bool |
| 20 | + expectError bool |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "HighlyAvailable topology - not external etcd", |
| 24 | + topology: configv1.HighlyAvailableTopologyMode, |
| 25 | + expectedResult: false, |
| 26 | + expectError: false, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "DualReplica topology - external etcd", |
| 30 | + topology: configv1.DualReplicaTopologyMode, |
| 31 | + expectedResult: true, |
| 32 | + expectError: false, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "SingleReplica topology - not external etcd", |
| 36 | + topology: configv1.SingleReplicaTopologyMode, |
| 37 | + expectedResult: false, |
| 38 | + expectError: false, |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + for _, tt := range tests { |
| 43 | + t.Run(tt.name, func(t *testing.T) { |
| 44 | + // Create fake infrastructure lister |
| 45 | + infraLister := testutils.FakeInfrastructureLister(t, tt.topology) |
| 46 | + |
| 47 | + // Test the function |
| 48 | + result, err := IsExternalEtcdCluster(context.Background(), infraLister) |
| 49 | + |
| 50 | + if tt.expectError { |
| 51 | + require.Error(t, err) |
| 52 | + } else { |
| 53 | + require.NoError(t, err) |
| 54 | + require.Equal(t, tt.expectedResult, result) |
| 55 | + } |
| 56 | + }) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestIsReadyForEtcdTransition(t *testing.T) { |
| 61 | + tests := []struct { |
| 62 | + name string |
| 63 | + operatorConditions []operatorv1.OperatorCondition |
| 64 | + expectedResult bool |
| 65 | + expectError bool |
| 66 | + }{ |
| 67 | + { |
| 68 | + name: "No conditions - not ready", |
| 69 | + operatorConditions: []operatorv1.OperatorCondition{}, |
| 70 | + expectedResult: false, |
| 71 | + expectError: false, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "ExternalEtcdReadyForTransition condition true - ready", |
| 75 | + operatorConditions: []operatorv1.OperatorCondition{ |
| 76 | + { |
| 77 | + Type: etcd.OperatorConditionExternalEtcdReadyForTransition, |
| 78 | + Status: operatorv1.ConditionTrue, |
| 79 | + }, |
| 80 | + }, |
| 81 | + expectedResult: true, |
| 82 | + expectError: false, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "ExternalEtcdReadyForTransition condition false - not ready", |
| 86 | + operatorConditions: []operatorv1.OperatorCondition{ |
| 87 | + { |
| 88 | + Type: etcd.OperatorConditionExternalEtcdReadyForTransition, |
| 89 | + Status: operatorv1.ConditionFalse, |
| 90 | + }, |
| 91 | + }, |
| 92 | + expectedResult: false, |
| 93 | + expectError: false, |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "Other conditions present but not ExternalEtcdReadyForTransition - not ready", |
| 97 | + operatorConditions: []operatorv1.OperatorCondition{ |
| 98 | + { |
| 99 | + Type: etcd.OperatorConditionEtcdRunningInCluster, |
| 100 | + Status: operatorv1.ConditionTrue, |
| 101 | + }, |
| 102 | + }, |
| 103 | + expectedResult: false, |
| 104 | + expectError: false, |
| 105 | + }, |
| 106 | + } |
| 107 | + |
| 108 | + for _, tt := range tests { |
| 109 | + t.Run(tt.name, func(t *testing.T) { |
| 110 | + // Create fake operator client |
| 111 | + operatorClient := testutils.FakeStaticPodOperatorClient(t, tt.operatorConditions) |
| 112 | + |
| 113 | + // Test the function |
| 114 | + result, err := IsReadyForEtcdTransition(context.Background(), operatorClient) |
| 115 | + |
| 116 | + if tt.expectError { |
| 117 | + require.Error(t, err) |
| 118 | + } else { |
| 119 | + require.NoError(t, err) |
| 120 | + require.Equal(t, tt.expectedResult, result) |
| 121 | + } |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func TestIsEtcdRunningInCluster(t *testing.T) { |
| 127 | + tests := []struct { |
| 128 | + name string |
| 129 | + operatorConditions []operatorv1.OperatorCondition |
| 130 | + expectedResult bool |
| 131 | + expectError bool |
| 132 | + }{ |
| 133 | + { |
| 134 | + name: "No conditions - not completed", |
| 135 | + operatorConditions: []operatorv1.OperatorCondition{}, |
| 136 | + expectedResult: false, |
| 137 | + expectError: false, |
| 138 | + }, |
| 139 | + { |
| 140 | + name: "EtcdRunningInCluster condition true - completed", |
| 141 | + operatorConditions: []operatorv1.OperatorCondition{ |
| 142 | + { |
| 143 | + Type: etcd.OperatorConditionEtcdRunningInCluster, |
| 144 | + Status: operatorv1.ConditionTrue, |
| 145 | + }, |
| 146 | + }, |
| 147 | + expectedResult: true, |
| 148 | + expectError: false, |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "EtcdRunningInCluster condition false - not completed", |
| 152 | + operatorConditions: []operatorv1.OperatorCondition{ |
| 153 | + { |
| 154 | + Type: etcd.OperatorConditionEtcdRunningInCluster, |
| 155 | + Status: operatorv1.ConditionFalse, |
| 156 | + }, |
| 157 | + }, |
| 158 | + expectedResult: false, |
| 159 | + expectError: false, |
| 160 | + }, |
| 161 | + } |
| 162 | + |
| 163 | + for _, tt := range tests { |
| 164 | + t.Run(tt.name, func(t *testing.T) { |
| 165 | + // Create fake operator client |
| 166 | + operatorClient := testutils.FakeStaticPodOperatorClient(t, tt.operatorConditions) |
| 167 | + |
| 168 | + // Test the function |
| 169 | + result, err := IsEtcdRunningInCluster(context.Background(), operatorClient) |
| 170 | + |
| 171 | + if tt.expectError { |
| 172 | + require.Error(t, err) |
| 173 | + } else { |
| 174 | + require.NoError(t, err) |
| 175 | + require.Equal(t, tt.expectedResult, result) |
| 176 | + } |
| 177 | + }) |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +func TestGetExternalEtcdClusterStatus(t *testing.T) { |
| 182 | + tests := []struct { |
| 183 | + name string |
| 184 | + topology configv1.TopologyMode |
| 185 | + operatorConditions []operatorv1.OperatorCondition |
| 186 | + expectedExternalEtcd bool |
| 187 | + expectedEtcdRunningInCluster bool |
| 188 | + expectedReadyForTransition bool |
| 189 | + expectError bool |
| 190 | + }{ |
| 191 | + { |
| 192 | + name: "HighlyAvailable topology - not external etcd", |
| 193 | + topology: configv1.HighlyAvailableTopologyMode, |
| 194 | + operatorConditions: []operatorv1.OperatorCondition{}, |
| 195 | + expectedExternalEtcd: false, |
| 196 | + expectedEtcdRunningInCluster: false, |
| 197 | + expectedReadyForTransition: false, |
| 198 | + expectError: false, |
| 199 | + }, |
| 200 | + { |
| 201 | + name: "DualReplica topology with no conditions", |
| 202 | + topology: configv1.DualReplicaTopologyMode, |
| 203 | + operatorConditions: []operatorv1.OperatorCondition{}, |
| 204 | + expectedExternalEtcd: true, |
| 205 | + expectedEtcdRunningInCluster: false, |
| 206 | + expectedReadyForTransition: false, |
| 207 | + expectError: false, |
| 208 | + }, |
| 209 | + { |
| 210 | + name: "DualReplica topology with bootstrap completed", |
| 211 | + topology: configv1.DualReplicaTopologyMode, |
| 212 | + operatorConditions: []operatorv1.OperatorCondition{ |
| 213 | + { |
| 214 | + Type: etcd.OperatorConditionEtcdRunningInCluster, |
| 215 | + Status: operatorv1.ConditionTrue, |
| 216 | + }, |
| 217 | + }, |
| 218 | + expectedExternalEtcd: true, |
| 219 | + expectedEtcdRunningInCluster: true, |
| 220 | + expectedReadyForTransition: false, |
| 221 | + expectError: false, |
| 222 | + }, |
| 223 | + { |
| 224 | + name: "DualReplica topology with bootstrap completed and ready for transition", |
| 225 | + topology: configv1.DualReplicaTopologyMode, |
| 226 | + operatorConditions: []operatorv1.OperatorCondition{ |
| 227 | + { |
| 228 | + Type: etcd.OperatorConditionEtcdRunningInCluster, |
| 229 | + Status: operatorv1.ConditionTrue, |
| 230 | + }, |
| 231 | + { |
| 232 | + Type: etcd.OperatorConditionExternalEtcdReadyForTransition, |
| 233 | + Status: operatorv1.ConditionTrue, |
| 234 | + }, |
| 235 | + }, |
| 236 | + expectedExternalEtcd: true, |
| 237 | + expectedEtcdRunningInCluster: true, |
| 238 | + expectedReadyForTransition: true, |
| 239 | + expectError: false, |
| 240 | + }, |
| 241 | + } |
| 242 | + |
| 243 | + for _, tt := range tests { |
| 244 | + t.Run(tt.name, func(t *testing.T) { |
| 245 | + // Create fake infrastructure lister |
| 246 | + infraLister := testutils.FakeInfrastructureLister(t, tt.topology) |
| 247 | + |
| 248 | + // Create fake operator client |
| 249 | + operatorClient := testutils.FakeStaticPodOperatorClient(t, tt.operatorConditions) |
| 250 | + |
| 251 | + // Test the function |
| 252 | + externalEtcdStatus, err := GetExternalEtcdClusterStatus( |
| 253 | + context.Background(), operatorClient, infraLister) |
| 254 | + |
| 255 | + if tt.expectError { |
| 256 | + require.Error(t, err) |
| 257 | + } else { |
| 258 | + require.NoError(t, err) |
| 259 | + require.Equal(t, tt.expectedExternalEtcd, externalEtcdStatus.IsExternalEtcdCluster) |
| 260 | + require.Equal(t, tt.expectedEtcdRunningInCluster, externalEtcdStatus.IsEtcdRunningInCluster) |
| 261 | + require.Equal(t, tt.expectedReadyForTransition, externalEtcdStatus.IsReadyForEtcdTransition) |
| 262 | + } |
| 263 | + }) |
| 264 | + } |
| 265 | +} |
0 commit comments