blob: 2505a77dd7017bde7cf12bd93d32444a87936a63 [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
Attila Fazekas7cf2a222013-08-02 13:49:10 +020019import tempest.stress.stressaction as stressaction
20import tempest.test
21
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)
55 if not tempest.test.call_until_true(func, self.check_timeout,
56 self.check_interval):
57 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'])
65 if not tempest.test.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):
Masayuki Igawa259c1132013-10-31 17:48:44 +090073 self.name = name = data_utils.rand_name("instance")
Attila Fazekas7cf2a222013-08-02 13:49:10 +020074 servers_client = self.manager.servers_client
75 self.logger.info("creating %s" % name)
76 vm_args = self.vm_extra_args.copy()
Attila Fazekas6c7244a2014-02-26 15:11:48 +010077 vm_args['security_groups'] = [self.sec_grp]
David Kranz668d3892015-02-16 09:20:08 -050078 server = servers_client.create_server(name, self.image,
79 self.flavor,
80 **vm_args)
Attila Fazekas7cf2a222013-08-02 13:49:10 +020081 self.server_id = server['id']
Attila Fazekas7cf2a222013-08-02 13:49:10 +020082 if self.wait_after_vm_create:
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +000083 waiters.wait_for_server_status(self.manager.servers_client,
84 self.server_id, 'ACTIVE')
Attila Fazekas7cf2a222013-08-02 13:49:10 +020085
86 def _destroy_vm(self):
87 self.logger.info("deleting %s" % self.server_id)
ghanshyam51e84f42014-10-02 17:28:18 +090088 self.manager.servers_client.delete_server(self.server_id)
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +000089 waiters.wait_for_server_termination(self.manager.servers_client,
90 self.server_id)
Attila Fazekas7cf2a222013-08-02 13:49:10 +020091 self.logger.info("deleted %s" % self.server_id)
92
93 def _create_sec_group(self):
94 sec_grp_cli = self.manager.security_groups_client
Ken'ichi Ohmichi80369a92015-04-06 23:41:14 +000095 s_name = data_utils.rand_name('sec_grp')
96 s_description = data_utils.rand_name('desc')
Ken'ichi Ohmichi34563cc2015-07-21 00:53:17 +000097 self.sec_grp = sec_grp_cli.create_security_group(
ghanshyamb610b772015-08-24 17:29:38 +090098 name=s_name, description=s_description)['security_group']
Attila Fazekas7cf2a222013-08-02 13:49:10 +020099 create_rule = sec_grp_cli.create_security_group_rule
Ken'ichi Ohmichieb7eeec2015-07-21 01:00:06 +0000100 create_rule(parent_group_id=self.sec_grp['id'], ip_protocol='tcp',
101 from_port=22, to_port=22)
102 create_rule(parent_group_id=self.sec_grp['id'], ip_protocol='icmp',
103 from_port=-1, to_port=-1)
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200104
105 def _destroy_sec_grp(self):
106 sec_grp_cli = self.manager.security_groups_client
Attila Fazekas6c7244a2014-02-26 15:11:48 +0100107 sec_grp_cli.delete_security_group(self.sec_grp['id'])
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200108
109 def _create_floating_ip(self):
110 floating_cli = self.manager.floating_ips_client
ghanshyam9a3a9a22015-08-18 17:03:55 +0900111 self.floating = (floating_cli.create_floating_ip(self.floating_pool)
112 ['floating_ip'])
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200113
114 def _destroy_floating_ip(self):
115 cli = self.manager.floating_ips_client
116 cli.delete_floating_ip(self.floating['id'])
117 cli.wait_for_resource_deletion(self.floating['id'])
118 self.logger.info("Deleted Floating IP %s", str(self.floating['ip']))
119
120 def setUp(self, **kwargs):
Matthew Treinish88f49ef2014-01-29 18:36:27 +0000121 self.image = CONF.compute.image_ref
122 self.flavor = CONF.compute.flavor_ref
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200123 self.vm_extra_args = kwargs.get('vm_extra_args', {})
124 self.wait_after_vm_create = kwargs.get('wait_after_vm_create',
125 True)
126 self.new_vm = kwargs.get('new_vm', False)
127 self.new_sec_grp = kwargs.get('new_sec_group', False)
128 self.new_floating = kwargs.get('new_floating', False)
129 self.reboot = kwargs.get('reboot', False)
130 self.floating_pool = kwargs.get('floating_pool', None)
131 self.verify = kwargs.get('verify', ('check_port_ssh',
132 'check_icmp_echo'))
133 self.check_timeout = kwargs.get('check_timeout', 120)
134 self.check_interval = kwargs.get('check_interval', 1)
135 self.wait_for_disassociate = kwargs.get('wait_for_disassociate',
136 True)
137
138 # allocate floating
139 if not self.new_floating:
140 self._create_floating_ip()
141 # add security group
142 if not self.new_sec_grp:
143 self._create_sec_group()
144 # create vm
145 if not self.new_vm:
146 self._create_vm()
147
148 def wait_disassociate(self):
149 cli = self.manager.floating_ips_client
150
151 def func():
ghanshyam9a3a9a22015-08-18 17:03:55 +0900152 floating = (cli.show_floating_ip(self.floating['id'])
153 ['floating_ip'])
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200154 return floating['instance_id'] is None
155
156 if not tempest.test.call_until_true(func, self.check_timeout,
157 self.check_interval):
158 raise RuntimeError("IP disassociate timeout!")
159
160 def run_core(self):
161 cli = self.manager.floating_ips_client
162 cli.associate_floating_ip_to_server(self.floating['ip'],
163 self.server_id)
164 for method in self.verify:
165 m = getattr(self, method)
166 m()
167 cli.disassociate_floating_ip_from_server(self.floating['ip'],
168 self.server_id)
169 if self.wait_for_disassociate:
170 self.wait_disassociate()
171
172 def run(self):
173 if self.new_sec_grp:
174 self._create_sec_group()
175 if self.new_floating:
176 self._create_floating_ip()
177 if self.new_vm:
178 self._create_vm()
179 if self.reboot:
180 self.manager.servers_client.reboot(self.server_id, 'HARD')
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +0000181 waiters.wait_for_server_status(self.manager.servers_client,
182 self.server_id, 'ACTIVE')
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200183
184 self.run_core()
185
186 if self.new_vm:
187 self._destroy_vm()
188 if self.new_floating:
189 self._destroy_floating_ip()
190 if self.new_sec_grp:
191 self._destroy_sec_grp()
192
193 def tearDown(self):
194 if not self.new_vm:
195 self._destroy_vm()
196 if not self.new_floating:
197 self._destroy_floating_ip()
198 if not self.new_sec_grp:
199 self._destroy_sec_grp()