@@ -24,16 +24,16 @@ import (
24
24
"strings"
25
25
26
26
"github.com/Masterminds/semver"
27
- "github.com/redhat-best-practices-for-k8s/certsuite/tests/common"
28
- "github.com/redhat-best-practices-for-k8s/certsuite/tests/identifiers"
29
- pdbv1 "github.com/redhat-best-practices-for-k8s/certsuite/tests/observability/pdb"
30
-
31
27
apiserv1 "github.com/openshift/api/apiserver/v1"
32
28
"github.com/redhat-best-practices-for-k8s/certsuite/internal/clientsholder"
33
29
"github.com/redhat-best-practices-for-k8s/certsuite/internal/log"
30
+ "github.com/redhat-best-practices-for-k8s/certsuite/pkg/autodiscover"
34
31
"github.com/redhat-best-practices-for-k8s/certsuite/pkg/checksdb"
35
32
"github.com/redhat-best-practices-for-k8s/certsuite/pkg/provider"
36
33
"github.com/redhat-best-practices-for-k8s/certsuite/pkg/testhelper"
34
+ "github.com/redhat-best-practices-for-k8s/certsuite/tests/common"
35
+ "github.com/redhat-best-practices-for-k8s/certsuite/tests/identifiers"
36
+ pdbv1 "github.com/redhat-best-practices-for-k8s/certsuite/tests/observability/pdb"
37
37
corev1 "k8s.io/api/core/v1"
38
38
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
39
39
"k8s.io/apimachinery/pkg/labels"
@@ -88,6 +88,13 @@ func LoadChecks() {
88
88
testAPICompatibilityWithNextOCPRelease (c , & env )
89
89
return nil
90
90
}))
91
+
92
+ checksGroup .Add (checksdb .NewCheck (identifiers .GetTestIDAndLabels (identifiers .TestPodCountIdentifier )).
93
+ WithSkipCheckFn (testhelper .GetNoPodsUnderTestSkipFn (& env )).
94
+ WithCheckFn (func (c * checksdb.Check ) error {
95
+ testComparePodCount (c , & env )
96
+ return nil
97
+ }))
91
98
}
92
99
93
100
// containerHasLoggingOutput helper function to get the last line of logging output from
@@ -423,3 +430,80 @@ func testAPICompatibilityWithNextOCPRelease(check *checksdb.Check, env *provider
423
430
// Add test results
424
431
check .SetResult (compliantObjects , nonCompliantObjects )
425
432
}
433
+
434
+ // Function to compare the number of running pods to those loaded during autodiscover at the start of test execution.
435
+ func testComparePodCount (check * checksdb.Check , env * provider.TestEnvironment ) {
436
+ oc := clientsholder .GetClientsHolder ()
437
+
438
+ originalPods := env .Pods
439
+
440
+ currentPods , _ := autodiscover .FindPodsByLabels (oc .K8sClient .CoreV1 (), autodiscover .CreateLabels (env .Config .PodsUnderTestLabels ), env .Namespaces )
441
+
442
+ var compliantObjects []* testhelper.ReportObject
443
+ var nonCompliantObjects []* testhelper.ReportObject
444
+
445
+ // Compare pod counts
446
+ originalPodCount := len (originalPods )
447
+ currentPodCount := len (currentPods )
448
+
449
+ check .LogInfo ("Original pod count: %d, Current pod count: %d" , originalPodCount , currentPodCount )
450
+
451
+ if originalPodCount == currentPodCount {
452
+ check .LogInfo ("Pod count is consistent" )
453
+ compliantObjects = append (compliantObjects ,
454
+ testhelper .NewReportObject ("Pod count is consistent" , "PodCount" , true ).
455
+ AddField ("OriginalCount" , fmt .Sprintf ("%d" , originalPodCount )).
456
+ AddField ("CurrentCount" , fmt .Sprintf ("%d" , currentPodCount )))
457
+ } else {
458
+ check .LogError ("Pod count mismatch: original=%d, current=%d" , originalPodCount , currentPodCount )
459
+ nonCompliantObjects = append (nonCompliantObjects ,
460
+ testhelper .NewReportObject ("Pod count mismatch" , "PodCount" , false ).
461
+ AddField ("OriginalCount" , fmt .Sprintf ("%d" , originalPodCount )).
462
+ AddField ("CurrentCount" , fmt .Sprintf ("%d" , currentPodCount )))
463
+ }
464
+
465
+ // Create maps for detailed comparison
466
+ originalPodsMap := make (map [string ]struct {})
467
+ for _ , pod := range originalPods {
468
+ key := fmt .Sprintf ("%s/%s" , pod .Namespace , pod .Name )
469
+ originalPodsMap [key ] = struct {}{}
470
+ }
471
+
472
+ currentPodsMap := make (map [string ]struct {})
473
+ for _ , pod := range currentPods {
474
+ key := fmt .Sprintf ("%s/%s" , pod .Namespace , pod .Name )
475
+ currentPodsMap [key ] = struct {}{}
476
+ }
477
+
478
+ // Check for missing pods (in original but not in current)
479
+ for _ , originalPod := range originalPods {
480
+ podKey := fmt .Sprintf ("%s/%s" , originalPod .Namespace , originalPod .Name )
481
+ if _ , exists := currentPodsMap [podKey ]; ! exists {
482
+ check .LogError ("Pod %q is missing from current state" , originalPod .String ())
483
+ nonCompliantObjects = append (nonCompliantObjects ,
484
+ testhelper .NewReportObject ("Pod is missing from current state" , testhelper .PodType , false ).
485
+ AddField (testhelper .PodName , originalPod .Name ).
486
+ AddField (testhelper .Namespace , originalPod .Namespace ))
487
+ } else {
488
+ check .LogInfo ("Pod %q is present in current state" , originalPod .String ())
489
+ compliantObjects = append (compliantObjects ,
490
+ testhelper .NewReportObject ("Pod is present in current state" , testhelper .PodType , true ).
491
+ AddField (testhelper .PodName , originalPod .Name ).
492
+ AddField (testhelper .Namespace , originalPod .Namespace ))
493
+ }
494
+ }
495
+
496
+ // Check for extra pods (in current but not in original)
497
+ for _ , currentPod := range currentPods {
498
+ podKey := fmt .Sprintf ("%s/%s" , currentPod .Namespace , currentPod .Name )
499
+ if _ , exists := originalPodsMap [podKey ]; ! exists {
500
+ check .LogError ("Extra pod %s/%s found in current state" , currentPod .Namespace , currentPod .Name )
501
+ nonCompliantObjects = append (nonCompliantObjects ,
502
+ testhelper .NewReportObject ("Extra pod found in current state" , testhelper .PodType , false ).
503
+ AddField (testhelper .PodName , currentPod .Name ).
504
+ AddField (testhelper .Namespace , currentPod .Namespace ))
505
+ }
506
+ }
507
+
508
+ check .SetResult (compliantObjects , nonCompliantObjects )
509
+ }
0 commit comments