|
| 1 | +# (c) Copyright IBM Corp. 2021 |
| 2 | +# (c) Copyright Instana Inc. 2020 |
| 3 | + |
| 4 | +import logging |
| 5 | +import os |
| 6 | +from typing import Generator |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from instana.agent.aws_fargate import AWSFargateAgent |
| 11 | +from instana.options import AWSFargateOptions |
| 12 | +from instana.singletons import get_agent |
| 13 | + |
| 14 | + |
| 15 | +class TestFargate: |
| 16 | + @pytest.fixture(autouse=True) |
| 17 | + def _resource(self) -> Generator[None, None, None]: |
| 18 | + """SetUp and TearDown""" |
| 19 | + # setup |
| 20 | + os.environ["AWS_EXECUTION_ENV"] = "AWS_ECS_FARGATE" |
| 21 | + os.environ["INSTANA_ENDPOINT_URL"] = "https://localhost/notreal" |
| 22 | + os.environ["INSTANA_AGENT_KEY"] = "Fake_Key" |
| 23 | + self.agent = AWSFargateAgent() |
| 24 | + yield |
| 25 | + # tearDown |
| 26 | + # Reset all environment variables of consequence |
| 27 | + if "AWS_EXECUTION_ENV" in os.environ: |
| 28 | + os.environ.pop("AWS_EXECUTION_ENV") |
| 29 | + if "INSTANA_EXTRA_HTTP_HEADERS" in os.environ: |
| 30 | + os.environ.pop("INSTANA_EXTRA_HTTP_HEADERS") |
| 31 | + if "INSTANA_ENDPOINT_URL" in os.environ: |
| 32 | + os.environ.pop("INSTANA_ENDPOINT_URL") |
| 33 | + if "INSTANA_ENDPOINT_PROXY" in os.environ: |
| 34 | + os.environ.pop("INSTANA_ENDPOINT_PROXY") |
| 35 | + if "INSTANA_AGENT_KEY" in os.environ: |
| 36 | + os.environ.pop("INSTANA_AGENT_KEY") |
| 37 | + if "INSTANA_LOG_LEVEL" in os.environ: |
| 38 | + os.environ.pop("INSTANA_LOG_LEVEL") |
| 39 | + if "INSTANA_SECRETS" in os.environ: |
| 40 | + os.environ.pop("INSTANA_SECRETS") |
| 41 | + if "INSTANA_DEBUG" in os.environ: |
| 42 | + os.environ.pop("INSTANA_DEBUG") |
| 43 | + if "INSTANA_TAGS" in os.environ: |
| 44 | + os.environ.pop("INSTANA_TAGS") |
| 45 | + |
| 46 | + def test_has_options(self) -> None: |
| 47 | + assert hasattr(self.agent, "options") |
| 48 | + assert isinstance(self.agent.options, AWSFargateOptions) |
| 49 | + |
| 50 | + def test_invalid_options(self) -> None: |
| 51 | + # None of the required env vars are available... |
| 52 | + if "INSTANA_EXTRA_HTTP_HEADERS" in os.environ: |
| 53 | + os.environ.pop("INSTANA_EXTRA_HTTP_HEADERS") |
| 54 | + if "INSTANA_ENDPOINT_URL" in os.environ: |
| 55 | + os.environ.pop("INSTANA_ENDPOINT_URL") |
| 56 | + if "INSTANA_AGENT_KEY" in os.environ: |
| 57 | + os.environ.pop("INSTANA_AGENT_KEY") |
| 58 | + |
| 59 | + agent = AWSFargateAgent() |
| 60 | + assert not agent.can_send() |
| 61 | + assert not agent.collector |
| 62 | + |
| 63 | + def test_default_secrets(self) -> None: |
| 64 | + assert not self.agent.options.secrets |
| 65 | + assert hasattr(self.agent.options, "secrets_matcher") |
| 66 | + assert self.agent.options.secrets_matcher == "contains-ignore-case" |
| 67 | + assert hasattr(self.agent.options, "secrets_list") |
| 68 | + assert self.agent.options.secrets_list == ["key", "pass", "secret"] |
| 69 | + |
| 70 | + def test_custom_secrets(self) -> None: |
| 71 | + os.environ["INSTANA_SECRETS"] = "equals:love,war,games" |
| 72 | + agent = AWSFargateAgent() |
| 73 | + |
| 74 | + assert hasattr(agent.options, "secrets_matcher") |
| 75 | + assert agent.options.secrets_matcher == "equals" |
| 76 | + assert hasattr(agent.options, "secrets_list") |
| 77 | + assert agent.options.secrets_list == ["love", "war", "games"] |
| 78 | + |
| 79 | + def test_default_tags(self) -> None: |
| 80 | + assert hasattr(self.agent.options, "tags") |
| 81 | + assert not self.agent.options.tags |
| 82 | + |
| 83 | + def test_has_extra_http_headers(self) -> None: |
| 84 | + assert hasattr(self.agent, "options") |
| 85 | + assert hasattr(self.agent.options, "extra_http_headers") |
| 86 | + |
| 87 | + def test_agent_extra_http_headers(self) -> None: |
| 88 | + os.environ["INSTANA_EXTRA_HTTP_HEADERS"] = ( |
| 89 | + "X-Test-Header;X-Another-Header;X-And-Another-Header" |
| 90 | + ) |
| 91 | + agent = AWSFargateAgent() |
| 92 | + assert agent.options.extra_http_headers |
| 93 | + assert agent.options.extra_http_headers == [ |
| 94 | + "x-test-header", |
| 95 | + "x-another-header", |
| 96 | + "x-and-another-header", |
| 97 | + ] |
| 98 | + |
| 99 | + def test_agent_default_log_level(self) -> None: |
| 100 | + assert self.agent.options.log_level == logging.WARNING |
| 101 | + |
| 102 | + def test_agent_custom_log_level(self) -> None: |
| 103 | + os.environ["INSTANA_LOG_LEVEL"] = "eRror" |
| 104 | + agent = AWSFargateAgent() |
| 105 | + assert agent.options.log_level == logging.ERROR |
| 106 | + |
| 107 | + def test_custom_proxy(self) -> None: |
| 108 | + os.environ["INSTANA_ENDPOINT_PROXY"] = "http://myproxy.123" |
| 109 | + agent = AWSFargateAgent() |
| 110 | + assert agent.options.endpoint_proxy == {"https": "http://myproxy.123"} |
0 commit comments