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