Add tracer configuration
[ric-plt/tracelibcpp.git] / tst / testcreate.cpp
1 /*
2  * Copyright (c) 2019 AT&T Intellectual Property.
3  * Copyright (c) 2018-2019 Nokia.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <gtest/gtest.h>
19 #include <gmock/gmock.h>
20
21 #include "tracelibcpp.hpp"
22 #include "config.hpp"
23
24 using namespace testing;
25 using namespace tracelibcpp;
26
27 class ConfMakerTest: public ::testing::Test
28 {
29 public:
30     ConfMaker cm;
31     ConfMakerTest():
32         cm(ConfMaker("testservice"))
33     {
34     }
35     ~ConfMakerTest()
36     {
37         unsetenv(JAEGER_SAMPLER_TYPE_ENV);
38         unsetenv(TRACING_ENABLED_ENV);
39         unsetenv(JAEGER_SAMPLER_PARAM_ENV);
40         unsetenv(JAEGER_AGENT_ADDR_ENV);
41         unsetenv(JAEGER_LOG_LEVEL_ENV);
42     }
43 };
44
45 TEST_F(ConfMakerTest, TestEnvReadingNonExisingReturnsDefaultValue)
46 {
47     EXPECT_THAT("foobar", StrEq(cm.getEnv("nonexistent", "foobar")));
48 }
49
50 TEST_F(ConfMakerTest, TestEnvReadingReturnsEnvironmentValValue)
51 {
52     setenv("FOO", "BAR", 1);
53     auto val = cm.getEnv("FOO", "foobar");
54     unsetenv("FOO");
55     EXPECT_THAT("BAR", StrEq(val));
56 }
57
58 TEST_F(ConfMakerTest, TestTracingIsDisabledByDefault)
59 {
60     EXPECT_FALSE(cm.isTracingEnabled());
61 }
62
63 TEST_F(ConfMakerTest, TestTracingCanBeEnabledWithEnvValTrue)
64 {
65     setenv(TRACING_ENABLED_ENV, "true", 1);
66     EXPECT_TRUE(cm.isTracingEnabled());
67     setenv(TRACING_ENABLED_ENV, "TRUE", 1);
68     EXPECT_TRUE(cm.isTracingEnabled());
69 }
70
71 TEST_F(ConfMakerTest, TestTracingCanBeEnabledWithEnvValOne)
72 {
73     setenv(TRACING_ENABLED_ENV, "1", 1);
74     EXPECT_TRUE(cm.isTracingEnabled());
75 }
76
77 TEST_F(ConfMakerTest, TestTracingEnabledWithUnknownValuesResultsDisabled)
78 {
79     setenv(TRACING_ENABLED_ENV, "0", 1);
80     EXPECT_FALSE(cm.isTracingEnabled());
81     setenv(TRACING_ENABLED_ENV, "off", 1);
82     EXPECT_FALSE(cm.isTracingEnabled());
83 }
84
85 TEST_F(ConfMakerTest, TestThatByDefaultConstSamplerIsCreated)
86 {
87     auto samplerconf = cm.getSamplerConfig();
88     EXPECT_THAT("const", StrEq(samplerconf.type()));
89     EXPECT_EQ(0.001, samplerconf.param());
90 }
91
92 TEST_F(ConfMakerTest, TestThatSamplerTypeCanBeDefined)
93 {
94     setenv(JAEGER_SAMPLER_TYPE_ENV, "probabilistic", 1);
95     auto samplerconf = cm.getSamplerConfig();
96     EXPECT_THAT("probabilistic", StrEq(samplerconf.type()));
97     EXPECT_EQ(0.001, samplerconf.param());
98 }
99
100 TEST_F(ConfMakerTest, TestThatSamplerParamCanBeDefined)
101 {
102     setenv(JAEGER_SAMPLER_PARAM_ENV, "0.01", 1);
103     setenv(JAEGER_SAMPLER_TYPE_ENV, "probabilistic", 1);
104     auto samplerconf = cm.getSamplerConfig();
105     EXPECT_EQ(0.01, samplerconf.param());
106 }
107
108 TEST_F(ConfMakerTest, TestThatReporterAgentAddrDefaultsToLocalhost)
109 {
110     auto reporterConf = cm.getReporterConfig();
111     EXPECT_THAT(reporterConf.localAgentHostPort(), StrEq("127.0.0.1:6831"));
112 }
113
114 TEST_F(ConfMakerTest, TestThatReporterAgentAddrCanBeDefined)
115 {
116     setenv(JAEGER_AGENT_ADDR_ENV, "1.1.1.1:1111", 1);
117     auto reporterConf = cm.getReporterConfig();
118     EXPECT_THAT(reporterConf.localAgentHostPort(), StrEq("1.1.1.1:1111"));
119 }
120
121 TEST_F(ConfMakerTest, TestThatIfAgentPortIsNotGivenDefaultIsUsed)
122 {
123     setenv(JAEGER_AGENT_ADDR_ENV, "1.1.1.1", 1);
124     auto reporterConf = cm.getReporterConfig();
125     EXPECT_THAT(reporterConf.localAgentHostPort(), StrEq("1.1.1.1:6831"));
126 }
127
128 TEST_F(ConfMakerTest, TestThatLoggingDefaultIsErr)
129 {
130     EXPECT_EQ(LOG_ERR, cm.getLoggingLevel());
131 }
132
133 TEST_F(ConfMakerTest, TestThatLoggingLevelCanBeConfigured)
134 {
135     setenv(JAEGER_LOG_LEVEL_ENV, "all", 1);
136     EXPECT_EQ(LOG_ALL, cm.getLoggingLevel());
137
138     setenv(JAEGER_LOG_LEVEL_ENV, "error", 1);
139     EXPECT_EQ(LOG_ERR, cm.getLoggingLevel());
140
141     setenv(JAEGER_LOG_LEVEL_ENV, "none", 1);
142     EXPECT_EQ(LOG_NONE, cm.getLoggingLevel());
143 }
144
145 TEST_F(ConfMakerTest, TestThatByDefaultDisabledConfigIsCreated)
146 {
147     auto conf = cm.getTraceConfig();
148     EXPECT_TRUE(conf.disabled());
149 }
150
151 TEST_F(ConfMakerTest, TestThatIfTracerCreationFailsNoopTracerIsReturned)
152 {
153     setenv(TRACING_ENABLED_ENV, "1", 1);
154     setenv(JAEGER_AGENT_ADDR_ENV, "foobar.invalid", 1); // invalid address causes an exception
155     auto tracer = createTracer("foo");
156     EXPECT_THAT(tracer, NotNull());
157 }
158
159 TEST(CreateTest, TestThatTraceCreateReturnsANewInstance) {
160     auto tracer = createTracer("foo");
161     EXPECT_THAT(tracer, NotNull());
162 }
163