@@ -608,139 +608,71 @@ def test_create_from_list_in_multi_resource_yaml_namespaced(self):
608608 app_api .delete_namespaced_deployment (
609609 name = "mock" , namespace = self .test_namespace , body = {})
610610
611- def test_get_metrics_from_cluster (self ):
611+ def test_metrics_utilities_integration (self ):
612612 """
613- Should be able to fetch node and pod metrics from the cluster .
614- This test requires metrics-server to be installed in the cluster.
613+ E2E validation of metrics utility functions .
614+ Note: Requires metrics-server to be running in cluster.
615615 """
616- k8s_client = client .api_client .ApiClient (configuration = self .config )
617- core_api = client .CoreV1Api (k8s_client )
616+ from time import sleep
617+
618+ api = client .api_client .ApiClient (configuration = self .config )
619+ v1 = client .CoreV1Api (api )
618620
619- # Create a test pod for metrics
621+ # Setup: deploy busybox pod
620622 utils .create_from_yaml (
621- k8s_client , self .path_prefix + "core-pod.yaml" ,
623+ api , self .path_prefix + "core-pod.yaml" ,
622624 namespace = self .test_namespace )
623625
624- # Wait for pod to be running (with timeout)
625- import time
626- max_wait = 60
627- waited = 0
628- pod_running = False
629- while waited < max_wait :
626+ # Wait for pod startup (simple polling)
627+ for _ in range (30 ):
630628 try :
631- pod = core_api .read_namespaced_pod (
632- name = "myapp-pod" , namespace = self .test_namespace )
633- if pod .status .phase == "Running" :
634- pod_running = True
629+ p = v1 .read_namespaced_pod ("myapp-pod" , self .test_namespace )
630+ if p .status .phase == "Running" :
635631 break
636- except ApiException :
632+ except :
637633 pass
638- time . sleep (2 )
639- waited += 2
640-
641- # Skip test if pod didn't start (cluster might be slow)
642- if not pod_running :
643- core_api . delete_namespaced_pod (
644- name = "myapp-pod" , namespace = self . test_namespace , body = {})
645- raise unittest .SkipTest ("Pod did not reach Running state in time " )
634+ sleep (2 )
635+ else :
636+ # Cleanup and skip if pod never started
637+ try :
638+ v1 . delete_namespaced_pod ( "myapp-pod" , self . test_namespace , body = {})
639+ except :
640+ pass
641+ raise unittest .SkipTest ("Pod startup timeout " )
646642
647- # Wait a bit more for metrics to be available
648- time . sleep (10 )
643+ # Allow metrics scrape interval
644+ sleep (10 )
649645
650- # Test node metrics retrieval
646+ # Test 1: Node metrics utility
651647 try :
652- node_metrics = utils .get_nodes_metrics (k8s_client )
653- self .assertIsNotNone (node_metrics )
654- self .assertEqual (node_metrics ['kind' ], 'NodeMetricsList' )
655- self .assertIn ('items' , node_metrics )
656- # We should have at least one node in the cluster
657- self .assertGreater (len (node_metrics ['items' ]), 0 )
658- # Check structure of first node metric
659- if len (node_metrics ['items' ]) > 0 :
660- node = node_metrics ['items' ][0 ]
661- self .assertIn ('metadata' , node )
662- self .assertIn ('name' , node ['metadata' ])
663- self .assertIn ('usage' , node )
664- self .assertIn ('cpu' , node ['usage' ])
665- self .assertIn ('memory' , node ['usage' ])
648+ result = utils .get_nodes_metrics (api )
649+ self .assertTrue ('items' in result and len (result ['items' ]) > 0 )
650+ self .assertTrue ('usage' in result ['items' ][0 ])
666651 except ApiException as e :
667- # If metrics-server is not installed, skip this test
668652 if e .status == 404 :
669- core_api .delete_namespaced_pod (
670- name = "myapp-pod" , namespace = self .test_namespace , body = {})
671- raise unittest .SkipTest ("Metrics server not available in cluster" )
653+ v1 .delete_namespaced_pod ("myapp-pod" , self .test_namespace , body = {})
654+ raise unittest .SkipTest ("Metrics API unavailable" )
672655 raise
673656
674- # Test pod metrics retrieval
675- try :
676- pod_metrics = utils .get_pods_metrics (
677- k8s_client , self .test_namespace )
678- self .assertIsNotNone (pod_metrics )
679- self .assertEqual (pod_metrics ['kind' ], 'PodMetricsList' )
680- self .assertIn ('items' , pod_metrics )
681- # We should have our test pod
682- self .assertGreater (len (pod_metrics ['items' ]), 0 )
683- # Check structure of pod metrics
684- found_test_pod = False
685- for pod in pod_metrics ['items' ]:
686- if pod ['metadata' ]['name' ] == 'myapp-pod' :
687- found_test_pod = True
688- self .assertIn ('containers' , pod )
689- self .assertGreater (len (pod ['containers' ]), 0 )
690- container = pod ['containers' ][0 ]
691- self .assertIn ('name' , container )
692- self .assertIn ('usage' , container )
693- self .assertIn ('cpu' , container ['usage' ])
694- self .assertIn ('memory' , container ['usage' ])
695- # Our test pod should appear in metrics
696- self .assertTrue (found_test_pod , "Test pod not found in metrics" )
697- except ApiException as e :
698- if e .status == 404 :
699- core_api .delete_namespaced_pod (
700- name = "myapp-pod" , namespace = self .test_namespace , body = {})
701- raise unittest .SkipTest ("Metrics server not available in cluster" )
702- raise
657+ # Test 2: Pod metrics utility (basic)
658+ result = utils .get_pods_metrics (api , self .test_namespace )
659+ self .assertTrue ('items' in result )
660+ pod_names = [item ['metadata' ]['name' ] for item in result ['items' ]]
661+ self .assertIn ('myapp-pod' , pod_names )
703662
704- # Test pod metrics with label selector
705- try :
706- filtered_metrics = utils .get_pods_metrics (
707- k8s_client , self .test_namespace , label_selector = 'app=myapp' )
708- self .assertIsNotNone (filtered_metrics )
709- self .assertEqual (filtered_metrics ['kind' ], 'PodMetricsList' )
710- self .assertIn ('items' , filtered_metrics )
711- # Should have our pod with the matching label
712- self .assertGreater (len (filtered_metrics ['items' ]), 0 )
713- for pod in filtered_metrics ['items' ]:
714- # All returned pods should have the label we filtered for
715- self .assertEqual (pod ['metadata' ]['name' ], 'myapp-pod' )
716- except ApiException as e :
717- if e .status == 404 :
718- core_api .delete_namespaced_pod (
719- name = "myapp-pod" , namespace = self .test_namespace , body = {})
720- raise unittest .SkipTest ("Metrics server not available in cluster" )
721- raise
663+ # Test 3: Pod metrics with label filtering
664+ result = utils .get_pods_metrics (api , self .test_namespace , 'app=myapp' )
665+ self .assertEqual (len (result ['items' ]), 1 )
666+ self .assertEqual (result ['items' ][0 ]['metadata' ]['name' ], 'myapp-pod' )
722667
723- # Test multi-namespace metrics collection
724- try :
725- multi_ns_metrics = utils .get_pods_metrics_in_all_namespaces (
726- k8s_client , [self .test_namespace , 'default' ])
727- self .assertIsNotNone (multi_ns_metrics )
728- self .assertIn (self .test_namespace , multi_ns_metrics )
729- self .assertIn ('default' , multi_ns_metrics )
730- # Our test namespace should have metrics
731- test_ns_result = multi_ns_metrics [self .test_namespace ]
732- self .assertNotIn ('error' , test_ns_result )
733- self .assertEqual (test_ns_result ['kind' ], 'PodMetricsList' )
734- except ApiException as e :
735- if e .status == 404 :
736- core_api .delete_namespaced_pod (
737- name = "myapp-pod" , namespace = self .test_namespace , body = {})
738- raise unittest .SkipTest ("Metrics server not available in cluster" )
739- raise
668+ # Test 4: Multi-namespace aggregation
669+ result = utils .get_pods_metrics_in_all_namespaces (
670+ api , [self .test_namespace , 'default' ])
671+ self .assertIn (self .test_namespace , result )
672+ self .assertNotIn ('error' , result [self .test_namespace ])
740673
741- # Cleanup
742- core_api .delete_namespaced_pod (
743- name = "myapp-pod" , namespace = self .test_namespace , body = {})
674+ # Teardown
675+ v1 .delete_namespaced_pod ("myapp-pod" , self .test_namespace , body = {})
744676
745677
746678class TestUtilsUnitTests (unittest .TestCase ):
0 commit comments