def test_load_config_external_app(self, mock_merge_config):
        """Test load config for external app"""
        mock_nulecule = mock.Mock(
            name='nulecule',
            spec=Nulecule('some-id', '0.0.2', {}, [], 'some/path')
        )
        params = [
            {'name': 'key1'},
            {'name': 'key2'}
        ]
        initial_config = {
            'general': {'a': 'b', 'key2': 'val2'},
            'some-app': {'key1': 'val1'}
        }

        nc = NuleculeComponent('some-app', 'some/path', params=params)
        nc._app = mock_nulecule
        nc.load_config(config=copy.deepcopy(initial_config))

        mock_nulecule.load_config.assert_called_once_with(
            config={
                'general': {'a': 'b', 'key2': 'val2'},
                'some-app': {'key1': 'val1', 'key2': 'val2'}
            }, ask=False, skip_asking=False)
        mock_merge_config.assert_called_once_with(
            nc.config, mock_nulecule.config)
    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_load_without_nodeps(self, mock_load_external_application):
        dryrun = False

        nc = NuleculeComponent('some-name', 'some/path', source='blah')
        nc.load(False, dryrun)

        mock_load_external_application.assert_called_once_with(dryrun)
    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_load_with_nodeps(self, mock_load_external_application):
        dryrun = False

        nc = NuleculeComponent('some-name', 'some/path', source='blah')
        nc.load(True, dryrun)

        self.assertEqual(mock_load_external_application.call_count, 0)
    def test_load_config_local_app(self):
        """Test load config for local app"""
        params = [
            {'name': 'key1', 'description': 'key1'},
            {'name': 'key2', 'description': 'key2'}
        ]
        initial_config = {
            'general': {'a': 'b', 'key2': 'val2'},
            'some-app': {'key1': 'val1'}
        }
        conf = Config(answers=initial_config)

        nc = NuleculeComponent('some-app', 'some/path',
                               params=params, config=conf)
        nc.load_config()
        runtime_answers = nc.config.runtime_answers()
        self.assertEqual(runtime_answers, {
            'general': {
                'a': 'b',
                'key2': 'val2',
                'provider': 'kubernetes',
                'namespace': 'default'
            },
            'some-app': {'key1': 'val1'}
        })
    def test_file_path(self, mock_os_path_isfile):
        mock_os_path_isfile.return_value = True

        nc = NuleculeComponent('some-name', 'some/path')

        self.assertEqual(
            nc._get_artifact_paths_for_path('some/path/to/file'),
            ['some/path/to/file'])
    def test_run_external_app(self):
        mock_nulecule = mock.Mock(name='nulecule')
        dryrun = False

        nc = NuleculeComponent('some-name', 'some/path')
        nc._app = mock_nulecule
        nc.run('some-provider', dryrun)

        mock_nulecule.run.assert_called_once_with('some-provider', dryrun)
    def test_render_for_local_app_with_missing_artifacts_from_nulecule(self):
        """
        Test rendering a Nulecule component with no artifacts provided in the
        Nulecule file.
        """
        nc = NuleculeComponent(name='some-app', basepath='some/path')
        nc.config = {}

        with self.assertRaises(NuleculeException):
            nc.render()
    def test_stop_external_app(self):
        """Test stopping an external application"""
        mock_nulecule = mock.Mock(name='nulecule')
        dryrun = False

        nc = NuleculeComponent('some-name', 'some/path')
        nc._app = mock_nulecule
        nc.stop('some-provider', dryrun)

        mock_nulecule.stop.assert_called_once_with('some-provider', dryrun)
    def test_render_for_external_app(self):
        """Test rendering a nulecule component pointing to an external app"""
        mock_nulecule = mock.Mock(name='nulecule')
        provider_key = 'some-provider'
        dryrun = False

        nc = NuleculeComponent(name='some-app', basepath='some/path')
        nc._app = mock_nulecule
        nc.render(provider_key, dryrun)

        mock_nulecule.render.assert_called_once_with(
            provider_key=provider_key, dryrun=dryrun)
    def test_loading_existing_app(self, mock_os_path_isdir, mock_Nulecule):
        dryrun, update = False, False
        mock_os_path_isdir.return_value = True
        expected_external_app_path = 'some/path/external/some-app'

        nc = NuleculeComponent('some-app', 'some/path')
        nc.load_external_application(dryrun=dryrun, update=update)

        mock_os_path_isdir.assert_called_once_with(
            expected_external_app_path)
        mock_Nulecule.load_from_path.assert_called_once_with(
            expected_external_app_path, dryrun=dryrun, update=update)
    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)
Example #15
0
    def test_render_for_external_app(self):
        """Test rendering a nulecule component pointing to an external app"""
        mock_nulecule = mock.Mock(name='nulecule')
        provider_key = 'some-provider'
        dryrun = False

        nc = NuleculeComponent(name='some-app',
                               basepath='some/path',
                               artifacts="/foo/bar")
        nc._app = mock_nulecule
        nc.render(provider_key, dryrun)

        mock_nulecule.render.assert_called_once_with(provider_key=provider_key,
                                                     dryrun=dryrun)
Example #16
0
    def test_loading_existing_app(self, mock_os_path_isdir, mock_Nulecule):
        dryrun, update = False, False
        mock_os_path_isdir.return_value = True
        expected_external_app_path = 'some/path/external/some-app'

        nc = NuleculeComponent('some-app', 'some/path')
        nc.load_external_application(dryrun=dryrun, update=update)

        mock_os_path_isdir.assert_called_once_with(expected_external_app_path)
        mock_Nulecule.load_from_path.assert_called_once_with(
            expected_external_app_path,
            dryrun=dryrun,
            namespace='some-app',
            update=update)
    def test_loading_app_by_unpacking(self, mock_os_path_isdir,
                                      mock_Nulecule):
        dryrun, update = False, False
        mock_os_path_isdir.return_value = False
        expected_external_app_path = 'some/path/external/some-app'

        nc = NuleculeComponent('some-app', 'some/path')
        nc.load_external_application(dryrun=dryrun, update=update)

        mock_os_path_isdir.assert_called_once_with(
            expected_external_app_path)
        mock_Nulecule.unpack.assert_called_once_with(
            nc.source, expected_external_app_path,
            namespace=nc.namespace, config=None, dryrun=dryrun, update=update)
Example #18
0
    def test_run_local_artifacts(self, mock_get_provider):
        mock_provider = mock.Mock(name='provider')
        mock_get_provider.return_value = ('some-provider-x', mock_provider)
        dryrun = False
        provider_key = 'some-provider'

        nc = NuleculeComponent('some-name', 'some/path')
        nc.rendered_artifacts = {'some-provider-x': ['a', 'b', 'c']}
        nc.run(provider_key, dryrun)

        mock_get_provider.assert_called_once_with(provider_key, dryrun)
        self.assertEqual(mock_provider.artifacts, ['a', 'b', 'c'])
        mock_provider.init.assert_called_once_with()
        mock_provider.run.assert_called_once_with()
    def test_run_local_artifacts(self, mock_get_provider):
        mock_provider = mock.Mock(name='provider')
        mock_get_provider.return_value = ('some-provider-x', mock_provider)
        dryrun = False
        provider_key = 'some-provider'

        nc = NuleculeComponent('some-name', 'some/path')
        nc.rendered_artifacts = {'some-provider-x': ['a', 'b', 'c']}
        nc.run(provider_key, dryrun)

        mock_get_provider.assert_called_once_with(provider_key, dryrun)
        self.assertEqual(mock_provider.artifacts, ['a', 'b', 'c'])
        mock_provider.init.assert_called_once_with()
        mock_provider.run.assert_called_once_with()
    def test_loading_app_by_unpacking(self, mock_os_path_isdir,
                                      mock_Nulecule):
        dryrun, update = False, False
        mock_os_path_isdir.return_value = False
        expected_external_app_path = 'some/path/external/some-app'

        nc = NuleculeComponent('some-app', 'some/path')
        nc.load_external_application(dryrun=dryrun, update=update)

        mock_os_path_isdir.assert_called_once_with(
            expected_external_app_path)
        mock_Nulecule.unpack.assert_called_once_with(
            nc.source, expected_external_app_path,
            namespace=nc.namespace, config=None, dryrun=dryrun, update=update)
    def test_stop_local_app(self, mock_get_provider):
        """Test stopping a local application"""
        dryrun = False
        provider_key = 'some-provider'
        mock_provider = mock.Mock(name='provider')
        mock_get_provider.return_value = ('some-provider-x', mock_provider)

        nc = NuleculeComponent('some-name', 'some/path')
        nc.rendered_artifacts = {'some-provider-x': ['a', 'b', 'c']}
        nc.stop(provider_key, dryrun)

        mock_get_provider.assert_called_once_with(provider_key, dryrun)
        self.assertEqual(mock_provider.artifacts, ['a', 'b', 'c'])
        mock_provider.init.assert_called_once_with()
        mock_provider.stop.assert_called_once_with()
Example #22
0
    def test_stop_local_app(self, mock_get_provider):
        """Test stopping a local application"""
        dryrun = False
        provider_key = 'some-provider'
        mock_provider = mock.Mock(name='provider')
        mock_get_provider.return_value = ('some-provider-x', mock_provider)

        nc = NuleculeComponent('some-name', 'some/path')
        nc.rendered_artifacts = {'some-provider-x': ['a', 'b', 'c']}
        nc.stop(provider_key, dryrun)

        mock_get_provider.assert_called_once_with(provider_key, dryrun)
        self.assertEqual(mock_provider.artifacts, ['a', 'b', 'c'])
        mock_provider.init.assert_called_once_with()
        mock_provider.stop.assert_called_once_with()
    def test_dir_path(self, mock_os_path_isfile, mock_os_path_isdir,
                      mock_os_listdir):
        mock_os_path_isfile.return_value = False
        mock_os_path_isdir.side_effect = lambda path: True if path.endswith('dir') else False
        mock_os_listdir.return_value = [
            '.foo',
            'some-dir',
            'file1',
            'file2'
        ]

        nc = NuleculeComponent('some-name', 'some/path')

        self.assertEqual(
            nc._get_artifact_paths_for_path('artifacts-dir'),
            ['artifacts-dir/file1', 'artifacts-dir/file2'])
    def test_dir_path(self, mock_os_path_isfile, mock_os_path_isdir,
                      mock_os_listdir):
        mock_os_path_isfile.return_value = False
        mock_os_path_isdir.side_effect = lambda path: True if path.endswith('dir') else False
        mock_os_listdir.return_value = [
            '.foo',
            'some-dir',
            'file1',
            'file2'
        ]

        nc = NuleculeComponent('some-name', 'some/path')

        self.assertEqual(
            nc._get_artifact_paths_for_path('artifacts-dir'),
            ['artifacts-dir/file1', 'artifacts-dir/file2'])
    def test_load_config_local_app(self):
        """Test load config for local app"""
        params = [
            {'name': 'key1'},
            {'name': 'key2'}
        ]
        initial_config = {
            'general': {'a': 'b', 'key2': 'val2'},
            'some-app': {'key1': 'val1'}
        }

        nc = NuleculeComponent('some-app', 'some/path', params=params)
        nc.load_config(config=copy.deepcopy(initial_config))

        self.assertEqual(nc.config, {
            'general': {'a': 'b', 'key2': 'val2'},
            'some-app': {'key1': 'val1', 'key2': 'val2'}
        })
    def test_load_config_local_app(self):
        """Test load config for local app"""
        params = [
            {'name': 'key1'},
            {'name': 'key2'}
        ]
        initial_config = {
            'general': {'a': 'b', 'key2': 'val2'},
            'some-app': {'key1': 'val1'}
        }

        nc = NuleculeComponent('some-app', 'some/path', params=params)
        nc.load_config(config=copy.deepcopy(initial_config))

        self.assertEqual(nc.config, {
            'general': {'a': 'b', 'key2': 'val2'},
            'some-app': {'key1': 'val1', 'key2': 'val2'}
        })
    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)
Example #28
0
    def test_load_config_external_app(self):
        """Test load config for external app"""
        params = [{
            'name': 'key1',
            'description': 'key1'
        }, {
            'name': 'key2',
            'description': 'key2'
        }]
        initial_config = {
            'general': {
                'a': 'b',
                'key2': 'val2'
            },
            'some-app': {
                'key1': 'val1'
            }
        }
        config = Config(answers=initial_config)
        mock_nulecule = mock.Mock(name='nulecule',
                                  spec=Nulecule('some-id', '0.0.2', config, [],
                                                'some/path'))

        nc = NuleculeComponent('some-app', 'some/path', params=params)
        nc._app = mock_nulecule
        nc.config = config
        nc.load_config()

        mock_nulecule.load_config.assert_called_once_with(config=config,
                                                          ask=False,
                                                          skip_asking=False)
Example #29
0
    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)
Example #30
0
    def test_load_config_local_app(self):
        """Test load config for local app"""
        params = [{
            'name': 'key1',
            'description': 'key1'
        }, {
            'name': 'key2',
            'description': 'key2'
        }]
        initial_config = {
            'general': {
                'a': 'b',
                'key2': 'val2'
            },
            'some-app': {
                'key1': 'val1'
            }
        }
        conf = Config(answers=initial_config)

        nc = NuleculeComponent('some-app',
                               'some/path',
                               params=params,
                               config=conf)
        nc.load_config()
        runtime_answers = nc.config.runtime_answers()
        self.assertEqual(
            runtime_answers, {
                'general': {
                    'a': 'b',
                    'key2': 'val2',
                    'provider': 'kubernetes',
                    'namespace': 'default'
                },
                'some-app': {
                    'key1': 'val1'
                }
            })
    def test_load_config_external_app(self):
        """Test load config for external app"""
        params = [
            {'name': 'key1', 'description': 'key1'},
            {'name': 'key2', 'description': 'key2'}
        ]
        initial_config = {
            'general': {'a': 'b', 'key2': 'val2'},
            'some-app': {'key1': 'val1'}
        }
        config = Config(answers=initial_config)
        mock_nulecule = mock.Mock(
            name='nulecule',
            spec=Nulecule('some-id', '0.0.2', config, [], 'some/path')
        )

        nc = NuleculeComponent('some-app', 'some/path', params=params)
        nc._app = mock_nulecule
        nc.config = config
        nc.load_config()

        mock_nulecule.load_config.assert_called_once_with(
            config=config, ask=False, skip_asking=False)
Example #32
0
class TestNuleculeXpathing(unittest.TestCase):

    # Create a temporary directory for our setup as well as load the required NuleculeComponent
    def setUp(self):
        self.tmpdir = tempfile.mkdtemp(prefix = "atomicapp-test", dir = "/tmp")
        self.artifact_path = os.path.dirname(__file__) + '/artifact_xpath_test/xpath.json'
        self.artifact_content = open(self.artifact_path, 'r').read();
        self.test = NuleculeComponent(name = None, basepath = self.tmpdir, params = None)

    def tearDown(self):
        pass

    # Let's check to see that xpathing is actually working. Fake the params get call
    @mock.patch("atomicapp.nulecule.base.NuleculeComponent.grab_artifact_params", mock_params_get_call)
    def test_xpathing_parse(self):
        self.test.apply_pointers(content=self.artifact_content, params={"image": ["/spec/containers/0/image"]})

    # Fail if we're unable to replace the /spec/containers/1/image pointer
    @mock.patch("atomicapp.nulecule.base.NuleculeComponent.grab_artifact_params", mock_params_get_call)
    def test_xpathing_not_found(self):
        with pytest.raises(NuleculeException):
            self.test.apply_pointers(content=self.artifact_content, params={"image": ["/spec/containers/1/image"]})

    # Test using the artifact path
    def test_artifact_path(self):
        self.test.artifacts = {"docker": [{"file://artifacts/docker/hello-apache-pod_run"}], "kubernetes": [{"file://artifacts/kubernetes/hello-apache-pod.json"}]}
        self.test.get_artifact_paths_for_provider("kubernetes")

    # Test the artifact with the "resource: " pointer
    def test_artifact_path_with_resource(self):
        self.test.artifacts = {"docker": [{"resource":"file://artifacts/docker/hello-apache-pod_run"}], "kubernetes": [{"resource":"file://artifacts/kubernetes/hello-apache-pod.json"}]}
        self.test.get_artifact_paths_for_provider("kubernetes")

    # Test combination of using "resource" and not
    def test_artifact_path_with_resource_and_old(self):
        self.test.artifacts = {"docker": [{"resource":"file://artifacts/docker/hello-apache-pod_run"}], "kubernetes": [{"file://artifacts/kubernetes/hello-apache-pod.json"}]}
        self.test.get_artifact_paths_for_provider("kubernetes")
Example #33
0
    def test_components_for_external_app(self):
        nc = NuleculeComponent('some-app', 'some/path')
        nc._app = mock.Mock(name='nulecule')
        nc._app.components = ['a', 'b', 'c']

        self.assertEqual(nc.components, ['a', 'b', 'c'])
Example #34
0
 def setUp(self):
     self.example_dir = os.path.dirname(__file__) + '/artifact_xpath_test/'
     self.artifact_path = os.path.dirname(__file__) + '/artifact_xpath_test/xpath.json'
     self.artifact_content = open(self.artifact_path, 'r').read();
     self.test = NuleculeComponent(name = None, basepath = self.example_dir, params = None)
Example #35
0
 def setUp(self):
     self.tmpdir = tempfile.mkdtemp(prefix = "atomicapp-test", dir = "/tmp")
     self.artifact_path = os.path.dirname(__file__) + '/artifact_xpath_test/xpath.json'
     self.artifact_content = open(self.artifact_path, 'r').read();
     self.test = NuleculeComponent(name = None, basepath = self.tmpdir, params = None)
Example #36
0
    def test_components_for_local_app(self):
        nc = NuleculeComponent('some-app', 'some/path')

        self.assertFalse(nc.components)
    def test_components_for_external_app(self):
        nc = NuleculeComponent('some-app', 'some/path')
        nc._app = mock.Mock(name='nulecule')
        nc._app.components = ['a', 'b', 'c']

        self.assertEqual(nc.components, ['a', 'b', 'c'])