Skip to content

Commit 21fc337

Browse files
authored
Prepare for requests removal : add 100% unit test coverage on extension.py (#166)
1 parent cb6e9ed commit 21fc337

File tree

3 files changed

+97
-4
lines changed

3 files changed

+97
-4
lines changed

datadog_lambda/extension.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import logging
2-
import requests
32
from os import path
43

4+
try:
5+
# only available in python 3
6+
# not an issue since the extension is not compatible with python 2.x runtime
7+
# https://docs.aws.amazon.com/lambda/latest/dg/using-extensions.html
8+
import urllib.request
9+
except ImportError:
10+
# safe since both calls to urllib are protected with try/expect and will return false
11+
urllib = None
12+
513
AGENT_URL = "http://127.0.0.1:8124"
614
HELLO_PATH = "/lambda/hello"
715
FLUSH_PATH = "/lambda/flush"
@@ -14,7 +22,7 @@ def is_extension_running():
1422
if not path.exists(EXTENSION_PATH):
1523
return False
1624
try:
17-
requests.get(AGENT_URL + HELLO_PATH)
25+
urllib.request.urlopen(AGENT_URL + HELLO_PATH)
1826
except Exception as e:
1927
logger.debug("Extension is not running, returned with error %s", e)
2028
return False
@@ -23,7 +31,8 @@ def is_extension_running():
2331

2432
def flush_extension():
2533
try:
26-
requests.post(AGENT_URL + FLUSH_PATH, data={})
34+
req = urllib.request.Request(AGENT_URL + FLUSH_PATH, "".encode("ascii"))
35+
urllib.request.urlopen(req)
2736
except Exception as e:
2837
logger.debug("Failed to flush extension, returned with error %s", e)
2938
return False

setup.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
"setuptools>=44.1.1; python_version < '3.0'",
4040
],
4141
extras_require={
42-
"dev": ["nose2==0.9.1", "flake8==3.7.9", "requests==2.22.0", "boto3==1.10.33"]
42+
"dev": [
43+
"nose2==0.9.1",
44+
"flake8==3.7.9",
45+
"requests==2.22.0",
46+
"boto3==1.10.33",
47+
"httpretty==0.9.7",
48+
]
4349
},
4450
)

tests/test_extension.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import os
2+
import sys
3+
import unittest
4+
import httpretty
5+
6+
try:
7+
from unittest.mock import patch
8+
except ImportError:
9+
from mock import patch
10+
11+
from datadog_lambda.extension import (
12+
is_extension_running,
13+
flush_extension,
14+
should_use_extension,
15+
)
16+
17+
18+
def exceptionCallback(request, uri, headers):
19+
raise Exception("oopsy!")
20+
21+
22+
class TestLambdaExtension(unittest.TestCase):
23+
# do not execute tests for Python v2.x
24+
__test__ = sys.version_info >= (3, 0)
25+
26+
@patch("datadog_lambda.extension.EXTENSION_PATH", os.path.abspath(__file__))
27+
def test_is_extension_running_true(self):
28+
httpretty.enable()
29+
last_request = httpretty.last_request()
30+
httpretty.register_uri(httpretty.GET, "http://127.0.0.1:8124/lambda/hello")
31+
assert is_extension_running() == True
32+
assert httpretty.last_request() != last_request
33+
httpretty.disable()
34+
35+
def test_is_extension_running_file_not_found(self):
36+
httpretty.enable()
37+
last_request = httpretty.last_request()
38+
httpretty.register_uri(httpretty.GET, "http://127.0.0.1:8124/lambda/hello")
39+
assert is_extension_running() == False
40+
assert httpretty.last_request() == last_request
41+
httpretty.disable()
42+
43+
@patch("datadog_lambda.extension.EXTENSION_PATH", os.path.abspath(__file__))
44+
def test_is_extension_running_http_failure(self):
45+
httpretty.enable()
46+
last_request = httpretty.last_request()
47+
httpretty.register_uri(
48+
httpretty.GET,
49+
"http://127.0.0.1:8124/lambda/hello",
50+
status=503,
51+
body=exceptionCallback,
52+
)
53+
assert is_extension_running() == False
54+
assert httpretty.last_request() != last_request
55+
httpretty.disable()
56+
57+
@patch("datadog_lambda.extension.EXTENSION_PATH", os.path.abspath(__file__))
58+
def test_flush_ok(self):
59+
httpretty.enable()
60+
last_request = httpretty.last_request()
61+
httpretty.register_uri(httpretty.POST, "http://127.0.0.1:8124/lambda/flush")
62+
assert flush_extension() == True
63+
assert httpretty.last_request() != last_request
64+
httpretty.disable()
65+
66+
@patch("datadog_lambda.extension.EXTENSION_PATH", os.path.abspath(__file__))
67+
def test_flush_not_ok(self):
68+
httpretty.enable()
69+
last_request = httpretty.last_request()
70+
httpretty.register_uri(
71+
httpretty.POST,
72+
"http://127.0.0.1:8124/lambda/flush",
73+
status=503,
74+
body=exceptionCallback,
75+
)
76+
assert flush_extension() == False
77+
assert httpretty.last_request() != last_request
78+
httpretty.disable()

0 commit comments

Comments
 (0)