예제 #1
0
    def test_integration_latest_version_is_default_when_version_was_not_specified(self, verification):
        # Arrange
        templates = {'tosca/resource/test': [ShellTemplate('test-resource', '', 'url', '8.1', 'resource')]}
        repo_info = ('quali', 'resource-test')

        zipfile = mock_template_zip_file()
        httpretty.register_uri(httpretty.GET, "https://api.github.com/repos/quali/resource-test/zipball/2.0.1",
                               body=zipfile.read(), content_type='application/zip',
                               content_disposition="attachment; filename=quali-resource-test-dd2ba19.zip", stream=True)
        template_compiler = Mock()

        standards = Mock()
        standards.fetch.return_value = {"resource": ['2.0.0', '2.0.1']}

        # Act
        with patch.object(TemplateRetriever, 'get_templates', return_value=templates), \
             patch('shellfoundry.utilities.template_url._parse_repo_url', return_value=repo_info), \
             patch.object(TempDirContext, '__enter__', return_value=self.fs.CreateDirectory('mock_temp').name):
            command_executor = NewCommandExecutor(template_retriever=TemplateRetriever(),
                                                  repository_downloader=RepositoryDownloader(),
                                                  template_compiler=template_compiler,
                                                  standards=standards,
                                                  standard_versions=StandardVersionsFactory())
            command_executor._get_template_params = Mock(return_value={})
            command_executor.new('new_shell', 'tosca/resource/test')

        # Assert
        template_compiler.compile_template.smarter_assert_called_once_with(
            CookiecutterTemplateCompiler.compile_template,
            shell_name='new_shell',
            template_path=os.path.join('mock_temp', 'root'),
            extra_context={},
            running_on_same_folder=False)
예제 #2
0
    def test_can_generate_shell_from_local_template(self, verification):
        # Arrange
        template_retriever = Mock()
        template_retriever.get_templates = Mock(return_value={'base': [ShellTemplate('base', '', 'url', '7.0')]})

        repo_downloader = Mock()
        repo_downloader.download_template.return_value = 'repo_path'

        template_compiler = Mock()

        standards = Mock()
        standards.fetch.return_value = {}

        command_executor = NewCommandExecutor(template_retriever=template_retriever,
                                              repository_downloader=repo_downloader,
                                              standards=standards,
                                              template_compiler=template_compiler)
        command_executor._get_template_params = Mock(return_value={})

        local_template = self.fs.CreateDirectory('shell_template_root').name

        # Act
        command_executor.new('new_shell', 'local:{template_dir}'.format(template_dir=local_template), 'master')

        template_compiler.compile_template.smarter_assert_called_once_with(
            CookiecutterTemplateCompiler.compile_template,
            shell_name='new_shell',
            template_path='shell_template_root',
            extra_context={},
            running_on_same_folder=False)
예제 #3
0
    def test_fail_to_generate_shell_when_requested_version_does_not_exists(self, verification):
        # Arrange
        templates = {'tosca/resource/test': [ShellTemplate('test-resource', '', 'url/test', '8.1', 'resource')]}
        repo_info = ('quali', 'resource-test')

        httpretty.register_uri(httpretty.GET, "https://api.github.com/repos/quali/resource-test/zipball/1.1",
                               body='', status=404, stream=True)
        template_compiler = Mock()

        standards = Mock()
        standards.fetch.return_value = {"resource": ['1.0.0', '1.0.1']}

        template_versions = ['master', '1.0.0', '1.0.1']

        # Act
        with patch.object(TemplateRetriever, 'get_templates', return_value=templates), \
             patch('shellfoundry.utilities.template_url._parse_repo_url', return_value=repo_info), \
             patch.object(TempDirContext, '__enter__', return_value=self.fs.CreateDirectory('mock_temp').name), \
             patch.object(TemplateVersions, 'get_versions_of_template', return_value=template_versions), \
             self.assertRaises(BadParameter) as context:
            command_executor = NewCommandExecutor(template_retriever=TemplateRetriever(),
                                                  repository_downloader=RepositoryDownloader(),
                                                  template_compiler=template_compiler,
                                                  standards=standards,
                                                  standard_versions=StandardVersionsFactory())
            command_executor._get_template_params = Mock(return_value={})
            command_executor.new('new_shell', 'tosca/resource/test', '1.1')

        # Assert
        self.assertTrue('Requested standard version (\'1.1\') does not match template version. \n'
                        'Available versions for test-resource: 1.0.0, 1.0.1' in context.exception,
                        'Actual: {}'.format(context.exception))
예제 #4
0
    def test_cookiecutter_called_for_existing_template(self, verification):
        # Arrange
        template_retriever = Mock()
        template_retriever.get_templates = Mock(
            return_value={'base': [ShellTemplate('base', '', 'https://fakegithub.com/user/repo', '7.0')]})

        repo_downloader = Mock()
        repo_downloader.download_template.return_value = 'repo_path'

        template_compiler = Mock()

        standards = Mock()
        standards.fetch.return_value = {}

        command_executor = NewCommandExecutor(template_retriever=template_retriever,
                                              repository_downloader=repo_downloader,
                                              template_compiler=template_compiler,
                                              standards=standards)

        # Act
        command_executor.new('nut_shell', 'base', 'master')

        # Assert
        template_compiler.compile_template.smarter_assert_called_once_with(
            CookiecutterTemplateCompiler.compile_template,
            shell_name='nut_shell',
            template_path='repo_path',
            extra_context={},
            running_on_same_folder=False)
예제 #5
0
    def test_can_generate_online_shell_into_same_directory(self, verification):
        # Arrange
        template_retriever = Mock()
        template_retriever.get_templates = Mock(return_value={'base': [ShellTemplate('base', '', 'url', '7.0')]})

        repo_downloader = Mock()
        repo_downloader.download_template.return_value = 'repo_path'

        template_compiler = Mock()

        standards = Mock()
        standards.fetch.return_value = {}

        command_executor = NewCommandExecutor(template_retriever=template_retriever,
                                              repository_downloader=repo_downloader,
                                              template_compiler=template_compiler,
                                              standards=standards)

        self.fs.CreateDirectory('linux-shell')
        os.chdir('linux-shell')

        # Act
        command_executor.new(os.path.curdir, 'base', 'master')

        template_compiler.compile_template.smarter_assert_called_once_with(
            CookiecutterTemplateCompiler.compile_template,
            shell_name='linux-shell',
            template_path='repo_path',
            extra_context={},
            running_on_same_folder=True)
예제 #6
0
    def test_can_generate_shell_from_local_template(self):
        # Arrange
        template_retriever = Mock()
        template_retriever.get_templates = Mock(return_value={'base': ShellTemplate('base', '', 'url')})

        repo_downloader = Mock()
        repo_downloader.download_template.return_value = 'repo_path'

        template_compiler = Mock()

        command_executor = NewCommandExecutor(template_retriever=template_retriever,
                                              repository_downloader=repo_downloader,
                                              template_compiler=template_compiler)

        local_template = self.fs.CreateDirectory('shell_template_root').name

        # Act
        command_executor.new('new_shell', 'local:{template_dir}'.format(template_dir=local_template))

        template_compiler.compile_template.smarter_assert_called_once_with(
            CookiecutterTemplateCompiler.compile_template,
            shell_name='new_shell',
            template_path='shell_template_root',
            extra_context={},
            running_on_same_folder=False)
예제 #7
0
    def test_can_generate_online_shell_into_same_directory(self):
        # Arrange
        template_retriever = Mock()
        template_retriever.get_templates = Mock(return_value={'base': ShellTemplate('base', '', 'url')})

        repo_downloader = Mock()
        repo_downloader.download_template.return_value = 'repo_path'

        template_compiler = Mock()

        command_executor = NewCommandExecutor(template_retriever=template_retriever,
                                              repository_downloader=repo_downloader,
                                              template_compiler=template_compiler)

        self.fs.CreateDirectory('linux-shell')
        os.chdir('linux-shell')

        # Act
        command_executor.new(os.path.curdir, 'base')

        template_compiler.compile_template.smarter_assert_called_once_with(
            CookiecutterTemplateCompiler.compile_template,
            shell_name='linux-shell',
            template_path='repo_path',
            extra_context={},
            running_on_same_folder=True)
예제 #8
0
    def test_can_generate_local_template_into_same_directory(self):
        # Arrange
        template_retriever = Mock()
        template_retriever.get_templates = Mock(return_value={'base': ShellTemplate('base', '', 'url')})

        repo_downloader = Mock()
        repo_downloader.download_template.return_value = 'repo_path'

        template_compiler = Mock()

        command_executor = NewCommandExecutor(template_retriever=template_retriever,
                                              repository_downloader=repo_downloader,
                                              template_compiler=template_compiler)

        local_template = os.path.abspath(self.fs.CreateDirectory('shell_template_root').name)

        shell_dir = self.fs.CreateDirectory('linux-shell').name
        os.chdir(shell_dir)

        # Act
        command_executor.new(os.path.curdir, 'local:{template_dir}'.format(template_dir=local_template))

        template_compiler.compile_template.smarter_assert_called_once_with(
            CookiecutterTemplateCompiler.compile_template,
            shell_name=shell_dir,
            template_path=local_template,
            extra_context={},
            running_on_same_folder=True)
예제 #9
0
    def test_can_generate_local_template_into_same_directory(self):
        # Arrange
        template_retriever = Mock()
        template_retriever.get_templates = Mock(
            return_value={'base': ShellTemplate('base', '', 'url', '7.0')})

        repo_downloader = Mock()
        repo_downloader.download_template.return_value = 'repo_path'

        template_compiler = Mock()

        command_executor = NewCommandExecutor(
            template_retriever=template_retriever,
            repository_downloader=repo_downloader,
            template_compiler=template_compiler)

        local_template = os.path.abspath(
            self.fs.CreateDirectory('shell_template_root').name)

        shell_dir = self.fs.CreateDirectory('linux-shell').name
        os.chdir(shell_dir)

        # Act
        command_executor.new(
            os.path.curdir,
            'local:{template_dir}'.format(template_dir=local_template),
            'master')

        template_compiler.compile_template.smarter_assert_called_once_with(
            CookiecutterTemplateCompiler.compile_template,
            shell_name=shell_dir,
            template_path=local_template,
            extra_context={},
            running_on_same_folder=True)
예제 #10
0
    def test_new_cmd_creates_gen2_in_latest_version_that_matches_the_standard_version_on_cs(
            self):
        # Arrange
        templates = """templates:
    - name : gen1/resource
      description : 1st generation shell template for basic inventory resources
      repository : https://github.com/QualiSystems/shell-resource-standard
      params:
        project_name :
      min_cs_ver: 7.0
    - name : gen2/networking/switch
      params:
        project_name :
        family_name: Switch
      description : 2nd generation shell template for a standard switch
      repository : https://github.com/QualiSystems/shellfoundry-tosca-networking-template
      min_cs_ver: 8.0"""

        template_compiler = Mock()

        standards = Mock()
        standards.fetch.return_value = [{
            'StandardName': "cloudshell_networking_standard",
            'Versions': ['5.0.0', '5.0.1']
        }]

        zipfile = mock_template_zip_file()

        httpretty.register_uri(httpretty.GET, TEMPLATES_YML, body=templates)
        httpretty.register_uri(
            httpretty.GET,
            "https://api.github.com/repos/QualiSystems/shellfoundry-tosca-networking-template/zipball/5.0.1",
            body=zipfile.read(),
            content_type='application/zip',
            content_disposition=
            "attachment; filename=quali-resource-test-dd2ba19.zip",
            stream=True)

        # Act
        with \
            patch.object(TemplateRetriever, '_get_templates_from_github', return_value=templates), \
            patch.object(TempDirContext, '__enter__', return_value=self.fs.CreateDirectory('mock_temp').name):
            cmd = NewCommandExecutor(
                template_retriever=TemplateRetriever(),
                repository_downloader=RepositoryDownloader(),
                template_compiler=template_compiler,
                standards=standards,
                standard_versions=StandardVersionsFactory())
            cmd.new('new_shell', 'gen2/networking/switch')

            # Assert
            template_compiler.compile_template.smarter_assert_called_once_with(
                CookiecutterTemplateCompiler.compile_template,
                shell_name='new_shell',
                template_path=os.path.join('mock_temp', 'root'),
                extra_context={
                    'family_name': "Switch",
                    "project_name": None
                },
                running_on_same_folder=False)
예제 #11
0
    def test_cookiecutter_called_for_existing_template(self, mock_cookiecutter):
        # Arrange
        template_retriever = Mock()
        template_retriever.get_templates = Mock(return_value={'base': ShellTemplate('base', '', 'url')})
        command_executor = NewCommandExecutor(template_retriever=template_retriever)

        # Act
        command_executor.new('nut_shell', 'base')

        # Assert
        mock_cookiecutter.assert_called_once_with('url', no_input=True, extra_context={u'project_name': 'base'})
예제 #12
0
    def test_cookiecutter_called_for_existing_template(self,
                                                       mock_cookiecutter):
        # Arrange
        template_retriever = Mock()
        template_retriever.get_templates = Mock(
            return_value={'base': ShellTemplate('base', '', 'url')})
        command_executor = NewCommandExecutor(
            template_retriever=template_retriever)

        # Act
        command_executor.new('nut_shell', 'base')

        # Assert
        mock_cookiecutter.assert_called_once_with(
            'url', no_input=True, extra_context={u'project_name': 'base'})
예제 #13
0
    def test_new_cmd_creates_gen2_when_get_cs_standards_feature_is_unavailable(
            self):
        # Arrange
        self.fs.add_real_file(ALTERNATIVE_STANDARDS_PATH)
        self.fs.add_real_file(ALTERNATIVE_TEMPLATES_PATH)

        template_compiler = Mock()
        zipfile = mock_template_zip_file()

        httpretty.register_uri(
            httpretty.GET,
            "https://api.github.com/repos/QualiSystems/shellfoundry-tosca-networking-template/zipball/5.0.0",
            body=zipfile.read(),
            content_type='application/zip',
            content_disposition=
            "attachment; filename=quali-resource-test-dd2ba19.zip",
            stream=True)

        # Act
        with \
            patch.object(Standards, '_fetch_from_cloudshell', side_effect=FeatureUnavailable()), \
            patch.object(TempDirContext, '__enter__', return_value=self.fs.CreateDirectory('mock_temp').name):
            cmd = NewCommandExecutor(
                template_retriever=TemplateRetriever(),
                repository_downloader=RepositoryDownloader(),
                template_compiler=template_compiler,
                standards=Standards(),
                standard_versions=StandardVersionsFactory())
            cmd.new('new_shell', 'gen2/networking/switch')

            # Assert
            template_compiler.compile_template.smarter_assert_called_once_with(
                CookiecutterTemplateCompiler.compile_template,
                shell_name='new_shell',
                template_path=os.path.join('mock_temp', 'root'),
                extra_context={
                    'family_name': "Switch",
                    "project_name": None
                },
                running_on_same_folder=False)
예제 #14
0
    def test_cookiecutter_called_for_existing_template(self):
        # Arrange
        template_retriever = Mock()
        template_retriever.get_templates = Mock(
            return_value={'base': ShellTemplate('base', '', 'https://fakegithub.com/user/repo')})

        repo_downloader = Mock()
        repo_downloader.download_template.return_value = 'repo_path'

        template_compiler = Mock()

        command_executor = NewCommandExecutor(template_retriever=template_retriever,
                                              repository_downloader=repo_downloader,
                                              template_compiler=template_compiler)
        # Act
        command_executor.new('nut_shell', 'base')

        # Assert
        template_compiler.compile_template.smarter_assert_called_once_with(
            CookiecutterTemplateCompiler.compile_template,
            shell_name='nut_shell',
            template_path='repo_path',
            extra_context={},
            running_on_same_folder=False)