OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2011 Reza Lotun http://reza.lotun.name |
| 2 # All rights reserved. |
| 3 # |
| 4 # Permission is hereby granted, free of charge, to any person obtaining a |
| 5 # copy of this software and associated documentation files (the |
| 6 # "Software"), to deal in the Software without restriction, including |
| 7 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 8 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 9 # persons to whom the Software is furnished to do so, subject to the fol- |
| 10 # lowing conditions: |
| 11 # |
| 12 # The above copyright notice and this permission notice shall be included |
| 13 # in all copies or substantial portions of the Software. |
| 14 # |
| 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 # IN THE SOFTWARE. |
| 22 |
| 23 """ |
| 24 Some unit tests for the AutoscaleConnection |
| 25 """ |
| 26 |
| 27 import unittest |
| 28 import time |
| 29 from boto.ec2.autoscale import AutoScaleConnection |
| 30 from boto.ec2.autoscale.activity import Activity |
| 31 from boto.ec2.autoscale.group import AutoScalingGroup, ProcessType |
| 32 from boto.ec2.autoscale.launchconfig import LaunchConfiguration |
| 33 from boto.ec2.autoscale.policy import AdjustmentType, MetricCollectionTypes, Sca
lingPolicy |
| 34 from boto.ec2.autoscale.scheduled import ScheduledUpdateGroupAction |
| 35 from boto.ec2.autoscale.instance import Instance |
| 36 from boto.ec2.autoscale.tag import Tag |
| 37 |
| 38 |
| 39 class AutoscaleConnectionTest(unittest.TestCase): |
| 40 ec2 = True |
| 41 autoscale = True |
| 42 |
| 43 def test_basic(self): |
| 44 # NB: as it says on the tin these are really basic tests that only |
| 45 # (lightly) exercise read-only behaviour - and that's only if you |
| 46 # have any autoscale groups to introspect. It's useful, however, to |
| 47 # catch simple errors |
| 48 |
| 49 print '--- running %s tests ---' % self.__class__.__name__ |
| 50 c = AutoScaleConnection() |
| 51 |
| 52 self.assertTrue(repr(c).startswith('AutoScaleConnection')) |
| 53 |
| 54 groups = c.get_all_groups() |
| 55 for group in groups: |
| 56 self.assertTrue(type(group), AutoScalingGroup) |
| 57 |
| 58 # get activities |
| 59 activities = group.get_activities() |
| 60 |
| 61 for activity in activities: |
| 62 self.assertEqual(type(activity), Activity) |
| 63 |
| 64 # get launch configs |
| 65 configs = c.get_all_launch_configurations() |
| 66 for config in configs: |
| 67 self.assertTrue(type(config), LaunchConfiguration) |
| 68 |
| 69 # get policies |
| 70 policies = c.get_all_policies() |
| 71 for policy in policies: |
| 72 self.assertTrue(type(policy), ScalingPolicy) |
| 73 |
| 74 # get scheduled actions |
| 75 actions = c.get_all_scheduled_actions() |
| 76 for action in actions: |
| 77 self.assertTrue(type(action), ScheduledUpdateGroupAction) |
| 78 |
| 79 # get instances |
| 80 instances = c.get_all_autoscaling_instances() |
| 81 for instance in instances: |
| 82 self.assertTrue(type(instance), Instance) |
| 83 |
| 84 # get all scaling process types |
| 85 ptypes = c.get_all_scaling_process_types() |
| 86 for ptype in ptypes: |
| 87 self.assertTrue(type(ptype), ProcessType) |
| 88 |
| 89 # get adjustment types |
| 90 adjustments = c.get_all_adjustment_types() |
| 91 for adjustment in adjustments: |
| 92 self.assertTrue(type(adjustment), AdjustmentType) |
| 93 |
| 94 # get metrics collection types |
| 95 types = c.get_all_metric_collection_types() |
| 96 self.assertTrue(type(types), MetricCollectionTypes) |
| 97 |
| 98 # create the simplest possible AutoScale group |
| 99 # first create the launch configuration |
| 100 time_string = '%d' % int(time.time()) |
| 101 lc_name = 'lc-%s' % time_string |
| 102 lc = LaunchConfiguration(name=lc_name, image_id='ami-2272864b', |
| 103 instance_type='t1.micro') |
| 104 c.create_launch_configuration(lc) |
| 105 found = False |
| 106 lcs = c.get_all_launch_configurations() |
| 107 for lc in lcs: |
| 108 if lc.name == lc_name: |
| 109 found = True |
| 110 break |
| 111 assert found |
| 112 |
| 113 # now create autoscaling group |
| 114 group_name = 'group-%s' % time_string |
| 115 group = AutoScalingGroup(name=group_name, launch_config=lc, |
| 116 availability_zones=['us-east-1a'], |
| 117 min_size=1, max_size=1) |
| 118 c.create_auto_scaling_group(group) |
| 119 found = False |
| 120 groups = c.get_all_groups() |
| 121 for group in groups: |
| 122 if group.name == group_name: |
| 123 found = True |
| 124 break |
| 125 assert found |
| 126 |
| 127 # now create a tag |
| 128 tag = Tag(key='foo', value='bar', resource_id=group_name, |
| 129 propagate_at_launch=True) |
| 130 c.create_or_update_tags([tag]) |
| 131 |
| 132 found = False |
| 133 tags = c.get_all_tags() |
| 134 for tag in tags: |
| 135 if tag.resource_id == group_name and tag.key == 'foo': |
| 136 found = True |
| 137 break |
| 138 assert found |
| 139 |
| 140 c.delete_tags([tag]) |
| 141 |
| 142 # shutdown instances and wait for them to disappear |
| 143 group.shutdown_instances() |
| 144 instances = True |
| 145 while instances: |
| 146 time.sleep(5) |
| 147 groups = c.get_all_groups() |
| 148 for group in groups: |
| 149 if group.name == group_name: |
| 150 if not group.instances: |
| 151 instances = False |
| 152 |
| 153 group.delete() |
| 154 lc.delete() |
| 155 |
| 156 found = True |
| 157 while found: |
| 158 found = False |
| 159 time.sleep(5) |
| 160 tags = c.get_all_tags() |
| 161 for tag in tags: |
| 162 if tag.resource_id == group_name and tag.key == 'foo': |
| 163 found = True |
| 164 |
| 165 assert not found |
| 166 |
| 167 print '--- tests completed ---' |
OLD | NEW |