Repository: inhumantsar/python-ec2-reaper

Issue:
lambda function doesn't honor the debug option properly
passed `DEBUG: false` as an env var but it still does NO-OPs and debug logging. the env var is probably being read by python in as a string.

Discussion:

Patch:
diff --git a/ec2_reaper/aws_lambda.py b/ec2_reaper/aws_lambda.py
index e9fcdd2..e90645d 100644
--- a/ec2_reaper/aws_lambda.py
+++ b/ec2_reaper/aws_lambda.py
@@ -25,10 +25,15 @@ TAG_MATCHER = json.loads(TAG_MATCHER) if isinstance(TAG_MATCHER, strclasses) els
 SLACK_ENDPOINT = os.environ.get('SLACK_ENDPOINT', None)
 
 DEBUG = os.environ.get('DEBUG', True)
+log.debug('startup: got value for DEBUG: {} ({})'.format(DEBUG, type(DEBUG)))
+if isinstance(DEBUG, str):
+    DEBUG = False if DEBUG.lower() == 'false' else True
+
 if DEBUG:
     log.setLevel(logging.DEBUG)
     logging.getLogger('botocore').setLevel(logging.INFO)
     logging.getLogger('boto3').setLevel(logging.INFO)
+    log.debug('startup: debug logging on')
 else:
     log.setLevel(logging.INFO)
     logging.getLogger('botocore').setLevel(logging.WARNING)


Test patch:
diff --git a/tests/test_lambda_handler.py b/tests/test_lambda_handler.py
index bc153b4..7ad0968 100644
--- a/tests/test_lambda_handler.py
+++ b/tests/test_lambda_handler.py
@@ -2,6 +2,7 @@ import logging
 import json
 import sys
 from datetime import datetime, timedelta
+import os
 
 from ec2_reaper import aws_lambda
 from ec2_reaper import LOCAL_TZ
@@ -10,12 +11,19 @@ logging.basicConfig(level=logging.DEBUG)
 logging.getLogger('botocore').setLevel(logging.INFO)
 logging.getLogger('boto3').setLevel(logging.INFO)
 
+# mock has some weirdness in python 3.3, 3.5, and 3.6
 if sys.version_info < (3, 0) or (sys.version_info >= (3, 5) and
                                  sys.version_info < (3, 6)):
     from mock import patch
 else:
     from unittest.mock import patch
 
+# py 2.7 has reload built in but it's moved around a bit in py3+
+if sys.version_info >= (3, 0) and sys.version_info < (3, 4):
+    from imp import reload
+elif sys.version_info >= (3, 4):
+    from importlib import reload
+
 # when no results, handler should have called reap, *not* called (slack) notify,
 # and should have returned a happy response json obj,
 @patch.object(aws_lambda, 'reap')
@@ -55,3 +63,27 @@ def test_reap_2neg_1pos(mock_notify, mock_reap):
     assert r['statusCode'] == 200
     assert r['body']['log'] == mock_reap_results
     assert r['body']['reaped'] == 1
+
+# env vars come in as strings, so bools like DEBUG need testing
+def test_debug_envvar():
+    from ec2_reaper import aws_lambda as al
+    # true
+    os.environ['DEBUG'] = 'true'
+    reload(al)
+    assert al.DEBUG == True
+    os.environ['DEBUG'] = 'True'
+    reload(al)
+    assert al.DEBUG == True
+
+    # default to safety
+    os.environ['DEBUG'] = 'mooooooooo'
+    reload(al)
+    assert al.DEBUG == True
+
+    # false
+    os.environ['DEBUG'] = 'False'
+    reload(al)
+    assert al.DEBUG == False
+    os.environ['DEBUG'] = 'false'
+    reload(al)
+    assert al.DEBUG == False