blob: 9fdb394d5d0bb831197c72cd8d0ae9872dcdc77a [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
Matthew Treinish01472ff2015-02-20 17:26:52 -050016from tempest_lib.common.utils import data_utils
17
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:
83 self.manager.servers_client.wait_for_server_status(self.server_id,
84 'ACTIVE')
85
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)
Attila Fazekas7cf2a222013-08-02 13:49:10 +020089 self.manager.servers_client.wait_for_server_termination(self.server_id)
90 self.logger.info("deleted %s" % self.server_id)
91
92 def _create_sec_group(self):
93 sec_grp_cli = self.manager.security_groups_client
Masayuki Igawa259c1132013-10-31 17:48:44 +090094 s_name = data_utils.rand_name('sec_grp-')
95 s_description = data_utils.rand_name('desc-')
David Kranz9964b4e2015-02-06 15:45:29 -050096 self.sec_grp = sec_grp_cli.create_security_group(s_name,
97 s_description)
Attila Fazekas7cf2a222013-08-02 13:49:10 +020098 create_rule = sec_grp_cli.create_security_group_rule
Attila Fazekas6c7244a2014-02-26 15:11:48 +010099 create_rule(self.sec_grp['id'], 'tcp', 22, 22)
100 create_rule(self.sec_grp['id'], 'icmp', -1, -1)
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200101
102 def _destroy_sec_grp(self):
103 sec_grp_cli = self.manager.security_groups_client
Attila Fazekas6c7244a2014-02-26 15:11:48 +0100104 sec_grp_cli.delete_security_group(self.sec_grp['id'])
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200105
106 def _create_floating_ip(self):
107 floating_cli = self.manager.floating_ips_client
David Kranz668d3892015-02-16 09:20:08 -0500108 self.floating = floating_cli.create_floating_ip(self.floating_pool)
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200109
110 def _destroy_floating_ip(self):
111 cli = self.manager.floating_ips_client
112 cli.delete_floating_ip(self.floating['id'])
113 cli.wait_for_resource_deletion(self.floating['id'])
114 self.logger.info("Deleted Floating IP %s", str(self.floating['ip']))
115
116 def setUp(self, **kwargs):
Matthew Treinish88f49ef2014-01-29 18:36:27 +0000117 self.image = CONF.compute.image_ref
118 self.flavor = CONF.compute.flavor_ref
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200119 self.vm_extra_args = kwargs.get('vm_extra_args', {})
120 self.wait_after_vm_create = kwargs.get('wait_after_vm_create',
121 True)
122 self.new_vm = kwargs.get('new_vm', False)
123 self.new_sec_grp = kwargs.get('new_sec_group', False)
124 self.new_floating = kwargs.get('new_floating', False)
125 self.reboot = kwargs.get('reboot', False)
126 self.floating_pool = kwargs.get('floating_pool', None)
127 self.verify = kwargs.get('verify', ('check_port_ssh',
128 'check_icmp_echo'))
129 self.check_timeout = kwargs.get('check_timeout', 120)
130 self.check_interval = kwargs.get('check_interval', 1)
131 self.wait_for_disassociate = kwargs.get('wait_for_disassociate',
132 True)
133
134 # allocate floating
135 if not self.new_floating:
136 self._create_floating_ip()
137 # add security group
138 if not self.new_sec_grp:
139 self._create_sec_group()
140 # create vm
141 if not self.new_vm:
142 self._create_vm()
143
144 def wait_disassociate(self):
145 cli = self.manager.floating_ips_client
146
147 def func():
Ken'ichi Ohmichi5628f3f2015-05-22 20:17:56 +0000148 floating = cli.show_floating_ip(self.floating['id'])
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200149 return floating['instance_id'] is None
150
151 if not tempest.test.call_until_true(func, self.check_timeout,
152 self.check_interval):
153 raise RuntimeError("IP disassociate timeout!")
154
155 def run_core(self):
156 cli = self.manager.floating_ips_client
157 cli.associate_floating_ip_to_server(self.floating['ip'],
158 self.server_id)
159 for method in self.verify:
160 m = getattr(self, method)
161 m()
162 cli.disassociate_floating_ip_from_server(self.floating['ip'],
163 self.server_id)
164 if self.wait_for_disassociate:
165 self.wait_disassociate()
166
167 def run(self):
168 if self.new_sec_grp:
169 self._create_sec_group()
170 if self.new_floating:
171 self._create_floating_ip()
172 if self.new_vm:
173 self._create_vm()
174 if self.reboot:
175 self.manager.servers_client.reboot(self.server_id, 'HARD')
Attila Fazekasd789b542014-04-25 07:01:22 +0200176 self.manager.servers_client.wait_for_server_status(self.server_id,
177 'ACTIVE')
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200178
179 self.run_core()
180
181 if self.new_vm:
182 self._destroy_vm()
183 if self.new_floating:
184 self._destroy_floating_ip()
185 if self.new_sec_grp:
186 self._destroy_sec_grp()
187
188 def tearDown(self):
189 if not self.new_vm:
190 self._destroy_vm()
191 if not self.new_floating:
192 self._destroy_floating_ip()
193 if not self.new_sec_grp:
194 self._destroy_sec_grp()