def test_create_with_download(self): subnettarget = MockRelationshipContext( target=MockCloudifyContext('subnet')) subnettarget.target.node.type_hierarchy =\ ['cloudify.nodes.aws.ec2.Subnet'] ctx = MockCloudifyContext("test_create") ctx.download_resource = MagicMock(return_value='abc') with patch(PATCH_PREFIX + 'LambdaBase'),\ patch(PATCH_PREFIX + 'utils') as utils,\ patch(PATCH_PREFIX + 'path_exists', MagicMock(return_value=False)),\ patch(PATCH_PREFIX + 'os_remove', MagicMock(return_value=True)),\ patch(PATCH_PREFIX + 'open', MagicMock(return_value=StringIO(u"test"))): fun = function.LambdaFunction(ctx) fun.logger = MagicMock() fun.resource_id = 'test_function' fake_client = self.make_client_function( 'create_function', return_value={'FunctionArn': 'test_function_arn', 'FunctionName': 'test_function'}) fun.client = fake_client resource_config = {'VpcConfig': {'SubnetIds': []}, 'Code': {'ZipFile': True}} utils.find_rels_by_node_type = MagicMock( return_value=[subnettarget]) utils.get_resource_id = MagicMock(return_value='test_id') utils.find_rel_by_node_type = MagicMock(return_value=subnettarget) utils.get_resource_id = MagicMock(return_value='Role') function.create(ctx, fun, resource_config) self.assertEqual(True, resource_config['Code']['ZipFile']) self.assertEqual({'SubnetIds': []}, resource_config['VpcConfig'])
def _run(self, ctx_kwargs, expected_script_path, actual_script_path, return_result=False, pass_parameter=False): def mock_download_resource(script_path): self.assertEqual(script_path, expected_script_path) return actual_script_path if 'properties' not in ctx_kwargs: ctx_kwargs['properties'] = {} if 'process' not in ctx_kwargs['properties']: ctx_kwargs['properties']['process'] = {} process_config = ctx_kwargs['properties']['process'] process_config.update({ 'ctx_proxy_type': self.ctx_proxy_type }) ctx = MockCloudifyContext(**ctx_kwargs) ctx.download_resource = mock_download_resource if pass_parameter: result = tasks.run(expected_script_path, ctx=ctx) else: result = tasks.run(ctx=ctx) if return_result: return ctx, result else: return ctx
def _instance_ctx(self): _ctx = MockCloudifyContext('node_name', properties={ 'd': 'c', 'b': 'a' }, runtime_properties={ 'a': 'b', 'c': 'd' }) _ctx.download_resource = Mock( side_effect=download_resource_side_effect) current_ctx.set(_ctx) return _ctx
def test_start_service_compose(self, mock_put, mock_sudo): ctx = MockCloudifyContext(node_id='test', properties={ 'compose_file': 'dummy.compose', }, runtime_properties={ 'ip': '1.1.1.1.', 'port': '9999', 'mgr_ssh_user': '******', 'mgr_ssh_keyfile': 'keyfile', }) ctx.download_resource = lambda self: "nopath" current_ctx.set(ctx=ctx) tasks.start_service() self.assertEquals(1, mock_sudo.call_count) self.assertEquals(1, mock_put.call_count)
def test_create_with_download(self): subnettarget = MockRelationshipContext( target=MockCloudifyContext('subnet')) subnettarget.target.node.type_hierarchy =\ ['cloudify.nodes.aws.ec2.Subnet'] ctx = MockCloudifyContext("test_create") ctx.download_resource = MagicMock(return_value='abc') with patch(PATCH_PREFIX + 'LambdaBase'),\ patch(PATCH_PREFIX + 'utils') as utils,\ patch(PATCH_PREFIX + 'path_exists', MagicMock(return_value=False)),\ patch(PATCH_PREFIX + 'os_remove', MagicMock(return_value=True)),\ patch(PATCH_PREFIX + 'open', MagicMock(return_value=StringIO(u"test"))): fun = function.LambdaFunction(ctx) fun.logger = MagicMock() fun.resource_id = 'test_function' fake_client = self.make_client_function('create_function', return_value={ 'FunctionArn': 'test_function_arn', 'FunctionName': 'test_function' }) fun.client = fake_client resource_config = { 'VpcConfig': { 'SubnetIds': [] }, 'Code': { 'ZipFile': True } } utils.find_rels_by_node_type = MagicMock( return_value=[subnettarget]) utils.get_resource_id = MagicMock(return_value='test_id') utils.find_rel_by_node_type = MagicMock(return_value=subnettarget) utils.get_resource_id = MagicMock(return_value='Role') function.create(ctx, fun, resource_config) self.assertEqual('test', resource_config['Code']['ZipFile']) self.assertEqual( { 'SubnetIds': ['Role'], 'SecurityGroupIds': ['Role'] }, resource_config['VpcConfig'])
def test_create_with_download(self, mock_subnet_attach, mock_security_groups_attach, mock_iam_role_attach): ctx = MockCloudifyContext("test_create") zip_file = self._mock_function_file() ctx.download_resource = MagicMock(return_value=zip_file) with patch(PATCH_PREFIX + 'LambdaBase'): fun = function.LambdaFunction(ctx) fun.logger = MagicMock() fun.resource_id = 'test_function' fake_client = self.make_client_function('create_function', return_value={ 'FunctionArn': 'test_function_arn', 'FunctionName': 'test_function' }) fun.client = fake_client resource_config = { 'VpcConfig': { 'SubnetIds': [] }, 'Code': { 'ZipFile': zip_file } } mock_iam_role_attach.return_value = ['role-1'] mock_security_groups_attach.return_value = ['sg-1'] mock_subnet_attach.return_value = ['subnet-1'] function.create(ctx, fun, resource_config) self.assertEqual(zip_file, resource_config['Code']['ZipFile']) self.assertEqual( { 'SubnetIds': ['subnet-1'], 'SecurityGroupIds': ['sg-1'] }, resource_config['VpcConfig'])
def mock_ctx(self, **kwargs): ctx = MockCloudifyContext(**kwargs) ctx.download_resource = lambda s_path: s_path current_ctx.set(ctx) return ctx
def mock_ctx(self, **kwargs): ctx = MockCloudifyContext(**kwargs) ctx.download_resource = lambda s_path: s_path return ctx