blob: 6774e815ffc563de1cb61021c1f13de31d994f7e [file] [log] [blame]
David Kranz779c7f82012-05-01 16:50:32 -04001# Copyright 2011 Quanta Research Cambridge, Inc.
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
Matthew Treinish8d6836b2012-12-10 10:07:56 -050015import random
16import telnetlib
David Kranz779c7f82012-05-01 16:50:32 -040017
David Kranz779c7f82012-05-01 16:50:32 -040018import pending_action
Matthew Treinish8d6836b2012-12-10 10:07:56 -050019import test_case
David Kranz779c7f82012-05-01 16:50:32 -040020
21
22class TestChangeFloatingIp(test_case.StressTestCase):
23 """Add or remove a floating ip from a vm."""
24
25 def __init__(self):
26 super(TestChangeFloatingIp, self).__init__()
27 self.server_ids = None
28
29 def run(self, manager, state, *pargs, **kwargs):
Zhongyue Luoe471d6e2012-09-17 17:02:43 +080030 if self.server_ids is None:
David Kranz779c7f82012-05-01 16:50:32 -040031 vms = state.get_instances()
32 self.server_ids = [k for k, v in vms.iteritems()]
33 floating_ip = random.choice(state.get_floating_ips())
34 if floating_ip.change_pending:
35 return None
36 floating_ip.change_pending = True
37 timeout = int(kwargs.get('timeout', 60))
Zhongyue Luoa1343de2013-01-04 16:21:35 +080038 cli = manager.floating_ips_client
Zhongyue Luoe471d6e2012-09-17 17:02:43 +080039 if floating_ip.server_id is None:
David Kranz779c7f82012-05-01 16:50:32 -040040 server = random.choice(self.server_ids)
41 address = floating_ip.address
42 self._logger.info('Adding %s to server %s' % (address, server))
Zhongyue Luoa1343de2013-01-04 16:21:35 +080043 resp, body = cli.associate_floating_ip_to_server(address,
44 server)
David Kranz779c7f82012-05-01 16:50:32 -040045 if resp.status != 202:
46 raise Exception("response: %s body: %s" % (resp, body))
47 floating_ip.server_id = server
48 return VerifyChangeFloatingIp(manager, floating_ip,
49 timeout, add=True)
50 else:
51 server = floating_ip.server_id
52 address = floating_ip.address
53 self._logger.info('Removing %s from server %s' % (address, server))
Zhongyue Luoa1343de2013-01-04 16:21:35 +080054 resp, body = cli.disassociate_floating_ip_from_server(address,
55 server)
David Kranz779c7f82012-05-01 16:50:32 -040056 if resp.status != 202:
57 raise Exception("response: %s body: %s" % (resp, body))
58 return VerifyChangeFloatingIp(manager, floating_ip,
59 timeout, add=False)
60
61
62class VerifyChangeFloatingIp(pending_action.PendingAction):
Sean Daguef237ccb2013-01-04 15:19:14 -050063 """Verify that floating ip was changed."""
David Kranz779c7f82012-05-01 16:50:32 -040064 def __init__(self, manager, floating_ip, timeout, add=None):
65 super(VerifyChangeFloatingIp, self).__init__(manager, timeout=timeout)
66 self.floating_ip = floating_ip
67 self.add = add
68
69 def retry(self):
70 """
71 Check to see that we can contact the server at its new address.
72 """
73 try:
74 conn = telnetlib.Telnet(self.floating_ip.address, 22, timeout=0.5)
75 conn.close()
76 if self.add:
77 self._logger.info('%s added [%.1f secs elapsed]' %
Zhongyue Luo79d8d362012-09-25 13:49:27 +080078 (self.floating_ip.address, self.elapsed()))
David Kranz779c7f82012-05-01 16:50:32 -040079 self.floating_ip.change_pending = False
80 return True
Matthew Treinish8d6836b2012-12-10 10:07:56 -050081 except Exception:
David Kranz779c7f82012-05-01 16:50:32 -040082 if not self.add:
83 self._logger.info('%s removed [%.1f secs elapsed]' %
Zhongyue Luo79d8d362012-09-25 13:49:27 +080084 (self.floating_ip.address, self.elapsed()))
David Kranz779c7f82012-05-01 16:50:32 -040085 self.floating_ip.change_pending = False
86 self.floating_ip.server_id = None
87 return True
88 return False