def test_replacements_invalid_list_items(self): test_file = self._create_file( "previewConfig: { replace: [ invalid ] }") config = GitOpsConfig(test_file) with pytest.raises(GitOpsException) as ex: config.replacements # pylint: disable=pointless-statement self.assertEqual( str(ex.value), "Every item in 'previewConfig.replace' should have a 'path' and and 'variabe'!" ) test_file = self._create_file( "previewConfig: { replace: [ { variable: v } ] }") config = GitOpsConfig(test_file) with pytest.raises(GitOpsException) as ex: config.replacements # pylint: disable=pointless-statement self.assertEqual( str(ex.value), "Every item in 'previewConfig.replace' should have a 'path' and and 'variabe'!" ) test_file = self._create_file( "previewConfig: { replace: [ { path: p } ] }") config = GitOpsConfig(test_file) with pytest.raises(GitOpsException) as ex: config.replacements # pylint: disable=pointless-statement self.assertEqual( str(ex.value), "Every item in 'previewConfig.replace' should have a 'path' and and 'variabe'!" )
def test_team_config_repo_missing(self): test_file = self._create_file("deploymentConfig: { }") config = GitOpsConfig(test_file) with pytest.raises(GitOpsException) as ex: config.team_config_repo # pylint: disable=pointless-statement self.assertEqual( str(ex.value), "Key 'deploymentConfig.repository' not found in GitOps config!")
def test_team_config_org_not_a_string(self): test_file = self._create_file("deploymentConfig: { org: [] }") config = GitOpsConfig(test_file) with pytest.raises(GitOpsException) as ex: config.team_config_org # pylint: disable=pointless-statement self.assertEqual( str(ex.value), "Key 'deploymentConfig.org' should be a string in GitOps config!")
def test_replacements_missing(self): test_file = self._create_file("previewConfig: { }") config = GitOpsConfig(test_file) with pytest.raises(GitOpsException) as ex: config.replacements # pylint: disable=pointless-statement self.assertEqual( str(ex.value), "Key 'previewConfig.replace' not found in GitOps config!")
def test_replacements_not_a_list(self): test_file = self._create_file("previewConfig: { replace: 1 }") config = GitOpsConfig(test_file) with pytest.raises(GitOpsException) as ex: config.replacements # pylint: disable=pointless-statement self.assertEqual( str(ex.value), "Key 'previewConfig.replace' should be a list in GitOps config!")
def test_application_name_not_a_string(self): test_file = self._create_file( "deploymentConfig: { applicationName: [] }") config = GitOpsConfig(test_file) with pytest.raises(GitOpsException) as ex: config.application_name # pylint: disable=pointless-statement self.assertEqual( str(ex.value), "Key 'deploymentConfig.applicationName' should be a string in GitOps config!" )
def test_route_host_not_a_string(self): test_file = self._create_file( "previewConfig: { route: { host: { template: 1 } } }") config = GitOpsConfig(test_file) with pytest.raises(GitOpsException) as ex: config.route_host # pylint: disable=pointless-statement self.assertEqual( str(ex.value), "Key 'previewConfig.route.host.template' should be a string in GitOps config!" )
def delete_preview_command( command, username, password, git_user, git_email, organisation, repository_name, git_provider, git_provider_url, preview_id, ): assert command is not None apps_tmp_dir = create_tmp_dir() root_tmp_dir = create_tmp_dir() try: apps_git = create_git( username, password, git_user, git_email, organisation, repository_name, git_provider, git_provider_url, apps_tmp_dir, ) apps_git.checkout("master") logging.info("App repo branch master checkout successful") try: gitops_config = GitOpsConfig( apps_git.get_full_file_path(".gitops.config.yaml")) except FileNotFoundError as ex: raise GitOpsException(f"Couldn't find .gitops.config.yaml") from ex logging.info("Read GitOpsConfig: %s", gitops_config) root_git = create_git( username, password, git_user, git_email, gitops_config.team_config_org, gitops_config.team_config_repo, git_provider, git_provider_url, root_tmp_dir, ) root_git.checkout("master") logging.info("Config repo branch master checkout successful") config_branch = "master" hashed_preview_id = hashlib.sha256( preview_id.encode("utf-8")).hexdigest()[:8] preview_folder_name = gitops_config.application_name + "-" + hashed_preview_id + "-preview" logging.info("Preview folder name: %s", preview_folder_name) branch_preview_env_exists = os.path.exists( root_git.get_full_file_path(preview_folder_name)) logging.info("Is preview env already existing for branch? %s", branch_preview_env_exists) if branch_preview_env_exists: shutil.rmtree(root_git.get_full_file_path(preview_folder_name), ignore_errors=True) else: raise GitOpsException( f"There was no preview with name: {preview_folder_name}") root_git.commit( f"Delete preview environment for '{gitops_config.application_name}' and preview id '{preview_id}'." ) root_git.push(config_branch) logging.info("Pushed branch %s", config_branch) finally: delete_tmp_dir(apps_tmp_dir) delete_tmp_dir(root_tmp_dir)
def test_team_config_repo(self): test_file = self._create_file( "deploymentConfig: { repository: my-repo }") config = GitOpsConfig(test_file) self.assertEqual(config.team_config_repo, "my-repo")
def test_team_config_org(self): test_file = self._create_file("deploymentConfig: { org: my-org }") config = GitOpsConfig(test_file) self.assertEqual(config.team_config_org, "my-org")
def test_application_name(self): test_file = self._create_file( "deploymentConfig: { applicationName: my-app }") config = GitOpsConfig(test_file) self.assertEqual(config.application_name, "my-app")
def test_replacements(self): test_file = self._create_file( "previewConfig: { replace: [ { path: p, variable: v } ] }") config = GitOpsConfig(test_file) self.assertEqual(config.replacements, [{"path": "p", "variable": "v"}])
def test_route_host(self): test_file = self._create_file( "previewConfig: { route: { host: { template: my-host-template } } }" ) config = GitOpsConfig(test_file) self.assertEqual(config.route_host, "my-host-template")
def delete_pr_preview_command( command, branch, username, password, git_user, git_email, organisation, repository_name, git_provider, git_provider_url, ): assert command == "delete-pr-preview" apps_tmp_dir = create_tmp_dir() root_tmp_dir = create_tmp_dir() try: apps_git = create_git( username, password, git_user, git_email, organisation, repository_name, git_provider, git_provider_url, apps_tmp_dir, ) app_master_branch_name = "master" apps_git.checkout(app_master_branch_name) logging.info("App repo branch %s checkout successful", app_master_branch_name) try: gitops_config = GitOpsConfig( apps_git.get_full_file_path(".gitops.config.yaml")) except FileNotFoundError as ex: raise GitOpsException(f"Couldn't find .gitops.config.yaml") from ex logging.info("Read GitOpsConfig: %s", gitops_config) root_git = create_git( username, password, git_user, git_email, gitops_config.team_config_org, gitops_config.team_config_repo, git_provider, git_provider_url, root_tmp_dir, ) root_git.checkout("master") logging.info("Config repo branch master checkout successful") delete_preview_command( command, username, password, git_user, git_email, organisation, repository_name, git_provider, git_provider_url, branch, ) finally: delete_tmp_dir(apps_tmp_dir) delete_tmp_dir(root_tmp_dir)
def create_preview_command( command, username, password, git_user, git_email, organisation, repository_name, git_provider, git_provider_url, git_hash, preview_id, deployment_already_up_to_date_callback=None, deployment_exists_callback=None, deployment_new_callback=None, ): assert command is not None apps_tmp_dir = create_tmp_dir() root_tmp_dir = create_tmp_dir() try: apps_git = create_git( username, password, git_user, git_email, organisation, repository_name, git_provider, git_provider_url, apps_tmp_dir, ) apps_git.checkout(git_hash) logging.info("App repo git hash %s checkout successful", git_hash) try: gitops_config = GitOpsConfig( apps_git.get_full_file_path(".gitops.config.yaml")) except FileNotFoundError as ex: raise GitOpsException(f"Couldn't find .gitops.config.yaml") from ex logging.info("Read .gitops.config.yaml: %s", gitops_config) root_git = create_git( username, password, git_user, git_email, gitops_config.team_config_org, gitops_config.team_config_repo, git_provider, git_provider_url, root_tmp_dir, ) root_git.checkout("master") logging.info("Config repo branch master checkout successful") preview_template_folder_name = ".preview-templates/" + gitops_config.application_name if os.path.isdir( root_git.get_full_file_path(preview_template_folder_name)): logging.info("Using the preview template folder: %s", preview_template_folder_name) else: raise GitOpsException( f"The preview template folder does not exist: {preview_template_folder_name}" ) hashed_preview_id = hashlib.sha256( preview_id.encode("utf-8")).hexdigest()[:8] new_preview_folder_name = gitops_config.application_name + "-" + hashed_preview_id + "-preview" logging.info("New folder for preview: %s", new_preview_folder_name) preview_env_already_exist = os.path.isdir( root_git.get_full_file_path(new_preview_folder_name)) logging.info("Is preview env already existing? %s", preview_env_already_exist) if not preview_env_already_exist: __create_new_preview_env( git_hash, new_preview_folder_name, preview_template_folder_name, root_git, gitops_config.application_name, ) logging.info("Using image tag from git hash: %s", git_hash) route_host = None value_replaced = False for replacement in gitops_config.replacements: route_host, value_replaced = __replace_value( gitops_config, git_hash, new_preview_folder_name, replacement, root_git, route_host, hashed_preview_id, value_replaced, ) if not value_replaced: logging.info( "The image tag %s has already been deployed. Doing nothing.", git_hash) if deployment_already_up_to_date_callback: deployment_already_up_to_date_callback(apps_git, git_hash) return root_git.commit( f"Update preview environment for '{gitops_config.application_name}' and git hash '{git_hash}'." ) root_git.push("master") logging.info("Pushed branch master") if preview_env_already_exist: if deployment_exists_callback: deployment_exists_callback(apps_git, gitops_config, route_host) else: if deployment_new_callback: deployment_new_callback(apps_git, gitops_config, route_host) finally: delete_tmp_dir(apps_tmp_dir) delete_tmp_dir(root_tmp_dir)