
Something must have changed either with cliff or with a different version of python. All of the cli commands end with: "'Namespace' object is not subscriptable" This is caused because of how the parsed_args fields were accessed. This patch addresses the issue. Now it accesses the fields the same way as aodhclient does. The unit tests for cli now use the arg parser to actually test this. During this I also discovered incorrect mocking in the configuration tests. I added another mock to the test cases which were missing it. Change-Id: Ib5dbbdb48bdfd3214ec76acc4c68649e25f695db
167 lines
5.8 KiB
Python
167 lines
5.8 KiB
Python
# Copyright 2023 Red Hat, Inc.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
from unittest import mock
|
|
|
|
import testtools
|
|
|
|
from observabilityclient.prometheus_client import PrometheusMetric
|
|
from observabilityclient.utils import metric_utils
|
|
from observabilityclient.v1 import cli
|
|
|
|
|
|
class CliTest(testtools.TestCase):
|
|
def setUp(self):
|
|
super(CliTest, self).setUp()
|
|
self.client = mock.Mock()
|
|
self.client.query = mock.Mock()
|
|
|
|
def test_list(self):
|
|
metric_names = ['name1', 'name2', 'name3']
|
|
expected = (['metric_name'], [['name1'], ['name2'], ['name3']])
|
|
cli_list = cli.List(mock.Mock(), mock.Mock())
|
|
|
|
parser = cli_list.get_parser("metric list")
|
|
test_parsed_args_enabled = parser.parse_args([
|
|
])
|
|
test_parsed_args_disabled = parser.parse_args([
|
|
"--disable-rbac"
|
|
])
|
|
|
|
with mock.patch.object(metric_utils, 'get_client',
|
|
return_value=self.client), \
|
|
mock.patch.object(self.client.query, 'list',
|
|
return_value=metric_names) as m:
|
|
ret1 = cli_list.take_action(test_parsed_args_enabled)
|
|
m.assert_called_with(disable_rbac=False)
|
|
|
|
ret2 = cli_list.take_action(test_parsed_args_disabled)
|
|
m.assert_called_with(disable_rbac=True)
|
|
|
|
self.assertEqual(ret1, expected)
|
|
self.assertEqual(ret2, expected)
|
|
|
|
def test_show(self):
|
|
metric = {
|
|
'value': [123456, 12],
|
|
'metric': {'label1': 'value1'}
|
|
}
|
|
prom_metric = [PrometheusMetric(metric)]
|
|
expected = ['label1', 'value'], [['value1', 12]]
|
|
|
|
cli_show = cli.Show(mock.Mock(), mock.Mock())
|
|
|
|
parser = cli_show.get_parser("metric show")
|
|
test_parsed_args_enabled = parser.parse_args([
|
|
"metric_name"
|
|
])
|
|
test_parsed_args_disabled = parser.parse_args([
|
|
"metric_name",
|
|
"--disable-rbac"
|
|
])
|
|
|
|
with mock.patch.object(metric_utils, 'get_client',
|
|
return_value=self.client), \
|
|
mock.patch.object(self.client.query, 'show',
|
|
return_value=prom_metric) as m:
|
|
|
|
ret1 = cli_show.take_action(test_parsed_args_enabled)
|
|
m.assert_called_with('metric_name', disable_rbac=False)
|
|
|
|
ret2 = cli_show.take_action(test_parsed_args_disabled)
|
|
m.assert_called_with('metric_name', disable_rbac=True)
|
|
|
|
self.assertEqual(ret1, expected)
|
|
self.assertEqual(ret2, expected)
|
|
|
|
def test_query(self):
|
|
query = ("some_query{label!~'not_this_value'} - "
|
|
"sum(second_metric{label='this'})")
|
|
|
|
metric = {
|
|
'value': [123456, 12],
|
|
'metric': {'label1': 'value1'}
|
|
}
|
|
|
|
prom_metric = [PrometheusMetric(metric)]
|
|
expected = ['label1', 'value'], [['value1', 12]]
|
|
|
|
cli_query = cli.Query(mock.Mock(), mock.Mock())
|
|
|
|
parser = cli_query.get_parser("metric query")
|
|
test_parsed_args_enabled = parser.parse_args([
|
|
query
|
|
])
|
|
test_parsed_args_disabled = parser.parse_args([
|
|
query,
|
|
"--disable-rbac"
|
|
])
|
|
|
|
with mock.patch.object(metric_utils, 'get_client',
|
|
return_value=self.client), \
|
|
mock.patch.object(self.client.query, 'query',
|
|
return_value=prom_metric) as m:
|
|
|
|
ret1 = cli_query.take_action(test_parsed_args_enabled)
|
|
m.assert_called_with(query, disable_rbac=False)
|
|
|
|
ret2 = cli_query.take_action(test_parsed_args_disabled)
|
|
m.assert_called_with(query, disable_rbac=True)
|
|
|
|
self.assertEqual(ret1, expected)
|
|
self.assertEqual(ret2, expected)
|
|
|
|
def test_delete(self):
|
|
match1 = "some_label_name"
|
|
match2 = "some_label_name2"
|
|
|
|
cli_delete = cli.Delete(mock.Mock(), mock.Mock())
|
|
|
|
parser = cli_delete.get_parser("metric delete")
|
|
test_parsed_args = parser.parse_args([
|
|
match1, match2,
|
|
"--start", "0",
|
|
"--end", "10"
|
|
])
|
|
|
|
with mock.patch.object(metric_utils, 'get_client',
|
|
return_value=self.client), \
|
|
mock.patch.object(self.client.query, 'delete') as m:
|
|
|
|
cli_delete.take_action(test_parsed_args)
|
|
m.assert_called_with([[match1, match2]], "0", "10")
|
|
|
|
def test_clean_combstones(self):
|
|
cli_clean_tombstones = cli.CleanTombstones(mock.Mock(), mock.Mock())
|
|
|
|
with mock.patch.object(metric_utils, 'get_client',
|
|
return_value=self.client), \
|
|
mock.patch.object(self.client.query, 'clean_tombstones') as m:
|
|
|
|
cli_clean_tombstones.take_action({})
|
|
m.assert_called_once()
|
|
|
|
def test_snapshot(self):
|
|
cli_snapshot = cli.Snapshot(mock.Mock(), mock.Mock())
|
|
file_name = 'some_file_name'
|
|
|
|
with mock.patch.object(metric_utils, 'get_client',
|
|
return_value=self.client), \
|
|
mock.patch.object(self.client.query, 'snapshot',
|
|
return_value=file_name) as m:
|
|
|
|
ret = cli_snapshot.take_action({})
|
|
m.assert_called_once()
|
|
self.assertEqual(ret, (["Snapshot file name"], [[file_name]]))
|