コード例 #1
0
def test_set_bearer_type_by_default_on_authorization(organizations):
    settings = {"auths": {"console.chaosiq.io": {"value": "blah"}}}
    with client_session(url=ENDPOINT,
                        organizations=organizations,
                        settings=settings) as s:
        assert "Authorization" in s.headers
        assert s.headers["Authorization"] == 'Bearer blah'
コード例 #2
0
def test_cannot_update_execution_with_invalid_execution_id(organizations,
                                                           default_org_id,
                                                           default_team_id):
    experiment_id = str(uuid.uuid4())
    x_id = str(uuid.uuid4())
    journal = {
        "experiment": {
            "extensions": [
                {
                    "name": "chaosiq",
                    "execution_id": x_id,
                    "experiment_id": experiment_id
                }
            ]
        }
    }
    with requests_mock.mock() as m:
        url = urls.full(
            urls.base(ENDPOINT), default_org_id, default_team_id,
            experiment_id, execution_id=x_id)
        m.put(
            url, status_code=404,
            headers={
                "content-type": "text/plain"
            }
        )
        with client_session(ENDPOINT, organizations) as s:
            r = publish_execution(s, journal)
            assert r.status_code == 404
コード例 #3
0
def test_execution_not_created_when_experiment_is_invalid_type(
                                                organizations, default_org_id,
                                                default_team_id):
    experiment_id = str(uuid.uuid4())
    # the remote endpoint cannot deal with anything but a experiment
    experiment = {
        "extensions": [
            {
                "name": "chaosiq",
                "experiment_id": experiment_id
            }
        ]
    }
    with requests_mock.mock() as m:
        url = urls.full(
            urls.base(ENDPOINT), default_org_id, default_team_id,
            experiment_id, with_executions=True)
        m.post(
            url, status_code=422, json=[
                {
                    "loc": ["a_dict"],
                    "msg": "value is not a valid dict",
                    "type": "type_error.dict"
                }
            ],
            headers={
                "content-type": "application/json"
            }
        )
        with client_session(ENDPOINT, organizations) as s:
            r = initialize_execution(s, experiment, {})
            assert r.status_code == 422
コード例 #4
0
def test_store_safeguards_to_journal(organizations, default_org_id,
                                     default_team_id):
    execution_id = str(uuid.uuid4())
    experiment_id = str(uuid.uuid4())
    safeguards = [{"name": "godzilla says stop"}]
    with requests_mock.mock() as m:
        url = urls.full(urls.base(ENDPOINT),
                        default_org_id,
                        default_team_id,
                        experiment_id,
                        execution_id=execution_id,
                        with_safeguards=True)
        m.get(url,
              status_code=200,
              json={
                  "allowed": False,
                  "policies": safeguards
              })

        extensions = [{
            "name": "chaosiq",
            "execution_id": execution_id,
            "experiment_id": experiment_id
        }]
        journal = {}
        with client_session(ENDPOINT, organizations) as s:
            with pytest.raises(InterruptExecution):
                is_allowed_to_continue(s, extensions)
            set_applied_safeguards_for_execution(extensions, journal)
            assert journal["extensions"][0]["safeguards"] == safeguards
コード例 #5
0
def test_cannot_create_execution_from_unknown_experiment_id(
                                                organizations, default_org_id,
                                                default_team_id):
    experiment_id = str(uuid.uuid4())
    experiment = {
        "title": "Hello there",
        "extensions": [
            {
                "name": "chaosiq",
                "experiment_id": experiment_id
            }
        ]
    }
    with requests_mock.mock() as m:
        url = urls.full(
            urls.base(ENDPOINT), default_org_id, default_team_id,
            experiment_id, with_executions=True)
        m.post(
            url, status_code=422,
            json=[],
            headers={
                "content-type": "application/json"
            }
        )
        with client_session(ENDPOINT, organizations) as s:
            r = initialize_execution(s, experiment, {})
            assert r.status_code == 422
            assert "execution_id" not in experiment["extensions"][0]
コード例 #6
0
def test_interrupt_experiment(organizations, default_org_id, default_team_id):
    execution_id = str(uuid.uuid4())
    experiment_id = str(uuid.uuid4())
    with requests_mock.mock() as m:
        url = urls.full(urls.base(ENDPOINT),
                        default_org_id,
                        default_team_id,
                        experiment_id,
                        execution_id=execution_id,
                        with_safeguards=True)
        m.get(url,
              status_code=200,
              json={
                  "allowed": False,
                  "policies": [{
                      "name": "godzilla says stop"
                  }]
              })
        with client_session(ENDPOINT, organizations) as s:
            with pytest.raises(InterruptExecution):
                is_allowed_to_continue(s, [{
                    "name": "chaosiq",
                    "execution_id": execution_id,
                    "experiment_id": experiment_id
                }])
コード例 #7
0
def test_create_execution(organizations, default_org_id, default_team_id):
    experiment_id = str(uuid.uuid4())
    x_id = str(uuid.uuid4())
    experiment = {
        "title": "Hello there",
        "extensions": [
            {
                "name": "chaosiq",
                "experiment_id": experiment_id
            }
        ]
    }
    with requests_mock.mock() as m:
        url = urls.full(
            urls.base(ENDPOINT), default_org_id, default_team_id,
            experiment_id, with_executions=True)
        m.post(
            url, status_code=201,
            json={
                "id": x_id,
            },
            headers={
                "content-type": "application/json",
                "content-location": "{}/{}".format(url, x_id)
            }
        )
        with client_session(ENDPOINT, organizations) as s:
            r = initialize_execution(s, experiment, {})
            assert r.status_code == 201
            # we injected the execution_id
            assert experiment["extensions"][0]["execution_id"] == x_id
コード例 #8
0
def test_publish_event(organizations, default_org_id, default_team_id):
    experiment_id = str(uuid.uuid4())
    x_id = str(uuid.uuid4())
    extensions = [
        {
            "name": "chaosiq",
            "execution_id": x_id,
            "experiment_id": experiment_id
        }
    ]
    activity = {}
    run = {}
    with requests_mock.mock() as m:
        url = urls.full(
            urls.base(ENDPOINT), default_org_id, default_team_id,
            experiment_id, x_id, with_events=True)
        m.post(url, status_code=201)
        with client_session(ENDPOINT, organizations) as s:
            publish_event(
                s, "start-experiment", activity, None, None, extensions, None,
                run)
            r = json.loads(m.last_request.body)
            assert r["specversion"] in ["0.3", "1.0"]
            assert r["datacontenttype"] == "application/json"
            assert r["type"] == "start-experiment"
            assert r["source"] == "chaosiq-cloud"
            assert "id" in r
            assert "time" in r
            assert "data" in r
コード例 #9
0
def test_experiment_not_created_when_unmodified(organizations, default_org_id,
                                                default_team_id):
    experiment = {"title": "hello"}
    with requests_mock.mock() as m:
        url = urls.full(urls.base(ENDPOINT),
                        default_org_id,
                        default_team_id,
                        with_experiments=True)
        m.post(url, status_code=204)
        with client_session(ENDPOINT, organizations) as s:
            r = publish_experiment(s, experiment)
            assert r.status_code == 204
コード例 #10
0
def test_cannot_create_experiment_on_requests_connection_timeout(
        organizations, default_org_id, default_team_id):
    experiment = {"title": "hello"}
    with requests_mock.mock() as m:
        url = urls.full(urls.base(ENDPOINT),
                        default_org_id,
                        default_team_id,
                        with_experiments=True)
        m.post(url, exc=requests.exceptions.ConnectTimeout)
        with client_session(ENDPOINT, organizations) as s:
            r = publish_experiment(s, experiment)
            assert r is None
コード例 #11
0
def test_set_authorization_from_settings(organizations):
    settings = {
        "auths": {
            "console.chaosiq.io": {
                "type": "digest",
                "value": "blah"
            }
        }
    }
    with client_session(url=ENDPOINT,
                        organizations=organizations,
                        settings=settings) as s:
        assert "Authorization" in s.headers
        assert s.headers["Authorization"] == 'Digest blah'
コード例 #12
0
def test_cannot_fetch_execution_non_published_experiment(organizations,
                                                         default_org_id,
                                                         default_team_id):
    experiment_id = str(uuid.uuid4())
    x_id = str(uuid.uuid4())
    journal = {
        "experiment": {}
    }
    with requests_mock.mock() as m:
        url = urls.full(
            urls.base(ENDPOINT), default_org_id, experiment_id,
            default_team_id, x_id)
        m.get(url, exc=requests.exceptions.ConnectTimeout)
        with client_session(ENDPOINT, organizations) as s:
            r = fetch_execution(s, journal)
            assert r is None
            assert m.call_count == 0
コード例 #13
0
def test_cannot_publish_event_non_published_execution(organizations,
                                                      default_org_id,
                                                      default_team_id):
    experiment_id = str(uuid.uuid4())
    x_id = str(uuid.uuid4())
    extensions = []
    activity = {}
    run = {}
    with requests_mock.mock() as m:
        url = urls.full(
            urls.base(ENDPOINT), default_org_id, experiment_id,
            default_team_id, x_id, with_events=True)
        m.post(url, status_code=201)
        with client_session(ENDPOINT, organizations) as s:
            publish_event(
                s, "start-experiment", activity, None, None, extensions, None,
                run)
            assert m.call_count == 0
コード例 #14
0
def test_create_experiment(organizations, default_org_id, default_team_id):
    x_id = str(uuid.uuid4())
    experiment = {"title": "hello"}
    with requests_mock.mock() as m:
        url = urls.full(urls.base(ENDPOINT),
                        default_org_id,
                        default_team_id,
                        with_experiments=True)
        m.post(url,
               status_code=201,
               json={"id": x_id},
               headers={
                   "content-type": "application/json",
                   "content-location": "{}/{}".format(url, x_id)
               })
        with client_session(ENDPOINT, organizations) as s:
            r = publish_experiment(s, experiment)
            assert r.status_code == 201
            assert r.headers["content-location"] == "{}/{}".format(url, x_id)
コード例 #15
0
def test_fetch_execution(organizations, default_org_id, default_team_id):
    experiment_id = str(uuid.uuid4())
    x_id = str(uuid.uuid4())
    journal = {
        "experiment": {
            "extensions": [
                {
                    "name": "chaosiq",
                    "execution_id": x_id,
                    "experiment_id": experiment_id
                }
            ]
        }
    }
    with requests_mock.mock() as m:
        url = urls.full(
            urls.base(ENDPOINT), default_org_id, default_team_id,
            experiment_id, x_id)
        m.get(url, json=journal)
        with client_session(ENDPOINT, organizations) as s:
            r = fetch_execution(s, journal)
            assert r.status_code == 200
コード例 #16
0
def test_cannot_update_execution_on_request_connection_timeout(
        organizations, default_org_id, default_team_id):
    experiment_id = str(uuid.uuid4())
    x_id = str(uuid.uuid4())
    journal = {
        "experiment": {
            "extensions": [
                {
                    "name": "chaosiq",
                    "execution_id": x_id,
                    "experiment_id": experiment_id
                }
            ]
        }
    }
    with requests_mock.mock() as m:
        url = urls.full(
            urls.base(ENDPOINT), default_org_id, experiment_id,
            default_team_id, x_id)
        m.put(url, exc=requests.exceptions.ConnectTimeout)
        with client_session(ENDPOINT, organizations) as s:
            r = publish_execution(s, journal)
            assert r is None
コード例 #17
0
def test_no_check_when_execution_is_not_found(organizations, default_org_id,
                                              default_team_id):
    execution_id = str(uuid.uuid4())
    experiment_id = str(uuid.uuid4())
    with requests_mock.mock() as m:
        url = urls.full(urls.base(ENDPOINT),
                        default_org_id,
                        default_team_id,
                        experiment_id,
                        execution_id=execution_id,
                        with_safeguards=True)
        m.get(url, status_code=404)
        try:
            with client_session(ENDPOINT, organizations) as s:
                is_allowed_to_continue(s, [{
                    "name": "chaosiq",
                    "execution_id": execution_id,
                    "experiment_id": experiment_id
                }])
        except InterruptExecution:
            pytest.fail(
                "Missing execution identifier in experiment should not "
                "lead to execution interruption")
コード例 #18
0
def test_cannot_create_execution_on_requests_connection_timeout(
                                                organizations, default_org_id,
                                                default_team_id):
    experiment_id = str(uuid.uuid4())
    experiment = {
        "title": "Hello there",
        "extensions": [
            {
                "name": "chaosiq",
                "experiment_id": experiment_id
            }
        ]
    }
    with requests_mock.mock() as m:
        url = urls.full(
            urls.base(ENDPOINT), default_org_id, default_team_id,
            experiment_id, with_executions=True)
        m.post(
            url,
            exc=requests.exceptions.ConnectTimeout
        )
        with client_session(ENDPOINT, organizations) as s:
            r = initialize_execution(s, experiment, {})
            assert r is None
コード例 #19
0
def test_do_not_set_auths_when_none_found_in_settings(organizations):
    with client_session(url=ENDPOINT, organizations=organizations) as s:
        assert "Authorization" not in s.headers