Exemplo n.º 1
0
    def test_list_shows_nothing_because_filter_is_set_for_templates_that_do_not_exist(
            self, max_width_mock, echo_mock):
        # Arrange
        max_width_mock.return_value = 40
        template_retriever = Mock()
        template_retriever.get_templates = Mock(return_value=OrderedDict([
            ('gen2/networking/switch',
             ShellTemplate(
                 'gen2/networking/switch',
                 'TOSCA based template for standard Switch devices/virtual appliances',
                 '', '8.0')),
            ('gen2/networking/WirelessController',
             ShellTemplate(
                 'gen2/networking/WirelessController',
                 'TOSCA based template for standard WirelessController devices/virtual appliances',
                 '', '8.0'))
        ]))
        flag_value = 'gen1'

        standards = Mock()
        standards.fetch.return_value = []

        list_command_executor = ListCommandExecutor(
            template_retriever=FilteredTemplateRetriever(
                flag_value, template_retriever),
            standards=standards)

        # Act
        list_command_executor.list()

        # Assert
        echo_mock.assert_called_once_with('No templates matched the criteria')
Exemplo n.º 2
0
    def test_filter_by_legacy_shows_all_legacy_templates(
            self, max_width_mock, echo_mock):
        # Arrange
        max_width_mock.return_value = 62
        template_retriever = Mock()
        template_retriever.get_templates = Mock(return_value=OrderedDict([
            ('gen2/networking/switch',
             ShellTemplate(
                 'gen2/networking/switch',
                 'TOSCA based template for standard Switch devices/virtual appliances',
                 '', '8.0')),
            ('gen2/networking/WirelessController',
             ShellTemplate(
                 'gen2/networking/WirelessController',
                 'TOSCA based template for standard WirelessController devices/virtual appliances',
                 '', '8.0')),
            ('gen1/base',
             ShellTemplate('gen1/base', 'base description', '', '7.0')),
            ('gen1/switch',
             ShellTemplate('gen1/switch', 'switch description', '', '7.0'))
        ]))
        flag_value = 'gen1'
        list_command_executor = ListCommandExecutor(
            template_retriever=FilteredTemplateRetriever(
                flag_value, template_retriever))

        # Act
        list_command_executor.list()

        # Assert
        echo_mock.assert_any_call(
            u' Template Name  CloudShell Ver.  Description        \n'
            u'----------------------------------------------------\n'
            u' gen1/base      7.0 and up       base description   \n'
            u' gen1/switch    7.0 and up       switch description ')
Exemplo n.º 3
0
    def test_devguide_text_note_appears_when_no_filter_was_selected(
            self, max_width_mock, echo_mock):
        # Arrange
        max_width_mock.return_value = 40
        template_retriever = Mock()
        template_retriever.get_templates = Mock(return_value=OrderedDict([
            ('gen2/networking/switch',
             ShellTemplate(
                 'gen2/networking/switch',
                 'TOSCA based template for standard Switch devices/virtual appliances',
                 '', '8.0')),
            ('gen2/networking/WirelessController',
             ShellTemplate(
                 'gen2/networking/WirelessController',
                 'TOSCA based template for standard WirelessController devices/virtual appliances',
                 '', '8.0'))
        ]))
        flag_value = None

        standards = Mock()
        standards.fetch.return_value = []

        list_command_executor = ListCommandExecutor(
            template_retriever=FilteredTemplateRetriever(
                flag_value, template_retriever),
            standards=standards)

        # Act
        list_command_executor.list()

        # Assert
        echo_mock.assert_any_call('''
As of CloudShell 8.0, CloudShell uses 2nd generation shells, to view the list of 1st generation shells use: shellfoundry list --gen1.
For more information, please visit our devguide: https://qualisystems.github.io/devguide/'''
                                  )
Exemplo n.º 4
0
    def test_two_templates_are_displayed(self, max_width_mock, echo_mock):
        # Arrange
        max_width_mock.return_value = 62  # mocking the max width to eliminate the distinction
        # between the running console size

        template_retriever = Mock()
        template_retriever.get_templates = Mock(return_value=OrderedDict(
            [('gen1/base',
              ShellTemplate('gen1/base', 'base description', '', '7.0', 'base')
              ),
             ('gen1/switch',
              ShellTemplate('gen1/switch', 'switch description', '', '7.0'))]))

        standards = Mock()
        standards.fetch.return_value = []

        list_command_executor = ListCommandExecutor(
            template_retriever=template_retriever, standards=standards)

        # Act
        list_command_executor.list()

        # Assert
        echo_mock.assert_any_call(
            u' Template Name  CloudShell Ver.  Description        \n'
            u'----------------------------------------------------\n'
            u' gen1/base      7.0 and up       base description   \n'
            u' gen1/switch    7.0 and up       switch description ')
Exemplo n.º 5
0
    def test_integration_can_generate_gen1_shell_from_specific_version(self, verification):
        # Arrange
        templates = {'tosca/resource/test': [ShellTemplate('test-resource', '', 'url/test', '8.1', 'resource')],
                     'gen1/resource': [ShellTemplate('gen1/resource', '', 'gen1/test', '7.0')]}
        repo_info = ('quali', 'resource-test')

        zipfile = mock_template_zip_file()
        httpretty.register_uri(httpretty.GET, "https://api.github.com/repos/quali/resource-test/zipball",
                               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": ['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):
            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', 'gen1/resource')

        # 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)
Exemplo n.º 6
0
    def test_console_size_small_description_wrapping_logic_ignored(
            self, max_width_mock, echo_mock):
        # Arrange
        max_width_mock.return_value = 0
        template_retriever = Mock()
        template_retriever.get_templates = Mock(return_value=OrderedDict([
            ('gen2/networking/switch',
             ShellTemplate(
                 'gen2/networking/switch',
                 'TOSCA based template for standard Switch devices/virtual appliances',
                 '', '8.0')),
            ('gen2/networking/WirelessController',
             ShellTemplate(
                 'gen2/networking/WirelessController',
                 'TOSCA based template for standard WirelessController devices/virtual appliances',
                 '', '8.0'))
        ]))

        list_command_executor = ListCommandExecutor(
            template_retriever=template_retriever)

        # Act
        list_command_executor.list()

        # Assert
        echo_mock.assert_called_once_with(
            u' Template Name                       CloudShell Ver.  Description                                                                     \n'
            u'--------------------------------------------------------------------------------------------------------------------------------------\n'
            u' gen2/networking/switch              8.0 and up       TOSCA based template for standard Switch devices/virtual appliances             \n'
            u' gen2/networking/WirelessController  8.0 and up       TOSCA based template for standard WirelessController devices/virtual appliances '
        )
Exemplo n.º 7
0
    def test_filter_by_all_shows_all_templates(self, max_width_mock,
                                               echo_mock):
        # Arrange
        max_width_mock.return_value = 40
        template_retriever = Mock()
        template_retriever.get_templates = Mock(
            return_value={
                'gen1/base':
                [ShellTemplate('gen1/base', 'base description', '', '7.0')],
                'gen1/switch': [
                    ShellTemplate('gen1/switch', 'switch description', '',
                                  '7.0')
                ],
                'gen2/networking/switch': [
                    ShellTemplate(
                        'gen2/networking/switch',
                        'TOSCA based template for standard Switch devices/virtual appliances',
                        '', '8.0')
                ],
                'gen2/networking/WirelessController': [
                    ShellTemplate(
                        'gen2/networking/WirelessController',
                        'TOSCA based template for standard WirelessController devices/virtual appliances',
                        '', '8.0')
                ]
            })
        flag_value = 'all'

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

        list_command_executor = ListCommandExecutor(
            template_retriever=FilteredTemplateRetriever(
                flag_value, template_retriever),
            standards=standards)

        # Act
        list_command_executor.list()

        # Assert
        echo_mock.assert_any_call(
            u' Template Name                       CloudShell Ver.  Description                              \n'
            u'-----------------------------------------------------------------------------------------------\n'
            u' gen2/networking/WirelessController  8.0 and up       TOSCA based template for standard        \n'
            u'                                                      WirelessController devices/virtual       \n'
            u'                                                      appliances                               \n'
            u' gen1/base                           7.0 and up       base description                         \n'
            u' gen1/switch                         7.0 and up       switch description                       \n'
            u' gen2/networking/switch              8.0 and up       TOSCA based template for standard Switch \n'
            u'                                                      devices/virtual appliances               '
        )
Exemplo n.º 8
0
    def test_single_template_is_displayed(self, max_width_mock, echo_mock):
        # Arrange
        max_width_mock.return_value = 62  # mocking the max width to eliminate the distinction
        # between the running console size

        template_retriever = Mock()
        template_retriever.get_templates = Mock(return_value={
            'gen1/base':
            [ShellTemplate('gen1/base', 'description', '', '7.0')]
        })

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

        list_command_executor = ListCommandExecutor(
            template_retriever=template_retriever, standards=standards)

        # Act
        list_command_executor.list()

        # Assert
        echo_mock.assert_any_call(
            u' Template Name  CloudShell Ver.  Description \n'
            u'---------------------------------------------\n'
            u' gen1/base      7.0 and up       description ')
Exemplo n.º 9
0
    def test_show_command_raise_no_versions_found_when_there_are_no_versions_at_all(
            self):
        # Arrange
        template_name = 'tosca/networking/switch'
        raw_response = """[]"""

        shell_template = ShellTemplate('tosca/networking/switch',
                                       'some description',
                                       'mock://tosca/networking/switch', '8.0')
        template_retriever_mock = Mock(spec=TemplateRetriever, autospec=True)
        template_retriever_mock.get_templates.return_value = {
            'tosca/networking/switch': shell_template
        }

        # Act
        with patch('shellfoundry.commands.show_command.requests.get') as get_response_mock, \
            self.assertRaises(click.ClickException) as context:
            type(get_response_mock.return_value).status_code = PropertyMock(
                return_value=200)
            type(get_response_mock.return_value).text = PropertyMock(
                return_value=raw_response)
            ShowCommandExecutor(template_retriever_mock).show(template_name)

        # Assert
        self.assertTrue("No versions have been found for this template" in
                        context.exception)
Exemplo n.º 10
0
    def test_show_template_one_version_shows_as_latest(self, echo_mock):
        # Arrange
        template_name = 'tosca/networking/switch'
        raw_response = """[
  {
    "name": "master"
  },
  {
    "name": "1.0"
  }
]"""
        shell_template = ShellTemplate('tosca/networking/switch',
                                       'some description',
                                       'mock://tosca/networking/switch', '8.0')
        template_retriever_mock = Mock(spec=TemplateRetriever, autospec=True)
        template_retriever_mock.get_templates.return_value = {
            'tosca/networking/switch': shell_template
        }

        # Act
        with patch('shellfoundry.commands.show_command.requests.get'
                   ) as get_response_mock:
            type(get_response_mock.return_value).status_code = PropertyMock(
                return_value=200)
            type(get_response_mock.return_value).text = PropertyMock(
                return_value=raw_response)
            ShowCommandExecutor(template_retriever_mock).show(template_name)

        # Assert
        echo_mock.assert_called_once_with('1.0 (latest)')
Exemplo n.º 11
0
    def test_integration_can_generate_shell_from_specific_version(self):
        # Arrange
        templates = {
            'tosca/resource/test': ShellTemplate('test-resource', '', 'url',
                                                 '7.0')
        }
        repo_info = ('quali', 'resource-test')

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

        # Act
        with patch.object(TemplateRetriever, 'get_templates', return_value=templates),\
            patch.object(RepositoryDownloader, '_parse_repo_url', return_value=repo_info),\
            patch.object(TempDirContext, '__enter__', return_value=self.fs.CreateDirectory('mock_temp').name):
            NewCommandExecutor(template_retriever=TemplateRetriever(),
                               repository_downloader=RepositoryDownloader(),
                               template_compiler=template_compiler)\
                .new('new_shell', 'tosca/resource/test', '1.1')

        # 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)
Exemplo n.º 12
0
    def get_templates(self, **kwargs):
        """
        :return: Dictionary of shellfoundry.ShellTemplate
        """

        alternative_path = kwargs.get('alternative', None)
        standards = kwargs.get('standards', [])

        if alternative_path:
            response = self._get_templates_from_path(alternative_path)
        else:
            response = self._get_templates_from_github()

        config = yaml.load(response)
        if not config or 'templates' not in config:
            return {}

        templatesdic = OrderedDict()
        for template in config['templates']:
            templatesdic[template['name']] = ShellTemplate(
                template['name'], template['description'],
                template['repository'], template['min_cs_ver'],
                self._get_standard_out_of_name(template['name']),
                template['params'])

        return self._filter_by_standards(templatesdic, standards)
Exemplo n.º 13
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)
Exemplo n.º 14
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))
Exemplo n.º 15
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)
Exemplo n.º 16
0
    def test_fail_to_generate_shell_when_requested_version_does_not_exists(
            self):
        # Arrange
        templates = {
            'tosca/resource/test': ShellTemplate('test-resource', '', 'url',
                                                 '7.0')
        }
        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()

        # Act
        with patch.object(TemplateRetriever, 'get_templates', return_value=templates), \
             patch.object(RepositoryDownloader, '_parse_repo_url', return_value=repo_info), \
             patch.object(TempDirContext, '__enter__', return_value=self.fs.CreateDirectory('mock_temp').name),\
             self.assertRaises(BadParameter) as context:
            NewCommandExecutor(template_retriever=TemplateRetriever(),
                               repository_downloader=RepositoryDownloader(),
                               template_compiler=template_compiler) \
                .new('new_shell', 'tosca/resource/test', '1.1')

        # Assert
        self.assertTrue(
            '1.1 does not exists or invalid value' in context.exception)
Exemplo n.º 17
0
    def test_show_template_versions_are_sorted_from_latest_to_earliest_version(
            self, echo_mock):
        # Arrange
        template_name = 'tosca/networking/switch'
        raw_response = [{"name": "master"}, {"name": "1.0"}, {"name": "1.1"}]

        shell_template = ShellTemplate('tosca/networking/switch',
                                       'some description',
                                       'mock://tosca/networking/switch', '8.0')
        template_retriever_mock = Mock(spec=TemplateRetriever, autospec=True)
        template_retriever_mock.get_templates.return_value = {
            'tosca/networking/switch': [shell_template]
        }

        # Act
        with patch('shellfoundry.commands.show_command.requests.get'
                   ) as get_response_mock:
            type(get_response_mock.return_value).status_code = PropertyMock(
                return_value=200)
            type(get_response_mock.return_value).json = Mock(
                side_effect=lambda: raw_response)
            ShowCommandExecutor(template_retriever_mock).show(template_name)

        # Assert
        calls = [call('1.1 (latest)'), call('1.0')]
        echo_mock.assert_has_calls(calls, any_order=False)
Exemplo n.º 18
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)
Exemplo n.º 19
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)
Exemplo n.º 20
0
    def test_two_long_named_templates_are_displayed_on_normal_window(
            self, max_width_mock, echo_mock):
        # Arrange
        max_width_mock.return_value = 40  # mocking the max width to eliminate the distinction
        # between the running console size

        template_retriever = Mock()
        template_retriever.get_templates = Mock(
            return_value={
                'gen2/networking/switch': [
                    ShellTemplate(
                        'gen2/networking/switch',
                        'TOSCA based template for standard Switch devices/virtual appliances',
                        '', '8.0')
                ],
                'gen2/networking/WirelessController': [
                    ShellTemplate(
                        'gen2/networking/WirelessController',
                        'TOSCA based template for standard WirelessController devices/virtual appliances',
                        '', '8.0')
                ]
            })

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

        list_command_executor = ListCommandExecutor(
            template_retriever=template_retriever, standards=standards)

        # Act
        list_command_executor.list()

        # Assert
        echo_mock.assert_any_call(
            u' Template Name                       CloudShell Ver.  Description                              \n'
            u'-----------------------------------------------------------------------------------------------\n'
            u' gen2/networking/WirelessController  8.0 and up       TOSCA based template for standard        \n'
            u'                                                      WirelessController devices/virtual       \n'
            u'                                                      appliances                               \n'
            u' gen2/networking/switch              8.0 and up       TOSCA based template for standard Switch \n'
            u'                                                      devices/virtual appliances               '
        )
Exemplo n.º 21
0
    def test_console_size_small_description_wrapping_logic_ignored(
            self, max_width_mock, echo_mock):
        # Arrange
        max_width_mock.return_value = 0
        template_retriever = Mock()
        template_retriever.get_templates = Mock(
            return_value={
                'gen2/networking/switch': [
                    ShellTemplate(
                        'gen2/networking/switch',
                        'TOSCA based template for standard Switch devices/virtual appliances',
                        '', '8.0')
                ],
                'gen2/networking/WirelessController': [
                    ShellTemplate(
                        'gen2/networking/WirelessController',
                        'TOSCA based template for standard WirelessController devices/virtual appliances',
                        '', '8.0')
                ]
            })

        standards = Mock()
        standards.fetch.return_value = {
            "networking": ['2.0.0'],
            "resource": ['5.0.0', '5.0.1'],
            "vido": ['3.0.1', '3.0.2', '3.0.3']
        }
        list_command_executor = ListCommandExecutor(
            template_retriever=template_retriever, standards=standards)

        # Act
        list_command_executor.list()

        # Assert
        echo_mock.assert_called_once_with(
            u' Template Name                       CloudShell Ver.  Description                                                                     \n'
            u'--------------------------------------------------------------------------------------------------------------------------------------\n'
            u' gen2/networking/WirelessController  8.0 and up       TOSCA based template for standard WirelessController devices/virtual appliances \n'
            u' gen2/networking/switch              8.0 and up       TOSCA based template for standard Switch devices/virtual appliances             '
        )
    def get_templates(self):
        """
        :return: Dictionary of shellfoundry.ShellTemplate
        """
        response = self._get_templates_from_github()
        config = yaml.load(response)
        if not config or 'templates' not in config:
            return []

        return {template['name']: ShellTemplate(
            template['name'],
            template['description'],
            template['repository']) for template in config['templates']}
Exemplo n.º 23
0
    def test_list_command_does_not_fail(self, echo_mock):
        # Arrange
        template_retriever = Mock()
        template_retriever.get_templates = Mock(
            return_value={'base': ShellTemplate('base', '', '')})

        list_command_executor = ListCommandExecutor(template_retriever)

        # Act
        list_command_executor.list()

        # Assert
        echo_mock.assert_called_once_with(
            u'Supported templates are: \r\n base')
Exemplo n.º 24
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'})
Exemplo n.º 25
0
    def test_list_shows_nothing_because_filter_is_set_for_templates_that_do_not_exist(
            self, max_width_mock):
        # Arrange
        max_width_mock.return_value = 40
        template_retriever = Mock()
        template_retriever.get_templates = Mock(
            return_value={
                'gen2/networking/switch': [
                    ShellTemplate(
                        'gen2/networking/switch',
                        'TOSCA based template for standard Switch devices/virtual appliances',
                        '', '8.0')
                ],
                'gen2/networking/WirelessController': [
                    ShellTemplate(
                        'gen2/networking/WirelessController',
                        'TOSCA based template for standard WirelessController devices/virtual appliances',
                        '', '8.0')
                ]
            })
        flag_value = 'gen1'

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

        list_command_executor = ListCommandExecutor(
            template_retriever=FilteredTemplateRetriever(
                flag_value, template_retriever),
            standards=standards)

        # Act
        with self.assertRaisesRegexp(
                ClickException,
                "No templates matched the view criteria\(gen1/gen2\) or "
                "available templates and standards are not compatible"):
            list_command_executor.list()
Exemplo n.º 26
0
    def get_templates(self):
        """
        :return: Dictionary of shellfoundry.ShellTemplate
        """
        response = self._get_templates_from_github()
        config = yaml.load(response)
        if not config or 'templates' not in config:
            return []

        templatesdic = OrderedDict()
        for template in config['templates']:
            templatesdic[template['name']] = ShellTemplate(
                template['name'], template['description'],
                template['repository'], template['min_cs_ver'],
                template['params'])

        return templatesdic
Exemplo n.º 27
0
    def test_repository_url_is_empty_raises_error(self):
        # Arrange
        template_name = 'tosca/networking/switch'

        repository = ''
        shell_template = ShellTemplate('tosca/networking/switch',
                                       'some description', repository, '8.0')
        template_retriever_mock = Mock(spec=TemplateRetriever, autospec=True)
        template_retriever_mock.get_templates.return_value = {
            'tosca/networking/switch': shell_template
        }

        # Act
        with self.assertRaises(click.ClickException) as context:
            ShowCommandExecutor(template_retriever_mock).show(template_name)

        # Assert
        self.assertTrue('Repository url is empty' in context.exception)
Exemplo n.º 28
0
    def test_integration_latest_version_is_default_when_version_was_not_specified(
            self):
        # 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 = [{
            'StandardName': "cloudshell_resource_standard",
            'Versions': ['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):
            NewCommandExecutor(template_retriever=TemplateRetriever(),
                               repository_downloader=RepositoryDownloader(),
                               template_compiler=template_compiler,
                               standards=standards,
                               standard_versions=StandardVersionsFactory()) \
                .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)
Exemplo n.º 29
0
    def test_show_template_shows_fail_message_when_template_does_not_exist(
            self):
        # Arrange
        template_name = 'idonot/exist'

        shell_template = ShellTemplate('notheright/one', 'some description',
                                       'mock://not/the/right/one', '8.0')
        template_retriever_mock = Mock(spec=TemplateRetriever, autospec=True)
        template_retriever_mock.get_templates.return_value = {
            'tosca/networking/switch': shell_template
        }

        # Act
        with self.assertRaises(click.ClickException) as context:
            ShowCommandExecutor(template_retriever_mock).show(template_name)

        # Assert
        self.assertTrue(
            "The template '{}' does not exist, please specify a valid 2nd Gen shell template."
            .format(template_name) in context.exception)
Exemplo n.º 30
0
    def test_show_command_versions_request_failed_raises_error(self):
        # Arrange
        template_name = 'tosca/networking/switch'

        shell_template = ShellTemplate('tosca/networking/switch',
                                       'some description',
                                       'mock://tosca/networking/switch', '8.0')
        template_retriever_mock = Mock(spec=TemplateRetriever, autospec=True)
        template_retriever_mock.get_templates.return_value = {
            'tosca/networking/switch': shell_template
        }

        # Act
        with patch('shellfoundry.commands.show_command.requests.get') as get_response_mock, \
            self.assertRaises(click.ClickException) as context:
            type(get_response_mock.return_value).status_code = PropertyMock(
                return_value=400)
            ShowCommandExecutor(template_retriever_mock).show(template_name)

        # Assert
        self.assertTrue(
            'Failed to receive versions from host' in context.exception)