예제 #1
0
def test_read_aws_credentials_envvar(monkeypatch):
    """Test read_aws_credentials() if through environment variables.

    read_aws_credentials() is challenging to test because we not only test the
    presence of an environment variable, but also the absence. The delvar
    monkeypatch method only removes the monkeypatched var; an actual
    environment variable of the same name can still leak through. Instead we
    put the logic of omitting variables into a function that replaces
    os.getenv.
    """
    v = {'LTD_MASON_AWS_ID': 'key-id',
         'LTD_MASON_AWS_SECRET': 'key-secret',
         'LTD_MASON_AWS_PROFILE': 'aws-profile'}
    missing = ('LTD_MASON_AWS_PROFILE',)

    def patch_envar(x):
        if x in missing:
            return None
        else:
            return v[x]

    monkeypatch.setattr(os, 'getenv', patch_envar)

    c = read_aws_credentials()
    print(c)
    assert c['aws_access_key_id'] == v['LTD_MASON_AWS_ID']
    assert c['aws_secret_access_key'] == v['LTD_MASON_AWS_SECRET']
    assert 'aws_profile' not in c
예제 #2
0
def test_read_aws_credentials_envvar(monkeypatch):
    """Test read_aws_credentials() if through environment variables.

    read_aws_credentials() is challenging to test because we not only test the
    presence of an environment variable, but also the absence. The delvar
    monkeypatch method only removes the monkeypatched var; an actual
    environment variable of the same name can still leak through. Instead we
    put the logic of omitting variables into a function that replaces
    os.getenv.
    """
    v = {
        'LTD_MASON_AWS_ID': 'key-id',
        'LTD_MASON_AWS_SECRET': 'key-secret',
        'LTD_MASON_AWS_PROFILE': 'aws-profile'
    }
    missing = ('LTD_MASON_AWS_PROFILE', )

    def patch_envar(x):
        if x in missing:
            return None
        else:
            return v[x]

    monkeypatch.setattr(os, 'getenv', patch_envar)

    c = read_aws_credentials()
    print(c)
    assert c['aws_access_key_id'] == v['LTD_MASON_AWS_ID']
    assert c['aws_secret_access_key'] == v['LTD_MASON_AWS_SECRET']
    assert 'aws_profile' not in c
예제 #3
0
def test_read_aws_credentials_fallback(monkeypatch, missing):
    """Test read_aws_credentials() if environment variables are missing,
    which causes a fallback to assume the user has default configs for boto3.
    """
    v = {'LTD_MASON_AWS_ID': 'key-id',
         'LTD_MASON_AWS_SECRET': 'key-secret',
         'LTD_MASON_AWS_PROFILE': 'aws-profile'}

    def patch_envar(x):
        if x in missing:
            return None
        else:
            return v[x]

    monkeypatch.setattr(os, 'getenv', patch_envar)

    c = read_aws_credentials()
    print(c)
    assert len(c) == 0
예제 #4
0
def test_read_aws_credentials_profile(monkeypatch):
    """Test read_aws_credentials() if through a profile environment variable.
    """
    v = {'LTD_MASON_AWS_ID': 'key-id',
         'LTD_MASON_AWS_SECRET': 'key-secret',
         'LTD_MASON_AWS_PROFILE': 'aws-profile'}
    missing = ('LTD_MASON_AWS_ID', 'LTD_MASON_AWS_SECRET')

    def patch_envar(x):
        if x in missing:
            return None
        else:
            return v[x]

    monkeypatch.setattr(os, 'getenv', patch_envar)

    c = read_aws_credentials()
    print(c)
    assert c['aws_profile'] == v['LTD_MASON_AWS_PROFILE']
    assert 'aws_access_key_id' not in c
    assert 'aws_secret_access_key' not in c
예제 #5
0
def test_read_aws_credentials_fallback(monkeypatch, missing):
    """Test read_aws_credentials() if environment variables are missing,
    which causes a fallback to assume the user has default configs for boto3.
    """
    v = {
        'LTD_MASON_AWS_ID': 'key-id',
        'LTD_MASON_AWS_SECRET': 'key-secret',
        'LTD_MASON_AWS_PROFILE': 'aws-profile'
    }

    def patch_envar(x):
        if x in missing:
            return None
        else:
            return v[x]

    monkeypatch.setattr(os, 'getenv', patch_envar)

    c = read_aws_credentials()
    print(c)
    assert len(c) == 0
예제 #6
0
def test_read_aws_credentials_profile(monkeypatch):
    """Test read_aws_credentials() if through a profile environment variable.
    """
    v = {
        'LTD_MASON_AWS_ID': 'key-id',
        'LTD_MASON_AWS_SECRET': 'key-secret',
        'LTD_MASON_AWS_PROFILE': 'aws-profile'
    }
    missing = ('LTD_MASON_AWS_ID', 'LTD_MASON_AWS_SECRET')

    def patch_envar(x):
        if x in missing:
            return None
        else:
            return v[x]

    monkeypatch.setattr(os, 'getenv', patch_envar)

    c = read_aws_credentials()
    print(c)
    assert c['aws_profile'] == v['LTD_MASON_AWS_PROFILE']
    assert 'aws_access_key_id' not in c
    assert 'aws_secret_access_key' not in c