python-observabilityclient 0.4.0__tar.gz → 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. python_observabilityclient-1.0.0/.coveragerc +7 -0
  2. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/.zuul.yaml +10 -2
  3. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/AUTHORS +2 -0
  4. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/ChangeLog +22 -0
  5. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/PKG-INFO +1 -1
  6. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/observabilityclient/prometheus_client.py +18 -7
  7. {python_observabilityclient-0.4.0/observabilityclient/v1 → python_observabilityclient-1.0.0/observabilityclient}/rbac.py +58 -52
  8. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/observabilityclient/tests/functional/test_cli.py +39 -81
  9. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/observabilityclient/tests/functional/test_python_api.py +2 -2
  10. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/observabilityclient/tests/unit/test_cli.py +6 -4
  11. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/observabilityclient/tests/unit/test_prometheus_client.py +7 -4
  12. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/observabilityclient/tests/unit/test_python_api.py +25 -23
  13. python_observabilityclient-1.0.0/observabilityclient/tests/unit/test_rbac.py +188 -0
  14. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/observabilityclient/tests/unit/test_utils.py +77 -6
  15. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/observabilityclient/utils/metric_utils.py +39 -5
  16. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/observabilityclient/v1/base.py +21 -2
  17. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/observabilityclient/v1/client.py +9 -4
  18. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/observabilityclient/v1/python_api.py +13 -9
  19. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/python_observabilityclient.egg-info/PKG-INFO +1 -1
  20. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/python_observabilityclient.egg-info/SOURCES.txt +5 -1
  21. python_observabilityclient-1.0.0/python_observabilityclient.egg-info/pbr.json +1 -0
  22. python_observabilityclient-1.0.0/releasenotes/notes/add-root-path-192ac0e6ea99b84f.yaml +9 -0
  23. python_observabilityclient-1.0.0/releasenotes/notes/refactor-rbac-967ed5edcc1ef2a4.yaml +13 -0
  24. python_observabilityclient-1.0.0/releasenotes/source/2025.1.rst +6 -0
  25. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/releasenotes/source/index.rst +1 -0
  26. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/setup.cfg +0 -8
  27. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/tox.ini +2 -2
  28. python_observabilityclient-0.4.0/observabilityclient/tests/unit/test_rbac.py +0 -145
  29. python_observabilityclient-0.4.0/python_observabilityclient.egg-info/pbr.json +0 -1
  30. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/LICENSE +0 -0
  31. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/README.md +0 -0
  32. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/doc/requirements.txt +0 -0
  33. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/observabilityclient/__init__.py +0 -0
  34. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/observabilityclient/client.py +0 -0
  35. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/observabilityclient/i18n.py +0 -0
  36. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/observabilityclient/plugin.py +0 -0
  37. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/observabilityclient/tests/functional/__init__.py +0 -0
  38. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/observabilityclient/tests/functional/base.py +0 -0
  39. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/observabilityclient/tests/unit/__init__.py +0 -0
  40. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/observabilityclient/utils/__init__.py +0 -0
  41. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/observabilityclient/v1/__init__.py +0 -0
  42. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/observabilityclient/v1/cli.py +0 -0
  43. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/pyproject.toml +0 -0
  44. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/python_observabilityclient.egg-info/dependency_links.txt +0 -0
  45. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/python_observabilityclient.egg-info/entry_points.txt +0 -0
  46. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/python_observabilityclient.egg-info/not-zip-safe +0 -0
  47. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/python_observabilityclient.egg-info/requires.txt +0 -0
  48. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/python_observabilityclient.egg-info/top_level.txt +0 -0
  49. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/releasenotes/notes/remove-py38-acd461c4350f0dca.yaml +0 -0
  50. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/releasenotes/source/_static/.placeholder +0 -0
  51. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/releasenotes/source/conf.py +0 -0
  52. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/releasenotes/source/unreleased.rst +0 -0
  53. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/requirements.txt +0 -0
  54. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/setup.py +0 -0
  55. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/test-requirements.txt +0 -0
  56. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/tools/fix_ca_bundle.sh +0 -0
  57. {python_observabilityclient-0.4.0 → python_observabilityclient-1.0.0}/tools/install_deps.sh +0 -0
@@ -0,0 +1,7 @@
1
+ [run]
2
+ branch = True
3
+ source = observabilityclient
4
+ omit = observabilityclient/tests/*
5
+
6
+ [report]
7
+ ignore_errors = True
@@ -8,16 +8,24 @@
8
8
  required-projects:
9
9
  - openstack/python-observabilityclient
10
10
  - openstack/ceilometer
11
- - infrawatch/sg-core
11
+ - openstack-k8s-operators/sg-core
12
+ - openstack/devstack-plugin-prometheus
12
13
  timeout: 4200
13
14
  vars:
14
15
  devstack_localrc:
15
16
  USE_PYTHON3: True
16
17
  PROMETHEUS_SERVICE_SCRAPE_TARGETS: prometheus,sg-core
17
18
  CEILOMETER_BACKEND: sg-core
19
+ PROMETHEUS_CUSTOM_SCRAPE_TARGETS: "localhost:3000,localhost:9090"
18
20
  devstack_plugins:
19
- sg-core: https://github.com/infrawatch/sg-core
21
+ sg-core: https://github.com/openstack-k8s-operators/sg-core
20
22
  ceilometer: https://opendev.org/openstack/ceilometer
23
+ devstack-plugin-prometheus: https://opendev.org/openstack/devstack-plugin-prometheus
24
+ devstack_services:
25
+ node_exporter: false
26
+ zuul_copy_output:
27
+ /etc/prometheus/prometheus.yml: logs
28
+ /etc/openstack/prometheus.yaml: logs
21
29
 
22
30
  - project:
23
31
  queue: telemetry
@@ -9,4 +9,6 @@ Leif Madsen <lmadsen@redhat.com>
9
9
  Marihan Girgis mgirgisf@redhat.com
10
10
  Martin Magr <mmagr@redhat.com>
11
11
  Martin Mágr <mmagr@redhat.com>
12
+ OpenStack Release Bot <infra-root@openstack.org>
12
13
  Takashi Kajinami <kajinamit@oss.nttdata.com>
14
+ Victoria Martinez de la Cruz <victoria@redhat.com>
@@ -1,6 +1,28 @@
1
1
  CHANGES
2
2
  =======
3
3
 
4
+ 1.0.0
5
+ -----
6
+
7
+ * Retrieve prometheus information from keystone
8
+ * Exclude test code from coverage report
9
+ * Deprecate disable-rbac option
10
+ * Use PROMETHEUS\_CUSTOM\_SCRAPE\_TARGETS to pass sg\_core scrape endpoint
11
+
12
+ 0.5.0
13
+ -----
14
+
15
+ * Add root\_path configuration option
16
+ * Refactor obsC rbac module
17
+ * Enable providing keystone session for Prometheus
18
+ * Additional tests and a fix for rbac code
19
+ * Fix rbac mocking in unit tests
20
+ * Fix observabilityclient-dsvm-functional job
21
+ * Bump hacking
22
+ * Check if the HOME env var is set, otherwise use a default
23
+ * Update master for stable/2025.1
24
+ * Ignore false-positive E231 error
25
+
4
26
  0.4.0
5
27
  -----
6
28
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-observabilityclient
3
- Version: 0.4.0
3
+ Version: 1.0.0
4
4
  Summary: OpenStack Observability Client
5
5
  Home-page: https://infrawatch.github.io/documentation/
6
6
  Author: OpenStack
@@ -53,10 +53,18 @@ class PrometheusMetric(object):
53
53
 
54
54
 
55
55
  class PrometheusAPIClient(object):
56
- def __init__(self, host):
56
+ def __init__(self, host, session=None, root_path=""):
57
57
  self._host = host
58
- self._session = requests.Session()
58
+ if not self._host.endswith('/'):
59
+ self._host += '/'
60
+ if session is None:
61
+ self._session = requests.Session()
62
+ else:
63
+ self._session = session
59
64
  self._session.verify = False
65
+ self._root_path = root_path
66
+ if root_path != "" and not self._root_path.endswith('/'):
67
+ self._root_path += '/'
60
68
 
61
69
  def set_ca_cert(self, ca_cert):
62
70
  self._session.verify = ca_cert
@@ -68,11 +76,15 @@ class PrometheusAPIClient(object):
68
76
  def set_basic_auth(self, auth_user, auth_password):
69
77
  self._session.auth = (auth_user, auth_password)
70
78
 
79
+ def _get_url(self, endpoint):
80
+ scheme = 'https' if self._session.verify else 'http'
81
+ return f"{scheme}://{self._host}{self._root_path}api/v1/{endpoint}"
82
+
71
83
  def _get(self, endpoint, params=None):
72
- url = (f"{'https' if self._session.verify else 'http'}://"
73
- f"{self._host}/api/v1/{endpoint}")
84
+ url = self._get_url(endpoint)
74
85
  resp = self._session.get(url, params=params,
75
- headers={'Accept': 'application/json'})
86
+ headers={'Accept': 'application/json',
87
+ 'Accept-Encoding': 'identity'})
76
88
  if resp.status_code != requests.codes.ok:
77
89
  raise PrometheusAPIClientError(resp)
78
90
  decoded = resp.json()
@@ -82,8 +94,7 @@ class PrometheusAPIClient(object):
82
94
  return decoded
83
95
 
84
96
  def _post(self, endpoint, params=None):
85
- url = (f"{'https' if self._session.verify else 'http'}://"
86
- f"{self._host}/api/v1/{endpoint}")
97
+ url = self._get_url(endpoint)
87
98
  resp = self._session.post(url, params=params,
88
99
  headers={'Accept': 'application/json'})
89
100
  if resp.status_code != requests.codes.ok:
@@ -14,32 +14,15 @@
14
14
 
15
15
  import re
16
16
 
17
- from keystoneauth1.exceptions.auth_plugins import MissingAuthPlugin
18
-
19
17
  from observabilityclient.utils.metric_utils import format_labels
20
18
 
21
19
 
22
- class ObservabilityRbacError(Exception):
23
- pass
24
-
25
-
26
- class Rbac(object):
27
- def __init__(self, client, session, disable_rbac=False):
28
- self.client = client
29
- self.session = session
30
- self.disable_rbac = disable_rbac
31
- try:
32
- self.project_id = self.session.get_project_id()
33
- self.default_labels = {
34
- "project": self.project_id
35
- }
36
- self.rbac_init_successful = True
37
- except MissingAuthPlugin:
38
- self.project_id = None
39
- self.default_labels = {
40
- "project": "no-project"
41
- }
42
- self.rbac_init_successful = False
20
+ class PromQLRbac(object):
21
+ def __init__(self, prom_api_client, project_id):
22
+ self.client = prom_api_client
23
+ self.labels = {
24
+ "project": project_id
25
+ }
43
26
 
44
27
  def _find_label_value_end(self, query, start, quote_char):
45
28
  end = start
@@ -71,6 +54,10 @@ class Rbac(object):
71
54
  return end
72
55
 
73
56
  def _find_label_section_end(self, query, start):
57
+ if query[start:].startswith('{}'):
58
+ # We have an empty label section, without any
59
+ # label pair
60
+ return start + 1
74
61
  nearest_curly_brace_pos = None
75
62
  while nearest_curly_brace_pos != -1:
76
63
  pair_end = self._find_label_pair_end(query, start)
@@ -84,23 +71,26 @@ class Rbac(object):
84
71
  # and the "=" or "~" is a part of the next section.
85
72
  return nearest_curly_brace_pos
86
73
  start = pair_end
74
+ # TODO(tkajinam): We should probably raise an exception here because
75
+ # this indicates illegal format without closing } .
87
76
  return -1
88
77
 
89
- def enrich_query(self, query, disable_rbac=False):
90
- """Add rbac labels to queries.
78
+ def _insert_labels(self, query, location, comma=False, braces=False):
79
+ comma_str = ", " if comma else ""
80
+ formatted_labels = format_labels(self.labels)
81
+ labels_str = f"{{{formatted_labels}}}" if braces else formatted_labels
82
+ return (f"{query[:location]}{comma_str}"
83
+ f"{labels_str}"
84
+ f"{query[location:]}")
85
+
86
+ def modify_query(self, query):
87
+ """Add rbac labels to a query.
91
88
 
92
- :param query: The query to enrich
89
+ :param query: The query to modify
93
90
  :type query: str
94
- :param disable_rbac: Disables rbac injection if set to True
95
- :type disable_rbac: boolean
96
91
  """
97
- # TODO(jwysogla): This should be properly tested
98
- if disable_rbac:
99
- return query
100
- labels = self.default_labels
101
-
102
92
  # We need to get all metric names, no matter the rbac
103
- metric_names = self.client.query.list(disable_rbac=False)
93
+ metric_names = self.client.label_values("__name__")
104
94
 
105
95
  # We need to detect the locations of metric names
106
96
  # inside the query
@@ -117,35 +107,51 @@ class Rbac(object):
117
107
  name_end_locations.append(potential_name.end())
118
108
 
119
109
  name_end_locations = sorted(name_end_locations, reverse=True)
110
+ if len(name_end_locations) == 0:
111
+ name_end_locations = [0]
120
112
  for name_end_location in name_end_locations:
121
113
  if (name_end_location < len(query) and
122
114
  query[name_end_location] == "{"):
123
- # There already are some labels
124
- labels_end = self._find_label_section_end(query,
125
- name_end_location)
126
- query = (f"{query[:labels_end]}, "
127
- f"{format_labels(labels)}"
128
- f"{query[labels_end:]}")
115
+ # There is already a label section
116
+ labels_end = self._find_label_section_end(
117
+ query,
118
+ name_end_location
119
+ )
120
+ if labels_end == name_end_location + 1:
121
+ query = self._insert_labels(
122
+ query,
123
+ labels_end,
124
+ comma=False,
125
+ braces=False
126
+ )
127
+ else:
128
+ query = self._insert_labels(
129
+ query,
130
+ labels_end,
131
+ comma=True,
132
+ braces=False
133
+ )
129
134
  else:
130
- query = (f"{query[:name_end_location]}"
131
- f"{{{format_labels(labels)}}}"
132
- f"{query[name_end_location:]}")
135
+ query = self._insert_labels(
136
+ query,
137
+ name_end_location,
138
+ comma=False,
139
+ braces=True
140
+ )
133
141
  return query
134
142
 
135
- def append_rbac(self, query, disable_rbac=False):
143
+ def append_rbac_labels(self, query):
136
144
  """Append rbac labels to queries.
137
145
 
138
- It's a simplified and faster version of enrich_query(). This just
146
+ It's a simplified and faster version of modify_query(). This just
139
147
  appends the labels at the end of the query string. For proper handling
140
148
  of complex queries, where metric names might occure elsewhere than
141
- just at the end, please use the enrich_query() function.
149
+ just at the end, please use the modify_query() function.
142
150
 
143
151
  :param query: The query to append to
144
152
  :type query: str
145
- :param disable_rbac: Disables rbac injection if set to True
146
- :type disable_rbac: boolean
147
153
  """
148
- labels = self.default_labels
149
- if disable_rbac:
150
- return query
151
- return f"{query}{{{format_labels(labels)}}}"
154
+ if any(c in query for c in "{}"):
155
+ return self.modify_query(query)
156
+ else:
157
+ return f"{query}{{{format_labels(self.labels)}}}"
@@ -14,98 +14,56 @@ from observabilityclient.tests.functional import base
14
14
  import time
15
15
 
16
16
 
17
- class CliTestFunctionalRBACDisabled(base.CliTestCase):
18
- """Functional tests for cli commands with disabled RBAC."""
17
+ class CliTestFunctionalRBACNOOP(base.CliTestCase):
18
+ """Functional tests for cli commands testing RBAC option."""
19
19
 
20
20
  def test_list(self):
21
- cmd_output = self.openstack(
22
- 'metric list --disable-rbac',
23
- parse_output=True,
24
- )
25
- name_list = [item.get('metric_name') for item in cmd_output]
26
- self.assertIn(
27
- 'up',
28
- name_list
29
- )
30
-
31
- def test_show(self):
32
- cmd_output = self.openstack(
33
- 'metric show up --disable-rbac',
34
- parse_output=True,
35
- )
36
- for metric in cmd_output:
37
- self.assertEqual(
38
- "up",
39
- metric["__name__"]
21
+ for cmdstr in ('metric list', 'metric list --disable-rbac'):
22
+ cmd_output = self.openstack(
23
+ cmdstr,
24
+ parse_output=True,
40
25
  )
41
- self.assertEqual(
42
- "1",
43
- metric["value"]
26
+ name_list = [item.get('metric_name') for item in cmd_output]
27
+ self.assertIn(
28
+ 'ceilometer_image_size',
29
+ name_list
44
30
  )
45
-
46
- def test_query(self):
47
- cmd_output = self.openstack(
48
- 'metric query up --disable-rbac',
49
- parse_output=True,
50
- )
51
- for metric in cmd_output:
52
- self.assertEqual(
53
- "up",
54
- metric["__name__"]
55
- )
56
- self.assertEqual(
57
- "1",
58
- metric["value"]
31
+ self.assertIn(
32
+ 'up',
33
+ name_list
59
34
  )
60
35
 
61
-
62
- class CliTestFunctionalRBACEnabled(base.CliTestCase):
63
- """Functional tests for cli commands with enabled RBAC."""
64
-
65
- def test_list(self):
66
- cmd_output = self.openstack(
67
- 'metric list',
68
- parse_output=True,
69
- )
70
- name_list = [item.get('metric_name') for item in cmd_output]
71
- self.assertIn(
72
- 'ceilometer_image_size',
73
- name_list
74
- )
75
- self.assertNotIn(
76
- 'up',
77
- name_list
78
- )
79
-
80
36
  def test_show(self):
81
- cmd_output = self.openstack(
82
- 'metric show ceilometer_image_size',
83
- parse_output=True,
84
- )
85
- for metric in cmd_output:
86
- self.assertEqual(
87
- "ceilometer_image_size",
88
- metric["__name__"]
89
- )
90
- self.assertEqual(
91
- "sg-core",
92
- metric["job"]
37
+ for cmdstr in ('metric show up', 'metric show up --disable-rbac'):
38
+ cmd_output = self.openstack(
39
+ cmdstr,
40
+ parse_output=True,
93
41
  )
42
+ for metric in cmd_output:
43
+ self.assertEqual(
44
+ "up",
45
+ metric["__name__"]
46
+ )
47
+ self.assertEqual(
48
+ "1",
49
+ metric["value"]
50
+ )
94
51
 
95
52
  def test_query(self):
96
- cmd_output = self.openstack(
97
- 'metric query ceilometer_image_size',
98
- parse_output=True,
99
- )
100
- for metric in cmd_output:
101
- self.assertEqual(
102
- "ceilometer_image_size",
103
- metric["__name__"]
104
- )
105
- self.assertEqual(
106
- "sg-core",
107
- metric["job"]
53
+ for cmdstr in ('metric query up', 'metric query up --disable-rbac'):
54
+ cmd_output = self.openstack(
55
+ cmdstr,
56
+ parse_output=True,
108
57
  )
58
+ for metric in cmd_output:
59
+ self.assertEqual(
60
+ "up",
61
+ metric["__name__"]
62
+ )
63
+ self.assertEqual(
64
+ "1",
65
+ metric["value"]
66
+ )
109
67
 
110
68
 
111
69
  class CliTestFunctionalAdminCommands(base.CliTestCase):
@@ -52,7 +52,7 @@ class PythonAPITestFunctionalRBACEnabled(base.PythonAPITestCase):
52
52
  for metric in ret:
53
53
  self.assertEqual("ceilometer_image_size",
54
54
  metric.labels["__name__"])
55
- self.assertEqual("sg-core",
55
+ self.assertEqual("custom",
56
56
  metric.labels["job"])
57
57
 
58
58
  def test_query(self):
@@ -62,7 +62,7 @@ class PythonAPITestFunctionalRBACEnabled(base.PythonAPITestCase):
62
62
  for metric in ret:
63
63
  self.assertEqual("ceilometer_image_size",
64
64
  metric.labels["__name__"])
65
- self.assertEqual("sg-core", metric.labels["job"])
65
+ self.assertEqual("custom", metric.labels["job"])
66
66
 
67
67
 
68
68
  class PythonAPITestFunctionalAdminCommands(base.PythonAPITestCase):
@@ -43,8 +43,9 @@ class CliTest(testtools.TestCase):
43
43
  return_value=self.client), \
44
44
  mock.patch.object(self.client.query, 'list',
45
45
  return_value=metric_names) as m:
46
+ # NOTE: disable-rbac option is NOOP, RBAC is disabled by default
46
47
  ret1 = cli_list.take_action(test_parsed_args_enabled)
47
- m.assert_called_with(disable_rbac=False)
48
+ m.assert_called_with(disable_rbac=True)
48
49
 
49
50
  ret2 = cli_list.take_action(test_parsed_args_disabled)
50
51
  m.assert_called_with(disable_rbac=True)
@@ -75,9 +76,9 @@ class CliTest(testtools.TestCase):
75
76
  return_value=self.client), \
76
77
  mock.patch.object(self.client.query, 'show',
77
78
  return_value=prom_metric) as m:
78
-
79
+ # NOTE: disable-rbac option is NOOP, RBAC is disabled by default
79
80
  ret1 = cli_show.take_action(test_parsed_args_enabled)
80
- m.assert_called_with('metric_name', disable_rbac=False)
81
+ m.assert_called_with('metric_name', disable_rbac=True)
81
82
 
82
83
  ret2 = cli_show.take_action(test_parsed_args_disabled)
83
84
  m.assert_called_with('metric_name', disable_rbac=True)
@@ -113,8 +114,9 @@ class CliTest(testtools.TestCase):
113
114
  mock.patch.object(self.client.query, 'query',
114
115
  return_value=prom_metric) as m:
115
116
 
117
+ # NOTE: disable-rbac option is NOOP, RBAC is disabled by default
116
118
  ret1 = cli_query.take_action(test_parsed_args_enabled)
117
- m.assert_called_with(query, disable_rbac=False)
119
+ m.assert_called_with(query, disable_rbac=True)
118
120
 
119
121
  ret2 = cli_query.take_action(test_parsed_args_disabled)
120
122
  m.assert_called_with(query, disable_rbac=True)
@@ -85,7 +85,8 @@ class PrometheusAPIClientTestBase(testtools.TestCase):
85
85
  class PrometheusAPIClientTest(PrometheusAPIClientTestBase):
86
86
  def test_get(self):
87
87
  url = "test"
88
- expected_url = "http://localhost:9090/api/v1/test"
88
+ root_path = "root_path"
89
+ expected_url = f"http://localhost:9090/{root_path}/api/v1/{url}"
89
90
 
90
91
  params = {"query": "ceilometer_image_size{publisher='localhost'}"}
91
92
  expected_params = params
@@ -93,12 +94,14 @@ class PrometheusAPIClientTest(PrometheusAPIClientTestBase):
93
94
  return_value = self.GoodResponse()
94
95
  with mock.patch.object(requests.Session, 'get',
95
96
  return_value=return_value) as m:
96
- c = client.PrometheusAPIClient("localhost:9090")
97
+ c = client.PrometheusAPIClient("localhost:9090",
98
+ root_path=root_path)
97
99
  c._get(url, params)
98
100
 
99
101
  m.assert_called_with(expected_url,
100
102
  params=expected_params,
101
- headers={'Accept': 'application/json'})
103
+ headers={'Accept': 'application/json',
104
+ 'Accept-Encoding': 'identity'})
102
105
 
103
106
  def test_get_error(self):
104
107
  url = "test"
@@ -120,7 +123,7 @@ class PrometheusAPIClientTest(PrometheusAPIClientTestBase):
120
123
 
121
124
  def test_post(self):
122
125
  url = "test"
123
- expected_url = "http://localhost:9090/api/v1/test"
126
+ expected_url = f"http://localhost:9090/api/v1/{url}"
124
127
 
125
128
  params = {"query": "ceilometer_image_size{publisher='localhost'}"}
126
129
  expected_params = params
@@ -17,11 +17,11 @@ from unittest import mock
17
17
  import testtools
18
18
 
19
19
  from observabilityclient import prometheus_client
20
+ from observabilityclient import rbac
20
21
  from observabilityclient.tests.unit.test_prometheus_client import (
21
22
  MetricListMatcher
22
23
  )
23
24
  from observabilityclient.v1 import python_api
24
- from observabilityclient.v1 import rbac
25
25
 
26
26
 
27
27
  class QueryManagerTest(testtools.TestCase):
@@ -31,9 +31,8 @@ class QueryManagerTest(testtools.TestCase):
31
31
  prom_client = prometheus_client.PrometheusAPIClient("somehost")
32
32
  self.client.prometheus_client = prom_client
33
33
 
34
- self.rbac = mock.Mock(wraps=rbac.Rbac(self.client, mock.Mock()))
35
- self.rbac.default_labels = {'project': 'project_id'}
36
- self.rbac.rbac_init_succesful = True
34
+ self.rbac = mock.Mock(wraps=rbac.PromQLRbac(prom_client, mock.Mock()))
35
+ self.rbac.labels = {'project': 'project_id'}
37
36
 
38
37
  self.manager = python_api.QueryManager(self.client)
39
38
 
@@ -41,16 +40,16 @@ class QueryManagerTest(testtools.TestCase):
41
40
  self.client.query = self.manager
42
41
 
43
42
  def test_list(self):
44
- returned_by_prom = {'data': ['metric1', 'test42', 'abc2']}
43
+ returned_data = {'data': ['metric1', 'test42', 'abc2']}
45
44
  expected = ['abc2', 'metric1', 'test42']
46
45
 
47
46
  with mock.patch.object(prometheus_client.PrometheusAPIClient, '_get',
48
- return_value=returned_by_prom):
47
+ return_value=returned_data):
49
48
  ret1 = self.manager.list()
50
- ret2 = self.manager.list(disable_rbac=True)
49
+ self.assertEqual(expected, ret1)
51
50
 
52
- self.assertEqual(expected, ret1)
53
- self.assertEqual(expected, ret2)
51
+ ret2 = self.manager.list(disable_rbac=True)
52
+ self.assertEqual(expected, ret2)
54
53
 
55
54
  def test_show(self):
56
55
  query = 'some_metric'
@@ -65,21 +64,23 @@ class QueryManagerTest(testtools.TestCase):
65
64
  }
66
65
  expected = [prometheus_client.PrometheusMetric(returned_by_prom)]
67
66
  expected_matcher = MetricListMatcher(expected)
67
+
68
68
  with mock.patch.object(prometheus_client.PrometheusAPIClient, '_get',
69
69
  return_value=returned_by_prom):
70
- ret1 = self.manager.show(query)
71
- self.rbac.append_rbac.assert_called_with(query,
72
- disable_rbac=False)
73
-
74
70
  ret2 = self.manager.show(query, disable_rbac=True)
75
- self.rbac.append_rbac.assert_called_with(query,
76
- disable_rbac=True)
71
+ self.rbac.append_rbac_labels.assert_not_called()
72
+
73
+ with mock.patch.object(prometheus_client.PrometheusAPIClient, '_get',
74
+ return_value=returned_by_prom):
75
+ ret1 = self.manager.show(query)
76
+ self.rbac.append_rbac_labels.assert_not_called()
77
77
 
78
78
  self.assertThat(ret1, expected_matcher)
79
79
  self.assertThat(ret2, expected_matcher)
80
80
 
81
81
  def test_query(self):
82
- query = 'some_metric'
82
+ queried_metric_name = 'some_metric'
83
+ query = queried_metric_name
83
84
  returned_by_prom = {
84
85
  'data': {
85
86
  'resultType': 'non-vector'
@@ -92,14 +93,15 @@ class QueryManagerTest(testtools.TestCase):
92
93
  expected = [prometheus_client.PrometheusMetric(returned_by_prom)]
93
94
  expected_matcher = MetricListMatcher(expected)
94
95
  with mock.patch.object(prometheus_client.PrometheusAPIClient, '_get',
95
- return_value=returned_by_prom):
96
- ret1 = self.manager.query(query)
97
- self.rbac.enrich_query.assert_called_with(query,
98
- disable_rbac=False)
96
+ return_value=returned_by_prom), \
97
+ mock.patch.object(python_api.QueryManager, 'list',
98
+ return_value=queried_metric_name):
99
+
100
+ ret1 = self.manager.query(query, disable_rbac=True)
101
+ self.rbac.modify_query.assert_not_called()
99
102
 
100
- ret2 = self.manager.query(query, disable_rbac=True)
101
- self.rbac.enrich_query.assert_called_with(query,
102
- disable_rbac=True)
103
+ ret2 = self.manager.query(query)
104
+ self.rbac.modify_query.assert_not_called()
103
105
 
104
106
  self.assertThat(ret1, expected_matcher)
105
107
  self.assertThat(ret2, expected_matcher)