blob: e84379383b52194337810e57e21c89d93611f2c4 [file] [log] [blame]
Steve Bakerdd7c6ce2013-06-24 14:46:47 +12001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# 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, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
Steve Bakerdd9fb002013-09-30 11:35:18 +130015import heatclient.exc as heat_exceptions
Matthew Treinishf4418592013-09-09 20:59:23 +000016import time
17
Steve Bakerdd7c6ce2013-06-24 14:46:47 +120018from tempest.scenario import manager
19from tempest.test import attr
20from tempest.test import call_until_true
Matthew Treinish2153ec02013-09-09 20:57:30 +000021from tempest.test import services
Steve Bakerdd7c6ce2013-06-24 14:46:47 +120022
23
Steve Bakerdd7c6ce2013-06-24 14:46:47 +120024class AutoScalingTest(manager.OrchestrationScenarioTest):
25
26 def setUp(self):
27 super(AutoScalingTest, self).setUp()
28 if not self.config.orchestration.image_ref:
29 raise self.skipException("No image available to test")
30 self.client = self.orchestration_client
31
32 def assign_keypair(self):
33 self.stack_name = self._stack_rand_name()
34 if self.config.orchestration.keypair_name:
35 self.keypair_name = self.config.orchestration.keypair_name
36 else:
Ken'ichi Ohmichi8ca06252013-08-23 22:36:17 +090037 self.keypair = self.create_keypair()
Steve Bakerdd7c6ce2013-06-24 14:46:47 +120038 self.keypair_name = self.keypair.id
Steve Bakerdd7c6ce2013-06-24 14:46:47 +120039
40 def launch_stack(self):
Steve Baker80252da2013-09-25 13:29:10 +120041 net = self._get_default_network()
Steve Bakerdd7c6ce2013-06-24 14:46:47 +120042 self.parameters = {
43 'KeyName': self.keypair_name,
44 'InstanceType': self.config.orchestration.instance_type,
45 'ImageId': self.config.orchestration.image_ref,
Steve Baker80252da2013-09-25 13:29:10 +120046 'StackStart': str(time.time()),
47 'Subnet': net['subnets'][0]
Steve Bakerdd7c6ce2013-06-24 14:46:47 +120048 }
49
50 # create the stack
51 self.template = self._load_template(__file__, 'test_autoscaling.yaml')
52 self.client.stacks.create(
53 stack_name=self.stack_name,
54 template=self.template,
55 parameters=self.parameters)
56
57 self.stack = self.client.stacks.get(self.stack_name)
58 self.stack_identifier = '%s/%s' % (self.stack_name, self.stack.id)
59
60 # if a keypair was set, do not delete the stack on exit to allow
61 # for manual post-mortums
62 if not self.config.orchestration.keypair_name:
63 self.set_resource('stack', self.stack)
64
65 @attr(type='slow')
Matthew Treinish2153ec02013-09-09 20:57:30 +000066 @services('orchestration', 'compute')
Steve Bakerdd7c6ce2013-06-24 14:46:47 +120067 def test_scale_up_then_down(self):
68
69 self.assign_keypair()
70 self.launch_stack()
71
72 sid = self.stack_identifier
73 timeout = self.config.orchestration.build_timeout
74 interval = 10
75
76 self.assertEqual('CREATE', self.stack.action)
77 # wait for create to complete.
Steve Bakerdd9fb002013-09-30 11:35:18 +130078 self.status_timeout(self.client.stacks, sid, 'COMPLETE',
79 error_status='FAILED')
Steve Bakerdd7c6ce2013-06-24 14:46:47 +120080
81 self.stack.get()
82 self.assertEqual('CREATE_COMPLETE', self.stack.stack_status)
83
84 # the resource SmokeServerGroup is implemented as a nested
85 # stack, so servers can be counted by counting the resources
86 # inside that nested stack
87 resource = self.client.resources.get(sid, 'SmokeServerGroup')
88 nested_stack_id = resource.physical_resource_id
89
90 def server_count():
91 # the number of servers is the number of resources
Chang Bo Guocc1623c2013-09-13 20:11:27 -070092 # in the nested stack
Steve Bakerdd7c6ce2013-06-24 14:46:47 +120093 self.server_count = len(
94 self.client.resources.list(nested_stack_id))
95 return self.server_count
96
97 def assertScale(from_servers, to_servers):
98 call_until_true(lambda: server_count() == to_servers,
99 timeout, interval)
100 self.assertEqual(to_servers, self.server_count,
Steve Bakerdd9fb002013-09-30 11:35:18 +1300101 'Failed scaling from %d to %d servers. '
102 'Current server count: %s' % (
103 from_servers, to_servers,
104 self.server_count))
Steve Bakerdd7c6ce2013-06-24 14:46:47 +1200105
106 # he marched them up to the top of the hill
107 assertScale(1, 2)
108 assertScale(2, 3)
109
110 # and he marched them down again
111 assertScale(3, 2)
112 assertScale(2, 1)
Steve Bakerdd9fb002013-09-30 11:35:18 +1300113
114 # delete stack on completion
115 self.stack.delete()
116 self.status_timeout(self.client.stacks, sid, 'COMPLETE',
117 error_status='FAILED',
118 not_found_exception=heat_exceptions.NotFound)
119
120 try:
121 self.stack.get()
122 self.assertEqual('DELETE_COMPLETE', self.stack.stack_status)
123 except heat_exceptions.NotFound:
124 pass