Skip to content

Commit

Permalink
unittest: formatted test folder structure and improved unittest coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Cagri Yonca <cagri@ibm.com>
  • Loading branch information
CagriYonca committed Oct 4, 2024
1 parent 34963c7 commit 4ff5db5
Show file tree
Hide file tree
Showing 19 changed files with 2,341 additions and 1,384 deletions.
21 changes: 13 additions & 8 deletions src/instana/agent/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@
"""
Base class for all the agent flavors
"""

import logging

import requests
from ..log import logger

from instana.log import logger


class BaseAgent(object):
""" Base class for all agent flavors """
"""Base class for all agent flavors"""

client = None
options = None

def __init__(self):
self.client = requests.Session()

def update_log_level(self):
""" Uses the value in <self.log_level> to update the global logger """
if self.options is None or self.options.log_level not in [logging.DEBUG,
logging.INFO,
logging.WARN,
logging.ERROR]:
"""Uses the value in <self.log_level> to update the global logger"""
if self.options is None or self.options.log_level not in [
logging.DEBUG,
logging.INFO,
logging.WARN,
logging.ERROR,
]:
logger.warning("BaseAgent.update_log_level: Unknown log level set")
return

logger.setLevel(self.options.log_level)

Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# (c) Copyright IBM Corp. 2021
# (c) Copyright Instana Inc. 2021

import os
import logging
import os
import unittest

from instana.tracer import InstanaTracer
from instana.agent.google_cloud_run import GCRAgent
from instana.options import GCROptions
from instana.recorder import StanRecorder
from instana.agent.google_cloud_run import GCRAgent
from instana.singletons import get_agent, set_agent, get_tracer, set_tracer
from instana.singletons import get_agent, get_tracer, set_agent, set_tracer
from instana.tracer import InstanaTracer


class TestGCR(unittest.TestCase):
def __init__(self, methodName='runTest'):
def __init__(self, methodName="runTest"):
super(TestGCR, self).__init__(methodName)
self.agent = None
self.span_recorder = None
Expand All @@ -31,7 +31,7 @@ def setUp(self):
os.environ["INSTANA_AGENT_KEY"] = "Fake_Key"

def tearDown(self):
""" Reset all environment variables of consequence """
"""Reset all environment variables of consequence"""
if "K_SERVICE" in os.environ:
os.environ.pop("K_SERVICE")
if "K_CONFIGURATION" in os.environ:
Expand Down Expand Up @@ -61,15 +61,17 @@ def tearDown(self):
set_tracer(self.original_tracer)

def create_agent_and_setup_tracer(self):
self.agent = GCRAgent(service="service", configuration="configuration", revision="revision")
self.agent = GCRAgent(
service="service", configuration="configuration", revision="revision"
)
self.span_recorder = StanRecorder(self.agent)
self.tracer = InstanaTracer(recorder=self.span_recorder)
set_agent(self.agent)
set_tracer(self.tracer)

def test_has_options(self):
self.create_agent_and_setup_tracer()
self.assertTrue(hasattr(self.agent, 'options'))
self.assertTrue(hasattr(self.agent, "options"))
self.assertTrue(isinstance(self.agent.options, GCROptions))

def test_invalid_options(self):
Expand All @@ -81,49 +83,53 @@ def test_invalid_options(self):
if "INSTANA_AGENT_KEY" in os.environ:
os.environ.pop("INSTANA_AGENT_KEY")

agent = GCRAgent(service="service", configuration="configuration", revision="revision")
agent = GCRAgent(
service="service", configuration="configuration", revision="revision"
)
self.assertFalse(agent.can_send())
self.assertIsNone(agent.collector)

def test_default_secrets(self):
self.create_agent_and_setup_tracer()
self.assertIsNone(self.agent.options.secrets)
self.assertTrue(hasattr(self.agent.options, 'secrets_matcher'))
self.assertEqual(self.agent.options.secrets_matcher, 'contains-ignore-case')
self.assertTrue(hasattr(self.agent.options, 'secrets_list'))
self.assertEqual(self.agent.options.secrets_list, ['key', 'pass', 'secret'])
self.assertTrue(hasattr(self.agent.options, "secrets_matcher"))
self.assertEqual(self.agent.options.secrets_matcher, "contains-ignore-case")
self.assertTrue(hasattr(self.agent.options, "secrets_list"))
self.assertEqual(self.agent.options.secrets_list, ["key", "pass", "secret"])

def test_custom_secrets(self):
os.environ["INSTANA_SECRETS"] = "equals:love,war,games"
self.create_agent_and_setup_tracer()

self.assertTrue(hasattr(self.agent.options, 'secrets_matcher'))
self.assertEqual(self.agent.options.secrets_matcher, 'equals')
self.assertTrue(hasattr(self.agent.options, 'secrets_list'))
self.assertEqual(self.agent.options.secrets_list, ['love', 'war', 'games'])
self.assertTrue(hasattr(self.agent.options, "secrets_matcher"))
self.assertEqual(self.agent.options.secrets_matcher, "equals")
self.assertTrue(hasattr(self.agent.options, "secrets_list"))
self.assertEqual(self.agent.options.secrets_list, ["love", "war", "games"])

def test_has_extra_http_headers(self):
self.create_agent_and_setup_tracer()
self.assertTrue(hasattr(self.agent, 'options'))
self.assertTrue(hasattr(self.agent.options, 'extra_http_headers'))
self.assertTrue(hasattr(self.agent, "options"))
self.assertTrue(hasattr(self.agent.options, "extra_http_headers"))

def test_agent_extra_http_headers(self):
os.environ['INSTANA_EXTRA_HTTP_HEADERS'] = "X-Test-Header;X-Another-Header;X-And-Another-Header"
os.environ["INSTANA_EXTRA_HTTP_HEADERS"] = (
"X-Test-Header;X-Another-Header;X-And-Another-Header"
)
self.create_agent_and_setup_tracer()
self.assertIsNotNone(self.agent.options.extra_http_headers)
should_headers = ['x-test-header', 'x-another-header', 'x-and-another-header']
should_headers = ["x-test-header", "x-another-header", "x-and-another-header"]
self.assertEqual(should_headers, self.agent.options.extra_http_headers)

def test_agent_default_log_level(self):
self.create_agent_and_setup_tracer()
assert self.agent.options.log_level == logging.WARNING

def test_agent_custom_log_level(self):
os.environ['INSTANA_LOG_LEVEL'] = "eRror"
os.environ["INSTANA_LOG_LEVEL"] = "eRror"
self.create_agent_and_setup_tracer()
assert self.agent.options.log_level == logging.ERROR

def test_custom_proxy(self):
os.environ["INSTANA_ENDPOINT_PROXY"] = "http://myproxy.123"
self.create_agent_and_setup_tracer()
assert self.agent.options.endpoint_proxy == {'https': "http://myproxy.123"}
assert self.agent.options.endpoint_proxy == {"https": "http://myproxy.123"}
Loading

0 comments on commit 4ff5db5

Please sign in to comment.