def read(self): """ :return: :rtype shellfoundry.models.install_config.Installconfig """ config_path = os.path.join(os.getcwd(), 'cloudshell_config.yml') if not os.path.isfile(config_path): return InstallConfig.get_default() with open(config_path) as stream: config = yaml.load(stream.read()) if not config or INSTALL not in config: return InstallConfig.get_default() install_config = config[INSTALL] host = self._get_with_default(install_config, HOST, DEFAULT_HOST) port = self._get_with_default(install_config, PORT, DEFAULT_PORT) username = self._get_with_default(install_config, USERNAME, DEFAULT_USERNAME) password = self._get_with_default(install_config, PASSWORD, DEFAULT_PASSWORD) domain = self._get_with_default(install_config, DOMAIN, DEFAULT_DOMAIN) return InstallConfig(host, port, username, password, domain)
def test_two_instances_should_be_equal(self): config1 = InstallConfig("localhost", 9000, "YOUR_USERNAME", "YOUR_PASSWORD", "Global", "author", "online_mode", "template_location") config2 = InstallConfig("localhost", 9000, "YOUR_USERNAME", "YOUR_PASSWORD", "Global", "author", "online_mode", "template_location") self.assertEqual(config1, config2)
def test_two_instances_should_not_be_equal(self): config1 = InstallConfig("localhost", 9000, "YOUR_USERNAME", "YOUR_PASSWORD", "Global", "author", "online_mode", "template_location", "github_login", "github_password") config2 = InstallConfig("remote", 1, "U", "P", "Local", "co-author", "False", "local_templates_location", "github_login_new", "github_password_new") self.assertNotEqual(config1, config2)
def test_when_config_files_exist_install_succeeds(self, secho_mock): # Arrange self.fs.CreateFile('nut_shell/shell.yml', contents=""" shell: name: nut_shell driver: NutShellDriver """) self.fs.CreateFile('nut_shell/cloudshell_config.yml', contents=""" install: host: localhost port: 9000 username: YOUR_USERNAME password: YOUR_PASSWORD domain: Global author: AUTHOR online_mode: ONLINE_MODE template_location: TEMPLATE_LOCATION """) os.chdir('nut_shell') mock_installer = MagicMock() command_executor = InstallCommandExecutor(installer=mock_installer) # Act command_executor.install() # Assert mock_installer.install.assert_called_once_with( 'nut_shell', InstallConfig('localhost', 9000, 'YOUR_USERNAME', 'YOUR_PASSWORD', 'Global', "AUTHOR", 'ONLINE_MODE', 'TEMPLATE_LOCATION')) secho_mock.assert_any_call('Successfully installed shell', fg='green')
def read_from_config(self, config): host = get_with_default(config, HOST, DEFAULT_HOST) port = get_with_default(config, PORT, DEFAULT_PORT) username = get_with_default(config, USERNAME, DEFAULT_USERNAME) password = get_with_default(config, PASSWORD, DEFAULT_PASSWORD) domain = get_with_default(config, DOMAIN, DEFAULT_DOMAIN) return InstallConfig(host, port, username, password, domain)
def test_when_install_called_it_uploads_package_to_cloudshell( self, mock_quali_api_client, mock_upload_environment_zip_file): # Arrange # Constructor should return None mock_quali_api_client.return_value = None file = self.fs.CreateFile("work/dest/nut_shell.zip") os.chdir("work") install_config = InstallConfig("localhost", 9000, "YOUR_USERNAME", "YOUR_PASSWORD", "Global", "author", "online_mode", "template_location") shell_installer = ShellInstaller() # Act with patch("click.echo"): shell_installer.install("nut_shell", install_config) shell_full_path = "{sep}work{sep}dist{sep}nut_shell.zip".format( sep=self.fs.path_separator) # self.fs.path_separator + "work" + self.fs.path_separator + "dist" + self.fs.path_separator + "nut_shell.zip" # Assert mock_upload_environment_zip_file.assert_called_once_with( shell_full_path)
def read_from_config(self, config): host = get_with_default(config, HOST, DEFAULT_HOST) port = get_with_default(config, PORT, DEFAULT_PORT) username = get_with_default(config, USERNAME, DEFAULT_USERNAME) password = get_with_default(config, PASSWORD, DEFAULT_PASSWORD) domain = get_with_default(config, DOMAIN, DEFAULT_DOMAIN) author = get_with_default(config, AUTHOR, DEFAULT_AUTHOR) online_mode = get_with_default(config, ONLINE_MODE, DEFAULT_ONLINE_MODE) template_location = get_with_default(config, TEMPLATE_LOCATION, DEFAULT_TEMPLATE_LOCATION) return InstallConfig(host, port, username, password, domain, author, online_mode, template_location)
def test_error_displayed_when_driver_generation_returns_error_code(self): self.fs.CreateFile("nut-shell/dist/NutShell.zip", contents="ZIP") self.fs.CreateFile("nut-shell/temp/data_model.py", contents="python data model content") ArchiveCreator.make_archive("nut-shell/temp/data-model", "zip", "nut-shell/temp") driver_generator = DriverGenerator() config = InstallConfig("TEST-HOST", 9000, "user", "pwd", "Global", "author", "online_mode", "template_location", "github_login", "github_password") with patch( "shellfoundry.utilities.driver_generator.PackagingRestApiClient" ) as mock_rest: rest_client_mock = Mock() rest_client_mock.token = "TEST-TOKEN" mock_rest.return_value = rest_client_mock with patch("shellfoundry.utilities.driver_generator.post" ) as post_mock: response = Mock() response.status_code = 500 response.content = "Error occurred" post_mock.return_value = response with patch("shellfoundry.utilities.driver_generator.click" ) as click_mock: click_mock.echo = MagicMock() # Act driver_generator.generate_driver( cloudshell_config=config, destination_path="nut-shell/src", package_full_path="nut-shell/dist/NutShell.zip", shell_filename="NutShell.zip", shell_name="NutShell") self.assertTrue(click_mock.echo.called, "click should have been called") # Assert assertFileDoesNotExist(self, "nut-shell/src/data_model.py")
def test_error_displayed_when_failed_to_connect_to_cloudshell_server(self): self.fs.CreateFile("nut-shell/dist/NutShell.zip", contents="ZIP") self.fs.CreateFile("nut-shell/temp/data_model.py", contents="python data model content") ArchiveCreator.make_archive("nut-shell/temp/data-model", "zip", "nut-shell/temp") driver_generator = DriverGenerator() config = InstallConfig("TEST-HOST", 9000, "user", "pwd", "Global", "author", "online_mode", "template_location", "github_login", "github_password") with patch( "shellfoundry.utilities.driver_generator.PackagingRestApiClient" ) as mock_rest: mock_rest.side_effect = urllib2.URLError("connected failed") with patch("shellfoundry.utilities.driver_generator.click" ) as click_mock: echo_mock = MagicMock() click_mock.echo = echo_mock # Act try: driver_generator.generate_driver( cloudshell_config=config, destination_path="nut-shell/src", package_full_path="nut-shell/dist/NutShell.zip", shell_filename="NutShell.zip", shell_name="NutShell") except urllib2.URLError: pass self.assertTrue(echo_mock.called, "click should have been called") self.assertEqual( echo_mock.call_args[0][0], u"Login to CloudShell failed. Please verify the credentials in cloudshell_config.yml" ) # Assert assertFileDoesNotExist(self, "nut-shell/src/data_model.py")
def test_driver_generated_successfully(self): self.fs.CreateFile("nut-shell/dist/NutShell.zip", contents="ZIP") self.fs.CreateFile("nut-shell/temp/data_model.py", contents="python data model content") ArchiveCreator.make_archive("nut-shell/temp/data-model", "zip", "nut-shell/temp") driver_generator = DriverGenerator() config = InstallConfig("TEST-HOST", 9000, "user", "pwd", "Global", "author", "online_mode", "template_location", "github_login", "github_password") with patch( "shellfoundry.utilities.driver_generator.PackagingRestApiClient" ) as mock_rest: rest_client_mock = Mock() rest_client_mock.token = "TEST-TOKEN" mock_rest.return_value = rest_client_mock with patch("shellfoundry.utilities.driver_generator.post" ) as post_mock: with open("nut-shell/temp/data-model.zip", "r") as data_model_file: file_content = data_model_file.read() response = Mock() response.status_code = 200 response.content = file_content post_mock.return_value = response # Act driver_generator.generate_driver( cloudshell_config=config, destination_path="nut-shell/src", package_full_path="nut-shell/dist/NutShell.zip", shell_filename="NutShell.zip", shell_name="NutShell") # Assert assertFileExists(self, "nut-shell/src/data_model.py")
def test_error_displayed_when_driver_generation_returns_error_code(self): self.fs.CreateFile('nut-shell/dist/NutShell.zip', contents='ZIP') self.fs.CreateFile('nut-shell/temp/data_model.py', contents='python data model content') ArchiveCreator.make_archive('nut-shell/temp/data-model', 'zip', 'nut-shell/temp') driver_generator = DriverGenerator() config = InstallConfig('TEST-HOST', 9000, 'user', 'pwd', 'Global') with patch( 'shellfoundry.utilities.driver_generator.PackagingRestApiClient' ) as mock_rest: rest_client_mock = Mock() rest_client_mock.token = 'TEST-TOKEN' mock_rest.return_value = rest_client_mock with patch('shellfoundry.utilities.driver_generator.post' ) as post_mock: response = Mock() response.status_code = 500 response.content = 'Error occurred' post_mock.return_value = response with patch('shellfoundry.utilities.driver_generator.click' ) as click_mock: click_mock.echo = MagicMock() # Act driver_generator.generate_driver( cloudshell_config=config, destination_path='nut-shell/src', package_full_path='nut-shell/dist/NutShell.zip', shell_filename='NutShell.zip', shell_name='NutShell') self.assertTrue(click_mock.echo.called, 'click should have been called') # Assert assertFileDoesNotExist(self, 'nut-shell/src/data_model.py')
def test_error_displayed_when_failed_to_connect_to_cloudshell_server(self): self.fs.CreateFile('nut-shell/dist/NutShell.zip', contents='ZIP') self.fs.CreateFile('nut-shell/temp/data_model.py', contents='python data model content') ArchiveCreator.make_archive('nut-shell/temp/data-model', 'zip', 'nut-shell/temp') driver_generator = DriverGenerator() config = InstallConfig('TEST-HOST', 9000, 'user', 'pwd', 'Global') with patch( 'shellfoundry.utilities.driver_generator.PackagingRestApiClient' ) as mock_rest: mock_rest.side_effect = urllib2.URLError('connected failed') with patch('shellfoundry.utilities.driver_generator.click' ) as click_mock: echo_mock = MagicMock() click_mock.echo = echo_mock # Act try: driver_generator.generate_driver( cloudshell_config=config, destination_path='nut-shell/src', package_full_path='nut-shell/dist/NutShell.zip', shell_filename='NutShell.zip', shell_name='NutShell') except urllib2.URLError: pass self.assertTrue(echo_mock.called, 'click should have been called') self.assertEqual( echo_mock.call_args[0][0], u'Login to CloudShell failed. Please verify the credentials in cloudshell_config.yml' ) # Assert assertFileDoesNotExist(self, 'nut-shell/src/data_model.py')
def test_driver_generated_successfully(self): self.fs.CreateFile('nut-shell/dist/NutShell.zip', contents='ZIP') self.fs.CreateFile('nut-shell/temp/data_model.py', contents='python data model content') ArchiveCreator.make_archive('nut-shell/temp/data-model', 'zip', 'nut-shell/temp') driver_generator = DriverGenerator() config = InstallConfig('TEST-HOST', 9000, 'user', 'pwd', 'Global') with patch( 'shellfoundry.utilities.driver_generator.PackagingRestApiClient' ) as mock_rest: rest_client_mock = Mock() rest_client_mock.token = 'TEST-TOKEN' mock_rest.return_value = rest_client_mock with patch('shellfoundry.utilities.driver_generator.post' ) as post_mock: with open('nut-shell/temp/data-model.zip', 'r') as data_model_file: file_content = data_model_file.read() response = Mock() response.status_code = 200 response.content = file_content post_mock.return_value = response # Act driver_generator.generate_driver( cloudshell_config=config, destination_path='nut-shell/src', package_full_path='nut-shell/dist/NutShell.zip', shell_filename='NutShell.zip', shell_name='NutShell') # Assert assertFileExists(self, 'nut-shell/src/data_model.py')
def test_when_install_called_it_uploads_package_to_cloudshell(self, mock_quali_api_client, mock_upload_environment_zip_file): # Arrange # Constructor should return None mock_quali_api_client.return_value = None file = self.fs.CreateFile('work/dest/nut_shell.zip') os.chdir('work') install_config = InstallConfig('localhost', 9000, 'YOUR_USERNAME', 'YOUR_PASSWORD', 'Global') shell_installer = ShellInstaller() # Act with patch('click.echo'): shell_installer.install('nut_shell', install_config) shell_full_path = self.fs.path_separator + 'work' + self.fs.path_separator + 'dist' + self.fs.path_separator + 'nut_shell.zip' # Assert mock_upload_environment_zip_file.assert_called_once_with(shell_full_path)
def readall(config_path, mark_defaults=None): """ reads configuration from given file and adds missing keys with their defaults """ config_data = None if os.path.exists(config_path): with open(config_path, mode="r") as conf_file: config_data = yaml.safe_load(conf_file) if not config_data or INSTALL not in config_data: config_data = {INSTALL: dict()} mark_defaults_f = Configuration._mark_defaults install_cfg_def = dict( (k, mark_defaults_f(v, mark_defaults)) for k, v in InstallConfig.get_default().__dict__.items()) sf_cfg_def = dict( (k, mark_defaults_f(v, mark_defaults)) for k, v in ShellFoundrySettings.get_default().__dict__.items()) all_cfg = dict(install_cfg_def) all_cfg.update(sf_cfg_def) all_cfg.update(config_data[INSTALL]) return {INSTALL: all_cfg}
def test_non_encrypted_password_field_stays_regular(self): config = InstallConfig("localhost", 9000, "YOUR_USERNAME", "admin", "Global", "author", "online_mode", "template_location") self.assertEqual("admin", config.password)
def test_encrypted_password_field_becomes_decrypted(self): config = InstallConfig("localhost", 9000, "YOUR_USERNAME", "DAUOAQc=", "Global", "author", "online_mode", "template_location") self.assertEqual("admin", config.password)
def test_non_encrypted_password_field_stays_regular(self): config = InstallConfig('localhost', 9000, 'YOUR_USERNAME', 'admin', 'Global') self.assertEqual("admin", config.password)
def test_two_instances_should_be_equal(self): config1 = InstallConfig('localhost', 9000, 'YOUR_USERNAME', 'YOUR_PASSWORD', 'Global') config2 = InstallConfig('localhost', 9000, 'YOUR_USERNAME', 'YOUR_PASSWORD', 'Global') self.assertEqual(config1, config2)
def get_defaults(self): return InstallConfig.get_default()
def test_encrypted_password_field_becomes_decrypted(self): config = InstallConfig('localhost', 9000, 'YOUR_USERNAME', 'DAUOAQc=', 'Global') self.assertEqual("admin", config.password)