def test_module_name_from_path(path, module_name): """ Get module name from path. If we are in /foo/bar/ directory the module name will be bar. If we are in the root directory - '/' - then the function should return the 'root' string. """ assert module_name_from_path(path) == module_name
def test_module_name_from_path_from_cur_dir(tmpdir): """ Get module name from current directory """ path = tmpdir.mkdir('foo') assert module_name_from_path(path) == 'foo'
def terraform_ci(**kwargs): """ Run Terraform action. The tool prepares environment, sets environment variables for API keys, passwords, roles etc. It then runs a terraform action which may be either plan or apply. ci-runner can be called in a CI environment or locally on a workstation. """ debug = kwargs["debug"] modules_path = kwargs["modules_path"] module_name = kwargs["module_name"] env_file = kwargs["env_file"] aws_assume_role_arn = kwargs["aws_assume_role_arn"] action = kwargs["action"] setup_logging(LOG, debug=debug) try: pull_request = not environ["TRAVIS_PULL_REQUEST"] == "false" except KeyError: pull_request = False try: setup_environment(env_file, role=aws_assume_role_arn) except FileNotFoundError: LOG.warning("Environment file %s doesn't exit", env_file) # module name is parent directory mod = module_name or module_name_from_path(modules_path) LOG.info("Processing module %s", mod) status = {mod: run_job(osp.join(modules_path), action)} outputs = terraform_output(osp.join(modules_path)) if "github_token" in outputs: LOG.info( "Setting GITHUB_TOKEN and TF_VAR_github_token environment variable from module outputs." ) environ["GITHUB_TOKEN"] = outputs["github_token"]["value"] environ["TF_VAR_github_token"] = outputs["github_token"]["value"] if status[mod]["success"]: LOG.info("%s success: %s", mod, status[mod]["success"]) else: LOG.error("Failed to process %s", mod) LOG.error("STDOUT: %s", status[mod]["stdout"].decode("utf-8")) LOG.error("STDERR: %s", status[mod]["stderr"].decode("utf-8")) sys.exit(EX_SOFTWARE) if pull_request: delete_outdated_comments(status, environ["TRAVIS_REPO_SLUG"], int(environ["TRAVIS_PULL_REQUEST"])) post_comment(comment=render_comment(status)) else: LOG.info("Standard output:") sys.stdout.write( convert_to_newlines(status[mod]["stdout"]) or "no output\n") LOG.info("Standard error output:") sys.stderr.write( convert_to_newlines(status[mod]["stderr"]) or "no output\n")