blob: 74a9739d7436c385e4ff914f6bbc8686e44c0f06 [file] [log] [blame]
Attila Fazekas7cf2a222013-08-02 13:49:10 +02001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import socket
16import subprocess
17
Masayuki Igawa259c1132013-10-31 17:48:44 +090018from tempest.common.utils import data_utils
Attila Fazekas7cf2a222013-08-02 13:49:10 +020019import tempest.stress.stressaction as stressaction
20import tempest.test
21
22
23class FloatingStress(stressaction.StressAction):
24
25 # from the scenario manager
26 def ping_ip_address(self, ip_address):
27 cmd = ['ping', '-c1', '-w1', ip_address]
28
29 proc = subprocess.Popen(cmd,
30 stdout=subprocess.PIPE,
31 stderr=subprocess.PIPE)
32 proc.wait()
33 success = proc.returncode == 0
34 self.logger.info("%s(%s): %s", self.server_id, self.floating['ip'],
35 "pong!" if success else "no pong :(")
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)
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):
60 def func():
61 return self.ping_ip_address(self.floating['ip'])
62 if not tempest.test.call_until_true(func, self.check_timeout,
63 self.check_interval):
64 raise RuntimeError("Cannot ping the machine.")
65
66 def _create_vm(self):
Masayuki Igawa259c1132013-10-31 17:48:44 +090067 self.name = name = data_utils.rand_name("instance")
Attila Fazekas7cf2a222013-08-02 13:49:10 +020068 servers_client = self.manager.servers_client
69 self.logger.info("creating %s" % name)
70 vm_args = self.vm_extra_args.copy()
71 vm_args['security_groups'] = [{'name': self.sec_grp}]
72 resp, server = servers_client.create_server(name, self.image,
73 self.flavor,
74 **vm_args)
75 self.server_id = server['id']
76 assert(resp.status == 202)
77 if self.wait_after_vm_create:
78 self.manager.servers_client.wait_for_server_status(self.server_id,
79 'ACTIVE')
80
81 def _destroy_vm(self):
82 self.logger.info("deleting %s" % self.server_id)
83 resp, _ = self.manager.servers_client.delete_server(self.server_id)
84 assert(resp.status == 204) # It cannot be 204 if I had to wait..
85 self.manager.servers_client.wait_for_server_termination(self.server_id)
86 self.logger.info("deleted %s" % self.server_id)
87
88 def _create_sec_group(self):
89 sec_grp_cli = self.manager.security_groups_client
Masayuki Igawa259c1132013-10-31 17:48:44 +090090 s_name = data_utils.rand_name('sec_grp-')
91 s_description = data_utils.rand_name('desc-')
Attila Fazekas7cf2a222013-08-02 13:49:10 +020092 _, _sec_grp = sec_grp_cli.create_security_group(s_name,
93 s_description)
94 self.sec_grp = _sec_grp['id']
95 create_rule = sec_grp_cli.create_security_group_rule
96 create_rule(self.sec_grp, 'tcp', 22, 22)
97 create_rule(self.sec_grp, 'icmp', -1, -1)
98
99 def _destroy_sec_grp(self):
100 sec_grp_cli = self.manager.security_groups_client
101 sec_grp_cli.delete_security_group(self.sec_grp)
102
103 def _create_floating_ip(self):
104 floating_cli = self.manager.floating_ips_client
105 _, self.floating = floating_cli.create_floating_ip(self.floating_pool)
106
107 def _destroy_floating_ip(self):
108 cli = self.manager.floating_ips_client
109 cli.delete_floating_ip(self.floating['id'])
110 cli.wait_for_resource_deletion(self.floating['id'])
111 self.logger.info("Deleted Floating IP %s", str(self.floating['ip']))
112
113 def setUp(self, **kwargs):
114 self.image = self.manager.config.compute.image_ref
115 self.flavor = self.manager.config.compute.flavor_ref
116 self.vm_extra_args = kwargs.get('vm_extra_args', {})
117 self.wait_after_vm_create = kwargs.get('wait_after_vm_create',
118 True)
119 self.new_vm = kwargs.get('new_vm', False)
120 self.new_sec_grp = kwargs.get('new_sec_group', False)
121 self.new_floating = kwargs.get('new_floating', False)
122 self.reboot = kwargs.get('reboot', False)
123 self.floating_pool = kwargs.get('floating_pool', None)
124 self.verify = kwargs.get('verify', ('check_port_ssh',
125 'check_icmp_echo'))
126 self.check_timeout = kwargs.get('check_timeout', 120)
127 self.check_interval = kwargs.get('check_interval', 1)
128 self.wait_for_disassociate = kwargs.get('wait_for_disassociate',
129 True)
130
131 # allocate floating
132 if not self.new_floating:
133 self._create_floating_ip()
134 # add security group
135 if not self.new_sec_grp:
136 self._create_sec_group()
137 # create vm
138 if not self.new_vm:
139 self._create_vm()
140
141 def wait_disassociate(self):
142 cli = self.manager.floating_ips_client
143
144 def func():
145 _, floating = cli.get_floating_ip_details(self.floating['id'])
146 return floating['instance_id'] is None
147
148 if not tempest.test.call_until_true(func, self.check_timeout,
149 self.check_interval):
150 raise RuntimeError("IP disassociate timeout!")
151
152 def run_core(self):
153 cli = self.manager.floating_ips_client
154 cli.associate_floating_ip_to_server(self.floating['ip'],
155 self.server_id)
156 for method in self.verify:
157 m = getattr(self, method)
158 m()
159 cli.disassociate_floating_ip_from_server(self.floating['ip'],
160 self.server_id)
161 if self.wait_for_disassociate:
162 self.wait_disassociate()
163
164 def run(self):
165 if self.new_sec_grp:
166 self._create_sec_group()
167 if self.new_floating:
168 self._create_floating_ip()
169 if self.new_vm:
170 self._create_vm()
171 if self.reboot:
172 self.manager.servers_client.reboot(self.server_id, 'HARD')
173
174 self.run_core()
175
176 if self.new_vm:
177 self._destroy_vm()
178 if self.new_floating:
179 self._destroy_floating_ip()
180 if self.new_sec_grp:
181 self._destroy_sec_grp()
182
183 def tearDown(self):
184 if not self.new_vm:
185 self._destroy_vm()
186 if not self.new_floating:
187 self._destroy_floating_ip()
188 if not self.new_sec_grp:
189 self._destroy_sec_grp()