|
| 1 | +# (c) Copyright IBM Corp. 2021 |
| 2 | +# (c) Copyright Instana Inc. 2021 |
| 3 | + |
| 4 | +import logging |
| 5 | +import os |
| 6 | +from typing import Generator |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from instana.agent.google_cloud_run import GCRAgent |
| 11 | +from instana.options import GCROptions |
| 12 | +from instana.recorder import StanRecorder |
| 13 | +from instana.singletons import get_agent, get_tracer, set_agent, set_tracer |
| 14 | +from instana.tracer import InstanaTracer, InstanaTracerProvider |
| 15 | + |
| 16 | + |
| 17 | +class TestGCR: |
| 18 | + @pytest.fixture(autouse=True) |
| 19 | + def _resource(self) -> Generator[None, None, None]: |
| 20 | + self.agent = None |
| 21 | + self.span_recorder = None |
| 22 | + self.tracer = None |
| 23 | + |
| 24 | + self.original_agent = get_agent() |
| 25 | + self.original_tracer = get_tracer() |
| 26 | + |
| 27 | + os.environ["K_SERVICE"] = "service" |
| 28 | + os.environ["K_CONFIGURATION"] = "configuration" |
| 29 | + os.environ["K_REVISION"] = "revision" |
| 30 | + os.environ["PORT"] = "port" |
| 31 | + os.environ["INSTANA_ENDPOINT_URL"] = "https://localhost/notreal" |
| 32 | + os.environ["INSTANA_AGENT_KEY"] = "Fake_Key" |
| 33 | + yield |
| 34 | + if "K_SERVICE" in os.environ: |
| 35 | + os.environ.pop("K_SERVICE") |
| 36 | + if "K_CONFIGURATION" in os.environ: |
| 37 | + os.environ.pop("K_CONFIGURATION") |
| 38 | + if "K_REVISION" in os.environ: |
| 39 | + os.environ.pop("K_REVISION") |
| 40 | + if "PORT" in os.environ: |
| 41 | + os.environ.pop("PORT") |
| 42 | + if "INSTANA_EXTRA_HTTP_HEADERS" in os.environ: |
| 43 | + os.environ.pop("INSTANA_EXTRA_HTTP_HEADERS") |
| 44 | + if "INSTANA_ENDPOINT_URL" in os.environ: |
| 45 | + os.environ.pop("INSTANA_ENDPOINT_URL") |
| 46 | + if "INSTANA_ENDPOINT_PROXY" in os.environ: |
| 47 | + os.environ.pop("INSTANA_ENDPOINT_PROXY") |
| 48 | + if "INSTANA_AGENT_KEY" in os.environ: |
| 49 | + os.environ.pop("INSTANA_AGENT_KEY") |
| 50 | + if "INSTANA_LOG_LEVEL" in os.environ: |
| 51 | + os.environ.pop("INSTANA_LOG_LEVEL") |
| 52 | + if "INSTANA_SECRETS" in os.environ: |
| 53 | + os.environ.pop("INSTANA_SECRETS") |
| 54 | + if "INSTANA_DEBUG" in os.environ: |
| 55 | + os.environ.pop("INSTANA_DEBUG") |
| 56 | + if "INSTANA_TAGS" in os.environ: |
| 57 | + os.environ.pop("INSTANA_TAGS") |
| 58 | + |
| 59 | + set_agent(self.original_agent) |
| 60 | + set_tracer(self.original_tracer) |
| 61 | + |
| 62 | + def create_agent_and_setup_tracer( |
| 63 | + self, tracer_provider: InstanaTracerProvider |
| 64 | + ) -> None: |
| 65 | + self.agent = GCRAgent( |
| 66 | + service="service", |
| 67 | + configuration="configuration", |
| 68 | + revision="revision", |
| 69 | + ) |
| 70 | + self.span_processor = StanRecorder(self.agent) |
| 71 | + self.tracer = InstanaTracer( |
| 72 | + tracer_provider.sampler, |
| 73 | + self.span_processor, |
| 74 | + tracer_provider._exporter, |
| 75 | + tracer_provider._propagators, |
| 76 | + ) |
| 77 | + set_agent(self.agent) |
| 78 | + set_tracer(self.tracer) |
| 79 | + |
| 80 | + def test_has_options(self, tracer_provider: InstanaTracerProvider) -> None: |
| 81 | + self.create_agent_and_setup_tracer(tracer_provider=tracer_provider) |
| 82 | + assert hasattr(self.agent, "options") |
| 83 | + assert isinstance(self.agent.options, GCROptions) |
| 84 | + |
| 85 | + def test_invalid_options(self): |
| 86 | + # None of the required env vars are available... |
| 87 | + if "INSTANA_EXTRA_HTTP_HEADERS" in os.environ: |
| 88 | + os.environ.pop("INSTANA_EXTRA_HTTP_HEADERS") |
| 89 | + if "INSTANA_ENDPOINT_URL" in os.environ: |
| 90 | + os.environ.pop("INSTANA_ENDPOINT_URL") |
| 91 | + if "INSTANA_AGENT_KEY" in os.environ: |
| 92 | + os.environ.pop("INSTANA_AGENT_KEY") |
| 93 | + |
| 94 | + agent = GCRAgent( |
| 95 | + service="service", configuration="configuration", revision="revision" |
| 96 | + ) |
| 97 | + assert not agent.can_send() |
| 98 | + assert not agent.collector |
| 99 | + |
| 100 | + def test_default_secrets(self, tracer_provider: InstanaTracerProvider) -> None: |
| 101 | + self.create_agent_and_setup_tracer(tracer_provider=tracer_provider) |
| 102 | + assert not self.agent.options.secrets |
| 103 | + assert hasattr(self.agent.options, "secrets_matcher") |
| 104 | + assert self.agent.options.secrets_matcher == "contains-ignore-case" |
| 105 | + assert hasattr(self.agent.options, "secrets_list") |
| 106 | + assert self.agent.options.secrets_list == ["key", "pass", "secret"] |
| 107 | + |
| 108 | + def test_custom_secrets(self, tracer_provider: InstanaTracerProvider) -> None: |
| 109 | + os.environ["INSTANA_SECRETS"] = "equals:love,war,games" |
| 110 | + self.create_agent_and_setup_tracer(tracer_provider=tracer_provider) |
| 111 | + |
| 112 | + assert hasattr(self.agent.options, "secrets_matcher") |
| 113 | + assert self.agent.options.secrets_matcher == "equals" |
| 114 | + assert hasattr(self.agent.options, "secrets_list") |
| 115 | + assert self.agent.options.secrets_list == ["love", "war", "games"] |
| 116 | + |
| 117 | + def test_has_extra_http_headers( |
| 118 | + self, tracer_provider: InstanaTracerProvider |
| 119 | + ) -> None: |
| 120 | + self.create_agent_and_setup_tracer(tracer_provider=tracer_provider) |
| 121 | + assert hasattr(self.agent, "options") |
| 122 | + assert hasattr(self.agent.options, "extra_http_headers") |
| 123 | + |
| 124 | + def test_agent_extra_http_headers( |
| 125 | + self, tracer_provider: InstanaTracerProvider |
| 126 | + ) -> None: |
| 127 | + os.environ["INSTANA_EXTRA_HTTP_HEADERS"] = ( |
| 128 | + "X-Test-Header;X-Another-Header;X-And-Another-Header" |
| 129 | + ) |
| 130 | + self.create_agent_and_setup_tracer(tracer_provider=tracer_provider) |
| 131 | + assert self.agent.options.extra_http_headers |
| 132 | + should_headers = ["x-test-header", "x-another-header", "x-and-another-header"] |
| 133 | + assert should_headers == self.agent.options.extra_http_headers |
| 134 | + |
| 135 | + def test_agent_default_log_level( |
| 136 | + self, tracer_provider: InstanaTracerProvider |
| 137 | + ) -> None: |
| 138 | + self.create_agent_and_setup_tracer(tracer_provider=tracer_provider) |
| 139 | + assert self.agent.options.log_level == logging.WARNING |
| 140 | + |
| 141 | + def test_agent_custom_log_level( |
| 142 | + self, tracer_provider: InstanaTracerProvider |
| 143 | + ) -> None: |
| 144 | + os.environ["INSTANA_LOG_LEVEL"] = "eRror" |
| 145 | + self.create_agent_and_setup_tracer(tracer_provider=tracer_provider) |
| 146 | + assert self.agent.options.log_level == logging.ERROR |
| 147 | + |
| 148 | + def test_custom_proxy(self, tracer_provider: InstanaTracerProvider) -> None: |
| 149 | + os.environ["INSTANA_ENDPOINT_PROXY"] = "http://myproxy.123" |
| 150 | + self.create_agent_and_setup_tracer(tracer_provider=tracer_provider) |
| 151 | + assert self.agent.options.endpoint_proxy == {"https": "http://myproxy.123"} |
0 commit comments