blob: 5bc8cacf8d3bf960b640e001a223549ca5817b28 [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
Masayuki Igawa259c1132013-10-31 17:48:44 +090016from tempest.common.utils import data_utils
Matthew Treinish88f49ef2014-01-29 18:36:27 +000017from tempest import config
Attila Fazekas7cf2a222013-08-02 13:49:10 +020018import tempest.stress.stressaction as stressaction
19import tempest.test
20
Matthew Treinish88f49ef2014-01-29 18:36:27 +000021CONF = config.CONF
22
Attila Fazekas7cf2a222013-08-02 13:49:10 +020023
24class FloatingStress(stressaction.StressAction):
25
26 # from the scenario manager
27 def ping_ip_address(self, ip_address):
28 cmd = ['ping', '-c1', '-w1', ip_address]
29
30 proc = subprocess.Popen(cmd,
31 stdout=subprocess.PIPE,
32 stderr=subprocess.PIPE)
Aaron Rosen4fd95092014-09-22 16:10:46 -070033 proc.communicate()
Attila Fazekas7cf2a222013-08-02 13:49:10 +020034 success = proc.returncode == 0
Attila Fazekas7cf2a222013-08-02 13:49:10 +020035 return success
36
37 def tcp_connect_scan(self, addr, port):
38 # like tcp
39 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
40 try:
41 s.connect((addr, port))
42 except socket.error as exc:
43 self.logger.info("%s(%s): %s", self.server_id, self.floating['ip'],
44 str(exc))
45 return False
46 self.logger.info("%s(%s): Connected :)", self.server_id,
47 self.floating['ip'])
48 s.close()
49 return True
50
51 def check_port_ssh(self):
52 def func():
53 return self.tcp_connect_scan(self.floating['ip'], 22)
54 if not tempest.test.call_until_true(func, self.check_timeout,
55 self.check_interval):
56 raise RuntimeError("Cannot connect to the ssh port.")
57
58 def check_icmp_echo(self):
Attila Fazekasd789b542014-04-25 07:01:22 +020059 self.logger.info("%s(%s): Pinging..",
60 self.server_id, self.floating['ip'])
61
Attila Fazekas7cf2a222013-08-02 13:49:10 +020062 def func():
63 return self.ping_ip_address(self.floating['ip'])
64 if not tempest.test.call_until_true(func, self.check_timeout,
65 self.check_interval):
Attila Fazekasd789b542014-04-25 07:01:22 +020066 raise RuntimeError("%s(%s): Cannot ping the machine.",
67 self.server_id, self.floating['ip'])
68 self.logger.info("%s(%s): pong :)",
69 self.server_id, self.floating['ip'])
Attila Fazekas7cf2a222013-08-02 13:49:10 +020070
71 def _create_vm(self):
Masayuki Igawa259c1132013-10-31 17:48:44 +090072 self.name = name = data_utils.rand_name("instance")
Attila Fazekas7cf2a222013-08-02 13:49:10 +020073 servers_client = self.manager.servers_client
74 self.logger.info("creating %s" % name)
75 vm_args = self.vm_extra_args.copy()
Attila Fazekas6c7244a2014-02-26 15:11:48 +010076 vm_args['security_groups'] = [self.sec_grp]
ghanshyam51e84f42014-10-02 17:28:18 +090077 _, server = servers_client.create_server(name, self.image,
78 self.flavor,
79 **vm_args)
Attila Fazekas7cf2a222013-08-02 13:49:10 +020080 self.server_id = server['id']
Attila Fazekas7cf2a222013-08-02 13:49:10 +020081 if self.wait_after_vm_create:
82 self.manager.servers_client.wait_for_server_status(self.server_id,
83 'ACTIVE')
84
85 def _destroy_vm(self):
86 self.logger.info("deleting %s" % self.server_id)
ghanshyam51e84f42014-10-02 17:28:18 +090087 self.manager.servers_client.delete_server(self.server_id)
Attila Fazekas7cf2a222013-08-02 13:49:10 +020088 self.manager.servers_client.wait_for_server_termination(self.server_id)
89 self.logger.info("deleted %s" % self.server_id)
90
91 def _create_sec_group(self):
92 sec_grp_cli = self.manager.security_groups_client
Masayuki Igawa259c1132013-10-31 17:48:44 +090093 s_name = data_utils.rand_name('sec_grp-')
94 s_description = data_utils.rand_name('desc-')
Attila Fazekas6c7244a2014-02-26 15:11:48 +010095 _, self.sec_grp = sec_grp_cli.create_security_group(s_name,
96 s_description)
Attila Fazekas7cf2a222013-08-02 13:49:10 +020097 create_rule = sec_grp_cli.create_security_group_rule
Attila Fazekas6c7244a2014-02-26 15:11:48 +010098 create_rule(self.sec_grp['id'], 'tcp', 22, 22)
99 create_rule(self.sec_grp['id'], 'icmp', -1, -1)
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200100
101 def _destroy_sec_grp(self):
102 sec_grp_cli = self.manager.security_groups_client
Attila Fazekas6c7244a2014-02-26 15:11:48 +0100103 sec_grp_cli.delete_security_group(self.sec_grp['id'])
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200104
105 def _create_floating_ip(self):
106 floating_cli = self.manager.floating_ips_client
107 _, self.floating = floating_cli.create_floating_ip(self.floating_pool)
108
109 def _destroy_floating_ip(self):
110 cli = self.manager.floating_ips_client
111 cli.delete_floating_ip(self.floating['id'])
112 cli.wait_for_resource_deletion(self.floating['id'])
113 self.logger.info("Deleted Floating IP %s", str(self.floating['ip']))
114
115 def setUp(self, **kwargs):
Matthew Treinish88f49ef2014-01-29 18:36:27 +0000116 self.image = CONF.compute.image_ref
117 self.flavor = CONF.compute.flavor_ref
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200118 self.vm_extra_args = kwargs.get('vm_extra_args', {})
119 self.wait_after_vm_create = kwargs.get('wait_after_vm_create',
120 True)
121 self.new_vm = kwargs.get('new_vm', False)
122 self.new_sec_grp = kwargs.get('new_sec_group', False)
123 self.new_floating = kwargs.get('new_floating', False)
124 self.reboot = kwargs.get('reboot', False)
125 self.floating_pool = kwargs.get('floating_pool', None)
126 self.verify = kwargs.get('verify', ('check_port_ssh',
127 'check_icmp_echo'))
128 self.check_timeout = kwargs.get('check_timeout', 120)
129 self.check_interval = kwargs.get('check_interval', 1)
130 self.wait_for_disassociate = kwargs.get('wait_for_disassociate',
131 True)
132
133 # allocate floating
134 if not self.new_floating:
135 self._create_floating_ip()
136 # add security group
137 if not self.new_sec_grp:
138 self._create_sec_group()
139 # create vm
140 if not self.new_vm:
141 self._create_vm()
142
143 def wait_disassociate(self):
144 cli = self.manager.floating_ips_client
145
146 def func():
147 _, floating = cli.get_floating_ip_details(self.floating['id'])
148 return floating['instance_id'] is None
149
150 if not tempest.test.call_until_true(func, self.check_timeout,
151 self.check_interval):
152 raise RuntimeError("IP disassociate timeout!")
153
154 def run_core(self):
155 cli = self.manager.floating_ips_client
156 cli.associate_floating_ip_to_server(self.floating['ip'],
157 self.server_id)
158 for method in self.verify:
159 m = getattr(self, method)
160 m()
161 cli.disassociate_floating_ip_from_server(self.floating['ip'],
162 self.server_id)
163 if self.wait_for_disassociate:
164 self.wait_disassociate()
165
166 def run(self):
167 if self.new_sec_grp:
168 self._create_sec_group()
169 if self.new_floating:
170 self._create_floating_ip()
171 if self.new_vm:
172 self._create_vm()
173 if self.reboot:
174 self.manager.servers_client.reboot(self.server_id, 'HARD')
Attila Fazekasd789b542014-04-25 07:01:22 +0200175 self.manager.servers_client.wait_for_server_status(self.server_id,
176 'ACTIVE')
Attila Fazekas7cf2a222013-08-02 13:49:10 +0200177
178 self.run_core()
179
180 if self.new_vm:
181 self._destroy_vm()
182 if self.new_floating:
183 self._destroy_floating_ip()
184 if self.new_sec_grp:
185 self._destroy_sec_grp()
186
187 def tearDown(self):
188 if not self.new_vm:
189 self._destroy_vm()
190 if not self.new_floating:
191 self._destroy_floating_ip()
192 if not self.new_sec_grp:
193 self._destroy_sec_grp()