# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import copy
import unittest
import mox
from nose.plugins.attrib import attr
from heat.tests.v1_1 import fakes
from heat.engine.resources import instance as instances
from heat.common import template_format
from heat.engine import parser
from heat.openstack.common import uuidutils
wp_template = '''
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "WordPress",
"Parameters" : {
"KeyName" : {
"Description" : "KeyName",
"Type" : "String",
"Default" : "test"
}
},
"Resources" : {
"WebServer": {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId" : "F17-x86_64-gold",
"InstanceType" : "m1.large",
"KeyName" : "test",
"UserData" : "wordpress"
}
}
}
}
'''
@attr(tag=['unit', 'resource', 'instance'])
@attr(speed='fast')
[docs]class instancesTest(unittest.TestCase):
[docs] def setUp(self):
self.m = mox.Mox()
self.fc = fakes.FakeClient()
self.path = os.path.dirname(os.path.realpath(__file__)).\
replace('heat/tests', 'templates')
[docs] def tearDown(self):
self.m.UnsetStubs()
print "instancesTest teardown complete"
def _setup_test_stack(self, stack_name):
t = template_format.parse(wp_template)
template = parser.Template(t)
stack = parser.Stack(utils.dummy_context(), stack_name, template,
stack_id=uuidutils.generate_uuid())
return (t, stack)
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 = instance._build_userdata(
instance.t['Properties']['UserData'])
instance.mime_string = "wordpress"
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(
return_server)
return instance
def _create_test_instance(self, return_server, name):
t = template_format.parse(wp_template)
stack_name = 'instance_create_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)
return instance
[docs] def test_instance_create(self):
f = open("%s/WordPress_Single_Instance_gold.template" % self.path)
t = template_format.parse(f.read())
f.close()
stack_name = 'instance_create_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)
# this makes sure the auto increment worked on instance creation
self.assertTrue(instance.id > 0)
self.assertEqual(instance.FnGetAtt('PublicIp'), '4.5.6.7')
self.assertEqual(instance.FnGetAtt('PrivateIp'), '4.5.6.7')
self.assertEqual(instance.FnGetAtt('PrivateDnsName'), '4.5.6.7')
self.assertEqual(instance.FnGetAtt('PrivateDnsName'), '4.5.6.7')
self.m.VerifyAll()
[docs] 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()
[docs] def test_build_nics(self):
return_server = self.fc.servers.list()[1]
instance = self._create_test_instance(return_server,
'test_build_nics')
self.assertEqual(None, instance._build_nics([]))
self.assertEqual(None, instance._build_nics(None))
self.assertEqual([
{'port-id': 'id3'}, {'port-id': 'id1'}, {'port-id': 'id2'}],
instance._build_nics([
'id3', 'id1', 'id2']))
self.assertEqual([
{'port-id': 'id1'},
{'port-id': 'id2'},
{'port-id': 'id3'}], instance._build_nics([
{'NetworkInterfaceId': 'id3', 'DeviceIndex': '3'},
{'NetworkInterfaceId': 'id1', 'DeviceIndex': '1'},
{'NetworkInterfaceId': 'id2', 'DeviceIndex': 2},
]))
self.assertEqual([
{'port-id': 'id1'},
{'port-id': 'id2'},
{'port-id': 'id3'},
{'port-id': 'id4'},
{'port-id': 'id5'}
], instance._build_nics([
{'NetworkInterfaceId': 'id3', 'DeviceIndex': '3'},
{'NetworkInterfaceId': 'id1', 'DeviceIndex': '1'},
{'NetworkInterfaceId': 'id2', 'DeviceIndex': 2},
'id4',
'id5'
]))