Skip to content

Commit

Permalink
Standardize parsing of boolean environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
rouge8 committed Oct 29, 2016
1 parent f13edb8 commit 50484ca
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
5 changes: 4 additions & 1 deletion tests/gs_integration_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import os
import pytest

from wal_e.cmd import parse_boolean_envvar


MANGLE_SUFFIX = None

Expand All @@ -31,7 +33,8 @@ def no_real_gs_credentials():
Phrased in the negative to make it read better with 'skipif'.
"""
if os.getenv('WALE_GS_INTEGRATION_TESTS') != 'TRUE':
if parse_boolean_envvar(
os.getenv('WALE_GS_INTEGRATION_TESTS')) is not True:
return True

if os.getenv('GOOGLE_APPLICATION_CREDENTIALS') is None:
Expand Down
4 changes: 3 additions & 1 deletion tests/s3_integration_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from boto.s3.connection import Location
from wal_e.blobstore import s3
from wal_e.blobstore.s3 import calling_format
from wal_e.cmd import parse_boolean_envvar


def bucket_name_mangle(bn, delimiter='-'):
Expand All @@ -18,7 +19,8 @@ def no_real_s3_credentials():
Phrased in the negative to make it read better with 'skipif'.
"""
if os.getenv('WALE_S3_INTEGRATION_TESTS') != 'TRUE':
if parse_boolean_envvar(os.getenv(
'WALE_S3_INTEGRATION_TESTS')) is not True:
return True

for e_var in ('AWS_ACCESS_KEY_ID',
Expand Down
17 changes: 17 additions & 0 deletions tests/test_cmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest

from wal_e.cmd import parse_boolean_envvar


@pytest.mark.parametrize('val,expected', [
('1', True),
('TRUE', True),
('true', True),
(None, False),
('', False),
('0', False),
('FALSE', False),
('false', False),
])
def test_parse_boolean_envvar(val, expected):
assert parse_boolean_envvar(val) == expected
5 changes: 4 additions & 1 deletion tests/wabs_integration_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@

import os

from wal_e.cmd import parse_boolean_envvar


def no_real_wabs_credentials():
"""Helps skip integration tests without live credentials.
Phrased in the negative to make it read better with 'skipif'.
"""
if os.getenv('WALE_WABS_INTEGRATION_TESTS') != 'TRUE':
if parse_boolean_envvar(os.getenv(
'WALE_WABS_INTEGRATION_TESTS')) is not True:
return True

for e_var in ('WABS_ACCOUNT_NAME', 'WABS_ACCESS_KEY'):
Expand Down
12 changes: 11 additions & 1 deletion wal_e/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@ def extract_segment(text_with_extractable_segment):
return SegmentNumber(log=groupdict['log'], seg=groupdict['seg'])


def parse_boolean_envvar(val):
"""Parse a boolean environment variable."""
if not val or val.lower() in {'false', '0'}:
return False
elif val.lower() in {'true', '1'}:
return True
else:
raise ValueError('Invalid boolean environment variable: %s' % val)


def build_parser():
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
Expand Down Expand Up @@ -461,7 +471,7 @@ def configure_backup_cxt(args):
# 'operator.Backup' protocol.
if store.is_s3:
use_instance_profile = args.aws_instance_profile or \
os.getenv('AWS_INSTANCE_PROFILE', '').lower() in ('true', '1')
parse_boolean_envvar(os.getenv('AWS_INSTANCE_PROFILE'))
if use_instance_profile:
creds = s3_instance_profile()
else:
Expand Down

0 comments on commit 50484ca

Please sign in to comment.