@@ -130,6 +130,46 @@ func TestValidateState(t *testing.T) {
130
130
ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Minute )
131
131
defer cancel ()
132
132
133
+ if testConfig .ValidateStateFile {
134
+ deployment := kubernetes .MustParseDeployment (noopDeploymentMap [testConfig .OSType ])
135
+ deploymentsClient := clientset .AppsV1 ().Deployments (namespace )
136
+
137
+ // Ensure pods exist on nodes to validate state files properly. Can obtain false positives without pods.
138
+ nodes , err := kubernetes .GetNodeListByLabelSelector (ctx , clientset , "kubernetes.io/os=" + testConfig .OSType )
139
+ require .NoError (t , err )
140
+ nodeCount := len (nodes .Items )
141
+ replicas := int32 (nodeCount ) * 2
142
+
143
+ deploymentExists , err := kubernetes .DeploymentExists (ctx , deploymentsClient , deployment .Name )
144
+ require .NoError (t , err )
145
+ if ! deploymentExists {
146
+ t .Logf ("Test deployment %s does not exist! Create %v pods in %s namespace" , deployment .Name , replicas , namespace )
147
+ // Create namespace if it doesn't exist
148
+ namespaceExists , err := kubernetes .NamespaceExists (ctx , clientset , namespace )
149
+ require .NoError (t , err )
150
+ if ! namespaceExists {
151
+ kubernetes .MustCreateNamespace (ctx , clientset , namespace )
152
+ }
153
+
154
+ kubernetes .MustCreateDeployment (ctx , deploymentsClient , deployment )
155
+ kubernetes .MustScaleDeployment (ctx , deploymentsClient , deployment , clientset , namespace , podLabelSelector , int (replicas ), false )
156
+ } else {
157
+ t .Log ("Test deployment exists! Use existing setup" )
158
+ replicas , err = kubernetes .GetDeploymentAvailableReplicas (ctx , deploymentsClient , deployment .Name ) // If test namespace exists then use existing Replicas
159
+ if replicas != 0 && err != nil {
160
+ require .NoError (t , err )
161
+ }
162
+ }
163
+ if replicas < int32 (nodeCount ) {
164
+ t .Logf ("Warning - current replica count %v is below current %s node count of %d. Raising replicas to minimum required to ensure there is a pod on every node." , replicas , testConfig .OSType , nodeCount )
165
+ replicas = int32 (nodeCount * 2 )
166
+ kubernetes .MustScaleDeployment (ctx , deploymentsClient , deployment , clientset , namespace , podLabelSelector , int (replicas ), false )
167
+ }
168
+ t .Log ("Ensure deployment is in ready status" )
169
+ err = kubernetes .WaitForPodDeployment (ctx , clientset , namespace , deployment .Name , podLabelSelector , int (replicas ))
170
+ require .NoError (t , err )
171
+ }
172
+
133
173
validator , err := validate .CreateValidator (ctx , clientset , config , namespace , testConfig .CNIType , testConfig .RestartCase , testConfig .OSType )
134
174
require .NoError (t , err )
135
175
@@ -182,10 +222,12 @@ func TestValidCNSStateDuringScaleAndCNSRestartToTriggerDropgzInstall(t *testing.
182
222
ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Minute )
183
223
defer cancel ()
184
224
185
- validator , err := validate .CreateValidator (ctx , clientset , config , namespace , testConfig .CNIType , testConfig .RestartCase , testConfig .OSType )
186
- require .NoError (t , err )
225
+ // Provide an option to validate state files with a proper environment before running test
226
+ if testConfig .ValidateStateFile {
227
+ t .Run ("Validate state file" , TestValidateState )
228
+ }
187
229
188
- err = validator . Validate (ctx )
230
+ validator , err := validate . CreateValidator (ctx , clientset , config , namespace , testConfig . CNIType , testConfig . RestartCase , testConfig . OSType )
189
231
require .NoError (t , err )
190
232
191
233
deployment := kubernetes .MustParseDeployment (noopDeploymentMap [testConfig .OSType ])
@@ -238,6 +280,7 @@ func TestValidCNSStateDuringScaleAndCNSRestartToTriggerDropgzInstall(t *testing.
238
280
kubernetes .MustDeleteDeployment (ctx , deploymentsClient , deployment )
239
281
err = kubernetes .WaitForPodsDelete (ctx , clientset , namespace , podLabelSelector )
240
282
require .NoError (t , err , "error waiting for pods to delete" )
283
+ validator .Cleanup (ctx )
241
284
}
242
285
}
243
286
@@ -258,6 +301,10 @@ func TestV4OverlayProperties(t *testing.T) {
258
301
t .Log ("Validating v4Overlay node labels" )
259
302
err = validator .ValidateV4OverlayControlPlane (ctx )
260
303
require .NoError (t , err )
304
+
305
+ if testConfig .Cleanup {
306
+ validator .Cleanup (ctx )
307
+ }
261
308
}
262
309
263
310
func TestDualStackProperties (t * testing.T ) {
0 commit comments