David Kranz | 779c7f8 | 2012-05-01 16:50:32 -0400 | [diff] [blame] | 1 | # 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 | |
| 15 | |
| 16 | # system imports |
| 17 | import random |
| 18 | import time |
| 19 | import telnetlib |
| 20 | import logging |
| 21 | |
| 22 | # local imports |
| 23 | import test_case |
| 24 | import pending_action |
| 25 | |
| 26 | |
| 27 | class TestChangeFloatingIp(test_case.StressTestCase): |
| 28 | """Add or remove a floating ip from a vm.""" |
| 29 | |
| 30 | def __init__(self): |
| 31 | super(TestChangeFloatingIp, self).__init__() |
| 32 | self.server_ids = None |
| 33 | |
| 34 | def run(self, manager, state, *pargs, **kwargs): |
Zhongyue Luo | e471d6e | 2012-09-17 17:02:43 +0800 | [diff] [blame] | 35 | if self.server_ids is None: |
David Kranz | 779c7f8 | 2012-05-01 16:50:32 -0400 | [diff] [blame] | 36 | vms = state.get_instances() |
| 37 | self.server_ids = [k for k, v in vms.iteritems()] |
| 38 | floating_ip = random.choice(state.get_floating_ips()) |
| 39 | if floating_ip.change_pending: |
| 40 | return None |
| 41 | floating_ip.change_pending = True |
| 42 | timeout = int(kwargs.get('timeout', 60)) |
Zhongyue Luo | e471d6e | 2012-09-17 17:02:43 +0800 | [diff] [blame] | 43 | if floating_ip.server_id is None: |
David Kranz | 779c7f8 | 2012-05-01 16:50:32 -0400 | [diff] [blame] | 44 | server = random.choice(self.server_ids) |
| 45 | address = floating_ip.address |
| 46 | self._logger.info('Adding %s to server %s' % (address, server)) |
| 47 | resp, body =\ |
| 48 | manager.floating_ips_client.associate_floating_ip_to_server( |
| 49 | address, |
| 50 | server) |
| 51 | if resp.status != 202: |
| 52 | raise Exception("response: %s body: %s" % (resp, body)) |
| 53 | floating_ip.server_id = server |
| 54 | return VerifyChangeFloatingIp(manager, floating_ip, |
| 55 | timeout, add=True) |
| 56 | else: |
| 57 | server = floating_ip.server_id |
| 58 | address = floating_ip.address |
| 59 | self._logger.info('Removing %s from server %s' % (address, server)) |
| 60 | resp, body =\ |
| 61 | manager.floating_ips_client.disassociate_floating_ip_from_server( |
| 62 | address, server) |
| 63 | if resp.status != 202: |
| 64 | raise Exception("response: %s body: %s" % (resp, body)) |
| 65 | return VerifyChangeFloatingIp(manager, floating_ip, |
| 66 | timeout, add=False) |
| 67 | |
| 68 | |
| 69 | class VerifyChangeFloatingIp(pending_action.PendingAction): |
| 70 | """Verify that floating ip was changed""" |
| 71 | def __init__(self, manager, floating_ip, timeout, add=None): |
| 72 | super(VerifyChangeFloatingIp, self).__init__(manager, timeout=timeout) |
| 73 | self.floating_ip = floating_ip |
| 74 | self.add = add |
| 75 | |
| 76 | def retry(self): |
| 77 | """ |
| 78 | Check to see that we can contact the server at its new address. |
| 79 | """ |
| 80 | try: |
| 81 | conn = telnetlib.Telnet(self.floating_ip.address, 22, timeout=0.5) |
| 82 | conn.close() |
| 83 | if self.add: |
| 84 | self._logger.info('%s added [%.1f secs elapsed]' % |
Zhongyue Luo | 79d8d36 | 2012-09-25 13:49:27 +0800 | [diff] [blame^] | 85 | (self.floating_ip.address, self.elapsed())) |
David Kranz | 779c7f8 | 2012-05-01 16:50:32 -0400 | [diff] [blame] | 86 | self.floating_ip.change_pending = False |
| 87 | return True |
| 88 | except: |
| 89 | if not self.add: |
| 90 | self._logger.info('%s removed [%.1f secs elapsed]' % |
Zhongyue Luo | 79d8d36 | 2012-09-25 13:49:27 +0800 | [diff] [blame^] | 91 | (self.floating_ip.address, self.elapsed())) |
David Kranz | 779c7f8 | 2012-05-01 16:50:32 -0400 | [diff] [blame] | 92 | self.floating_ip.change_pending = False |
| 93 | self.floating_ip.server_id = None |
| 94 | return True |
| 95 | return False |