def test_yaml_safe_load_from_file(): with tempfile.NamedTemporaryFile(suffix=".yaml") as f: f.write(experiments.UnsafeYamlExperiment.encode("utf-8")) f.seek(0) with pytest.raises(InvalidSource): parse_experiment_from_file(f.name)
def switch_team_during_verification_run( source: str, # noqa: C901 settings: Settings) -> bool: """ Verification may be run in a different team than the active team the user selected. Rather than preventing the verification from running, try to switch to the appropriate team's context for the duration of this run. It's all in memory and not changed on disk. """ if not has_chaosiq_extension_configured(settings): logger.fatal( "Please signin to ChaosIQ services first with `$ chaos signin`") return False base_url = get_endpoint_url(settings) verify_tls = get_verify_tls(settings) default_org = get_default_org(settings) team = get_default_team(default_org) if not team: logger.fatal("Please select a default team with `$chaos team`") return False team_id = team["id"] token = get_auth_token(settings, base_url) if not token: logger.fatal( "Please signin to ChaosIQ services first with `$ chaos signin`") p = urlparse(source) if p.scheme.lower() in ["http", "https"]: r = requests.get(source, headers={"Authorization": "Bearer {}".format(token)}, verify=verify_tls) if r.status_code != 200: logger.fatal("Failed to retrieve verification at '{}': {}".format( source, r.text)) return False experiment = r.json() experiment_team_id = get_team_id(experiment) if experiment_team_id: team_id = experiment_team_id else: if not os.path.exists(p.path): raise InvalidSource('Path "{}" does not exist.'.format(source)) experiment = parse_experiment_from_file(source) experiment_team_id = get_team_id(experiment) if experiment_team_id: team_id = experiment_team_id if not team_id: logger.fatal( "Failed to lookup the team identifier from the verification. " "Are you trying to run a verification using an experiment you " "created manually? This is not possible right now unfortunately.") return False if team["id"] != team_id: team_url = urls.team(urls.org(urls.base(base_url), organization_id=default_org["id"]), team_id=team_id) r = request_team(team_url, token, verify_tls) if r.status_code != 200: logger.fatal("You cannot access the team owning this verification." "Please request them to join the team.") return False team = r.json() if default_org["id"] != team["org_id"]: logger.fatal( "You must be signed in to the appropriate organization to run " "this verification. Please run `$ chaos signin`.") return False logger.debug("Running a verification in a team different from the " "active one. Activating '{}' for this run.".format( team["name"])) set_default_team(default_org, { "id": team_id, "default": True, "name": team["name"] }) return True