|
| 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