|
| 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 | + |
| 14 | +import json |
| 15 | +import time |
| 16 | +import uuid |
| 17 | + |
| 18 | +from tempest.lib import exceptions |
| 19 | + |
| 20 | +from openstackclient.tests.functional import base |
| 21 | + |
| 22 | + |
| 23 | +class ComputeTestCase(base.TestCase): |
| 24 | + """Common functional test bits for Compute commands""" |
| 25 | + |
| 26 | + flavor_name = None |
| 27 | + image_name = None |
| 28 | + network_arg = None |
| 29 | + |
| 30 | + def setUp(self): |
| 31 | + """Select common resources""" |
| 32 | + super(ComputeTestCase, self).setUp() |
| 33 | + self.flavor_name = self.get_flavor() |
| 34 | + self.image_name = self.get_image() |
| 35 | + self.network_arg = self.get_network() |
| 36 | + |
| 37 | + @classmethod |
| 38 | + def get_flavor(cls): |
| 39 | + # NOTE(rtheis): Get cirros256 or m1.tiny flavors since functional |
| 40 | + # tests may create other flavors. |
| 41 | + flavors = json.loads(cls.openstack( |
| 42 | + "flavor list -f json " |
| 43 | + )) |
| 44 | + server_flavor = None |
| 45 | + for flavor in flavors: |
| 46 | + if flavor['Name'] in ['m1.tiny', 'cirros256']: |
| 47 | + server_flavor = flavor['Name'] |
| 48 | + break |
| 49 | + return server_flavor |
| 50 | + |
| 51 | + @classmethod |
| 52 | + def get_image(cls): |
| 53 | + # NOTE(rtheis): Get first Cirros image since functional tests may |
| 54 | + # create other images. Image may be named '-uec' or |
| 55 | + # '-disk'. |
| 56 | + images = json.loads(cls.openstack( |
| 57 | + "image list -f json " |
| 58 | + )) |
| 59 | + server_image = None |
| 60 | + for image in images: |
| 61 | + if (image['Name'].startswith('cirros-') and |
| 62 | + (image['Name'].endswith('-uec') or |
| 63 | + image['Name'].endswith('-disk'))): |
| 64 | + server_image = image['Name'] |
| 65 | + break |
| 66 | + return server_image |
| 67 | + |
| 68 | + @classmethod |
| 69 | + def get_network(cls): |
| 70 | + try: |
| 71 | + # NOTE(rtheis): Get private network since functional tests may |
| 72 | + # create other networks. |
| 73 | + cmd_output = json.loads(cls.openstack( |
| 74 | + 'network show private -f json' |
| 75 | + )) |
| 76 | + except exceptions.CommandFailed: |
| 77 | + return '' |
| 78 | + return '--nic net-id=' + cmd_output['id'] |
| 79 | + |
| 80 | + def server_create(self, name=None, cleanup=True): |
| 81 | + """Create server, with cleanup""" |
| 82 | + if not self.flavor_name: |
| 83 | + self.flavor_name = self.get_flavor() |
| 84 | + if not self.image_name: |
| 85 | + self.image_name = self.get_image() |
| 86 | + if not self.network_arg: |
| 87 | + self.network_arg = self.get_network() |
| 88 | + name = name or uuid.uuid4().hex |
| 89 | + cmd_output = json.loads(self.openstack( |
| 90 | + 'server create -f json ' + |
| 91 | + '--flavor ' + self.flavor_name + ' ' + |
| 92 | + '--image ' + self.image_name + ' ' + |
| 93 | + self.network_arg + ' ' + |
| 94 | + '--wait ' + |
| 95 | + name |
| 96 | + )) |
| 97 | + self.assertIsNotNone(cmd_output["id"]) |
| 98 | + self.assertEqual( |
| 99 | + name, |
| 100 | + cmd_output["name"], |
| 101 | + ) |
| 102 | + if cleanup: |
| 103 | + self.addCleanup(self.server_delete, name) |
| 104 | + return cmd_output |
| 105 | + |
| 106 | + def server_delete(self, name): |
| 107 | + """Delete server by name""" |
| 108 | + raw_output = self.openstack('server delete ' + name) |
| 109 | + self.assertOutput('', raw_output) |
| 110 | + |
| 111 | + def wait_for_status( |
| 112 | + self, |
| 113 | + name, |
| 114 | + expected_status='ACTIVE', |
| 115 | + wait=900, |
| 116 | + interval=10, |
| 117 | + ): |
| 118 | + """Wait until server reaches expected status""" |
| 119 | + # TODO(thowe): Add a server wait command to osc |
| 120 | + failures = ['ERROR'] |
| 121 | + total_sleep = 0 |
| 122 | + while total_sleep < wait: |
| 123 | + cmd_output = json.loads(self.openstack( |
| 124 | + 'server show -f json ' + |
| 125 | + name |
| 126 | + )) |
| 127 | + status = cmd_output['status'] |
| 128 | + print('Waiting for {}, current status: {}'.format( |
| 129 | + expected_status, |
| 130 | + status, |
| 131 | + )) |
| 132 | + if status == expected_status: |
| 133 | + break |
| 134 | + self.assertNotIn(status, failures) |
| 135 | + time.sleep(interval) |
| 136 | + total_sleep += interval |
| 137 | + |
| 138 | + cmd_output = json.loads(self.openstack( |
| 139 | + 'server show -f json ' + |
| 140 | + name |
| 141 | + )) |
| 142 | + status = cmd_output['status'] |
| 143 | + self.assertEqual(status, expected_status) |
| 144 | + # give it a little bit more time |
| 145 | + time.sleep(5) |
0 commit comments