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