Debian Codegraph @

heatclient/tests/functional/osc/v1/base.py

raw symbols SCIP
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
15 from tempest.lib.cli import base
16 from tempest.lib.cli import output_parser
17 from tempest.lib import exceptions as tempest_exc
18
19
20 class OpenStackClientTestBase(base.ClientTestBase):
21 """Command line client base functions."""
22
23 def setUp(self):
24 super().setUp()
25 self.parser = output_parser
26
27 def _get_clients(self):
28 cli_dir = os.environ.get(
29 'OS_HEATCLIENT_EXEC_DIR',
30 os.path.join(os.path.abspath('.'), '.tox/functional/bin'))
31
32 return base.CLIClient(
33 username=os.environ.get('OS_USERNAME'),
34 password=os.environ.get('OS_PASSWORD'),
35 tenant_name=os.environ.get('OS_TENANT_NAME'),
36 uri=os.environ.get('OS_AUTH_URL'),
37 cli_dir=cli_dir)
38
39 def openstack(self, *args, **kwargs):
40 return self.clients.openstack(*args, **kwargs)
41
42 def get_template_path(self, templ_name):
43 return os.path.join(os.path.dirname(os.path.realpath(__file__)),
44 '../../templates/%s' % templ_name)
45
46 def show_to_dict(self, output):
47 obj = {}
48 items = self.parser.listing(output)
49 for item in items:
50 obj[item['Field']] = str(item['Value'])
51 return {self._key_name(k): v for k, v in obj.items()}
52
53 def _key_name(self, key):
54 return key.lower().replace(' ', '_')
55
56 def list_to_dict(self, output, id):
57 obj = {}
58 items = self.parser.listing(output)
59 for item in items:
60 if item['ID'] == id:
61 obj = item
62 break
63 return {self._key_name(k): v for k, v in obj.items()}
64
65 def _stack_create(self, name, template, parameters=[], wait=True):
66 cmd = 'stack create ' + name
67 if template:
68 cmd += ' -t ' + template
69 if wait:
70 cmd += ' --wait'
71
72 for parameter in parameters:
73 cmd += ' --parameter ' + parameter
74 stack_raw = self.openstack(cmd)
75 stack = self.show_to_dict(stack_raw)
76 self.addCleanup(self._stack_delete, stack['id'])
77 return stack
78
79 def _stack_delete(self, id, wait=False):
80 cmd = 'stack delete ' + id + ' --yes'
81 if wait:
82 cmd += ' --wait'
83 if id in self.openstack('stack list --short'):
84 try:
85 self.openstack(cmd)
86 except tempest_exc.CommandFailed as e:
87 msg = "Stack not found: %s" % id
88 if msg in str(e.stdout):
89 return
90 raise
91
92 def _stack_suspend(self, id, wait=True):
93 cmd = 'stack suspend ' + id
94 if wait:
95 cmd += ' --wait'
96 stack_raw = self.openstack(cmd)
97 stack = self.list_to_dict(stack_raw, id)
98 return stack
99
100 def _stack_resume(self, id, wait=True):
101 cmd = 'stack resume ' + id
102 if wait:
103 cmd += ' --wait'
104 stack_raw = self.openstack(cmd)
105 stack = self.list_to_dict(stack_raw, id)
106 return stack
107
108 def _stack_snapshot_create(self, id, name):
109 cmd = 'stack snapshot create ' + id + ' --name ' + name
110 snapshot_raw = self.openstack(cmd)
111 snapshot = self.show_to_dict(snapshot_raw)
112 self.addCleanup(self._stack_snapshot_delete, id, snapshot['id'])
113 return snapshot
114
115 def _stack_snapshot_delete(self, id, snapshot_id):
116 cmd = 'stack snapshot delete ' + id + ' ' + snapshot_id
117 if snapshot_id in self.openstack('stack snapshot list ' + id):
118 self.openstack(cmd)
119
120 def _stack_snapshot_restore(self, id, snapshot_id):
121 cmd = 'stack snapshot restore ' + id + ' ' + snapshot_id
122 self.openstack(cmd)
123
124 def _stack_show(self, stack_id):
125 cmd = 'stack show ' + stack_id
126 stack_raw = self.openstack(cmd)
127 return self.show_to_dict(stack_raw)