258633ee70
blueprint sahara-image Depends-On: I94ac401034787955f0f39caf6fd055339b7be69c Change-Id: I8928f7d2b5fbcb278ba55c0425e2a562a6d6d763
113 lines
4.4 KiB
Python
113 lines
4.4 KiB
Python
#
|
|
# 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 mock
|
|
|
|
from heat.common import template_format
|
|
from heat.engine.clients.os import glance
|
|
from heat.engine.clients.os import sahara
|
|
from heat.engine.resources.openstack.sahara import sahara_image
|
|
from heat.engine import scheduler
|
|
from heat.tests import common
|
|
from heat.tests import utils
|
|
|
|
|
|
sahara_image_template = """
|
|
heat_template_version: 2015-10-15
|
|
resources:
|
|
sahara-image:
|
|
type: OS::Sahara::ImageRegistry
|
|
properties:
|
|
image: sahara-icehouse-vanilla-1.2.1-ubuntu-13.10
|
|
username: ubuntu
|
|
tags:
|
|
- vanilla
|
|
- 1.2.1
|
|
"""
|
|
|
|
|
|
class SaharaImageTest(common.HeatTestCase):
|
|
def setUp(self):
|
|
super(SaharaImageTest, self).setUp()
|
|
t = template_format.parse(sahara_image_template)
|
|
self.stack = utils.parse_stack(t)
|
|
resource_defns = self.stack.t.resource_definitions(self.stack)
|
|
self.rsrc_defn = resource_defns['sahara-image']
|
|
self.client = mock.Mock()
|
|
self.patchobject(sahara_image.SaharaImageRegistry, 'client',
|
|
return_value=self.client)
|
|
self.patchobject(glance.GlanceClientPlugin, 'get_image_id',
|
|
return_value='12345')
|
|
|
|
def _create_resource(self, name, snippet, stack):
|
|
img = sahara_image.SaharaImageRegistry(name, snippet, stack)
|
|
scheduler.TaskRunner(img.create)()
|
|
return img
|
|
|
|
def test_create(self):
|
|
img = self._create_resource('sahara-image', self.rsrc_defn, self.stack)
|
|
args = ('12345', 'ubuntu', '')
|
|
self.client.images.update_image.assert_called_once_with(*args)
|
|
self.client.images.update_tags.assert_called_once_with(
|
|
'12345', ['vanilla', '1.2.1'])
|
|
self.assertEqual('12345', img.resource_id)
|
|
expected_state = (img.CREATE, img.COMPLETE)
|
|
self.assertEqual(expected_state, img.state)
|
|
|
|
def test_update(self):
|
|
img = self._create_resource('sahara-image', self.rsrc_defn, self.stack)
|
|
self.rsrc_defn['Properties']['tags'] = []
|
|
self.rsrc_defn['Properties']['description'] = 'test image'
|
|
scheduler.TaskRunner(img.update, self.rsrc_defn)()
|
|
tags_update_calls = [
|
|
mock.call('12345', ['vanilla', '1.2.1']),
|
|
mock.call('12345', [])
|
|
]
|
|
image_update_calls = [
|
|
mock.call('12345', 'ubuntu', ''),
|
|
mock.call('12345', 'ubuntu', 'test image')
|
|
]
|
|
self.client.images.update_image.assert_has_calls(image_update_calls)
|
|
self.client.images.update_tags.assert_has_calls(tags_update_calls)
|
|
self.assertEqual((img.UPDATE, img.COMPLETE), img.state)
|
|
|
|
def test_delete(self):
|
|
img = self._create_resource('sahara-image', self.rsrc_defn, self.stack)
|
|
scheduler.TaskRunner(img.delete)()
|
|
self.assertEqual((img.DELETE, img.COMPLETE), img.state)
|
|
self.client.images.unregister_image.assert_called_once_with(
|
|
img.resource_id)
|
|
|
|
def test_delete_not_found(self):
|
|
img = self._create_resource('sahara-image', self.rsrc_defn, self.stack)
|
|
self.client.images.unregister_image.side_effect = (
|
|
sahara.sahara_base.APIException(error_code=404))
|
|
scheduler.TaskRunner(img.delete)()
|
|
self.assertEqual((img.DELETE, img.COMPLETE), img.state)
|
|
self.client.images.unregister_image.assert_called_once_with(
|
|
img.resource_id)
|
|
|
|
def test_show_attribute(self):
|
|
img = self._create_resource('sahara-image', self.rsrc_defn, self.stack)
|
|
value = mock.MagicMock()
|
|
value.to_dict.return_value = {'img': 'info'}
|
|
self.client.images.get.return_value = value
|
|
self.assertEqual({'img': 'info'}, img.FnGetAtt('show'))
|
|
|
|
def test_resource_mapping(self):
|
|
mapping = sahara_image.resource_mapping()
|
|
self.assertEqual(1, len(mapping))
|
|
self.assertEqual(sahara_image.SaharaImageRegistry,
|
|
mapping['OS::Sahara::ImageRegistry'])
|