def test_instance_create_duplicate_image_name_err(self): stack_name = 'test_instance_create_image_name_err_stack' (tmpl, stack) = self._setup_test_stack(stack_name) # create an instance with a non unique image name wsp = tmpl.t['Resources']['WebServer']['Properties'] wsp['ImageId'] = 'CentOS 5.2' resource_defns = tmpl.resource_definitions(stack) instance = instances.Instance('instance_create_image_err', resource_defns['WebServer'], stack) self._mock_get_image_id_fail('CentOS 5.2', exception.PhysicalResourceNameAmbiguity( name='CentOS 5.2')) self.m.ReplayAll() create = scheduler.TaskRunner(instance.create) error = self.assertRaises(exception.ResourceFailure, create) self.assertEqual( 'StackValidationFailed: Property error : WebServer: ' 'ImageId Multiple physical resources were ' 'found with name (CentOS 5.2).', str(error)) self.m.VerifyAll()
def _setup_test_instance(self, return_server, name, image_id=None): stack_name = '%s_stack' % name (t, stack) = self._setup_test_stack(stack_name) t['Resources']['WebServer']['Properties']['ImageId'] = \ image_id or 'CentOS 5.2' t['Resources']['WebServer']['Properties']['InstanceType'] = \ '256 MB Server' instance = instances.Instance('%s_name' % name, t['Resources']['WebServer'], stack) self.m.StubOutWithMock(instance, 'nova') instance.nova().MultipleTimes().AndReturn(self.fc) instance.t = instance.stack.resolve_runtime_data(instance.t) # need to resolve the template functions server_userdata = nova_utils.build_userdata( instance, instance.t['Properties']['UserData']) instance.mime_string = server_userdata self.m.StubOutWithMock(self.fc.servers, 'create') self.fc.servers.create(image=1, flavor=1, key_name='test', name=utils.PhysName(stack_name, instance.name), security_groups=None, userdata=server_userdata, scheduler_hints=None, meta=None, nics=None, availability_zone=None).AndReturn(return_server) return instance
def _setup_test_instance(self, return_server, name, image_id=None, stub_create=True): stack_name = '%s_s' % name tmpl, stack = self._get_test_template(stack_name, image_id) resource_defns = tmpl.resource_definitions(stack) instance = instances.Instance(name, resource_defns['WebServer'], stack) self._mock_get_image_id_success(image_id or 'CentOS 5.2', 1) self.m.StubOutWithMock(nova.NovaClientPlugin, '_create') nova.NovaClientPlugin._create().MultipleTimes().AndReturn(self.fc) if stub_create: self.m.StubOutWithMock(self.fc.servers, 'create') self.fc.servers.create( image=1, flavor=1, key_name='test', name=utils.PhysName( stack_name, instance.name, limit=instance.physical_resource_name_limit), security_groups=None, userdata=mox.IgnoreArg(), scheduler_hints={'foo': ['spam', 'ham', 'baz'], 'bar': 'eggs'}, meta=None, nics=None, availability_zone=None).AndReturn( return_server) return instance
def test_loadbalancer(self): self._mock_get_image_id_success( u'Fedora-Cloud-Base-20141203-21.x86_64', 746) self._create_stubs() self.m.ReplayAll() t = template_format.parse(lb_template) s = utils.parse_stack(t) s.store() rsrc = self.create_loadbalancer(t, s, 'LoadBalancer') id_list = [] resource_defns = s.t.resource_definitions(s) for inst_name in ['WikiServerOne1', 'WikiServerOne2']: inst = instance.Instance(inst_name, resource_defns['WikiServerOne'], s) id_list.append(inst.FnGetRefId()) prop_diff = {'Instances': id_list} props = copy.copy(rsrc.properties.data) props.update(prop_diff) update_defn = rsrc_defn.ResourceDefinition(rsrc.name, rsrc.type(), props) rsrc.handle_update(update_defn, {}, prop_diff) self.assertIsNone(rsrc.handle_update(rsrc.t, {}, {})) self.m.VerifyAll()
def _create_test_instance(self, return_server, name): stack_name = '%s_s' % name t = template_format.parse(wp_template) template = parser.Template(t) kwargs = { 'KeyName': 'test', 'InstanceType': 'm1.large', 'SubnetId': '4156c7a5-e8c4-4aff-a6e1-8f3c7bc83861' } stack = parser.Stack(utils.dummy_context(), stack_name, template, environment.Environment(kwargs), stack_id=str(uuid.uuid4())) image_id = 'CentOS 5.2' t['Resources']['WebServer']['Properties']['ImageId'] = image_id resource_defns = stack.t.resource_definitions(stack) instance = instances.Instance('%s_name' % name, resource_defns['WebServer'], stack) metadata = instance.metadata_get() self.m.StubOutWithMock(nova.NovaClientPlugin, '_create') nova.NovaClientPlugin._create().AndReturn(self.fc) self._mock_get_image_id_success(image_id, 1) self.m.StubOutWithMock(instance, 'neutron') instance.neutron().MultipleTimes().AndReturn(FakeNeutron()) self.m.StubOutWithMock(neutron.NeutronClientPlugin, '_create') neutron.NeutronClientPlugin._create().MultipleTimes().AndReturn( FakeNeutron()) # need to resolve the template functions server_userdata = instance.client_plugin().build_userdata( metadata, instance.t['Properties']['UserData'], 'ec2-user') self.m.StubOutWithMock(nova.NovaClientPlugin, 'build_userdata') nova.NovaClientPlugin.build_userdata( metadata, instance.t['Properties']['UserData'], 'ec2-user').AndReturn(server_userdata) self.m.StubOutWithMock(self.fc.servers, 'create') self.fc.servers.create( image=1, flavor=3, key_name='test', name=utils.PhysName(stack_name, instance.name), security_groups=None, userdata=server_userdata, scheduler_hints=None, meta=None, nics=[{ 'port-id': '64d913c1-bcb1-42d2-8f0a-9593dbcaf251' }], availability_zone=None, block_device_mapping=None).AndReturn(return_server) self.m.ReplayAll() scheduler.TaskRunner(instance.create)() return instance
def _setup_test_instance(self, return_server, name, image_id=None, stub_create=True): stack_name = '%s_s' % name (t, stack) = self._setup_test_stack(stack_name) t['Resources']['WebServer']['Properties']['ImageId'] = \ image_id or 'CentOS 5.2' t['Resources']['WebServer']['Properties']['InstanceType'] = \ '256 MB Server' instance = instances.Instance(name, t['Resources']['WebServer'], stack) self.m.StubOutWithMock(instance, 'nova') instance.nova().MultipleTimes().AndReturn(self.fc) self.m.StubOutWithMock(clients.OpenStackClients, 'nova') clients.OpenStackClients.nova().MultipleTimes().AndReturn(self.fc) instance.t = instance.stack.resolve_runtime_data(instance.t) if stub_create: self.m.StubOutWithMock(self.fc.servers, 'create') self.fc.servers.create( image=1, flavor=1, key_name='test', name=utils.PhysName( stack_name, instance.name, limit=instance.physical_resource_name_limit), security_groups=None, userdata=mox.IgnoreArg(), scheduler_hints={'foo': ['spam', 'ham', 'baz'], 'bar': 'eggs'}, meta=None, nics=None, availability_zone=None).AndReturn( return_server) return instance
def test_loadbalancer(self): self._mock_get_image_id_success(u'F20-x86_64-cfntools', 746) self._create_stubs() self.m.ReplayAll() t = template_format.parse(lb_template) s = utils.parse_stack(t) s.store() rsrc = self.create_loadbalancer(t, s, 'LoadBalancer') hc = { 'Target': 'HTTP:80/', 'HealthyThreshold': '3', 'UnhealthyThreshold': '5', 'Interval': '30', 'Timeout': '5' } rsrc.t['Properties']['HealthCheck'] = hc self.assertIsNone(rsrc.validate()) hc['Timeout'] = 35 self.assertEqual({'Error': 'Interval must be larger than Timeout'}, rsrc.validate()) hc['Timeout'] = 5 self.assertEqual('LoadBalancer', rsrc.FnGetRefId()) templ = template_format.parse(lb.lb_template_default) ha_cfg = rsrc._haproxy_config(templ, rsrc.properties['Instances']) self.assertRegexpMatches(ha_cfg, 'bind \*:80') self.assertRegexpMatches( ha_cfg, 'server server1 1\.2\.3\.4:80 ' 'check inter 30s fall 5 rise 3') self.assertRegexpMatches(ha_cfg, 'timeout check 5s') id_list = [] resource_defns = s.t.resource_definitions(s) for inst_name in ['WikiServerOne1', 'WikiServerOne2']: inst = instance.Instance(inst_name, resource_defns['WikiServerOne'], s) id_list.append(inst.FnGetRefId()) prop_diff = {'Instances': id_list} props = copy.copy(rsrc.properties.data) props.update(prop_diff) update_defn = rsrc_defn.ResourceDefinition(rsrc.name, rsrc.type(), props) rsrc.handle_update(update_defn, {}, prop_diff) self.assertEqual('4.5.6.7', rsrc.FnGetAtt('DNSName')) self.assertEqual('', rsrc.FnGetAtt('SourceSecurityGroup.GroupName')) self.assertRaises(exception.InvalidTemplateAttribute, rsrc.FnGetAtt, 'Foo') self.assertIsNone(rsrc.handle_update(rsrc.t, {}, {})) self.m.VerifyAll()
def test_instance_create_duplicate_image_name_err(self): stack_name = 'test_instance_create_image_name_err_stack' (t, stack) = self._setup_test_stack(stack_name) # create an instance with a non unique image name t['Resources']['WebServer']['Properties']['ImageId'] = 'CentOS 5.2' instance = instances.Instance('instance_create_image_err', t['Resources']['WebServer'], stack) self.m.StubOutWithMock(instance, 'nova') instance.nova().MultipleTimes().AndReturn(self.fc) self.m.StubOutWithMock(self.fc.client, "get_images_detail") self.fc.client.get_images_detail().AndReturn((200, { 'images': [{ 'id': 1, 'name': 'CentOS 5.2' }, { 'id': 4, 'name': 'CentOS 5.2' }] })) self.m.ReplayAll() self.assertRaises(exception.PhysicalResourceNameAmbiguity, instance.handle_create) self.m.VerifyAll()
def _setup_test_instance(self, return_server, name): stack_name = '%s_stack' % name t = template_format.parse(wp_template) template = parser.Template(t) params = parser.Parameters(stack_name, template, {'KeyName': 'test'}) stack = parser.Stack(None, stack_name, template, params, stack_id=uuidutils.generate_uuid()) t['Resources']['WebServer']['Properties']['ImageId'] = 'CentOS 5.2' t['Resources']['WebServer']['Properties']['InstanceType'] = \ '256 MB Server' instance = instances.Instance('%s_name' % name, t['Resources']['WebServer'], stack) self.m.StubOutWithMock(instance, 'nova') instance.nova().MultipleTimes().AndReturn(self.fc) instance.t = instance.stack.resolve_runtime_data(instance.t) # need to resolve the template functions server_userdata = instance._build_userdata( instance.t['Properties']['UserData']) self.m.StubOutWithMock(self.fc.servers, 'create') self.fc.servers.create( image=1, flavor=1, key_name='test', name=utils.PhysName(stack_name, instance.name), security_groups=None, userdata=server_userdata, scheduler_hints=None, meta=None, nics=None, availability_zone=None).AndReturn( return_server) return instance
def test_nokey_create(self): stack_name = 'instance_create_test_nokey_stack' t = template_format.parse(nokey_template) stack = utils.parse_stack(t, stack_name=stack_name) t['Resources']['WebServer']['Properties']['ImageId'] = 'CentOS 5.2' t['Resources']['WebServer']['Properties']['InstanceType'] = \ '256 MB Server' instance = instances.Instance('create_instance_name', t['Resources']['WebServer'], stack) self.m.StubOutWithMock(instance, 'nova') instance.nova().MultipleTimes().AndReturn(self.fc) instance.t = instance.stack.resolve_runtime_data(instance.t) # need to resolve the template functions server_userdata = nova_utils.build_userdata( instance, instance.t['Properties']['UserData']) instance.mime_string = server_userdata self.m.StubOutWithMock(self.fc.servers, 'create') self.fc.servers.create( image=1, flavor=1, key_name=None, name=utils.PhysName(stack_name, instance.name), security_groups=None, userdata=server_userdata, scheduler_hints=None, meta=None, nics=None, availability_zone=None).AndReturn( self.fc.servers.list()[1]) self.m.ReplayAll() scheduler.TaskRunner(instance.create)()
def _create_test_instance_with_nic(self, return_server, name): stack_name = '%s_s' % name t = template_format.parse(wp_template_with_nic) template = parser.Template(t) kwargs = { 'KeyName': 'test', 'InstanceType': 'm1.large', 'SubnetId': '4156c7a5-e8c4-4aff-a6e1-8f3c7bc83861' } stack = parser.Stack(utils.dummy_context(), stack_name, template, environment.Environment(kwargs), stack_id=uuidutils.generate_uuid()) t['Resources']['WebServer']['Properties']['ImageId'] = 'CentOS 5.2' nic = network_interfaces.NetworkInterface('%s_nic' % name, t['Resources']['nic1'], stack) instance = instances.Instance('%s_name' % name, t['Resources']['WebServer'], stack) self.m.StubOutWithMock(nic, 'neutron') nic.neutron().MultipleTimes().AndReturn(FakeNeutron()) self.m.StubOutWithMock(instance, 'nova') instance.nova().MultipleTimes().AndReturn(self.fc) nic.t = nic.stack.resolve_runtime_data(nic.t) instance.t = instance.stack.resolve_runtime_data(instance.t) # need to resolve the template functions server_userdata = nova_utils.build_userdata( instance, instance.t['Properties']['UserData']) instance.mime_string = server_userdata self.m.StubOutWithMock(self.fc.servers, 'create') self.fc.servers.create(image=1, flavor=3, key_name='test', name=utils.PhysName(stack_name, instance.name), security_groups=None, userdata=server_userdata, scheduler_hints=None, meta=None, nics=[{ 'port-id': '64d913c1-bcb1-42d2-8f0a-9593dbcaf251' }], availability_zone=None).AndReturn(return_server) self.m.ReplayAll() # create network interface scheduler.TaskRunner(nic.create)() stack.resources["nic1"] = nic scheduler.TaskRunner(instance.create)() return instance
def test_loadbalancer(self): self._create_stubs() self.m.ReplayAll() t = template_format.parse(lb_template) s = utils.parse_stack(t) s.store() rsrc = self.create_loadbalancer(t, s, 'LoadBalancer') hc = { 'Target': 'HTTP:80/', 'HealthyThreshold': '3', 'UnhealthyThreshold': '5', 'Interval': '30', 'Timeout': '5' } rsrc.t['Properties']['HealthCheck'] = hc self.assertEqual(None, rsrc.validate()) hc['Timeout'] = 35 self.assertEqual({'Error': 'Interval must be larger than Timeout'}, rsrc.validate()) hc['Timeout'] = 5 self.assertEqual('LoadBalancer', rsrc.FnGetRefId()) templ = template_format.parse(lb.lb_template) ha_cfg = rsrc._haproxy_config(templ, rsrc.properties['Instances']) self.assertRegexpMatches(ha_cfg, 'bind \*:80') self.assertRegexpMatches( ha_cfg, 'server server1 1\.2\.3\.4:80 ' 'check inter 30s fall 5 rise 3') self.assertRegexpMatches(ha_cfg, 'timeout check 5s') id_list = [] for inst_name in ['WikiServerOne1', 'WikiServerOne2']: inst = instance.Instance(inst_name, s.t['Resources']['WikiServerOne'], s) id_list.append(inst.FnGetRefId()) rsrc.handle_update(rsrc.json_snippet, {}, {'Instances': id_list}) self.assertEqual('4.5.6.7', rsrc.FnGetAtt('DNSName')) self.assertEqual('', rsrc.FnGetAtt('SourceSecurityGroup.GroupName')) try: rsrc.FnGetAtt('Foo') raise Exception('Expected InvalidTemplateAttribute') except exception.InvalidTemplateAttribute: pass self.assertEqual(None, rsrc.handle_update({}, {}, {})) self.m.VerifyAll()
def handle_delete(self): if self.resource_id is not None: conf = self.properties['LaunchConfigurationName'] inst_list = self.resource_id.split(',') logger.debug('handle_delete %s' % str(inst_list)) for victim in inst_list: logger.debug('handle_delete %s' % victim) inst = instance.Instance(victim, self.stack.t['Resources'][conf], self.stack) inst.destroy()
def test_instance_create_delete(self): f = open("%s/WordPress_Single_Instance_gold.template" % self.path) t = json.loads(f.read()) f.close() t['Parameters']['KeyName']['Value'] = 'test' stack = parser.Stack(None, 'test_stack', parser.Template(t), stack_id=-1) self.m.StubOutWithMock(db_api, 'resource_get_by_name_and_stack') db_api.resource_get_by_name_and_stack(None, 'test_resource_name', stack).AndReturn(None) self.m.StubOutWithMock(instances.Instance, 'nova') instances.Instance.nova().AndReturn(self.fc) instances.Instance.nova().AndReturn(self.fc) instances.Instance.nova().AndReturn(self.fc) instances.Instance.nova().AndReturn(self.fc) self.m.ReplayAll() t['Resources']['WebServer']['Properties']['ImageId'] = 'CentOS 5.2' t['Resources']['WebServer']['Properties']['InstanceType'] = \ '256 MB Server' instance = instances.Instance('test_resource_name', t['Resources']['WebServer'], stack) instance.t = instance.stack.resolve_runtime_data(instance.t) # need to resolve the template functions server_userdata = instance._build_userdata( instance.t['Properties']['UserData']) self.m.StubOutWithMock(self.fc.servers, 'create') self.fc.servers.create(image=1, flavor=1, key_name='test', name='test_resource_name', security_groups=None, userdata=server_userdata, scheduler_hints=None, meta=None).AndReturn(self.fc.servers.list()[1]) self.m.ReplayAll() instance.instance_id = 1234 instance.create() # this makes sure the auto increment worked on instance creation self.assertTrue(instance.id > 0) instance.delete() self.assertTrue(instance.instance_id is None) self.assertEqual(instance.state, instance.DELETE_COMPLETE)
def test_instance_create_delete(self): f = open("%s/WordPress_Single_Instance_gold.template" % self.path) t = template_format.parse(f.read()) f.close() stack_name = 'instance_create_delete_test_stack' template = parser.Template(t) params = parser.Parameters(stack_name, template, {'KeyName': 'test'}) stack = parser.Stack(None, stack_name, template, params, stack_id=uuidutils.generate_uuid()) t['Resources']['WebServer']['Properties']['ImageId'] = 'CentOS 5.2' t['Resources']['WebServer']['Properties']['InstanceType'] = \ '256 MB Server' instance = instances.Instance('create_delete_instance_name', t['Resources']['WebServer'], stack) self.m.StubOutWithMock(instance, 'nova') instance.nova().MultipleTimes().AndReturn(self.fc) instance.t = instance.stack.resolve_runtime_data(instance.t) # need to resolve the template functions server_userdata = instance._build_userdata( instance.t['Properties']['UserData']) self.m.StubOutWithMock(self.fc.servers, 'create') self.fc.servers.create( image=1, flavor=1, key_name='test', name='%s.%s' % (stack_name, instance.name), security_groups=None, userdata=server_userdata, scheduler_hints=None, meta=None, nics=None, availability_zone=None).AndReturn( self.fc.servers.list()[1]) self.m.ReplayAll() self.assertEqual(instance.create(), None) instance.resource_id = 1234 # this makes sure the auto increment worked on instance creation self.assertTrue(instance.id > 0) self.m.StubOutWithMock(self.fc.client, 'get_servers_1234') get = self.fc.client.get_servers_1234 get().AndRaise(instances.clients.novaclient.exceptions.NotFound(404)) mox.Replay(get) instance.delete() self.assertTrue(instance.resource_id is None) self.assertEqual(instance.state, instance.DELETE_COMPLETE) self.m.VerifyAll()
def _create_test_instance(self, return_server, name): stack_name = '%s_stack' % name t = template_format.parse(wp_template) template = parser.Template(t) kwargs = { 'KeyName': 'test', 'InstanceType': 'm1.large', 'SubnetId': '4156c7a5-e8c4-4aff-a6e1-8f3c7bc83861' } params = parser.Parameters(stack_name, template, kwargs) stack = parser.Stack(None, stack_name, template, params, stack_id=uuidutils.generate_uuid()) t['Resources']['WebServer']['Properties']['ImageId'] = 'CentOS 5.2' instance = instances.Instance('%s_name' % name, t['Resources']['WebServer'], stack) self.m.StubOutWithMock(instance, 'nova') instance.nova().MultipleTimes().AndReturn(self.fc) self.m.StubOutWithMock(instance, 'quantum') instance.quantum().MultipleTimes().AndReturn(FakeQuantum()) instance.t = instance.stack.resolve_runtime_data(instance.t) # need to resolve the template functions server_userdata = instance._build_userdata( instance.t['Properties']['UserData']) self.m.StubOutWithMock(self.fc.servers, 'create') self.fc.servers.create(image=1, flavor=3, key_name='test', name=utils.PhysName(stack_name, instance.name), security_groups=None, userdata=server_userdata, scheduler_hints=None, meta=None, nics=[{ 'port-id': '64d913c1-bcb1-42d2-8f0a-9593dbcaf251' }], availability_zone=None).AndReturn(return_server) self.m.ReplayAll() scheduler.TaskRunner(instance.create)() return instance
def test_nokey_create(self): stack_name = 's_nokey' t = template_format.parse(nokey_template) stack = utils.parse_stack(t, stack_name=stack_name) t['Resources']['WebServer']['Properties']['ImageId'] = 'CentOS 5.2' t['Resources']['WebServer']['Properties']['InstanceType'] = \ '256 MB Server' resource_defns = stack.t.resource_definitions(stack) instance = instances.Instance('create_instance_name', resource_defns['WebServer'], stack) self.m.StubOutWithMock(nova.NovaClientPlugin, '_create') nova.NovaClientPlugin._create().AndReturn(self.fc) g_cli_mock = self.m.CreateMockAnything() self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create') glance.GlanceClientPlugin._create().MultipleTimes().AndReturn( g_cli_mock) self.m.StubOutWithMock(glance_utils, 'get_image_id') glance_utils.get_image_id(g_cli_mock, 'CentOS 5.2').MultipleTimes().\ AndReturn(1) # need to resolve the template functions server_userdata = nova_utils.build_userdata( instance, instance.t['Properties']['UserData'], 'ec2-user') self.m.StubOutWithMock(nova_utils, 'build_userdata') nova_utils.build_userdata(instance, instance.t['Properties']['UserData'], 'ec2-user').AndReturn(server_userdata) self.m.StubOutWithMock(self.fc.servers, 'create') self.fc.servers.create(image=1, flavor=1, key_name=None, name=utils.PhysName(stack_name, instance.name), security_groups=None, userdata=server_userdata, scheduler_hints=None, meta=None, nics=None, availability_zone=None).AndReturn( self.fc.servers.list()[1]) self.m.ReplayAll() scheduler.TaskRunner(instance.create)() self.m.VerifyAll()
def test_instance_create_image_id_err(self): stack_name = 'test_instance_create_image_id_err_stack' (t, stack) = self._setup_test_stack(stack_name) # create an instance with non exist image Id t['Resources']['WebServer']['Properties']['ImageId'] = '1' instance = instances.Instance('instance_create_image_err', t['Resources']['WebServer'], stack) self._mock_get_image_id_fail('1', glance_exceptions.NotFound(404)) self.m.ReplayAll() self.assertRaises(ValueError, instance.handle_create) self.m.VerifyAll()
def test_instance_create_image_name_err(self): stack_name = 'test_instance_create_image_name_err_stack' (t, stack) = self._setup_test_stack(stack_name) # create an instance with non exist image name t['Resources']['WebServer']['Properties']['ImageId'] = 'Slackware' instance = instances.Instance('instance_create_image_err', t['Resources']['WebServer'], stack) self.m.StubOutWithMock(instance, 'nova') instance.nova().MultipleTimes().AndReturn(self.fc) self.m.ReplayAll() self.assertRaises(exception.ImageNotFound, instance.handle_create) self.m.VerifyAll()
def test_instance_validate(self): stack_name = 'test_instance_validate_stack' (t, stack) = self._setup_test_stack(stack_name) t['Resources']['WebServer']['Properties']['ImageId'] = '1' instance = instances.Instance('instance_create_image', t['Resources']['WebServer'], stack) self.m.StubOutWithMock(clients.OpenStackClients, 'nova') clients.OpenStackClients.nova().MultipleTimes().AndReturn(self.fc) self._mock_get_image_id_success('1', 1) self.m.ReplayAll() self.assertIsNone(instance.validate()) self.m.VerifyAll()
def test_instance_create_image_name_err(self): stack_name = 'test_instance_create_image_name_err_stack' (t, stack) = self._setup_test_stack(stack_name) # create an instance with non exist image name t['Resources']['WebServer']['Properties']['ImageId'] = 'Slackware' instance = instances.Instance('instance_create_image_err', t['Resources']['WebServer'], stack) g_cli_moc = self.m.CreateMockAnything() self.m.StubOutWithMock(clients.OpenStackClients, 'glance') clients.OpenStackClients.glance().MultipleTimes().AndReturn(g_cli_moc) self.m.ReplayAll() self.assertRaises(ValueError, instance.handle_create) self.m.VerifyAll()
def test_instance_create_duplicate_image_name_err(self): stack_name = 'test_instance_create_image_name_err_stack' (t, stack) = self._setup_test_stack(stack_name) # create an instance with a non unique image name t['Resources']['WebServer']['Properties']['ImageId'] = 'CentOS 5.2' instance = instances.Instance('instance_create_image_err', t['Resources']['WebServer'], stack) self._mock_get_image_id_fail( 'CentOS 5.2', exception.PhysicalResourceNameAmbiguity(name='CentOS 5.2')) self.m.ReplayAll() self.assertRaises(ValueError, instance.handle_create) self.m.VerifyAll()
def test_instance_validate(self): stack_name = 'test_instance_validate_stack' (tmpl, stack) = self._setup_test_stack(stack_name) tmpl.t['Resources']['WebServer']['Properties']['ImageId'] = '1' resource_defns = tmpl.resource_definitions(stack) instance = instances.Instance('instance_create_image', resource_defns['WebServer'], stack) self.m.StubOutWithMock(nova.NovaClientPlugin, '_create') nova.NovaClientPlugin._create().MultipleTimes().AndReturn(self.fc) self._mock_get_image_id_success('1', 1) self.m.ReplayAll() self.assertIsNone(instance.validate()) self.m.VerifyAll()
def _setup_test_instance(self, intags=None, nova_tags=None): stack_name = 'tag_test' t = template_format.parse(instance_template) template = parser.Template(t) stack = parser.Stack(utils.dummy_context(), stack_name, template, environment.Environment({'KeyName': 'test'}), stack_id=str(uuid.uuid4())) t['Resources']['WebServer']['Properties']['Tags'] = intags instance = instances.Instance(stack_name, t['Resources']['WebServer'], stack) self.m.StubOutWithMock(instance, 'nova') instance.nova().MultipleTimes().AndReturn(self.fc) self.m.StubOutWithMock(clients.OpenStackClients, 'nova') clients.OpenStackClients.nova().MultipleTimes().AndReturn(self.fc) instance.t = instance.stack.resolve_runtime_data(instance.t) # need to resolve the template functions server_userdata = nova_utils.build_userdata( instance, instance.t['Properties']['UserData']) self.m.StubOutWithMock(nova_utils, 'build_userdata') nova_utils.build_userdata( instance, instance.t['Properties']['UserData']).AndReturn(server_userdata) self.m.StubOutWithMock(self.fc.servers, 'create') self.fc.servers.create(image=1, flavor=1, key_name='test', name=utils.PhysName(stack_name, instance.name), security_groups=None, userdata=server_userdata, scheduler_hints=None, meta=nova_tags, nics=None, availability_zone=None).AndReturn( self.fc.servers.list()[1]) return instance
def test_instance_validate(self): stack_name = 'test_instance_validate_stack' (t, stack) = self._setup_test_stack(stack_name) # create an instance with non exist image Id t['Resources']['WebServer']['Properties']['ImageId'] = '1' instance = instances.Instance('instance_create_image_err', t['Resources']['WebServer'], stack) self.m.StubOutWithMock(instance, 'nova') instance.nova().MultipleTimes().AndReturn(self.fc) self.m.StubOutWithMock(uuidutils, "is_uuid_like") uuidutils.is_uuid_like('1').AndReturn(True) self.m.ReplayAll() self.assertIsNone(instance.validate()) self.m.VerifyAll()
def _setup_test_instance(self, intags=None, nova_tags=None): stack_name = 'tag_test' t = template_format.parse(instance_template) template = parser.Template(t) stack = parser.Stack(utils.dummy_context(), stack_name, template, environment.Environment({'KeyName': 'test'}), stack_id=str(uuid.uuid4())) t['Resources']['WebServer']['Properties']['Tags'] = intags resource_defns = template.resource_definitions(stack) instance = instances.Instance(stack_name, resource_defns['WebServer'], stack) self.m.StubOutWithMock(nova.NovaClientPlugin, '_create') nova.NovaClientPlugin._create().AndReturn(self.fc) self._mock_get_image_id_success('CentOS 5.2', 1) # need to resolve the template functions metadata = instance.metadata_get() server_userdata = instance.client_plugin().build_userdata( metadata, instance.t['Properties']['UserData'], 'ec2-user') self.m.StubOutWithMock(nova.NovaClientPlugin, 'build_userdata') nova.NovaClientPlugin.build_userdata( metadata, instance.t['Properties']['UserData'], 'ec2-user').AndReturn(server_userdata) self.m.StubOutWithMock(self.fc.servers, 'create') self.fc.servers.create(image=1, flavor=1, key_name='test', name=utils.PhysName(stack_name, instance.name), security_groups=None, userdata=server_userdata, scheduler_hints=None, meta=nova_tags, nics=None, availability_zone=None, block_device_mapping=None).AndReturn( self.fc.servers.list()[1]) return instance
def test_instance_update_metadata(self): f = open("%s/WordPress_Single_Instance_gold.template" % self.path) t = template_format.parse(f.read()) f.close() stack_name = 'instance_update_test_stack' template = parser.Template(t) params = parser.Parameters(stack_name, template, {'KeyName': 'test'}) stack = parser.Stack(None, stack_name, template, params, stack_id=uuidutils.generate_uuid()) t['Resources']['WebServer']['Properties']['ImageId'] = 'CentOS 5.2' t['Resources']['WebServer']['Properties']['InstanceType'] = \ '256 MB Server' instance = instances.Instance('create_instance_name', t['Resources']['WebServer'], stack) self.m.StubOutWithMock(instance, 'nova') instance.nova().MultipleTimes().AndReturn(self.fc) instance.t = instance.stack.resolve_runtime_data(instance.t) # need to resolve the template functions server_userdata = instance._build_userdata( instance.t['Properties']['UserData']) self.m.StubOutWithMock(self.fc.servers, 'create') self.fc.servers.create( image=1, flavor=1, key_name='test', name='%s.%s' % (stack_name, instance.name), security_groups=None, userdata=server_userdata, scheduler_hints=None, meta=None, nics=None, availability_zone=None).AndReturn( self.fc.servers.list()[1]) self.m.ReplayAll() self.assertEqual(instance.create(), None) update_template = copy.deepcopy(instance.t) update_template['Metadata'] = {'test': 123} self.assertEqual(instance.update(update_template), instance.UPDATE_COMPLETE) self.assertEqual(instance.metadata, {'test': 123})
def test_instance_create_image_id_err(self): stack_name = 'test_instance_create_image_id_err_stack' (t, stack) = self._setup_test_stack(stack_name) # create an instance with non exist image Id t['Resources']['WebServer']['Properties']['ImageId'] = '1' instance = instances.Instance('instance_create_image_err', t['Resources']['WebServer'], stack) self.m.StubOutWithMock(instance, 'nova') instance.nova().MultipleTimes().AndReturn(self.fc) self.m.StubOutWithMock(uuidutils, "is_uuid_like") uuidutils.is_uuid_like('1').AndReturn(True) self.m.StubOutWithMock(self.fc.client, "get_images_1") self.fc.client.get_images_1().AndRaise( instances.clients.novaclient.exceptions.NotFound(404)) self.m.ReplayAll() self.assertRaises(exception.ImageNotFound, instance.handle_create) self.m.VerifyAll()
def test_instance_create_image_id_err(self): stack_name = 'test_instance_create_image_id_err_stack' (tmpl, stack) = self._setup_test_stack(stack_name) # create an instance with non exist image Id tmpl.t['Resources']['WebServer']['Properties']['ImageId'] = '1' resource_defns = tmpl.resource_definitions(stack) instance = instances.Instance('instance_create_image_err', resource_defns['WebServer'], stack) self._mock_get_image_id_fail('1', glance_exceptions.NotFound(404)) self.m.ReplayAll() create = scheduler.TaskRunner(instance.create) error = self.assertRaises(exception.ResourceFailure, create) self.assertEqual( 'StackValidationFailed: Property error : WebServer: ' 'ImageId 404 (HTTP 404)', str(error)) self.m.VerifyAll()
def test_instance_create_image_name_err(self): stack_name = 'test_instance_create_image_name_err_stack' (tmpl, stack) = self._setup_test_stack(stack_name) # create an instance with non exist image name tmpl.t['Resources']['WebServer']['Properties']['ImageId'] = 'Slackware' resource_defns = tmpl.resource_definitions(stack) instance = instances.Instance('instance_create_image_err', resource_defns['WebServer'], stack) self._mock_get_image_id_fail('Slackware', exception.ImageNotFound( image_name='Slackware')) self.m.ReplayAll() create = scheduler.TaskRunner(instance.create) error = self.assertRaises(exception.ResourceFailure, create) self.assertEqual( 'StackValidationFailed: Property error : WebServer: ' 'ImageId Error validating value \'Slackware\': ' 'The Image (Slackware) could not be found.', str(error)) self.m.VerifyAll()