Skip to content

Commit

Permalink
add unit test for sampler_config_fetcher
Browse files Browse the repository at this point in the history
Signed-off-by: thomas.ebner <[email protected]>
  • Loading branch information
samohte committed Dec 1, 2023
1 parent 133c5e4 commit fbddf4b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ void SamplerConfigFetcher::onSuccess(const Http::AsyncClient::Request& /*request
ENVOY_LOG(error, "SamplerConfigFetcher received a non-success status code: {}", response_code);
} else {
ENVOY_LOG(info, "SamplerConfigFetcher received success status code: {}", response_code);
// TODO: parse respone
// sampler_config_.parse(http_response->bodyAsString());
sampler_config_.parse("{\n \"rootSpansPerMinute\" : 2000 \n }");
sampler_config_.parse(http_response->bodyAsString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ envoy_extension_cc_test(
srcs = [
"dynatrace_sampler_test.cc",
"dynatrace_tracestate_test.cc",
"sampler_config_fetcher_test.cc",
"sampler_config_test.cc",
"tracestate_test.cc",
],
Expand All @@ -37,6 +38,7 @@ envoy_extension_cc_test(
"//source/extensions/tracers/opentelemetry/samplers/dynatrace:dynatrace_sampler_lib",
"//test/mocks/server:tracer_factory_context_mocks",
"//test/test_common:utility_lib",
"@envoy_api//envoy/config/core/v3:pkg_cc_proto",
"@envoy_api//envoy/extensions/tracers/opentelemetry/samplers/v3:pkg_cc_proto",
],
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <memory>
#include <string>
#include <utility>

#include "envoy/config/core/v3/http_service.pb.h"

#include "source/extensions/tracers/opentelemetry/samplers/dynatrace/sampler_config.h"
#include "source/extensions/tracers/opentelemetry/samplers/dynatrace/sampler_config_fetcher.h"

#include "test/mocks/common.h"
#include "test/mocks/http/mocks.h"
#include "test/mocks/server/tracer_factory_context.h"
#include "test/mocks/tracing/mocks.h"

#include "gtest/gtest.h"

namespace Envoy {
namespace Extensions {
namespace Tracers {
namespace OpenTelemetry {

using testing::NiceMock;
using testing::Return;
using testing::ReturnRef;

class SamplerConfigFetcherTest : public testing::Test {

protected:
NiceMock<Envoy::Server::Configuration::MockTracerFactoryContext> tracerFactoryContext_;
envoy::config::core::v3::HttpService http_service_;
NiceMock<Event::MockTimer>* timer_;
};

TEST_F(SamplerConfigFetcherTest, Test) {
NiceMock<Envoy::Server::Configuration::MockTracerFactoryContext> tracerFactoryContext_;

const std::string yaml_string = R"EOF(
http_uri:
cluster: "cluster_name"
uri: "https://some-o11y.com/otlp/v1/traces"
timeout: 0.250s
request_headers_to_add:
- header:
key: "Authorization"
value: "auth-token"
)EOF";

TestUtility::loadFromYaml(yaml_string, http_service_);

ON_CALL(tracerFactoryContext_.server_factory_context_.cluster_manager_, getThreadLocalCluster(_))
.WillByDefault(Return(
&tracerFactoryContext_.server_factory_context_.cluster_manager_.thread_local_cluster_));

timer_ =
new NiceMock<Event::MockTimer>(&tracerFactoryContext_.server_factory_context_.dispatcher_);
ON_CALL(tracerFactoryContext_.server_factory_context_.dispatcher_, createTimer_(_))
.WillByDefault(Invoke([this](Event::TimerCb) { return timer_; }));

EXPECT_CALL(tracerFactoryContext_.server_factory_context_.cluster_manager_.thread_local_cluster_
.async_client_,
send_(_, _, _));

SamplerConfigFetcher f(tracerFactoryContext_, http_service_);
timer_->invokeCallback();

Http::MockAsyncClientRequest request_(&tracerFactoryContext_.server_factory_context_
.cluster_manager_.thread_local_cluster_.async_client_);
Http::ResponseMessagePtr message(new Http::ResponseMessageImpl(
Http::ResponseHeaderMapPtr{new Http::TestResponseHeaderMapImpl{{":status", "200"}}}));
message->body().add("{\n \"rootSpansPerMinute\" : 4356 \n }");
f.onSuccess(request_, std::move(message));
EXPECT_EQ(f.getSamplerConfig().getRootSpansPerMinute(), 4356);
}

} // namespace OpenTelemetry
} // namespace Tracers
} // namespace Extensions
} // namespace Envoy

0 comments on commit fbddf4b

Please sign in to comment.