Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | # you may not use this file except in compliance with the License. |
| 3 | # You may obtain 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, |
| 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | # See the License for the specific language governing permissions and |
| 11 | # limitations under the License. |
| 12 | |
| 13 | import socket |
| 14 | import subprocess |
| 15 | |
Fei Long Wang | d39431f | 2015-05-14 11:30:48 +1200 | [diff] [blame] | 16 | from tempest.common.utils import data_utils |
Ken'ichi Ohmichi | 0eb153c | 2015-07-13 02:18:25 +0000 | [diff] [blame] | 17 | from tempest.common import waiters |
Matthew Treinish | 88f49ef | 2014-01-29 18:36:27 +0000 | [diff] [blame] | 18 | from tempest import config |
Jordan Pittier | 35a6375 | 2016-08-30 13:09:12 +0200 | [diff] [blame] | 19 | from tempest.lib.common.utils import test_utils |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 20 | import tempest.stress.stressaction as stressaction |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 21 | |
Matthew Treinish | 88f49ef | 2014-01-29 18:36:27 +0000 | [diff] [blame] | 22 | CONF = config.CONF |
| 23 | |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 24 | |
| 25 | class FloatingStress(stressaction.StressAction): |
| 26 | |
| 27 | # from the scenario manager |
| 28 | def ping_ip_address(self, ip_address): |
| 29 | cmd = ['ping', '-c1', '-w1', ip_address] |
| 30 | |
| 31 | proc = subprocess.Popen(cmd, |
| 32 | stdout=subprocess.PIPE, |
| 33 | stderr=subprocess.PIPE) |
Aaron Rosen | 4fd9509 | 2014-09-22 16:10:46 -0700 | [diff] [blame] | 34 | proc.communicate() |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 35 | success = proc.returncode == 0 |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 36 | return success |
| 37 | |
| 38 | def tcp_connect_scan(self, addr, port): |
| 39 | # like tcp |
| 40 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 41 | try: |
| 42 | s.connect((addr, port)) |
| 43 | except socket.error as exc: |
| 44 | self.logger.info("%s(%s): %s", self.server_id, self.floating['ip'], |
| 45 | str(exc)) |
| 46 | return False |
| 47 | self.logger.info("%s(%s): Connected :)", self.server_id, |
| 48 | self.floating['ip']) |
| 49 | s.close() |
| 50 | return True |
| 51 | |
| 52 | def check_port_ssh(self): |
| 53 | def func(): |
| 54 | return self.tcp_connect_scan(self.floating['ip'], 22) |
Jordan Pittier | 35a6375 | 2016-08-30 13:09:12 +0200 | [diff] [blame] | 55 | if not test_utils.call_until_true(func, self.check_timeout, |
| 56 | self.check_interval): |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 57 | raise RuntimeError("Cannot connect to the ssh port.") |
| 58 | |
| 59 | def check_icmp_echo(self): |
Attila Fazekas | d789b54 | 2014-04-25 07:01:22 +0200 | [diff] [blame] | 60 | self.logger.info("%s(%s): Pinging..", |
| 61 | self.server_id, self.floating['ip']) |
| 62 | |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 63 | def func(): |
| 64 | return self.ping_ip_address(self.floating['ip']) |
Jordan Pittier | 35a6375 | 2016-08-30 13:09:12 +0200 | [diff] [blame] | 65 | if not test_utils.call_until_true(func, self.check_timeout, |
| 66 | self.check_interval): |
Attila Fazekas | d789b54 | 2014-04-25 07:01:22 +0200 | [diff] [blame] | 67 | raise RuntimeError("%s(%s): Cannot ping the machine.", |
| 68 | self.server_id, self.floating['ip']) |
| 69 | self.logger.info("%s(%s): pong :)", |
| 70 | self.server_id, self.floating['ip']) |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 71 | |
| 72 | def _create_vm(self): |
zhufl | 63ce488 | 2016-09-06 16:04:35 +0800 | [diff] [blame] | 73 | self.name = name = data_utils.rand_name( |
| 74 | self.__class__.__name__ + "-instance") |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 75 | servers_client = self.manager.servers_client |
| 76 | self.logger.info("creating %s" % name) |
| 77 | vm_args = self.vm_extra_args.copy() |
Attila Fazekas | 6c7244a | 2014-02-26 15:11:48 +0100 | [diff] [blame] | 78 | vm_args['security_groups'] = [self.sec_grp] |
Ken'ichi Ohmichi | f2d436e | 2015-09-03 01:13:16 +0000 | [diff] [blame] | 79 | server = servers_client.create_server(name=name, imageRef=self.image, |
| 80 | flavorRef=self.flavor, |
ghanshyam | 0f82525 | 2015-08-25 16:02:50 +0900 | [diff] [blame] | 81 | **vm_args)['server'] |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 82 | self.server_id = server['id'] |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 83 | if self.wait_after_vm_create: |
Ken'ichi Ohmichi | 0eb153c | 2015-07-13 02:18:25 +0000 | [diff] [blame] | 84 | waiters.wait_for_server_status(self.manager.servers_client, |
| 85 | self.server_id, 'ACTIVE') |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 86 | |
| 87 | def _destroy_vm(self): |
| 88 | self.logger.info("deleting %s" % self.server_id) |
ghanshyam | 51e84f4 | 2014-10-02 17:28:18 +0900 | [diff] [blame] | 89 | self.manager.servers_client.delete_server(self.server_id) |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 90 | waiters.wait_for_server_termination(self.manager.servers_client, |
| 91 | self.server_id) |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 92 | self.logger.info("deleted %s" % self.server_id) |
| 93 | |
| 94 | def _create_sec_group(self): |
John Warren | f234551 | 2015-12-10 13:39:30 -0500 | [diff] [blame] | 95 | sec_grp_cli = self.manager.compute_security_groups_client |
zhufl | 63ce488 | 2016-09-06 16:04:35 +0800 | [diff] [blame] | 96 | s_name = data_utils.rand_name(self.__class__.__name__ + '-sec_grp') |
Ken'ichi Ohmichi | 80369a9 | 2015-04-06 23:41:14 +0000 | [diff] [blame] | 97 | s_description = data_utils.rand_name('desc') |
Ken'ichi Ohmichi | 34563cc | 2015-07-21 00:53:17 +0000 | [diff] [blame] | 98 | self.sec_grp = sec_grp_cli.create_security_group( |
ghanshyam | b610b77 | 2015-08-24 17:29:38 +0900 | [diff] [blame] | 99 | name=s_name, description=s_description)['security_group'] |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 100 | create_rule = sec_grp_cli.create_security_group_rule |
Ken'ichi Ohmichi | eb7eeec | 2015-07-21 01:00:06 +0000 | [diff] [blame] | 101 | create_rule(parent_group_id=self.sec_grp['id'], ip_protocol='tcp', |
| 102 | from_port=22, to_port=22) |
| 103 | create_rule(parent_group_id=self.sec_grp['id'], ip_protocol='icmp', |
| 104 | from_port=-1, to_port=-1) |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 105 | |
| 106 | def _destroy_sec_grp(self): |
John Warren | f234551 | 2015-12-10 13:39:30 -0500 | [diff] [blame] | 107 | sec_grp_cli = self.manager.compute_security_groups_client |
Attila Fazekas | 6c7244a | 2014-02-26 15:11:48 +0100 | [diff] [blame] | 108 | sec_grp_cli.delete_security_group(self.sec_grp['id']) |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 109 | |
| 110 | def _create_floating_ip(self): |
John Warren | e74890a | 2015-11-11 15:18:01 -0500 | [diff] [blame] | 111 | floating_cli = self.manager.compute_floating_ips_client |
ghanshyam | 9a3a9a2 | 2015-08-18 17:03:55 +0900 | [diff] [blame] | 112 | self.floating = (floating_cli.create_floating_ip(self.floating_pool) |
| 113 | ['floating_ip']) |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 114 | |
| 115 | def _destroy_floating_ip(self): |
John Warren | e74890a | 2015-11-11 15:18:01 -0500 | [diff] [blame] | 116 | cli = self.manager.compute_floating_ips_client |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 117 | cli.delete_floating_ip(self.floating['id']) |
| 118 | cli.wait_for_resource_deletion(self.floating['id']) |
| 119 | self.logger.info("Deleted Floating IP %s", str(self.floating['ip'])) |
| 120 | |
| 121 | def setUp(self, **kwargs): |
Matthew Treinish | 88f49ef | 2014-01-29 18:36:27 +0000 | [diff] [blame] | 122 | self.image = CONF.compute.image_ref |
| 123 | self.flavor = CONF.compute.flavor_ref |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 124 | self.vm_extra_args = kwargs.get('vm_extra_args', {}) |
| 125 | self.wait_after_vm_create = kwargs.get('wait_after_vm_create', |
| 126 | True) |
| 127 | self.new_vm = kwargs.get('new_vm', False) |
| 128 | self.new_sec_grp = kwargs.get('new_sec_group', False) |
| 129 | self.new_floating = kwargs.get('new_floating', False) |
| 130 | self.reboot = kwargs.get('reboot', False) |
| 131 | self.floating_pool = kwargs.get('floating_pool', None) |
| 132 | self.verify = kwargs.get('verify', ('check_port_ssh', |
| 133 | 'check_icmp_echo')) |
| 134 | self.check_timeout = kwargs.get('check_timeout', 120) |
| 135 | self.check_interval = kwargs.get('check_interval', 1) |
| 136 | self.wait_for_disassociate = kwargs.get('wait_for_disassociate', |
| 137 | True) |
| 138 | |
| 139 | # allocate floating |
| 140 | if not self.new_floating: |
| 141 | self._create_floating_ip() |
| 142 | # add security group |
| 143 | if not self.new_sec_grp: |
| 144 | self._create_sec_group() |
| 145 | # create vm |
| 146 | if not self.new_vm: |
| 147 | self._create_vm() |
| 148 | |
| 149 | def wait_disassociate(self): |
John Warren | e74890a | 2015-11-11 15:18:01 -0500 | [diff] [blame] | 150 | cli = self.manager.compute_floating_ips_client |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 151 | |
| 152 | def func(): |
ghanshyam | 9a3a9a2 | 2015-08-18 17:03:55 +0900 | [diff] [blame] | 153 | floating = (cli.show_floating_ip(self.floating['id']) |
| 154 | ['floating_ip']) |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 155 | return floating['instance_id'] is None |
| 156 | |
Jordan Pittier | 35a6375 | 2016-08-30 13:09:12 +0200 | [diff] [blame] | 157 | if not test_utils.call_until_true(func, self.check_timeout, |
| 158 | self.check_interval): |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 159 | raise RuntimeError("IP disassociate timeout!") |
| 160 | |
| 161 | def run_core(self): |
John Warren | e74890a | 2015-11-11 15:18:01 -0500 | [diff] [blame] | 162 | cli = self.manager.compute_floating_ips_client |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 163 | cli.associate_floating_ip_to_server(self.floating['ip'], |
| 164 | self.server_id) |
| 165 | for method in self.verify: |
| 166 | m = getattr(self, method) |
| 167 | m() |
| 168 | cli.disassociate_floating_ip_from_server(self.floating['ip'], |
| 169 | self.server_id) |
| 170 | if self.wait_for_disassociate: |
| 171 | self.wait_disassociate() |
| 172 | |
| 173 | def run(self): |
| 174 | if self.new_sec_grp: |
| 175 | self._create_sec_group() |
| 176 | if self.new_floating: |
| 177 | self._create_floating_ip() |
| 178 | if self.new_vm: |
| 179 | self._create_vm() |
| 180 | if self.reboot: |
| 181 | self.manager.servers_client.reboot(self.server_id, 'HARD') |
Ken'ichi Ohmichi | 0eb153c | 2015-07-13 02:18:25 +0000 | [diff] [blame] | 182 | waiters.wait_for_server_status(self.manager.servers_client, |
| 183 | self.server_id, 'ACTIVE') |
Attila Fazekas | 7cf2a22 | 2013-08-02 13:49:10 +0200 | [diff] [blame] | 184 | |
| 185 | self.run_core() |
| 186 | |
| 187 | if self.new_vm: |
| 188 | self._destroy_vm() |
| 189 | if self.new_floating: |
| 190 | self._destroy_floating_ip() |
| 191 | if self.new_sec_grp: |
| 192 | self._destroy_sec_grp() |
| 193 | |
| 194 | def tearDown(self): |
| 195 | if not self.new_vm: |
| 196 | self._destroy_vm() |
| 197 | if not self.new_floating: |
| 198 | self._destroy_floating_ip() |
| 199 | if not self.new_sec_grp: |
| 200 | self._destroy_sec_grp() |