Skip to content

Commit

Permalink
tests: test metrics.k8s.io/v1beta1
Browse files Browse the repository at this point in the history
We assume that if this API is available and returns 'expected' results,
the `kubectl top` command works (since it uses this API under the hood).

See: 241c355
See: #2049
See: #2057
  • Loading branch information
NicolasT committed Dec 12, 2019
1 parent ec577c8 commit 456cf72
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/post/features/monitoring.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
66 changes: 66 additions & 0 deletions tests/post/steps/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {{{

Expand Down Expand Up @@ -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 {{{

Expand Down

0 comments on commit 456cf72

Please sign in to comment.