Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NH-67051 Add report_init_event tests #240

Merged
merged 9 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 176 additions & 4 deletions tests/unit/test_configurator/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,50 @@

import pytest

# ==================================================================
# Configurator stdlib fixtures
# ==================================================================

@pytest.fixture(name="mock_sys")
def mock_sys(mocker):
mock_version_info = mocker.PropertyMock()
mock_version_info.return_value = [3, 11, 12]
mock_version = mocker.PropertyMock()
mock_version.return_value = "foo-runtime"
mock_exec = mocker.PropertyMock()
mock_exec.return_value = "/foo/path"

mock_sys = mocker.patch(
"solarwinds_apm.configurator.sys"
)
type(mock_sys).version_info = mock_version_info
type(mock_sys).version = mock_version
type(mock_sys).executable = mock_exec
type(mock_sys).implementation = mocker.PropertyMock()
type(mock_sys).implementation.name = "foo-name"

return mock_sys

@pytest.fixture(name="mock_sys_error_version_info")
def mock_sys_error_version_info(mocker):
mock_version_info = mocker.PropertyMock()
mock_version_info.return_value = []
mock_version = mocker.PropertyMock()
mock_version.return_value = "foo-runtime"
mock_exec = mocker.PropertyMock()
mock_exec.return_value = "/foo/path"

mock_sys = mocker.patch(
"solarwinds_apm.configurator.sys"
)
type(mock_sys).version_info = mock_version_info
type(mock_sys).version = mock_version
type(mock_sys).executable = mock_exec
type(mock_sys).implementation = mocker.PropertyMock()
type(mock_sys).implementation.name = "foo-name"

return mock_sys

# ==================================================================
# Configurator Otel fixtures
# ==================================================================
Expand Down Expand Up @@ -53,10 +97,16 @@ def mock_meterprovider(mocker):
)

# ==================================================================
# Configurator APM Python mocks
# Configurator APM Python ApmConfig mocks
# ==================================================================

def get_apmconfig_mocks(mocker, enabled=True, exp_otel_col=True):
def get_apmconfig_mocks(
mocker,
enabled=True,
exp_otel_col=True,
is_lambda=False,
md_is_valid=True,
):
mock_get_otelcol = mocker.Mock()
if exp_otel_col == True:
mock_get_otelcol.configure_mock(return_value=True)
Expand All @@ -72,12 +122,61 @@ def get_apmconfig_mocks(mocker, enabled=True, exp_otel_col=True):
)
)

# mock the extension that is linked to ApmConfig
mock_ext_config = mocker.Mock()
mock_ext_config.configure_mock(
**{
"getVersionString": mocker.Mock(return_value="1.1.1")
}
)

mock_ext_context = mocker.Mock()
mock_ext_context.configure_mock(
**{
"set": mocker.Mock()
}
)

mock_event = mocker.Mock()
mock_event.configure_mock(
**{
"addInfo": mocker.Mock()
}
)
mock_create_event = mocker.Mock(return_value=mock_event)

mock_make_random = mocker.Mock()
mock_make_random.configure_mock(
**{
"isValid": mocker.Mock(return_value=md_is_valid),
"createEvent": mock_create_event
}
)

mock_ext_metadata = mocker.Mock()
mock_ext_metadata.configure_mock(
**{
"makeRandom": mock_make_random
}
)

mock_ext = mocker.Mock()
mock_ext.configure_mock(
**{
"Config": mock_ext_config,
"Context": mock_ext_context,
"Metadata": mock_ext_metadata,
}
)

mock_apmconfig = mocker.Mock()
mock_apmconfig.configure_mock(
**{
"agent_enabled": enabled,
"get": mock_get_exp,
"service_name": "foo-service",
"is_lambda": is_lambda,
"extension": mock_ext
}
)
return mock_apmconfig
Expand All @@ -96,13 +195,39 @@ def mock_apmconfig_enabled(mocker):
get_apmconfig_mocks(mocker, True, False)
)

@pytest.fixture(name="mock_apmconfig_enabled_md_invalid")
def mock_apmconfig_enabled_md_invalid(mocker):
return mocker.patch(
"solarwinds_apm.configurator.SolarWindsApmConfig",
get_apmconfig_mocks(
mocker,
True,
False,
False,
False,
)
)

@pytest.fixture(name="mock_apmconfig_enabled_expt")
def mock_apmconfig_enabled_expt(mocker):
return mocker.patch(
"solarwinds_apm.configurator.SolarWindsApmConfig",
get_apmconfig_mocks(mocker, True, True)
)

@pytest.fixture(name="mock_apmconfig_enabled_is_lambda")
def mock_apmconfig_enabled_is_lambda(mocker):
return mocker.patch(
"solarwinds_apm.configurator.SolarWindsApmConfig",
get_apmconfig_mocks(
mocker,
True,
True,
True,
True,
)
)

@pytest.fixture(name="mock_apmconfig_enabled_reporter_settings")
def mock_apmconfig_enabled_reporter_settings(mocker):
mock_reporter = mocker.Mock()
Expand All @@ -126,9 +251,22 @@ def mock_apmconfig_enabled_reporter_settings(mocker):
)
return mock_apmconfig

@pytest.fixture(name="mock_extension")
def mock_extension(mocker):
# ==================================================================
# Configurator APM Python extension mocks
# ==================================================================

def get_extension_mocks(
mocker,
status_code=0,
):
mock_reporter = mocker.Mock()
mock_reporter.configure_mock(
**{
"init_status": status_code,
"sendStatus": mocker.Mock()
}
)

mock_ext = mocker.Mock()
mock_ext.configure_mock(
**{
Expand All @@ -137,6 +275,40 @@ def mock_extension(mocker):
)
return mock_ext

@pytest.fixture(name="mock_extension")
def mock_extension(mocker):
return get_extension_mocks(mocker)

@pytest.fixture(name="mock_extension_status_code_already_init")
def mock_extension_status_code_already_init(mocker):
return get_extension_mocks(mocker, -1)

@pytest.fixture(name="mock_extension_status_code_invalid_protocol")
def mock_extension_status_code_invalid_protocol(mocker):
return get_extension_mocks(mocker, 2)
Copy link
Contributor

@cheempz cheempz Nov 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great addition of these different scenarios "ok", "already_init" and "invalid_protocol"! It's pointing out something a little subtle--we're checking the reporter initialization status as part of reporting the Init event, and actually ignoring the return code from the report Init event sendStatus call.


# ==================================================================
# Configurator APM Python other mocks
# ==================================================================

def add_fw_versions(input_dict):
input_dict.update({"foo-fw": "bar-version"})
return input_dict

@pytest.fixture(name="mock_fw_versions")
def mock_fw_versions(mocker):
return mocker.patch(
"solarwinds_apm.configurator.SolarWindsConfigurator._add_all_instrumented_python_framework_versions",
side_effect=add_fw_versions
)

@pytest.fixture(name="mock_apm_version")
def mock_apm_version(mocker):
return mocker.patch(
"solarwinds_apm.configurator.__version__",
new="0.0.0",
)

@pytest.fixture(name="mock_fwkv_manager")
def mock_fwkv_manager(mocker):
return mocker.patch(
Expand Down
21 changes: 8 additions & 13 deletions tests/unit/test_configurator/fixtures/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,20 @@

def get_trace_mocks(mocker):
mock_add_span_processor = mocker.Mock()
mock_resource = mocker.Mock()
mock_resource.configure_mock(
**{
"attributes": {"foo": "bar"}
}
)


mock_attributes = mocker.PropertyMock()
mock_attributes.return_value = {"foo": "bar"}
mock_resource = mocker.PropertyMock()
type(mock_resource).attributes = mock_attributes
mock_tracer = mocker.Mock()
mock_tracer.configure_mock(
**{
"resource": mock_resource
}
)
type(mock_tracer).resource = mock_resource
mock_get_tracer = mocker.Mock(return_value=mock_tracer)

mock_tracer_provider = mocker.Mock()
mock_tracer_provider.configure_mock(
**{
"add_span_processor": mock_add_span_processor,
"get_tracer": mock_tracer,
"get_tracer": mock_get_tracer,
}
)
mock_get_tracer_provider = mocker.Mock(
Expand Down
Loading
Loading