def test_env_accessors_no_env(): """Sould all raise an exception.""" with pytest.raises(EnvError): delenv() with pytest.raises(EnvError): setenv() with pytest.raises(EnvError): getenv()
def test_aws_unsigned_subenv(gdalenv): """Create an Env with no AWS signing.""" with rasterio.Env( aws_access_key_id='id', aws_secret_access_key='key', aws_session_token='token', region_name='null-island-1'): with rasterio.env.Env(aws_unsigned=True): assert getenv()['AWS_NO_SIGN_REQUEST'] == 'YES' assert getenv().get('AWS_ACCESS_KEY_ID') is None
def test_azure_session_credentials(gdalenv): """Create an Env with azure session.""" azure_session = AzureSession(azure_storage_account='foo', azure_storage_access_key='bar') with rasterio.env.Env(session=azure_session) as s: s.credentialize() assert getenv()['AZURE_STORAGE_ACCOUNT'] == 'foo' assert getenv()['AZURE_STORAGE_ACCESS_KEY'] == 'bar'
def test_swift_session_credentials(gdalenv): """Create an Env with a oss session.""" swift_session = SwiftSession(swift_storage_url='foo', swift_auth_token='bar') with rasterio.env.Env(session=swift_session) as s: s.credentialize() assert getenv()['SWIFT_STORAGE_URL'] == 'foo' assert getenv()['SWIFT_AUTH_TOKEN'] == 'bar'
def test_env_options(gdalenv): """Test env options.""" env = rasterio.Env(foo='x') assert env.options == {'foo': 'x'} assert not env.previous_options assert getenv() == rasterio.env._env.options == {} with env: assert getenv() == rasterio.env._env.options == {'foo': 'x'} assert getenv() == rasterio.env._env.options == {}
def test_oss_session_credentials(gdalenv): """Create an Env with a oss session.""" oss_session = OSSSession(oss_access_key_id='id', oss_secret_access_key='key', oss_endpoint='null-island-1') with rasterio.env.Env(session=oss_session): assert getenv()['OSS_ACCESS_KEY_ID'] == 'id' assert getenv()['OSS_SECRET_ACCESS_KEY'] == 'key' assert getenv()['OSS_ENDPOINT'] == 'null-island-1'
def test_swift_session_credentials(gdalenv): """Create an Env with a oss session.""" swift_session = SwiftSession( swift_storage_url='foo', swift_auth_token='bar') with rasterio.env.Env(session=swift_session) as s: s.credentialize() assert getenv()['SWIFT_STORAGE_URL'] == 'foo' assert getenv()['SWIFT_AUTH_TOKEN'] == 'bar'
def test_aws_unsigned_subenv(gdalenv): """Create an Env with no AWS signing.""" with rasterio.Env( aws_access_key_id='id', aws_secret_access_key='key', aws_session_token='token', region_name='null-island-1'): with rasterio.env.Env(aws_unsigned=True) as inner: inner.credentialize() assert getenv()['AWS_NO_SIGN_REQUEST'] == 'YES' assert getenv().get('AWS_ACCESS_KEY_ID') is None
def test_env_options(gdalenv): """Test env options.""" env = Env(foo='x') assert env.options == {'foo': 'x'} assert not env.previous_options assert getenv() == rasterio.env._env.options == {} with env: assert getenv() == rasterio.env._env.options == {'foo': 'x'} assert getenv() == rasterio.env._env.options == {}
def test_with_aws_session_credentials(gdalenv): """Create an Env with a boto3 session.""" with Env(aws_access_key_id='id', aws_secret_access_key='key', aws_session_token='token', region_name='null-island-1') as s: assert getenv() == rasterio.env._env.options == {} s.get_aws_credentials() assert getenv() == rasterio.env._env.options == { 'aws_access_key_id': 'id', 'aws_region': 'null-island-1', 'aws_secret_access_key': 'key', 'aws_session_token': 'token'}
def test_aws_session_credentials(gdalenv): """Create an Env with a boto3 session.""" aws_session = boto3.Session( aws_access_key_id='id', aws_secret_access_key='key', aws_session_token='token', region_name='null-island-1') with rasterio.env.Env(session=aws_session): assert getenv()['AWS_ACCESS_KEY_ID'] == 'id' assert getenv()['AWS_REGION'] == 'null-island-1' assert getenv()['AWS_SECRET_ACCESS_KEY'] == 'key' assert getenv()['AWS_SESSION_TOKEN'] == 'token'
def test_aws_session_credentials(gdalenv): """Create an Env with a boto3 session.""" aws_session = boto3.Session( aws_access_key_id='id', aws_secret_access_key='key', aws_session_token='token', region_name='null-island-1') with rasterio.env.Env(aws_session=aws_session) as s: s.get_aws_credentials() assert getenv()['aws_access_key_id'] == 'id' assert getenv()['aws_region'] == 'null-island-1' assert getenv()['aws_secret_access_key'] == 'key' assert getenv()['aws_session_token'] == 'token'
def test_oss_session_credentials(gdalenv): """Create an Env with a oss session.""" oss_session = OSSSession( oss_access_key_id='id', oss_secret_access_key='key', oss_endpoint='null-island-1') with rasterio.env.Env(session=oss_session) as s: s.credentialize() assert getenv()['OSS_ACCESS_KEY_ID'] == 'id' assert getenv()['OSS_SECRET_ACCESS_KEY'] == 'key' assert getenv()['OSS_ENDPOINT'] == 'null-island-1'
def test_aws_session_credentials(gdalenv): """Create an Env with a boto3 session.""" aws_session = boto3.Session( aws_access_key_id='id', aws_secret_access_key='key', aws_session_token='token', region_name='null-island-1') with rasterio.env.Env(session=aws_session) as s: s.credentialize() assert getenv()['AWS_ACCESS_KEY_ID'] == 'id' assert getenv()['AWS_REGION'] == 'null-island-1' assert getenv()['AWS_SECRET_ACCESS_KEY'] == 'key' assert getenv()['AWS_SESSION_TOKEN'] == 'token'
def test_with_aws_session_credentials(gdalenv): """Create an Env with a boto3 session.""" with rasterio.Env( aws_access_key_id='id', aws_secret_access_key='key', aws_session_token='token', region_name='null-island-1') as s: expected = default_options.copy() assert getenv() == rasterio.env._env.options == expected s.get_aws_credentials() expected.update({ 'aws_access_key_id': 'id', 'aws_region': 'null-island-1', 'aws_secret_access_key': 'key', 'aws_session_token': 'token'}) assert getenv() == rasterio.env._env.options == expected
def test_with_aws_session_credentials(gdalenv): """Create an Env with a boto3 session.""" with rasterio.Env( aws_access_key_id='id', aws_secret_access_key='key', aws_session_token='token', region_name='null-island-1') as s: expected = default_options.copy() assert getenv() == rasterio.env.local._env.options == expected s.get_aws_credentials() expected.update({ 'aws_access_key_id': 'id', 'aws_region': 'null-island-1', 'aws_secret_access_key': 'key', 'aws_session_token': 'token'}) assert getenv() == rasterio.env.local._env.options == expected
def test_env_options(gdalenv): """Test env options.""" env = rasterio.Env(foo='x') assert env.options == {'foo': 'x'} assert not env.previous_options expected = default_options.copy() assert getenv() == rasterio.env._env.options == expected expected.update({'foo': 'x'}) with env: assert getenv() == rasterio.env._env.options == expected del expected['foo'] assert getenv() == rasterio.env._env.options == expected
def test_with_aws_session_credentials(gdalenv): """Create an Env with a boto3 session.""" with rasterio.Env(aws_access_key_id='id', aws_secret_access_key='key', aws_session_token='token', region_name='null-island-1') as s: assert getenv() == rasterio.env._env.options == {} s.get_aws_credentials() assert getenv() == rasterio.env._env.options == { 'aws_access_key_id': 'id', 'aws_region': 'null-island-1', 'aws_secret_access_key': 'key', 'aws_session_token': 'token' }
def test_env_accessors(gdalenv): """High level GDAL env access.""" defenv() setenv(foo='1', bar='2') expected = default_options.copy() expected.update({'foo': '1', 'bar': '2'}) assert getenv() == rasterio.env._env.options assert getenv() == expected assert get_gdal_config('foo') == '1' assert get_gdal_config('bar') == '2' delenv() with pytest.raises(EnvError): getenv() assert get_gdal_config('foo') is None assert get_gdal_config('bar') is None
def test_session_env_lazy(monkeypatch, gdalenv): """Create an Env with AWS env vars.""" monkeypatch.setenv('AWS_ACCESS_KEY_ID', 'id') monkeypatch.setenv('AWS_SECRET_ACCESS_KEY', 'key') monkeypatch.setenv('AWS_SESSION_TOKEN', 'token') with rasterio.Env(): assert getenv() == rasterio.env.local._env.options expected = { 'AWS_ACCESS_KEY_ID': 'id', 'AWS_SECRET_ACCESS_KEY': 'key', 'AWS_SESSION_TOKEN': 'token'} for k, v in expected.items(): assert getenv()[k] == v monkeypatch.undo()
def test_env_accessors(gdalenv): """High level GDAL env access.""" defenv() setenv(foo='1', bar='2') expected = default_options.copy() expected.update({'foo': '1', 'bar': '2'}) assert getenv() == rasterio.env.local._env.options assert getenv() == expected assert get_gdal_config('foo') == 1 assert get_gdal_config('bar') == 2 delenv() with pytest.raises(EnvError): getenv() assert get_gdal_config('foo') is None assert get_gdal_config('bar') is None
def test_swift_session_by_user_key(): def mock_init(self, session=None, swift_storage_url=None, swift_auth_token=None, swift_auth_v1_url=None, swift_user=None, swift_key=None): self._creds = {'SWIFT_STORAGE_URL':'foo', 'SWIFT_AUTH_TOKEN':'bar'} with mock.patch('rasterio.session.SwiftSession.__init__', new=mock_init): swift_session = SwiftSession( swift_auth_v1_url='foo', swift_user='******', swift_key='key') with rasterio.env.Env(session=swift_session) as s: s.credentialize() assert getenv()['SWIFT_STORAGE_URL'] == 'foo' assert getenv()['SWIFT_AUTH_TOKEN'] == 'bar'
def test_ensure_env_decorator(gdalenv): def f(): return getenv()['WITH_RASTERIO_ENV'] wrapper = ensure_env(f) assert wrapper() is True assert 'WITH_RASTERIO_ENV' not in getenv()
def test_session_env_lazy(monkeypatch, gdalenv): """Create an Env with AWS env vars.""" monkeypatch.setenv('AWS_ACCESS_KEY_ID', 'id') monkeypatch.setenv('AWS_SECRET_ACCESS_KEY', 'key') monkeypatch.setenv('AWS_SESSION_TOKEN', 'token') with Env() as s: s.get_aws_credentials() assert getenv() == rasterio.env._env.options expected = { 'aws_access_key_id': 'id', 'aws_secret_access_key': 'key', 'aws_session_token': 'token'} for k, v in expected.items(): assert getenv()[k] == v monkeypatch.undo()
def test_session_env_lazy(monkeypatch, gdalenv): """Create an Env with AWS env vars.""" monkeypatch.setenv('AWS_ACCESS_KEY_ID', 'id') monkeypatch.setenv('AWS_SECRET_ACCESS_KEY', 'key') monkeypatch.setenv('AWS_SESSION_TOKEN', 'token') with rasterio.Env() as s: s.get_aws_credentials() assert getenv() == rasterio.env._env.options expected = { 'aws_access_key_id': 'id', 'aws_secret_access_key': 'key', 'aws_session_token': 'token'} for k, v in expected.items(): assert getenv()[k] == v monkeypatch.undo()
def test_session_env_lazy(monkeypatch, gdalenv): """Create an Env with AWS env vars.""" monkeypatch.setenv('AWS_ACCESS_KEY_ID', 'id') monkeypatch.setenv('AWS_SECRET_ACCESS_KEY', 'key') monkeypatch.setenv('AWS_SESSION_TOKEN', 'token') with rasterio.Env() as s: s.credentialize() assert getenv() == rasterio.env.local._env.options expected = { 'AWS_ACCESS_KEY_ID': 'id', 'AWS_SECRET_ACCESS_KEY': 'key', 'AWS_SESSION_TOKEN': 'token'} for k, v in expected.items(): assert getenv()[k] == v monkeypatch.undo()
def test_nested_credentials(monkeypatch): """Check that rasterio.open() doesn't wipe out surrounding credentials""" @ensure_env_credentialled def fake_opener(path): return getenv() with rasterio.Env(session=AWSSession(aws_access_key_id='foo', aws_secret_access_key='bar')): assert getenv()['AWS_ACCESS_KEY_ID'] == 'foo' assert getenv()['AWS_SECRET_ACCESS_KEY'] == 'bar' monkeypatch.setenv('AWS_ACCESS_KEY_ID', 'lol') monkeypatch.setenv('AWS_SECRET_ACCESS_KEY', 'wut') gdalenv = fake_opener('s3://foo/bar') assert gdalenv['AWS_ACCESS_KEY_ID'] == 'foo' assert gdalenv['AWS_SECRET_ACCESS_KEY'] == 'bar'
def test_env_accessors(gdalenv): """High level GDAL env access.""" defenv() setenv(foo='1', bar='2') expected = {'foo': '1', 'bar': '2'} items = getenv() assert items == rasterio.env.local._env.options # Comparison below requires removal of GDAL_DATA. items.pop('GDAL_DATA', None) assert items == expected assert get_gdal_config('foo') == 1 assert get_gdal_config('bar') == 2 delenv() with pytest.raises(EnvError): getenv() assert get_gdal_config('foo') is None assert get_gdal_config('bar') is None
def test_azure_session_credentials_connection_string(gdalenv): """Create an Env with azure session.""" azure_session = AzureSession( azure_storage_connection_string='AccountName=myaccount;AccountKey=MY_ACCOUNT_KEY', ) with rasterio.env.Env(session=azure_session) as s: s.credentialize() assert getenv()['AZURE_STORAGE_CONNECTION_STRING'] == 'AccountName=myaccount;AccountKey=MY_ACCOUNT_KEY'
def test_env_accessors(gdalenv): """High level GDAL env access.""" defenv() setenv(foo='1', bar='2') assert getenv() == rasterio.env._env.options == {'foo': '1', 'bar': '2'} assert get_gdal_config('foo') == '1' assert get_gdal_config('bar') == '2' delenv() assert getenv() == rasterio.env._env.options == {} assert get_gdal_config('foo') is None assert get_gdal_config('bar') is None rasterio.env._env = None with pytest.raises(EnvError): delenv() with pytest.raises(EnvError): setenv() with pytest.raises(EnvError): getenv()
def test_env_accessors(gdalenv): """High level GDAL env access""" defenv() setenv(foo='1', bar='2') assert getenv() == rasterio.env._env.options == {'foo': '1', 'bar': '2'} assert get_gdal_config('foo') == '1' assert get_gdal_config('bar') == '2' delenv() assert getenv() == rasterio.env._env.options == {} assert get_gdal_config('foo') is None assert get_gdal_config('bar') is None rasterio.env._env = None with pytest.raises(EnvError): delenv() with pytest.raises(EnvError): setenv() with pytest.raises(EnvError): getenv()
def test_with_aws_session_credentials(gdalenv): """Create an Env with a boto3 session.""" env = rasterio.Env( aws_access_key_id='id', aws_secret_access_key='key', aws_session_token='token', region_name='null-island-1') expected = default_options.copy() with env: expected.update({ 'AWS_ACCESS_KEY_ID': 'id', 'AWS_REGION': 'null-island-1', 'AWS_SECRET_ACCESS_KEY': 'key', 'AWS_SESSION_TOKEN': 'token'}) assert getenv() == expected
def test_with_aws_session_credentials(gdalenv): """Create an Env with a boto3 session.""" env = rasterio.Env( aws_access_key_id='id', aws_secret_access_key='key', aws_session_token='token', region_name='null-island-1') with env: expected = { 'AWS_ACCESS_KEY_ID': 'id', 'AWS_REGION': 'null-island-1', 'AWS_SECRET_ACCESS_KEY': 'key', 'AWS_SESSION_TOKEN': 'token'} items = getenv() # Comparison below requires removal of GDAL_DATA. items.pop('GDAL_DATA', None) assert items == expected
def test_with_aws_session_credentials(gdalenv): """Create an Env with a boto3 session.""" env = rasterio.Env(aws_access_key_id='id', aws_secret_access_key='key', aws_session_token='token', region_name='null-island-1') expected = default_options.copy() with env: expected.update({ 'AWS_ACCESS_KEY_ID': 'id', 'AWS_REGION': 'null-island-1', 'AWS_SECRET_ACCESS_KEY': 'key', 'AWS_SESSION_TOKEN': 'token' }) assert getenv() == expected
def f(): return getenv()['GDAL_DATA']
def f(): return getenv()['WITH_RASTERIO_ENV']
def test_aws_unsigned(gdalenv): """Create an Env with no AWS signing.""" with rasterio.env.Env(aws_unsigned=True) as s: s.credentialize() assert getenv()['AWS_NO_SIGN_REQUEST'] == 'YES' assert getenv().get('AWS_ACCESS_KEY_ID') is None
def f(): return getenv()['RASTERIO_ENV']
def f(path): return getenv()
def fake_opener(path): return getenv()
def f(fp): return getenv()