File: scenario.py

package info (click to toggle)
rally-openstack 3.0.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,928 kB
  • sloc: python: 53,131; sh: 262; makefile: 38
file content (142 lines) | stat: -rw-r--r-- 5,520 bytes parent folder | download | duplicates (4)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Copyright 2015: Mirantis Inc.
# All Rights Reserved.
#
#    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 functools
import random

from osprofiler import profiler
from rally.common import cfg
from rally.common.plugin import plugin
from rally.task import context
from rally.task import scenario

from rally_openstack.common import osclients


configure = functools.partial(scenario.configure, platform="openstack")

CONF = cfg.CONF


@context.add_default_context("users@openstack", {})
@plugin.default_meta(inherit=True)
class OpenStackScenario(scenario.Scenario):
    """Base class for all OpenStack scenarios."""

    def __init__(self, context=None, admin_clients=None, clients=None):
        super(OpenStackScenario, self).__init__(context)
        if context:
            if admin_clients is None and "admin" in context:
                self._admin_clients = osclients.Clients(
                    context["admin"]["credential"])
            if clients is None:
                if "users" in context and "user" not in context:
                    self._choose_user(context)

                if "user" in context:
                    self._clients = osclients.Clients(
                        context["user"]["credential"])

        if admin_clients:
            self._admin_clients = admin_clients

        if clients:
            self._clients = clients

        self._init_profiler(context)

    def _choose_user(self, context):
        """Choose one user from users context

        We are choosing on each iteration one user

        """
        if context["user_choice_method"] == "random":
            user = random.choice(context["users"])
            tenant = context["tenants"][user["tenant_id"]]
        else:
            # Second and last case - 'round_robin'.
            tenants_amount = len(context["tenants"])
            # NOTE(amaretskiy): iteration is subtracted by `1' because it
            #                   starts from `1' but we count from `0'
            iteration = context["iteration"] - 1
            tenant_index = int(iteration % tenants_amount)
            tenant_id = sorted(context["tenants"].keys())[tenant_index]
            tenant = context["tenants"][tenant_id]
            users = context["tenants"][tenant_id]["users"]
            user_index = int((iteration / tenants_amount) % len(users))
            user = users[user_index]

        context["user"], context["tenant"] = user, tenant

    def clients(self, client_type, version=None):
        """Returns a python openstack client of the requested type.

        Only one non-admin user is used per every run of scenario.

        :param client_type: Client type ("nova"/"glance" etc.)
        :param version: client version ("1"/"2" etc.)

        :returns: Standard python OpenStack client instance
        """
        client = getattr(self._clients, client_type)

        return client(version) if version is not None else client()

    def admin_clients(self, client_type, version=None):
        """Returns a python admin openstack client of the requested type.

        :param client_type: Client type ("nova"/"glance" etc.)
        :param version: client version ("1"/"2" etc.)

        :returns: Python openstack client object
        """
        client = getattr(self._admin_clients, client_type)

        return client(version) if version is not None else client()

    def _init_profiler(self, context):
        """Inits the profiler."""
        if not CONF.openstack.enable_profiler:
            return

        # False statement here means that Scenario class is used outside the
        # runner as some kind of utils
        if context is not None and "iteration" in context:

            profiler_hmac_key = None
            profiler_conn_str = None
            if context.get("admin"):
                cred = context["admin"]["credential"]
                if cred.profiler_hmac_key is not None:
                    profiler_hmac_key = cred.profiler_hmac_key
                    profiler_conn_str = cred.profiler_conn_str
            if context.get("user"):
                cred = context["user"]["credential"]
                if cred.profiler_hmac_key is not None:
                    profiler_hmac_key = cred.profiler_hmac_key
                    profiler_conn_str = cred.profiler_conn_str
            if profiler_hmac_key is None:
                return
            profiler.init(profiler_hmac_key)
            trace_id = profiler.get().get_base_id()
            complete_data = {"title": "OSProfiler Trace-ID",
                             "chart_plugin": "OSProfiler",
                             "data": {"trace_id": trace_id,
                                      "conn_str": profiler_conn_str,
                                      "taskID": context["task"]["uuid"],
                                      "workload_uuid": context["owner_id"],
                                      "iteration": context["iteration"]}}
            self.add_output(complete=complete_data)