def test_list_checkpoints_by_plan_id(self): collection = self._create_test_collection() plan_1 = fake_protection_plan() plan_1["id"] = "fake_plan_id_1" plan_1['provider_id'] = "fake_provider_id_1" plan_1["project_id"] = "fake_project_id_1" provider_id_1 = plan_1['provider_id'] checkpoints_plan_1 = {collection.create(plan_1).id for i in range(10)} plan_2 = fake_protection_plan() plan_2["id"] = "fake_plan_id_2" plan_2['provider_id'] = "fake_provider_id_2" plan_2["project_id"] = "fake_project_id_2" provider_id_2 = plan_1['provider_id'] checkpoints_plan_2 = {collection.create(plan_2).id for i in range(10)} self.assertEqual( set( collection.list_ids(project_id="fake_project_id_1", provider_id=provider_id_1, plan_id="fake_plan_id_1")), checkpoints_plan_1) self.assertEqual( set( collection.list_ids(project_id="fake_project_id_2", provider_id=provider_id_2, plan_id="fake_plan_id_2")), checkpoints_plan_2)
def test_list_checkpoints_by_plan_id(self): collection = self._create_test_collection() plan_1 = fake_protection_plan() plan_1["id"] = "fake_plan_id_1" checkpoints_plan_1 = {collection.create(plan_1).id for i in range(10)} plan_2 = fake_protection_plan() plan_2["id"] = "fake_plan_id_2" checkpoints_plan_2 = {collection.create(plan_2).id for i in range(10)} self.assertEqual(set(collection.list_ids(plan_id="fake_plan_id_1")), checkpoints_plan_1) self.assertEqual(set(collection.list_ids(plan_id="fake_plan_id_2")), checkpoints_plan_2)
def setUp(self): self.load_engine = flow_manager.Worker._load_engine flow_manager.Worker._load_engine = mock.Mock() flow_manager.Worker._load_engine.return_value = fakes.FakeFlowEngine() super(ProtectionServiceTest, self).setUp() self.pro_manager = manager.ProtectionManager() self.protection_plan = fakes.fake_protection_plan()
def setUp(self): self.load_engine = flow_manager.Worker._load_engine flow_manager.Worker._load_engine = mock.Mock() flow_manager.Worker._load_engine.return_value = fakes.FakeFlowEngine() super(ProtectionServiceTest, self).setUp() self.pro_manager = manager.ProtectionManager() self.protection_plan = fakes.fake_protection_plan()
def test_resource_graph(self): bank = bank_plugin.Bank(_InMemoryBankPlugin()) bank_lease = _InMemoryLeasePlugin() checkpoints_section = bank_plugin.BankSection(bank, "/checkpoints") indices_section = bank_plugin.BankSection(bank, "/indices") owner_id = bank.get_owner_id() plan = fake_protection_plan() cp = checkpoint.Checkpoint.create_in_section( checkpoints_section=checkpoints_section, indices_section=indices_section, bank_lease=bank_lease, owner_id=owner_id, plan=plan) resource_graph = graph.build_graph([A, B, C, D], resource_map.__getitem__) cp.resource_graph = resource_graph cp.commit() checkpoint_data = cp._md_cache self.assertEqual( checkpoint_data, bank._plugin.get_object( "/checkpoints/%s/%s" % (checkpoint_data["id"], checkpoint._INDEX_FILE_NAME))) self.assertEqual(len(resource_graph), len(cp.resource_graph)) for start_node in resource_graph: self.assertIn(start_node, cp.resource_graph)
def test_delete_checkpoint(self): collection = self._create_test_collection() result = { collection.create(fake_protection_plan()).id for i in range(10)} checkpoint = collection.get(result.pop()) checkpoint.purge() self.assertEqual(set(collection.list_ids()), result)
def test_list_checkpoints_by_date(self): collection = self._create_test_collection() date1 = datetime.strptime("2016-06-12", "%Y-%m-%d") timeutils.utcnow = mock.MagicMock() timeutils.utcnow.return_value = date1 plan = fake_protection_plan() provider_id = plan['provider_id'] project_id = plan['project_id'] checkpoints_date_1 = {collection.create(plan).id for i in range(10)} date2 = datetime.strptime("2016-06-13", "%Y-%m-%d") timeutils.utcnow = mock.MagicMock() timeutils.utcnow.return_value = date2 checkpoints_date_2 = {collection.create(plan).id for i in range(10)} self.assertEqual( set( collection.list_ids(project_id=project_id, provider_id=provider_id, start_date=date1, end_date=date1)), checkpoints_date_1) self.assertEqual( set( collection.list_ids(project_id=project_id, provider_id=provider_id, start_date=date2, end_date=date2)), checkpoints_date_2)
def test_resource_graph(self): bank = bank_plugin.Bank(_InMemoryBankPlugin()) bank_lease = _InMemoryLeasePlugin() checkpoints_section = bank_plugin.BankSection(bank, "/checkpoints") indices_section = bank_plugin.BankSection(bank, "/indices") owner_id = bank.get_owner_id() plan = fake_protection_plan() cp = checkpoint.Checkpoint.create_in_section( checkpoints_section=checkpoints_section, indices_section=indices_section, bank_lease=bank_lease, owner_id=owner_id, plan=plan) resource_graph = graph.build_graph([A, B, C, D], resource_map.__getitem__) cp.resource_graph = resource_graph cp.commit() checkpoint_data = cp._md_cache self.assertEqual( checkpoint_data, bank._plugin.get_object( "/checkpoints/%s/%s" % (checkpoint_data["id"], checkpoint._INDEX_FILE_NAME) ) ) self.assertEqual(len(resource_graph), len(cp.resource_graph)) for start_node in resource_graph: self.assertIn(start_node, cp.resource_graph)
def test_create_checkpoint(self): collection = self._create_test_collection() checkpoint = collection.create(fake_protection_plan()) checkpoint.status = "finished" checkpoint.commit() self.assertEqual( checkpoint.status, collection.get(checkpoint_id=checkpoint.id).status, )
def test_list_checkpoints_by_date(self): collection = self._create_test_collection() date1 = datetime.strptime("2016-06-12", "%Y-%m-%d") timeutils.utcnow = mock.MagicMock() timeutils.utcnow.return_value = date1 checkpoints_date_1 = { collection.create(fake_protection_plan()).id for i in range(10)} date2 = datetime.strptime("2016-06-13", "%Y-%m-%d") timeutils.utcnow = mock.MagicMock() timeutils.utcnow.return_value = date2 checkpoints_date_2 = { collection.create(fake_protection_plan()).id for i in range(10)} self.assertEqual(set(collection.list_ids(start_date=date1, end_date=date1)), checkpoints_date_1) self.assertEqual(set(collection.list_ids(start_date=date2, end_date=date2)), checkpoints_date_2)
def test_list_checkpoints_with_all_tenants(self): collection = self._create_test_collection() plan_1 = fake_protection_plan() plan_1["id"] = "fake_plan_id_1" plan_1["project_id"] = "fake_project_id_1" provider_id_1 = plan_1['provider_id'] checkpoints_plan_1 = {collection.create(plan_1).id for i in range(10)} plan_2 = fake_protection_plan() plan_2["id"] = "fake_plan_id_2" plan_2["project_id"] = "fake_project_id_2" checkpoints_plan_2 = {collection.create(plan_2).id for i in range(10)} checkpoints_plan_1.update(checkpoints_plan_2) self.assertEqual( set( collection.list_ids(project_id="fake_project_id_1", provider_id=provider_id_1, all_tenants=True)), checkpoints_plan_1)
def test_write_checkpoint_with_invalid_lease(self): collection = self._create_test_collection() checkpoint = collection.create(fake_protection_plan()) collection._bank_lease.check_lease_validity = mock.MagicMock() collection._bank_lease.check_lease_validity.return_value = False checkpoint.status = "finished" self.assertNotEqual( checkpoint.status, collection.get(checkpoint_id=checkpoint.id).status, )
def test_list_checkpoints(self): collection = self._create_test_collection() plan = fake_protection_plan() provider_id = plan['provider_id'] project_id = plan['project_id'] result = {collection.create(plan).id for i in range(10)} self.assertEqual( set( collection.list_ids(project_id=project_id, provider_id=provider_id)), result)
def test_delete_checkpoint(self): collection = self._create_test_collection() plan = fake_protection_plan() provider_id = plan['provider_id'] project_id = plan['project_id'] result = {collection.create(plan).id for i in range(10)} checkpoint = collection.get(result.pop()) checkpoint.purge() self.assertEqual( set( collection.list_ids(project_id=project_id, provider_id=provider_id)), result)
def test_build_protect_task_flow(self, mock_build_graph): pr = ProviderRegistry() self.assertEqual(len(pr.providers), 1) plugable_provider = pr.providers["fake_id1"] cntxt = "fake_cntxt" plan = fake_protection_plan() workflow_engine = FakeWorkflowEngine() operation = constants.OPERATION_PROTECT ctx = { "context": cntxt, "plan": plan, "workflow_engine": workflow_engine, "operation_type": operation, } expected_calls = [ ("on_resource_start", 'A', True), ("on_resource_start", 'C', True), ("on_resource_start", 'D', True), ("on_resource_end", 'D'), ("on_resource_start", 'E', True), ("on_resource_end", 'E'), ("on_resource_end", 'C'), ("on_resource_end", 'A'), ("on_resource_start", 'B', True), ("on_resource_start", 'C', False), ("on_resource_start", 'D', False), ("on_resource_end", 'D'), ("on_resource_start", 'E', False), ("on_resource_end", 'E'), ("on_resource_end", 'C'), ("on_resource_end", 'B'), ] fake_registry = FakeProtectableRegistry() plugable_provider.protectable_registry = fake_registry fake_registry.build_graph = mock.MagicMock() resource_graph = build_graph(plan_resources, resource_map.__getitem__) mock_build_graph.return_value = resource_graph fake_protection_plugin = FakeProtectionPlugin(expected_calls) plugable_provider._plugin_map = {"fake_plugin": fake_protection_plugin} result = plugable_provider.build_task_flow(ctx) self.assertEqual(len(result["status_getters"]), 5) self.assertEqual(len(result["task_flow"]), 5)
def test_list_checkpoints_by_plan_with_marker(self): collection = self._create_test_collection() plan = fake_protection_plan() plan["id"] = "fake_plan_id" plan['provider_id'] = "fake_provider_id" plan["project_id"] = "fake_project_id" provider_id = plan['provider_id'] checkpoints_plan = { collection.create(plan, { 'checkpoint_id': i }).id for i in range(10) } checkpoints_sorted = sorted(checkpoints_plan) self.assertEqual( len( collection.list_ids(project_id="fake_project_id", provider_id=provider_id, plan_id="fake_plan_id", marker=checkpoints_sorted[0])) < 10, True)
def test_create_in_section(self): bank = bank_plugin.Bank(_InMemoryBankPlugin()) bank_lease = _InMemoryLeasePlugin() checkpoints_section = bank_plugin.BankSection(bank, "/checkpoints") indices_section = bank_plugin.BankSection(bank, "/indices") owner_id = bank.get_owner_id() plan = fake_protection_plan() cp = checkpoint.Checkpoint.create_in_section( checkpoints_section=checkpoints_section, indices_section=indices_section, bank_lease=bank_lease, owner_id=owner_id, plan=plan) checkpoint_data = cp._md_cache self.assertEqual( checkpoint_data, bank._plugin.get_object( "/checkpoints/%s/%s" % (checkpoint_data['id'], checkpoint._INDEX_FILE_NAME))) self.assertEqual(owner_id, cp.owner_id) self.assertEqual("protecting", cp.status)
def test_list_checkpoints_by_date_with_marker(self): collection = self._create_test_collection() date = datetime.strptime("2018-11-12", "%Y-%m-%d") timeutils.utcnow = mock.MagicMock() timeutils.utcnow.return_value = date plan = fake_protection_plan() plan["id"] = "fake_plan_id" plan['provider_id'] = "fake_provider_id" plan["project_id"] = "fake_project_id" provider_id = plan['provider_id'] checkpoints_plan = { collection.create(plan, { 'checkpoint_id': i }).id for i in range(10) } checkpoints_sorted = sorted(checkpoints_plan) self.assertEqual( len( collection.list_ids(project_id="fake_project_id", provider_id=provider_id, start_date=date, marker=checkpoints_sorted[0])) < 10, True)
def test_create_in_section(self): bank = bank_plugin.Bank(_InMemoryBankPlugin()) bank_lease = _InMemoryLeasePlugin() checkpoints_section = bank_plugin.BankSection(bank, "/checkpoints") indices_section = bank_plugin.BankSection(bank, "/indices") owner_id = bank.get_owner_id() plan = fake_protection_plan() cp = checkpoint.Checkpoint.create_in_section( checkpoints_section=checkpoints_section, indices_section=indices_section, bank_lease=bank_lease, owner_id=owner_id, plan=plan) checkpoint_data = cp._md_cache self.assertEqual( checkpoint_data, bank._plugin.get_object( "/checkpoints/%s/%s" % (checkpoint_data['id'], checkpoint._INDEX_FILE_NAME) ) ) self.assertEqual(owner_id, cp.owner_id) self.assertEqual("protecting", cp.status)
def test_list_checkpoints_by_date_with_all_tenants(self): collection = self._create_test_collection() date1 = datetime.strptime("2018-11-15", "%Y-%m-%d") timeutils.utcnow = mock.MagicMock() timeutils.utcnow.return_value = date1 plan_1 = fake_protection_plan() plan_1["id"] = "fake_plan_id_1" plan_1["project_id"] = "fake_project_id_1" provider_id_1 = plan_1['provider_id'] checkpoints_1 = {collection.create(plan_1).id for i in range(10)} date2 = datetime.strptime("2018-11-17", "%Y-%m-%d") timeutils.utcnow = mock.MagicMock() timeutils.utcnow.return_value = date2 plan_1["id"] = "fake_plan_id_2" plan_1["project_id"] = "fake_project_id_2" checkpoints_2 = {collection.create(plan_1).id for i in range(10)} checkpoints_2.update(checkpoints_1) self.assertEqual( set( collection.list_ids(project_id="fake_project_id_1", provider_id=provider_id_1, start_date=date1, all_tenants=True)), checkpoints_2)
def test_protect(self, mock_provider): mock_provider.return_value = fakes.FakeProvider() self.pro_manager.protect(None, fakes.fake_protection_plan())
def test_protect(self, mock_provider, mock_operation_log_create, mock_operation_log_update): mock_provider.return_value = fakes.FakeProvider() self.pro_manager.protect(None, fakes.fake_protection_plan())
def test_protect(self, mock_provider): mock_provider.return_value = fakes.FakeProvider() self.pro_manager.protect(None, fakes.fake_protection_plan())
def test_protect_in_error(self, mock_flow): mock_flow.side_effect = Exception() self.assertRaises(oslo_messaging.ExpectedException, self.pro_manager.protect, None, fakes.fake_protection_plan())
def test_protect_in_error(self, mock_flow): mock_flow.side_effect = Exception() self.assertRaises(oslo_messaging.ExpectedException, self.pro_manager.protect, None, fakes.fake_protection_plan())
def test_list_checkpoints(self): collection = self._create_test_collection() result = { collection.create(fake_protection_plan()).id for i in range(10)} self.assertEqual(set(collection.list_ids()), result)