def _gen_ctx(self): _ctx = MockCloudifyContext('node_name', properties={ "connection_config": { "username": "******", "password": "******", "host": "vcenter_ip", "port": 443, "datacenter_name": "vcenter_datacenter", "resource_pool_name": "vcenter_resource_pool", "auto_placement": "vsphere_auto_placement", "allow_insecure": True } }, runtime_properties={}) _ctx._execution_id = "execution_id" _ctx.instance.host_ip = None _ctx._operation = mock.Mock() current_ctx.set(_ctx) return _ctx
def test_throw_cloudify_exceptions(self): # create without error fake_ctx = MockCloudifyContext( 'node_name', properties={}, ) fake_ctx._operation = Mock() fake_ctx.operation._operation_retry = None current_ctx.set(fake_ctx) fake_ctx.instance._runtime_properties = DirtyTrackingDict({"c": "d"}) @utils.throw_cloudify_exceptions def test(*argc, **kwargs): return argc, kwargs fake_ctx._operation.name = "cloudify.interfaces.lifecycle.create" self.assertEqual(test(ctx=fake_ctx), ((), {'ctx': fake_ctx})) self.assertEqual(fake_ctx.instance._runtime_properties, {"c": "d"}) # delete without error fake_ctx.instance._runtime_properties = DirtyTrackingDict({"a": "b"}) fake_ctx._operation.name = "cloudify.interfaces.lifecycle.delete" self.assertEqual(test(ctx=fake_ctx), ((), {'ctx': fake_ctx})) self.assertFalse(fake_ctx.instance._runtime_properties) # delete postponed by gcp fake_ctx.instance._runtime_properties = DirtyTrackingDict( {"_operation": "b"}) fake_ctx._operation.name = "cloudify.interfaces.lifecycle.delete" self.assertEqual(test(ctx=fake_ctx), ((), {'ctx': fake_ctx})) self.assertEqual(fake_ctx.instance._runtime_properties, {"_operation": "b"}) # postponed by cloudify @utils.throw_cloudify_exceptions def test(ctx, *argc, **kwargs): ctx.operation._operation_retry = 'Should be retried' return argc, kwargs fake_ctx.instance._runtime_properties = DirtyTrackingDict({"a": "b"}) fake_ctx._operation.name = "cloudify.interfaces.lifecycle.delete" self.assertEqual(test(ctx=fake_ctx), ((), {})) self.assertEqual(fake_ctx.instance._runtime_properties, {"a": "b"}) # postponed by exeption @utils.throw_cloudify_exceptions def test(ctx, *argc, **kwargs): raise Exception('Should be retried') fake_ctx.instance._runtime_properties = DirtyTrackingDict({"a": "b"}) fake_ctx._operation.name = "cloudify.interfaces.lifecycle.delete" with self.assertRaises(NonRecoverableError): test(ctx=fake_ctx) self.assertEqual(fake_ctx.instance._runtime_properties, {"a": "b"})
def _gen_ctx(self): _ctx = MockCloudifyContext( 'node_name', properties={}, ) _ctx._operation = Mock() _ctx._execution_id = "execution_id" _ctx.instance.host_ip = None _ctx.instance._runtime_properties = DirtyTrackingDict({}) current_ctx.set(_ctx) return _ctx
def mock_ctx(self, test_name, use_secret_store=False, use_secrets_if_exist=False): key_path = tempfile.mkdtemp() test_node_id = test_name if use_secret_store or use_secrets_if_exist: test_properties = { 'use_secret_store': use_secret_store, 'use_secrets_if_exist': use_secrets_if_exist, 'key_name': test_name, 'resource_config': { 'public_key_path': '{0}/{1}.pem.pub'.format( key_path, test_name), 'openssh_format': True, 'algorithm': 'RSA', 'bits': 2048 } } else: test_properties = { 'use_secret_store': use_secret_store, 'resource_config': { 'private_key_path': '{0}/{1}'.format( key_path, test_name), 'public_key_path': '{0}/{1}.pub'.format( key_path, test_name), 'openssh_format': True, 'algorithm': 'RSA', 'bits': 2048 }, } ctx = MockCloudifyContext( node_id=test_node_id, properties=test_properties ) ctx.instance._runtime_properties = DirtyTrackingDict({}) ctx._operation = mock.Mock() return ctx