Debian Codegraph @

aodhclient/tests/functional/base.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import os
14 import time
15
16 from openstack import config as occ
17 from oslo_utils import uuidutils
18 from tempest.lib.cli import base
19 from tempest.lib import exceptions
20
21
22 class AodhClient:
23 """Aodh Client for tempest-lib
24
25 This client doesn't use any authentication system
26 """
27
28 def __init__(self):
29 self.cli_dir = os.environ.get('AODH_CLIENT_EXEC_DIR')
30 self.endpoint = os.environ.get('AODH_ENDPOINT')
31 self.cloud = os.environ.get('OS_ADMIN_CLOUD', 'devstack-admin')
32 self.user_id = uuidutils.generate_uuid()
33 self.project_id = uuidutils.generate_uuid()
34
35 def aodh(self, action, flags='', params='',
36 fail_ok=False, merge_stderr=False):
37 auth_args = []
38 if self.cloud is None:
39 auth_args.append("--os-auth-type none")
40 elif self.cloud != '':
41 conf = occ.OpenStackConfig()
42 creds = conf.get_one(cloud=self.cloud).get_auth_args()
43 auth_args.append(f"--os-auth-url {creds['auth_url']}")
44 auth_args.append(f"--os-username {creds['username']}")
45 auth_args.append(f"--os-password {creds['password']}")
46 auth_args.append(f"--os-project-name {creds['project_name']}")
47 auth_args.append(f"--os-user-domain-id {creds['user_domain_id']}")
48 auth_args.append("--os-project-domain-id "
49 f"{creds['project_domain_id']}")
50 endpoint_arg = "--aodh-endpoint %s" % self.endpoint
51
52 flags = " ".join(auth_args + [endpoint_arg] + [flags])
53
54 return base.execute("aodh", action, flags, params, fail_ok,
55 merge_stderr, self.cli_dir)
56
57
58 class ClientTestBase(base.ClientTestBase):
59 """Base class for aodhclient tests.
60
61 Establishes the aodhclient and retrieves the essential environment
62 information.
63 """
64
65 def _get_clients(self):
66 return AodhClient()
67
68 def retry_aodh(self, retry, *args, **kwargs):
69 result = ""
70 while not result.strip() and retry > 0:
71 result = self.aodh(*args, **kwargs)
72 if not result:
73 time.sleep(1)
74 retry -= 1
75 return result
76
77 def aodh(self, *args, **kwargs):
78 return self.clients.aodh(*args, **kwargs)
79
80 def get_token(self):
81 cloud = os.environ.get('OS_ADMIN_CLOUD', 'devstack-admin')
82 if cloud is not None and cloud != "":
83 conf = occ.OpenStackConfig()
84 region_conf = conf.get_one(cloud=cloud)
85 return region_conf.get_auth().get_token(region_conf.get_session())
86 else:
87 return ""
88
89 def details_multiple(self, output_lines, with_label=False):
90 """Return list of dicts with item details from cli output tables.
91
92 If with_label is True, key '__label' is added to each items dict.
93 For more about 'label' see OutputParser.tables().
94
95 NOTE(sileht): come from tempest-lib just because cliff use
96 Field instead of Property as first columun header.
97 """
98 items = []
99 tables_ = self.parser.tables(output_lines)
100 for table_ in tables_:
101 if ('Field' not in table_['headers']
102 or 'Value' not in table_['headers']):
103 raise exceptions.InvalidStructure()
104 item = {}
105 for value in table_['values']:
106 item[value[0]] = value[1]
107 if with_label:
108 item['__label'] = table_['label']
109 items.append(item)
110 return items