blob: 845b4a7b391899d4896351e1086affc3f90f1d59 [file] [log] [blame]
Attila Fazekas7cf2a222013-08-02 13:49:10 +02001# 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
13import socket
14import subprocess
15
Fei Long Wangd39431f2015-05-14 11:30:48 +120016from tempest.common.utils import data_utils
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +000017from tempest.common import waiters
Matthew Treinish88f49ef2014-01-29 18:36:27 +000018from tempest import config
Jordan Pittier35a63752016-08-30 13:09:12 +020019from tempest.lib.common.utils import test_utils
Attila Fazekas7cf2a222013-08-02 13:49:10 +020020import tempest.stress.stressaction as stressaction
Attila Fazekas7cf2a222013-08-02 13:49:10 +020021
Matthew Treinish88f49ef2014-01-29 18:36:27 +000022CONF = config.CONF
23
Attila Fazekas7cf2a222013-08-02 13:49:10 +020024
25class 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 Rosen4fd95092014-09-22 16:10:46 -070034 proc.communicate()
Attila Fazekas7cf2a222013-08-02 13:49:10 +020035 success = proc.returncode == 0
Attila Fazekas7cf2a222013-08-02 13:49:10 +020036 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 Pittier35a63752016-08-30 13:09:12 +020055 if not test_utils.call_until_true(func, self.check_timeout,
56 self.check_interval):
Attila Fazekas7cf2a222013-08-02 13:49:10 +020057 raise RuntimeError("Cannot connect to the ssh port.")
58
59 def check_icmp_echo(self):
Attila Fazekasd789b542014-04-25 07:01:22 +020060 self.logger.info("%s(%s): Pinging..",
61 self.server_id, self.floating['ip'])
62
Attila Fazekas7cf2a222013-08-02 13:49:10 +020063 def func():
64 return self.ping_ip_address(self.floating['ip'])
Jordan Pittier35a63752016-08-30 13:09:12 +020065 if not test_utils.call_until_true(func, self.check_timeout,
66 self.check_interval):
Attila Fazekasd789b542014-04-25 07:01:22 +020067 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 Fazekas7cf2a222013-08-02 13:49:10 +020071
72 def _create_vm(self):
zhufl63ce4882016-09-06 16:04:35 +080073 self.name = name = data_utils.rand_name(
74 self.__class__.__name__ + "-instance")
Attila Fazekas7cf2a222013-08-02 13:49:10 +020075 servers_client = self.manager.servers_client
76 self.logger.info("creating %s" % name)
77 vm_args = self.vm_extra_args.copy()
Attila Fazekas6c7244a2014-02-26 15:11:48 +010078 vm_args['security_groups'] = [self.sec_grp]
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000079 server = servers_client.create_server(name=name, imageRef=self.image,
80 flavorRef=self.flavor,
ghanshyam0f825252015-08-25 16:02:50 +090081 **vm_args)['server']
Attila Fazekas7cf2a222013-08-02 13:49:10 +020082 self.server_id = server['id']
Attila Fazekas7cf2a222013-08-02 13:49:10 +020083 if self.wait_after_vm_create:
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +000084 waiters.wait_for_server_status(self.manager.servers_client,
85 self.server_id, 'ACTIVE')
Attila Fazekas7cf2a222013-08-02 13:49:10 +020086
87 def _destroy_vm(self):
88 self.logger.info("deleting %s" % self.server_id)
ghanshyam51e84f42014-10-02 17:28:18 +090089 self.manager.servers_client.delete_server(self.server_id)
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +000090 waiters.wait_for_server_termination(self.manager.servers_client,
91 self.server_id)
Attila Fazekas7cf2a222013-08-02 13:49:10 +020092 self.logger.info("deleted %s" % self.server_id)
93
94 def _create_sec_group(self):
John Warrenf2345512015-12-10 13:39:30 -050095 sec_grp_cli = self.manager.compute_security_groups_client
zhufl63ce4882016-09-06 16:04:35 +080096 s_name = data_utils.rand_name(self.__class__.__name__ + '-sec_grp')
Ken'ichi Ohmichi80369a92015-04-06 23:41:14 +000097 s_description = data_utils.rand_name('desc')
Ken'ichi Ohmichi34563cc2015-07-21 00:53:17 +000098 self.sec_grp = sec_grp_cli.create_security_group(
ghanshyamb610b772015-08-24 17:29:38 +090099 name=s_name, description=s_description)['security_group']
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200100 create_rule = sec_grp_cli.create_security_group_rule
Ken'ichi Ohmichieb7eeec2015-07-21 01:00:06 +0000101 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 Fazekas7cf2a222013-08-02 13:49:10 +0200105
106 def _destroy_sec_grp(self):
John Warrenf2345512015-12-10 13:39:30 -0500107 sec_grp_cli = self.manager.compute_security_groups_client
Attila Fazekas6c7244a2014-02-26 15:11:48 +0100108 sec_grp_cli.delete_security_group(self.sec_grp['id'])
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200109
110 def _create_floating_ip(self):
John Warrene74890a2015-11-11 15:18:01 -0500111 floating_cli = self.manager.compute_floating_ips_client
ghanshyam9a3a9a22015-08-18 17:03:55 +0900112 self.floating = (floating_cli.create_floating_ip(self.floating_pool)
113 ['floating_ip'])
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200114
115 def _destroy_floating_ip(self):
John Warrene74890a2015-11-11 15:18:01 -0500116 cli = self.manager.compute_floating_ips_client
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200117 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 Treinish88f49ef2014-01-29 18:36:27 +0000122 self.image = CONF.compute.image_ref
123 self.flavor = CONF.compute.flavor_ref
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200124 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 Warrene74890a2015-11-11 15:18:01 -0500150 cli = self.manager.compute_floating_ips_client
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200151
152 def func():
ghanshyam9a3a9a22015-08-18 17:03:55 +0900153 floating = (cli.show_floating_ip(self.floating['id'])
154 ['floating_ip'])
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200155 return floating['instance_id'] is None
156
Jordan Pittier35a63752016-08-30 13:09:12 +0200157 if not test_utils.call_until_true(func, self.check_timeout,
158 self.check_interval):
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200159 raise RuntimeError("IP disassociate timeout!")
160
161 def run_core(self):
John Warrene74890a2015-11-11 15:18:01 -0500162 cli = self.manager.compute_floating_ips_client
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200163 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 Ohmichi0eb153c2015-07-13 02:18:25 +0000182 waiters.wait_for_server_status(self.manager.servers_client,
183 self.server_id, 'ACTIVE')
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200184
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()