Beispiel #1
0
def test_arg2_with_entry_for_new_env_which_is_NOT_set_RAISES():
    with raises(UnexpectedEnvVarError) as exc:
        with ScopedEnvVars({}, {"ENV_VAR": "bye"}):
            pass

    assert exc.value.expected() == {"ENV_VAR": "bye"}
    assert exc.value.actual() == {}
def test_load_user_data_returns_object_from_file(mocker, fs):
    set_env_vars = {'CDB_USER_DATA': '/some/random/file.json'}
    with ScopedEnvVars({}, set_env_vars):
        os.environ['CDB_USER_DATA'] = "/some/random/file.json"
        fs.create_file("/some/random/file.json",
                       contents='{"super_cool": "user data"}')
        user_data = load_user_data()
        assert user_data == {"super_cool": "user data"}
Beispiel #3
0
def test_arg2_with_entry_for_new_env_var_which_is_set_to_a_DIFFERENT_value_RAISES(
):
    with raises(UnexpectedEnvVarError) as exc:
        with ScopedEnvVars({}, {"ENV_VAR": "bye"}):
            os.environ["ENV_VAR"] = "hello"

    assert exc.value.expected() == {"ENV_VAR": "bye"}
    assert exc.value.actual() == {"ENV_VAR": "hello"}
Beispiel #4
0
def test_arg1_env_vars_that_already_exist_RAISE():
    os.environ["EXISTING_ENV_VAR"] = "Wonderland"

    env = {"EXISTING_ENV_VAR": "Adventures"}
    with raises(AlreadyExistingEnvVarError) as exc:
        with ScopedEnvVars(env):
            pass

    assert exc.value.env_vars() == {"EXISTING_ENV_VAR": "Wonderland"}
Beispiel #5
0
def test_existing_env_vars_are_restored_when_the_with_statement_exits():
    os.environ["EXISTING_ENV_VAR"] = "Alice"
    os.environ["AND_ANOTHER"] = "Wonderland"

    with ScopedEnvVars({}):
        os.environ["EXISTING_ENV_VAR"] = "Different"
        os.environ["AND_ANOTHER"] = "Different"

    assert os.getenv("EXISTING_ENV_VAR") == "Alice"
    assert os.getenv("AND_ANOTHER") == "Wonderland"
Beispiel #6
0
def test_arg2_with_NO_entry_for_new_env_var_RAISES():
    with raises(UnexpectedEnvVarError) as exc:
        with ScopedEnvVars({}, {"IN_ARG2": "wibble"}):
            os.environ["ENV_VAR_NOT_IN_ARG2"] = "XXXX"
            os.environ["IN_ARG2"] = "wibble"

    assert exc.value.expected() == {"IN_ARG2": "wibble"}
    assert exc.value.actual() == {
        "ENV_VAR_NOT_IN_ARG2": "XXXX",
        "IN_ARG2": "wibble"
    }
def test_503_exception_for_put_pipeline_main(capsys, mocker):
    host = 'http://test.compliancedb.com'
    url = host + '/api/v1/projects/merkely/'
    _, _, api_token = stub_http_503('PUT', 1 + MAX_RETRY_COUNT, url)
    env = {
        'CDB_HOST': host,
        'CDB_API_TOKEN': 'random-api-token',
    }
    mocker.patch.object(sys, 'argv',
                        ['name', '--project', '/app/tests/data/pipefile.json'])

    with retry_backoff_factor(0.001), ScopedEnvVars(env):
        exit_code = main_put_pipeline()

    assert exit_code != 0
    verify_approval(capsys)
def test_503_exception_for_put_artifact_main(capsys, mocker):
    host = 'http://test.compliancedb.com'
    url = host + '/api/v1/projects/merkely/test-pipefile/artifacts/'
    _, _, api_token = stub_http_503('PUT', 1 + MAX_RETRY_COUNT, url)
    env = {
        'CDB_HOST': host,
        'CDB_API_TOKEN': 'random-api-token',
        'CDB_ARTIFACT_FILENAME': 'tests/data/coverage.txt'
    }
    mocker.patch.object(sys, 'argv',
                        ['name', '--project', '/app/tests/data/pipefile.json'])

    set_env_vars = {
        'CDB_ARTIFACT_SHA':
        'ccee89ccdc05772d90dc6929ad4f1fbc14aa105addf3326aa5cf575a104f51dc'
    }
    with retry_backoff_factor(0.001), ScopedEnvVars(env, set_env_vars):
        exit_code = main_put_artifact()

    assert exit_code != 0
    verify_approval(capsys)
def test_load_user_data_None_if_not_in_env(mocker):
    with ScopedEnvVars({}):
        assert not os.getenv('CDB_USER_DATA')
        user_data = load_user_data()
        assert user_data is None
Beispiel #10
0
def test_with_statements_as_var_is_env_with_arg1_vars_set():
    new_env = {"THE_AS_VARIABLE": "Salmo-the-leaper"}
    with ScopedEnvVars(new_env) as ev:
        assert ev.get("THE_AS_VARIABLE") == "Salmo-the-leaper"
Beispiel #11
0
def test_arg2_with_entry_whose_name_is_in_arg1_with_different_value():
    assert os.getenv("ENV_VAR") is None

    with ScopedEnvVars({"ENV_VAR": "a"}, {"ENV_VAR": "b"}):
        os.environ["ENV_VAR"] = "b"
Beispiel #12
0
def test_arg2_must_specify_ALL_new_env_vars_set_inside_the_with_statement():
    env = {"ENV_SET_INSIDE_WITH": "Humpty"}
    with ScopedEnvVars({}, env):
        os.environ["ENV_SET_INSIDE_WITH"] = "Humpty"
        assert os.getenv("ENV_SET_INSIDE_WITH") == "Humpty"
Beispiel #13
0
def test_arg1_env_vars_are_only_available_inside_the_with_statement():
    new_env = {"NEW_ENV_VAR": "Fishing"}
    with ScopedEnvVars(new_env):
        assert os.getenv("NEW_ENV_VAR") == "Fishing"

    assert os.getenv("NEW_ENV_VAR") is None