File: base.py

package info (click to toggle)
python-aodhclient 3.9.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 596 kB
  • sloc: python: 2,812; sh: 34; makefile: 24
file content (110 lines) | stat: -rw-r--r-- 4,085 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#    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.

import os
import time

from openstack import config as occ
from oslo_utils import uuidutils
from tempest.lib.cli import base
from tempest.lib import exceptions


class AodhClient:
    """Aodh Client for tempest-lib

    This client doesn't use any authentication system
    """

    def __init__(self):
        self.cli_dir = os.environ.get('AODH_CLIENT_EXEC_DIR')
        self.endpoint = os.environ.get('AODH_ENDPOINT')
        self.cloud = os.environ.get('OS_ADMIN_CLOUD', 'devstack-admin')
        self.user_id = uuidutils.generate_uuid()
        self.project_id = uuidutils.generate_uuid()

    def aodh(self, action, flags='', params='',
             fail_ok=False, merge_stderr=False):
        auth_args = []
        if self.cloud is None:
            auth_args.append("--os-auth-type none")
        elif self.cloud != '':
            conf = occ.OpenStackConfig()
            creds = conf.get_one(cloud=self.cloud).get_auth_args()
            auth_args.append(f"--os-auth-url {creds['auth_url']}")
            auth_args.append(f"--os-username {creds['username']}")
            auth_args.append(f"--os-password {creds['password']}")
            auth_args.append(f"--os-project-name {creds['project_name']}")
            auth_args.append(f"--os-user-domain-id {creds['user_domain_id']}")
            auth_args.append("--os-project-domain-id "
                             f"{creds['project_domain_id']}")
        endpoint_arg = "--aodh-endpoint %s" % self.endpoint

        flags = " ".join(auth_args + [endpoint_arg] + [flags])

        return base.execute("aodh", action, flags, params, fail_ok,
                            merge_stderr, self.cli_dir)


class ClientTestBase(base.ClientTestBase):
    """Base class for aodhclient tests.

    Establishes the aodhclient and retrieves the essential environment
    information.
    """

    def _get_clients(self):
        return AodhClient()

    def retry_aodh(self, retry, *args, **kwargs):
        result = ""
        while not result.strip() and retry > 0:
            result = self.aodh(*args, **kwargs)
            if not result:
                time.sleep(1)
                retry -= 1
        return result

    def aodh(self, *args, **kwargs):
        return self.clients.aodh(*args, **kwargs)

    def get_token(self):
        cloud = os.environ.get('OS_ADMIN_CLOUD', 'devstack-admin')
        if cloud is not None and cloud != "":
            conf = occ.OpenStackConfig()
            region_conf = conf.get_one(cloud=cloud)
            return region_conf.get_auth().get_token(region_conf.get_session())
        else:
            return ""

    def details_multiple(self, output_lines, with_label=False):
        """Return list of dicts with item details from cli output tables.

        If with_label is True, key '__label' is added to each items dict.
        For more about 'label' see OutputParser.tables().

        NOTE(sileht): come from tempest-lib just because cliff use
        Field instead of Property as first columun header.
        """
        items = []
        tables_ = self.parser.tables(output_lines)
        for table_ in tables_:
            if ('Field' not in table_['headers']
                    or 'Value' not in table_['headers']):
                raise exceptions.InvalidStructure()
            item = {}
            for value in table_['values']:
                item[value[0]] = value[1]
            if with_label:
                item['__label'] = table_['label']
            items.append(item)
        return items