def test_restore(self): heat_template = HeatTemplate() resource = Resource( id="123", type=constants.VOLUME_RESOURCE_TYPE, name="fake", ) checkpoint = self._get_checkpoint() section = checkpoint.get_resource_bank_section() section.update_object('metadata', { 'backup_id': '456', }) parameters = { "restore_name": "karbor restore volume", "restore_description": "karbor restore", } operation = self.plugin.get_restore_operation(resource) call_hooks(operation, checkpoint, resource, self.cntxt, parameters, heat_template=heat_template) self.assertEqual(1, len(heat_template._resources)) heat_resource_id = heat_template._original_id_resource_map["123"] template_dict = { "heat_template_version": str(datetime.date(2015, 10, 15)), "description": "karbor restore template", "resources": { heat_resource_id: { "type": "OS::Cinder::Volume", "properties": { "description": "karbor restore", "backup_id": "456", "name": "karbor restore volume", } } } } self.assertEqual(template_dict, heat_template.to_dict())
def test_restore_backup(self): heat_template = HeatTemplate() resource = Resource(id="123", type=constants.VOLUME_RESOURCE_TYPE, name="fake") resource_node = ResourceNode(value=resource, child_nodes=[]) resource_definition = {"backup_id": "456"} kwargs = {"node": resource_node, "heat_template": heat_template, "restore_name": "karbor restore volume", "restore_description": "karbor restore"} fake_bank_section.get_object = mock.MagicMock() fake_bank_section.get_object.return_value = resource_definition self.plugin.restore_backup(self.cntxt, self.checkpoint, **kwargs) self.assertEqual(1, len(heat_template._resources)) heat_resource_id = heat_template._original_id_resource_map["123"] template_dict = { "heat_template_version": str(datetime.date(2015, 10, 15)), "description": "karbor restore template", "resources": { heat_resource_id: { "type": "OS::Cinder::Volume", "properties": { "description": "karbor restore", "backup_id": "456", "name": "karbor restore volume", } } } } self.assertEqual(template_dict, heat_template.to_dict())
def get_flow(context, workflow_engine, operation_type, checkpoint, provider, restore, restore_auth): target = restore.get('restore_target', None) # TODO(luobin): create a heat_client # Initialize a heat client using current login tenant when the # restore_target and restore_auth is not provided. if target is not None: username = None password = None if restore_auth is not None: auth_type = restore_auth.get("type", None) if auth_type == "password": username = restore_auth["username"] password = restore_auth["password"] kwargs = { "auth_url": target, "username": username, "password": password } else: kwargs = {} heat_client = ClientFactory.create_client("heat", context=context, **kwargs) # TODO(luobin): create a heat_template heat_template = HeatTemplate() ctx = { 'context': context, 'checkpoint': checkpoint, 'workflow_engine': workflow_engine, 'operation_type': operation_type, 'restore': restore, 'heat_template': heat_template } flow_name = "create_restoration_" + checkpoint.id restoration_flow = workflow_engine.build_flow(flow_name, 'linear') result = provider.build_task_flow(ctx) resource_flow = result.get('task_flow') workflow_engine.add_tasks( restoration_flow, resource_flow, CreateStackTask(heat_client, heat_template), SyncStackStatusTask(checkpoint, heat_client, restore)) flow_engine = workflow_engine.get_engine(restoration_flow) return flow_engine
def test_restore_succeed(self, mock_update_restore, mock_cinder_create): resource = Resource( id="123", type=constants.VOLUME_RESOURCE_TYPE, name="fake", ) checkpoint = self._get_checkpoint() section = checkpoint.get_resource_bank_section() section.update_object('metadata', { 'backup_id': '456', }) parameters = { "restore_name": "karbor restore volume", "restore_description": "karbor restore", } operation = self.plugin.get_restore_operation(resource) mock_cinder_create.return_value = self.cinder_client with mock.patch.multiple( self.cinder_client, volumes=mock.DEFAULT, restores=mock.DEFAULT, ) as mocks: volume_id = 456 mocks['volumes'].get.return_value = mock.Mock() mocks['volumes'].get.return_value.status = 'available' mocks['restores'].restore = RestoreResponse(volume_id) call_hooks(operation, checkpoint, resource, self.cntxt, parameters, **{ 'restore': None, 'heat_template': HeatTemplate() }) mocks['volumes'].update.assert_called_with( volume_id, **{ 'name': parameters['restore_name'], 'description': parameters['restore_description'] }) mock_update_restore.assert_called_with(None, resource.type, volume_id, 'available')
def setUp(self): super(HeatTemplateTest, self).setUp() self.heat_template = HeatTemplate()
class HeatTemplateTest(base.TestCase): def setUp(self): super(HeatTemplateTest, self).setUp() self.heat_template = HeatTemplate() def test_put_resource(self): fake_original_id = "123456" fake_resource_id = "restore_123456" fake_resource_type = "OS::Cinder::Volume" heat_resource = HeatResource(fake_resource_id, fake_resource_type) properties = { "volume_type": "lvmdriver-1", "size": 1 } for key, value in properties.items(): heat_resource.set_property(key, value) self.heat_template.put_resource(fake_original_id, heat_resource) self.assertEqual(1, len(self.heat_template._resources)) self.assertEqual( fake_resource_id, self.heat_template._original_id_resource_map[fake_original_id] ) def test_put_parameter(self): fake_original_id = "123456" fake_parameter = "restored_123456" self.heat_template.put_parameter(fake_original_id, fake_parameter) self.assertEqual(1, len(self.heat_template._original_id_parameter_map)) self.assertEqual( fake_parameter, self.heat_template._original_id_parameter_map[fake_original_id] ) def test_get_resource_reference(self): fake_original_id = "123456" fake_resource_id = "restore_123456" fake_resource_type = "OS::Cinder::Volume" heat_resource = HeatResource(fake_resource_id, fake_resource_type) properties = { "volume_type": "lvmdriver-1", "size": 1 } for key, value in properties.items(): heat_resource.set_property(key, value) self.heat_template.put_resource(fake_original_id, heat_resource) reference = self.heat_template.get_resource_reference(fake_original_id) self.assertEqual({"get_resource": "restore_123456"}, reference) fake_original_id = '23456' fake_parameter = 'restored_23456' self.heat_template.put_parameter(fake_original_id, fake_parameter) reference = self.heat_template.get_resource_reference(fake_original_id) self.assertEqual(fake_parameter, reference) def test_to_dict(self): fake_original_id = "123456" fake_resource_id = "restore_123456" fake_resource_type = "OS::Cinder::Volume" heat_resource = HeatResource(fake_resource_id, fake_resource_type) properties = { "volume_type": "lvmdriver-1", "size": 1 } for key, value in properties.items(): heat_resource.set_property(key, value) self.heat_template.put_resource(fake_original_id, heat_resource) fake_original_id_2 = '23456' fake_parameter = 'restored_23456' self.heat_template.put_parameter(fake_original_id_2, fake_parameter) template_dict = self.heat_template.to_dict() target_dict = { "heat_template_version": str(datetime.date(2015, 10, 15)), "description": "karbor restore template", "resources": { "restore_123456": { "type": "OS::Cinder::Volume", "properties": { "volume_type": "lvmdriver-1", "size": 1 } } } } self.assertEqual(target_dict, template_dict) def test_dump_to_yaml_file(self): fake_original_id = "123456" temp_dir = tempfile.mkdtemp() temp_file = temp_dir + "/template.yaml" fake_resource_id = "restore_123456" fake_resource_type = "OS::Cinder::Volume" heat_resource = HeatResource(fake_resource_id, fake_resource_type) properties = { "volume_type": "lvmdriver-1", "size": 1 } for key, value in properties.items(): heat_resource.set_property(key, value) self.heat_template.put_resource(fake_original_id, heat_resource) self.heat_template.dump_to_yaml_file(temp_file) with open(temp_file, "r") as f: template_dict = yaml.load(f) target_dict = { "heat_template_version": str(datetime.date(2015, 10, 15)), "description": "karbor restore template", "resources": { "restore_123456": { "type": "OS::Cinder::Volume", "properties": { "volume_type": "lvmdriver-1", "size": 1 } } } } self.assertEqual(target_dict, template_dict)