File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import logging
22from os import path
33
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-
134AGENT_URL = "http://127.0.0.1:8124"
14- HELLO_PATH = "/lambda/hello"
155FLUSH_PATH = "/lambda/flush"
166EXTENSION_PATH = "/opt/extensions/datadog-agent"
177
188logger = logging .getLogger (__name__ )
199
2010
21- def is_extension_running ():
22- if not path .exists (EXTENSION_PATH ):
23- return False
24- try :
25- urllib .request .urlopen (AGENT_URL + HELLO_PATH )
26- except Exception as e :
27- logger .debug ("Extension is not running, returned with error %s" , e )
28- return False
29- return True
11+ def is_extension_present ():
12+ if path .exists (EXTENSION_PATH ):
13+ return True
14+ return False
3015
3116
3217def flush_extension ():
3318 try :
19+ import urllib .request
20+
3421 req = urllib .request .Request (AGENT_URL + FLUSH_PATH , "" .encode ("ascii" ))
3522 urllib .request .urlopen (req )
3623 except Exception as e :
@@ -39,4 +26,4 @@ def flush_extension():
3926 return True
4027
4128
42- should_use_extension = is_extension_running ()
29+ should_use_extension = is_extension_present ()
Original file line number Diff line number Diff line change 66from unittest .mock import patch
77
88from datadog_lambda .extension import (
9- is_extension_running ,
9+ is_extension_present ,
1010 flush_extension ,
1111 should_use_extension ,
1212)
@@ -48,19 +48,12 @@ def tearDown(self):
4848
4949 @patch ("datadog_lambda.extension.EXTENSION_PATH" , os .path .abspath (__file__ ))
5050 def test_is_extension_running_true (self ):
51- assert is_extension_running ()
52- assert self .server .called
51+ assert is_extension_present ()
5352
5453 def test_is_extension_running_file_not_found (self ):
55- assert not is_extension_running ()
54+ assert not is_extension_present ()
5655 assert not self .server .called
5756
58- @patch ("datadog_lambda.extension.EXTENSION_PATH" , os .path .abspath (__file__ ))
59- def test_is_extension_running_http_failure (self ):
60- self .server .raises = True
61- assert not is_extension_running ()
62- assert self .server .called
63-
6457 @patch ("datadog_lambda.extension.EXTENSION_PATH" , os .path .abspath (__file__ ))
6558 def test_flush_ok (self ):
6659 assert flush_extension ()
You can’t perform that action at this time.
0 commit comments