diff --git a/tests/post/features/monitoring.feature b/tests/post/features/monitoring.feature index 4ba09c7a7f..5069d68322 100644 --- a/tests/post/features/monitoring.feature +++ b/tests/post/features/monitoring.feature @@ -30,3 +30,11 @@ Feature: Monitoring is up and running And we have 1 running pod labeled 'name=prometheus-adapter' in namespace 'metalk8s-monitoring' And the 'v1beta1.metrics.k8s.io' APIService exists Then the 'v1beta1.metrics.k8s.io' APIService is Available + + Scenario: Pod metrics can be retrieved using metrics.k8s.io/v1beta1 + Given the Kubernetes API is available + Then a pod with label 'component=kube-apiserver' in namespace 'kube-system' has metrics + + Scenario: Node metrics can be retrieved using metrics.k8s.io/v1beta1 + Given the Kubernetes API is available + Then a node with label 'node-role.kubernetes.io/bootstrap=' has metrics diff --git a/tests/post/steps/test_monitoring.py b/tests/post/steps/test_monitoring.py index f6204c33f9..044de8e3ae 100644 --- a/tests/post/steps/test_monitoring.py +++ b/tests/post/steps/test_monitoring.py @@ -27,6 +27,20 @@ def test_monitored_components(host): pass +@scenario( + '../features/monitoring.feature', + 'Pod metrics can be retrieved using metrics.k8s.io/v1beta1') +def test_pod_metrics(host): + pass + + +@scenario( + '../features/monitoring.feature', + 'Node metrics can be retrieved using metrics.k8s.io/v1beta1') +def test_node_metrics(host): + pass + + # }}} # Given {{{ @@ -108,6 +122,58 @@ def _check_object_exists(): utils.retry(_check_object_exists, times=20, wait=3) +@then(parsers.parse( + "a pod with label '{label}' in namespace '{namespace}' has metrics")) +def pod_has_metrics(label, namespace, k8s_apiclient): + def _pod_has_metrics(): + result = k8s_apiclient.call_api( + resource_path='/apis/metrics.k8s.io/v1beta1/' + 'namespaces/{namespace}/pods', + method='GET', + response_type=object, + path_params={ + 'namespace': namespace, + }, + query_params=[ + ('labelSelector', label), + ], + _return_http_data_only=True, + ) + + assert result['apiVersion'] == 'metrics.k8s.io/v1beta1' + assert result['kind'] == 'PodMetricsList' + assert result['items'] != [] + assert result['items'][0]['containers'] != [] + assert result['items'][0]['containers'][0]['usage']['cpu'] + assert result['items'][0]['containers'][0]['usage']['memory'] + + # Metrics are only available after a while (by design) + utils.retry(_pod_has_metrics, times=60, wait=3) + + +@then(parsers.parse("a node with label '{label}' has metrics")) +def node_has_metrics(label, k8s_apiclient): + def _node_has_metrics(): + result = k8s_apiclient.call_api( + resource_path='/apis/metrics.k8s.io/v1beta1/nodes', + method='GET', + response_type=object, + query_params=[ + ('labelSelector', label), + ], + _return_http_data_only=True, + ) + + assert result['apiVersion'] == 'metrics.k8s.io/v1beta1' + assert result['kind'] == 'NodeMetricsList' + assert result['items'] != [] + assert result['items'][0]['usage']['cpu'] + assert result['items'][0]['usage']['memory'] + + # Metrics are only available after a while (by design) + utils.retry(_node_has_metrics, times=60, wait=3) + + # }}} # Helpers {{{