def test_get_templates_from_data_folder(self): # Arrange self.fs.CreateFile('/data/templates.yml', contents=""" 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/software-asset params: project_name : family_name : description : 2nd generation shell template for software assets repository : https://github.com/QualiSystems/shellfoundry-tosca-software_asset-template min_cs_ver: 8.1 """) template_retriever = TemplateRetriever() # Act templates = template_retriever.get_templates( alternative='/data/templates.yml') # Assert self.assertEqual(len(templates), 2) self.assertTrue('gen1/resource' in templates) self.assertTrue('gen2/software-asset' in templates) self.assertEqual(templates['gen1/resource'].standard, None) self.assertEqual(templates['gen2/software-asset'].standard, 'software-asset')
def test_session_max_retires(self): # Arrange template_retriever = TemplateRetriever() httpretty.register_uri('GET', TEMPLATES_YML, body=self.mock_get_templates_from_github()) # Act templates = template_retriever.get_templates() # Assert self.assertEqual(templates['router'].name, 'router') self.assertEqual(templates['router'].description, 'Basic router template') self.assertEqual( templates['router'].repository, 'https://github.com/QualiSystems/shellfoundry-router-template') self.assertEqual(templates['switch'].name, 'switch') self.assertEqual(templates['switch'].description, 'Basic switch template') self.assertEqual( templates['switch'].repository, 'https://github.com/QualiSystems/shellfoundry-switch-template')
def test_empty_templates_should_return_when_not_templates_found_on_github(self): # Arrange template_retriever = TemplateRetriever() # Act templates = template_retriever.get_templates() # Assert self.assertEqual(templates, {})
def test_get_templates(self): # Arrange template_retriever = TemplateRetriever() # Act templates = template_retriever.get_templates() # Assert self.assertEqual(templates['router'].name, 'router') self.assertEqual(templates['router'].description, 'Basic router template') self.assertEqual(templates['router'].repository, 'https://github.com/QualiSystems/shellfoundry-router-template') self.assertEqual(templates['switch'].name, 'switch') self.assertEqual(templates['switch'].description, 'Basic switch template') self.assertEqual(templates['switch'].repository, 'https://github.com/QualiSystems/shellfoundry-switch-template')
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)
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)
def test_requested_template_does_not_exists_raises_an_error(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()) # Assert output_msg = "Template gen2/doesnot/exists does not exist. Supported templates are: gen1/resource, " \ "gen1/resource-clean, gen1/deployed-app, gen1/networking/switch, gen1/networking/router," \ " gen1/pdu, gen1/firewall, gen1/compute, layer-1-switch, gen2/networking/switch, " \ "gen2/networking/router, gen2/networking/wireless-controller, gen2/compute, " \ "gen2/deployed-app, gen2/pdu, gen2/resource, gen2/firewall" self.assertRaisesRegexp(BadParameter, output_msg, cmd.new, 'new_shell', 'gen2/doesnot/exists')
def test_filtered_template_retriever_gen2_success(self): # Arrange template_retriever = FilteredTemplateRetriever(GEN_TWO, TemplateRetriever()) httpretty.register_uri('GET', TEMPLATES_YML, body=self.mock_get_templates_from_github()) # Act templates = template_retriever.get_templates() # Assert self.assertEqual(len(templates), 2) self.assertEqual(templates['gen2/switch'].name, 'gen2/switch') self.assertEqual(templates['gen2/switch'].description, 'Basic switch template') self.assertEqual( templates['gen2/switch'].repository, 'https://github.com/QualiSystems/shellfoundry-switch-template') self.assertEqual(templates['gen2/software-asset'].name, 'gen2/software-asset') self.assertEqual(templates['gen2/software-asset'].description, 'Basic software-asset template') self.assertEqual( templates['gen2/software-asset'].repository, 'https://github.com/QualiSystems/shellfoundry-software-asset-template' )
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)
def __init__(self, template_compiler=None, template_retriever=None, repository_downloader=None, standards=None, standard_versions=None, shell_name_validations=None): """ :param CookiecutterTemplateCompiler template_compiler: :param TemplateRetriever template_retriever: :param RepositoryDownloader repository_downloader: :param Standards standards: :param StandardVersionsFactory standard_versions: :param ShellNameValidations shell_name_validations: """ self.cloudshell_config_reader = Configuration(CloudShellConfigReader()) self.template_retriever = template_retriever or TemplateRetriever() self.repository_downloader = repository_downloader or RepositoryDownloader( ) self.template_compiler = template_compiler or CookiecutterTemplateCompiler( ) self.standards = standards or Standards() self.standard_versions = standard_versions or StandardVersionsFactory() self.shell_name_validations = shell_name_validations or ShellNameValidations( )
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)
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))
def __init__(self, template_compiler=None, template_retriever=None, repository_downloader=None): self.template_retriever = template_retriever or TemplateRetriever() self.repository_downloader = repository_downloader or RepositoryDownloader( ) self.template_compiler = template_compiler or CookiecutterTemplateCompiler( )
def __init__(self, repository_downloader=None, template_retriever=None): """ :param TemplateRetriever template_retriever: :param RepositoryDownloader repository_downloader: """ self.cloudshell_config_reader = Configuration(CloudShellConfigReader()) self.template_retriever = template_retriever or TemplateRetriever() self.repository_downloader = repository_downloader or RepositoryDownloader()
def __init__(self, default_view=None, template_retriever=None, standards=None): """ :param str default_view: :param Standards standards: """ dv = default_view or Configuration( ShellFoundryConfig()).read().defaultview self.template_retriever = template_retriever or FilteredTemplateRetriever( dv, TemplateRetriever()) self.show_info_msg = default_view is None self.standards = standards or Standards()
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)
def test_get_cs_standards_unavailable_shows_cs_8_0_shipped_templates( self, max_width_mock, echo_mock): # Assert max_width_mock.return_value = 60 from shellfoundry import ALTERNATIVE_TEMPLATES_PATH self.fs.add_real_file(ALTERNATIVE_TEMPLATES_PATH) standards = Mock(fetch=Mock(side_effect=FeatureUnavailable())) template_retriever = FilteredTemplateRetriever('all', TemplateRetriever()) list_command_executor = ListCommandExecutor( template_retriever=template_retriever, standards=standards) # Act list_command_executor.list() # Assert templates_output = self.get_8_0_templates_output() echo_mock.assert_any_call(templates_output)
def __init__(self, template_retriever=None): self.template_retriever = template_retriever or FilteredTemplateRetriever(GEN_TWO_FILTER, TemplateRetriever())
def test_templates_are_filtered_based_upon_the_result_of_cs_standards( self, _get_min_cs_version, conf_class, max_width_mock, echo_mock): # Arrange _get_min_cs_version.return_value = None configuration = MagicMock(read=MagicMock(return_value=MagicMock( online_mode="True"))) conf_class.return_value = configuration max_width_mock.return_value = 40 templates = """templates: - name : gen1/resource description : base description repository : https://github.com/QualiSystems/shell-resource-standard params: project_name : min_cs_ver: 7.0 - name : gen1/switch description : switch description repository : https://github.com/QualiSystems/shell-switch-standard params: project_name : min_cs_ver: 7.0 - name : gen2/resource params: project_name : family_name: description : 2nd generation shell template for a standard resource repository : https://github.com/QualiSystems/shellfoundry-tosca-resource-template min_cs_ver: 8.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 - name : gen2/networking/wireless-controller params: project_name : family_name: WirelessController description : 2nd generation shell template for a standard wireless controller repository : https://github.com/QualiSystems/shellfoundry-tosca-networking-template min_cs_ver: 8.0""" flag_value = 'all' standards = Mock() standards.fetch.return_value = {"resource": ['5.0.0']} template_retriever = FilteredTemplateRetriever(flag_value, TemplateRetriever()) httpretty.register_uri(httpretty.GET, TEMPLATES_YML, body=templates) 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/resource 7.0 and up base description \n' u' gen1/switch 7.0 and up switch description \n' u' gen2/resource 8.0 and up 2nd generation shell template for a \n' u' standard resource ' )
def __init__(self, default_view=None, template_retriever=None): dv = default_view or Configuration( ShellFoundryConfig()).read().defaultview self.template_retriever = template_retriever or FilteredTemplateRetriever( dv, TemplateRetriever()) self.show_info_msg = default_view is None
def __init__(self, template_retriever=None): self.template_retriever = template_retriever or TemplateRetriever()
def test_templates_are_filtered_based_upon_the_result_of_cs_standards_gen2( self, max_width_mock, echo_mock): # Arrange max_width_mock.return_value = 40 templates = """templates: - name : gen1/resource description : base description repository : https://github.com/QualiSystems/shell-resource-standard params: project_name : min_cs_ver: 7.0 - name : gen1/switch description : switch description repository : https://github.com/QualiSystems/shell-switch-standard params: project_name : min_cs_ver: 7.0 - name : gen2/resource params: project_name : family_name: description : 2nd generation shell template for a standard resource repository : https://github.com/QualiSystems/shellfoundry-tosca-resource-template min_cs_ver: 8.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 - name : gen2/networking/wireless-controller params: project_name : family_name: WirelessController description : 2nd generation shell template for a standard wireless controller repository : https://github.com/QualiSystems/shellfoundry-tosca-networking-template min_cs_ver: 8.0""" flag_value = 'gen2' standards = Mock() standards.fetch.return_value = [{ 'StandardName': 'cloudshell_networking_standard', "Versions": ['5.0.0'] }] template_retriever = FilteredTemplateRetriever(flag_value, TemplateRetriever()) httpretty.register_uri(httpretty.GET, TEMPLATES_YML, body=templates) 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/switch 8.0 and up 2nd generation shell template for a \n' u' standard switch \n' u' gen2/networking/wireless-controller 8.0 and up 2nd generation shell template for a \n' u' standard wireless controller ' )