def test_empty_transport_disables_send(): client = TempStoreClient(service_name="x", transport_class=None, metrics_interval="0ms") assert len(client.config.errors) == 1 assert "TRANSPORT_CLASS" in client.config.errors assert client.config.disable_send client.close()
def test_docker_kubernetes_system_info_from_environ_overrides_cgroups(): # initialize agent only after overriding environment zuqa_client = TempStoreClient(metrics_interval="0ms") # mock docker/kubernetes data here to get consistent behavior if test is run in docker with mock.patch("zuqa.utils.cgroup.get_cgroup_container_metadata" ) as mock_metadata, mock.patch( "socket.gethostname") as mock_gethostname: mock_metadata.return_value = { "container": { "id": "123" }, "kubernetes": { "pod": { "uid": "456" } } } mock_gethostname.return_value = "foo" system_info = zuqa_client.get_system_info() assert "kubernetes" in system_info assert system_info["kubernetes"] == { "pod": { "uid": "podid", "name": "pod" }, "node": { "name": "node" }, "namespace": "namespace", } assert system_info["container"] == {"id": "123"}
def test_docker_kubernetes_system_info_except_hostname_from_environ(): # initialize agent only after overriding environment elasticapm_client = TempStoreClient(metrics_interval="0ms") # mock docker/kubernetes data here to get consistent behavior if test is run in docker with mock.patch("elasticapm.utils.cgroup.get_cgroup_container_metadata") as mock_metadata, mock.patch( "socket.gethostname" ) as mock_gethostname: mock_metadata.return_value = {} mock_gethostname.return_value = "foo" system_info = elasticapm_client.get_system_info() assert "kubernetes" in system_info assert system_info["kubernetes"] == {"pod": {"name": "foo"}, "namespace": "namespace"}
def test_client_doesnt_start_collector_thread_in_master_process(is_master_process, mock_start_collect_timer): # when in the master process, the client should not start worker threads is_master_process.return_value = True before = mock_start_collect_timer.call_count client = TempStoreClient(server_url="http://example.com", service_name="app_name", secret_token="secret") assert mock_start_collect_timer.call_count == before client.close() before = mock_start_collect_timer.call_count is_master_process.return_value = False client = TempStoreClient(server_url="http://example.com", service_name="app_name", secret_token="secret") assert mock_start_collect_timer.call_count == before + 1 client.close()
def test_config_non_string_types(): """ tests if we can handle non string types as configuration, e.g. Value types from django-configuration """ class MyValue(object): def __init__(self, content): self.content = content def __str__(self): return str(self.content) def __repr__(self): return repr(self.content) client = TempStoreClient( server_url="localhost", service_name=MyValue("bar"), secret_token=MyValue("bay"), metrics_interval="0ms" ) assert isinstance(client.config.secret_token, compat.string_types) assert isinstance(client.config.service_name, compat.string_types) client.close()
def test_config_by_environment(): with mock.patch.dict("os.environ", {"ELASTIC_APM_SERVICE_NAME": "envapp", "ELASTIC_APM_SECRET_TOKEN": "envtoken"}): client = TempStoreClient(metrics_interval="0ms") assert client.config.service_name == "envapp" assert client.config.secret_token == "envtoken" assert client.config.disable_send is False with mock.patch.dict("os.environ", {"ELASTIC_APM_DISABLE_SEND": "true"}): client = TempStoreClient(metrics_interval="0ms") assert client.config.disable_send is True client.close()