blob: fcc5904d19374c76d73e9947b0c5ff21614025b3 [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
David Kranz779c7f82012-05-01 16:50:32 -040015import logging
Matthew Treinish8d6836b2012-12-10 10:07:56 -050016import random
17import telnetlib
18import time
David Kranz779c7f82012-05-01 16:50:32 -040019
David Kranz779c7f82012-05-01 16:50:32 -040020import pending_action
Matthew Treinish8d6836b2012-12-10 10:07:56 -050021import test_case
David Kranz779c7f82012-05-01 16:50:32 -040022
23
24class TestChangeFloatingIp(test_case.StressTestCase):
25 """Add or remove a floating ip from a vm."""
26
27 def __init__(self):
28 super(TestChangeFloatingIp, self).__init__()
29 self.server_ids = None
30
31 def run(self, manager, state, *pargs, **kwargs):
Zhongyue Luoe471d6e2012-09-17 17:02:43 +080032 if self.server_ids is None:
David Kranz779c7f82012-05-01 16:50:32 -040033 vms = state.get_instances()
34 self.server_ids = [k for k, v in vms.iteritems()]
35 floating_ip = random.choice(state.get_floating_ips())
36 if floating_ip.change_pending:
37 return None
38 floating_ip.change_pending = True
39 timeout = int(kwargs.get('timeout', 60))
Zhongyue Luoe471d6e2012-09-17 17:02:43 +080040 if floating_ip.server_id is None:
David Kranz779c7f82012-05-01 16:50:32 -040041 server = random.choice(self.server_ids)
42 address = floating_ip.address
43 self._logger.info('Adding %s to server %s' % (address, server))
44 resp, body =\
45 manager.floating_ips_client.associate_floating_ip_to_server(
46 address,
47 server)
48 if resp.status != 202:
49 raise Exception("response: %s body: %s" % (resp, body))
50 floating_ip.server_id = server
51 return VerifyChangeFloatingIp(manager, floating_ip,
52 timeout, add=True)
53 else:
54 server = floating_ip.server_id
55 address = floating_ip.address
56 self._logger.info('Removing %s from server %s' % (address, server))
57 resp, body =\
58 manager.floating_ips_client.disassociate_floating_ip_from_server(
59 address, server)
60 if resp.status != 202:
61 raise Exception("response: %s body: %s" % (resp, body))
62 return VerifyChangeFloatingIp(manager, floating_ip,
63 timeout, add=False)
64
65
66class VerifyChangeFloatingIp(pending_action.PendingAction):
67 """Verify that floating ip was changed"""
68 def __init__(self, manager, floating_ip, timeout, add=None):
69 super(VerifyChangeFloatingIp, self).__init__(manager, timeout=timeout)
70 self.floating_ip = floating_ip
71 self.add = add
72
73 def retry(self):
74 """
75 Check to see that we can contact the server at its new address.
76 """
77 try:
78 conn = telnetlib.Telnet(self.floating_ip.address, 22, timeout=0.5)
79 conn.close()
80 if self.add:
81 self._logger.info('%s added [%.1f secs elapsed]' %
Zhongyue Luo79d8d362012-09-25 13:49:27 +080082 (self.floating_ip.address, self.elapsed()))
David Kranz779c7f82012-05-01 16:50:32 -040083 self.floating_ip.change_pending = False
84 return True
Matthew Treinish8d6836b2012-12-10 10:07:56 -050085 except Exception:
David Kranz779c7f82012-05-01 16:50:32 -040086 if not self.add:
87 self._logger.info('%s removed [%.1f secs elapsed]' %
Zhongyue Luo79d8d362012-09-25 13:49:27 +080088 (self.floating_ip.address, self.elapsed()))
David Kranz779c7f82012-05-01 16:50:32 -040089 self.floating_ip.change_pending = False
90 self.floating_ip.server_id = None
91 return True
92 return False