Skip to content

Commit 6fc4d37

Browse files
committed
Add propagator unit tests
1 parent d17b087 commit 6fc4d37

File tree

3 files changed

+175
-24
lines changed

3 files changed

+175
-24
lines changed

sdk/src/configuration/configuration_parser.cc

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,34 +1031,37 @@ static std::unique_ptr<PropagatorConfiguration> ParsePropagatorConfiguration(
10311031
auto model = std::make_unique<PropagatorConfiguration>();
10321032

10331033
std::unique_ptr<DocumentNode> child;
1034-
child = node->GetRequiredChildNode("composite");
1034+
child = node->GetChildNode("composite");
10351035
std::string name;
10361036
int num_child = 0;
10371037

1038-
for (auto it = child->begin(); it != child->end(); ++it)
1038+
if (child)
10391039
{
1040-
// This is an entry in the composite array
1041-
std::unique_ptr<DocumentNode> element(*it);
1042-
num_child++;
1043-
int count = 0;
1044-
1045-
// Find out its name, we expect an object with a unique property.
1046-
for (auto it2 = element->begin_properties(); it2 != element->end_properties(); ++it2)
1047-
{
1048-
name = it2.Name();
1049-
count++;
1050-
}
1051-
1052-
if (count != 1)
1040+
for (auto it = child->begin(); it != child->end(); ++it)
10531041
{
1054-
std::string message("Illegal composite child ");
1055-
message.append(std::to_string(num_child));
1056-
message.append(", properties count: ");
1057-
message.append(std::to_string(count));
1058-
throw InvalidSchemaException(message);
1042+
// This is an entry in the composite array
1043+
std::unique_ptr<DocumentNode> element(*it);
1044+
num_child++;
1045+
int count = 0;
1046+
1047+
// Find out its name, we expect an object with a unique property.
1048+
for (auto it2 = element->begin_properties(); it2 != element->end_properties(); ++it2)
1049+
{
1050+
name = it2.Name();
1051+
count++;
1052+
}
1053+
1054+
if (count != 1)
1055+
{
1056+
std::string message("Illegal composite child ");
1057+
message.append(std::to_string(num_child));
1058+
message.append(", properties count: ");
1059+
message.append(std::to_string(count));
1060+
throw InvalidSchemaException(message);
1061+
}
1062+
1063+
model->composite.push_back(name);
10591064
}
1060-
1061-
model->composite.push_back(name);
10621065
}
10631066

10641067
model->composite_list = node->GetString("composite_list", "");

sdk/test/configuration/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Copyright The OpenTelemetry Authors
22
# SPDX-License-Identifier: Apache-2.0
33

4-
foreach(testname yaml_test yaml_trace_test yaml_resource_test yaml_logs_test
5-
yaml_metrics_test)
4+
foreach(testname yaml_test yaml_propagator_test yaml_trace_test
5+
yaml_resource_test yaml_logs_test yaml_metrics_test)
66
add_executable(${testname} "${testname}.cc")
77
target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES}
88
${CMAKE_THREAD_LIBS_INIT} opentelemetry_configuration)
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#include <gtest/gtest.h>
5+
#include <memory>
6+
#include <string>
7+
#include <vector>
8+
9+
#include "opentelemetry/sdk/configuration/configuration.h"
10+
#include "opentelemetry/sdk/configuration/propagator_configuration.h"
11+
#include "opentelemetry/sdk/configuration/yaml_configuration_parser.h"
12+
13+
static std::unique_ptr<opentelemetry::sdk::configuration::Configuration> DoParse(
14+
const std::string &yaml)
15+
{
16+
static const std::string source("test");
17+
return opentelemetry::sdk::configuration::YamlConfigurationParser::ParseString(source, yaml);
18+
}
19+
20+
TEST(YamlPropagator, empty_propagator)
21+
{
22+
std::string yaml = R"(
23+
file_format: xx.yy
24+
propagator:
25+
)";
26+
27+
auto config = DoParse(yaml);
28+
ASSERT_NE(config, nullptr);
29+
ASSERT_NE(config->propagator, nullptr);
30+
ASSERT_EQ(config->propagator->composite.size(), 0);
31+
ASSERT_EQ(config->propagator->composite_list, "");
32+
}
33+
34+
TEST(YamlPropagator, empty_composite)
35+
{
36+
std::string yaml = R"(
37+
file_format: xx.yy
38+
propagator:
39+
composite:
40+
)";
41+
42+
auto config = DoParse(yaml);
43+
ASSERT_NE(config, nullptr);
44+
ASSERT_NE(config->propagator, nullptr);
45+
ASSERT_EQ(config->propagator->composite.size(), 0);
46+
ASSERT_EQ(config->propagator->composite_list, "");
47+
}
48+
49+
TEST(YamlPropagator, old_propagator_1)
50+
{
51+
// This is the old format, must fail
52+
std::string yaml = R"(
53+
file_format: xx.yy
54+
propagator:
55+
composite:
56+
- foo
57+
- bar
58+
)";
59+
60+
auto config = DoParse(yaml);
61+
ASSERT_EQ(config, nullptr);
62+
}
63+
64+
TEST(YamlPropagator, old_propagator_2)
65+
{
66+
// This is the old format, must fail
67+
std::string yaml = R"(
68+
file_format: xx.yy
69+
propagator:
70+
composite: [foo, bar]
71+
)";
72+
73+
auto config = DoParse(yaml);
74+
ASSERT_EQ(config, nullptr);
75+
}
76+
77+
TEST(YamlPropagator, propagator_array_ok)
78+
{
79+
std::string yaml = R"(
80+
file_format: xx.yy
81+
propagator:
82+
composite:
83+
- foo:
84+
- bar:
85+
- baz:
86+
)";
87+
88+
auto config = DoParse(yaml);
89+
ASSERT_NE(config, nullptr);
90+
ASSERT_NE(config->propagator, nullptr);
91+
ASSERT_EQ(config->propagator->composite.size(), 3);
92+
ASSERT_EQ(config->propagator->composite[0], "foo");
93+
ASSERT_EQ(config->propagator->composite[1], "bar");
94+
ASSERT_EQ(config->propagator->composite[2], "baz");
95+
ASSERT_EQ(config->propagator->composite_list, "");
96+
}
97+
98+
TEST(YamlPropagator, propagator_array_broken)
99+
{
100+
std::string yaml = R"(
101+
file_format: xx.yy
102+
propagator:
103+
composite:
104+
- foo:
105+
- bar:
106+
baz:
107+
)";
108+
109+
auto config = DoParse(yaml);
110+
ASSERT_EQ(config, nullptr);
111+
}
112+
113+
TEST(YamlPropagator, propagator_composite_list)
114+
{
115+
std::string yaml = R"(
116+
file_format: xx.yy
117+
propagator:
118+
composite_list: "foo,bar,baz"
119+
)";
120+
121+
auto config = DoParse(yaml);
122+
ASSERT_NE(config, nullptr);
123+
ASSERT_NE(config->propagator, nullptr);
124+
ASSERT_EQ(config->propagator->composite.size(), 0);
125+
ASSERT_EQ(config->propagator->composite_list, "foo,bar,baz");
126+
}
127+
128+
TEST(YamlPropagator, propagator_both)
129+
{
130+
std::string yaml = R"(
131+
file_format: xx.yy
132+
propagator:
133+
composite:
134+
- aaa:
135+
- bbb:
136+
- ccc:
137+
composite_list: "ddd,eee,fff"
138+
)";
139+
140+
auto config = DoParse(yaml);
141+
ASSERT_NE(config, nullptr);
142+
ASSERT_NE(config->propagator, nullptr);
143+
ASSERT_EQ(config->propagator->composite.size(), 3);
144+
ASSERT_EQ(config->propagator->composite[0], "aaa");
145+
ASSERT_EQ(config->propagator->composite[1], "bbb");
146+
ASSERT_EQ(config->propagator->composite[2], "ccc");
147+
ASSERT_EQ(config->propagator->composite_list, "ddd,eee,fff");
148+
}

0 commit comments

Comments
 (0)