def test_render_artifact(self, mock_open): source_content = 'some text: $key1' expected_rendered_content = 'some text: val1' context = {'key1': 'val1'} # Mock context for opening file. mock_open_source_file_context = mock.MagicMock( name='source_artifact_context') mock_open_target_file_context = mock.MagicMock( name='target_artifact_context') # Mock file objects mock_source_file = mock_open_source_file_context.__enter__() mock_target_file = mock_open_target_file_context.__enter__() mock_source_file.read.return_value = source_content def mock_open_resp(path, mode): if path == 'some/path/artifact': return mock_open_source_file_context elif path == 'some/path/.artifact': return mock_open_target_file_context mock_open.side_effect = mock_open_resp nc = NuleculeComponent(name='some-name', basepath='some/path') nc.artifacts = {'some-provider': [{}]} self.assertEqual( nc.render_artifact('some/path/artifact', context, 'some-provider'), '.artifact') mock_source_file.read.assert_called_once_with() mock_target_file.write.assert_called_once_with( expected_rendered_content)
def test_render_for_local_app_with_artifacts_for_provider( self, mock_render_artifact, mock_get_artifact_paths_for_provider, mock_get_context): """Test rendering artifacts for a local Nulecule component""" provider_key = 'some-provider' dryrun = False expected_rendered_artifacts = [ 'some/path/.artifact1', 'some/path/.artifact2'] context = {'a': 'b'} mock_get_artifact_paths_for_provider.return_value = [ 'some/path/artifact1', 'some/path/artifact2'] mock_render_artifact.side_effect = lambda path, context, provider: path.replace('artifact', '.artifact') mock_get_context.return_value = context nc = NuleculeComponent(name='some-app', basepath='some/path') nc.config = {'general': {'key1': 'val1'}, 'some-provider': {'a': 'b'}} nc.artifacts = { 'some-provider': ['artifact1', 'artifact2'], 'x': ['foo'] } nc.render(provider_key, dryrun) mock_get_artifact_paths_for_provider.assert_called_once_with( provider_key) mock_render_artifact.assert_any_call('some/path/artifact1', context, 'some-provider') mock_render_artifact.assert_any_call('some/path/artifact2', context, 'some-provider') mock_get_artifact_paths_for_provider.assert_called_once_with( provider_key) self.assertEqual(nc.rendered_artifacts[provider_key], expected_rendered_artifacts)
def test_artifact_paths_for_provider( self, mock_get_artifact_paths_for_path): provider_key = 'some-provider' expected_artifact_paths = [ 'some/path/relative/path/to/artifact1', '/abs/path/to/artifact2', 'some/path/x/artifact3' ] mock_get_artifact_paths_for_path.side_effect = lambda path: [path] nc = NuleculeComponent(name='some-app', basepath='some/path') nc.artifacts = { provider_key: [ 'file://relative/path/to/artifact1', 'file:///abs/path/to/artifact2', { 'inherit': ['x-provider'] } ], 'x-provider': [ 'file://x/artifact3' ] } self.assertEqual(nc.get_artifact_paths_for_provider(provider_key), expected_artifact_paths)
def test_render_for_local_app_with_artifacts_for_provider( self, mock_render_artifact, mock_get_artifact_paths_for_provider, mock_get_context): """Test rendering artifacts for a local Nulecule component""" provider_key = 'some-provider' dryrun = False expected_rendered_artifacts = [ 'some/path/.artifact1', 'some/path/.artifact2' ] context = {'a': 'b'} mock_get_artifact_paths_for_provider.return_value = [ 'some/path/artifact1', 'some/path/artifact2' ] mock_render_artifact.side_effect = lambda path, context, provider: path.replace( 'artifact', '.artifact') mock_get_context.return_value = context nc = NuleculeComponent(name='some-app', basepath='some/path') nc.config = {'general': {'key1': 'val1'}, 'some-provider': {'a': 'b'}} nc.artifacts = { 'some-provider': ['artifact1', 'artifact2'], 'x': ['foo'] } nc.render(provider_key, dryrun) mock_get_artifact_paths_for_provider.assert_called_once_with( provider_key) mock_render_artifact.assert_any_call('some/path/artifact1', context, 'some-provider') mock_render_artifact.assert_any_call('some/path/artifact2', context, 'some-provider') mock_get_artifact_paths_for_provider.assert_called_once_with( provider_key) self.assertEqual(nc.rendered_artifacts[provider_key], expected_rendered_artifacts)
def test_render_for_local_app_with_missing_artifacts_for_provider(self): """ Test rendering a Nulecule component with missing artifacts for a provider. """ provider_key = 'some-provider' dryrun = False nc = NuleculeComponent(name='some-app', basepath='some/path') nc.config = {} nc.artifacts = {'x': ['some-artifact']} self.assertRaises(NuleculeException, nc.render, provider_key, dryrun)
def test_render_for_local_app_with_missing_artifacts_for_provider(self): """ Test rendering a Nulecule component with missing artifacts for a provider. """ provider_key = 'some-provider' dryrun = False nc = NuleculeComponent(name='some-app', basepath='some/path') nc.config = Config() nc.artifacts = {'x': ['some-artifact']} self.assertRaises(NuleculeException, nc.render, provider_key, dryrun)