blob: 78025eeb0bc6e8939ea5208fa39f86d8fb79d860 [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 Bakerdd7c6ce2013-06-24 14:46:47 +120015from tempest.scenario import manager
16from tempest.test import attr
17from tempest.test import call_until_true
18import time
19
20
Steve Bakerdd7c6ce2013-06-24 14:46:47 +120021class AutoScalingTest(manager.OrchestrationScenarioTest):
22
23 def setUp(self):
24 super(AutoScalingTest, self).setUp()
25 if not self.config.orchestration.image_ref:
26 raise self.skipException("No image available to test")
27 self.client = self.orchestration_client
28
29 def assign_keypair(self):
30 self.stack_name = self._stack_rand_name()
31 if self.config.orchestration.keypair_name:
32 self.keypair_name = self.config.orchestration.keypair_name
33 else:
Ken'ichi Ohmichi8ca06252013-08-23 22:36:17 +090034 self.keypair = self.create_keypair()
Steve Bakerdd7c6ce2013-06-24 14:46:47 +120035 self.keypair_name = self.keypair.id
Steve Bakerdd7c6ce2013-06-24 14:46:47 +120036
37 def launch_stack(self):
38 self.parameters = {
39 'KeyName': self.keypair_name,
40 'InstanceType': self.config.orchestration.instance_type,
41 'ImageId': self.config.orchestration.image_ref,
42 'StackStart': str(time.time())
43 }
44
45 # create the stack
46 self.template = self._load_template(__file__, 'test_autoscaling.yaml')
47 self.client.stacks.create(
48 stack_name=self.stack_name,
49 template=self.template,
50 parameters=self.parameters)
51
52 self.stack = self.client.stacks.get(self.stack_name)
53 self.stack_identifier = '%s/%s' % (self.stack_name, self.stack.id)
54
55 # if a keypair was set, do not delete the stack on exit to allow
56 # for manual post-mortums
57 if not self.config.orchestration.keypair_name:
58 self.set_resource('stack', self.stack)
59
60 @attr(type='slow')
61 def test_scale_up_then_down(self):
62
63 self.assign_keypair()
64 self.launch_stack()
65
66 sid = self.stack_identifier
67 timeout = self.config.orchestration.build_timeout
68 interval = 10
69
70 self.assertEqual('CREATE', self.stack.action)
71 # wait for create to complete.
72 self.status_timeout(self.client.stacks, sid, 'COMPLETE')
73
74 self.stack.get()
75 self.assertEqual('CREATE_COMPLETE', self.stack.stack_status)
76
77 # the resource SmokeServerGroup is implemented as a nested
78 # stack, so servers can be counted by counting the resources
79 # inside that nested stack
80 resource = self.client.resources.get(sid, 'SmokeServerGroup')
81 nested_stack_id = resource.physical_resource_id
82
83 def server_count():
84 # the number of servers is the number of resources
85 # in the nexted stack
86 self.server_count = len(
87 self.client.resources.list(nested_stack_id))
88 return self.server_count
89
90 def assertScale(from_servers, to_servers):
91 call_until_true(lambda: server_count() == to_servers,
92 timeout, interval)
93 self.assertEqual(to_servers, self.server_count,
94 'Failed scaling from %d to %d servers' % (
95 from_servers, to_servers))
96
97 # he marched them up to the top of the hill
98 assertScale(1, 2)
99 assertScale(2, 3)
100
101 # and he marched them down again
102 assertScale(3, 2)
103 assertScale(2, 1)