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, 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_extracts_and_calls_api_url_from_git_addrses(self): test_dir = '/test_dir' self.fs.CreateDirectory(test_dir) input_https_address = "[email protected]:org/repo.git" expected_api_url = "https://api.github.com/repos/org/repo/zipball/master" expected_versions_url = 'https://api.github.com/repos/org/repo/branches' httpretty.register_uri( httpretty.GET, expected_api_url, body= "repo-main/,repo-main/shell.txt,repo-main/datamodel/datamodel.xml", streaming=True, status=200) httpretty.register_uri(httpretty.GET, expected_versions_url, body="[{\"name\": \"master\"}]", streaming=True, status=200) RepositoryDownloader(repo_extractor=TestRepositoryDownloader.FakeExtractor(self.fs)) \ .download_template(test_dir, input_https_address, 'master') self.assertIsNotNone(httpretty.last_request())
def test_returns_the_root_folder_of_the_git_repo(self): test_dir = '/test_dir' self.fs.CreateDirectory(test_dir) input_https_address = "[email protected]:org/repo.git" expected_api_url = "https://api.github.com/repos/org/repo/zipball/master" expected_versions_url = 'https://api.github.com/repos/org/repo/branches' httpretty.register_uri( httpretty.GET, expected_api_url, body= "repo-main/,repo-main/shell.txt,repo-main/datamodel/datamodel.xml", streaming=True, status=200) httpretty.register_uri(httpretty.GET, expected_versions_url, body="[{\"name\": \"master\"}]", streaming=True, status=200) result = RepositoryDownloader(repo_extractor=TestRepositoryDownloader.FakeExtractor(self.fs)) \ .download_template(test_dir, input_https_address, 'master') self.assertEqual(result, os.path.join(test_dir, "repo-main/"))
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_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): 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, repository_downloader=None, shell_name_validations=None, shell_gen_validations=None): """ :param RepositoryDownloader repository_downloader: :param ShellNameValidations shell_name_validations: """ self.repository_downloader = repository_downloader or RepositoryDownloader( ) self.shell_name_validations = shell_name_validations or ShellNameValidations( ) self.shell_gen_validations = shell_gen_validations or ShellGenerationValidations( ) self.cloudshell_config_reader = Configuration(CloudShellConfigReader())
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)