|
8 | 8 | #include <string>
|
9 | 9 | #include <unordered_map>
|
10 | 10 | #include <utility>
|
| 11 | +#include<fstream> |
11 | 12 |
|
12 | 13 | #include "opentelemetry/nostd/variant.h"
|
13 | 14 | #include "opentelemetry/sdk/common/attribute_utils.h"
|
| 15 | +#include "opentelemetry/sdk/common/container.h" |
14 | 16 | #include "opentelemetry/sdk/resource/resource.h"
|
15 | 17 | #include "opentelemetry/sdk/resource/resource_detector.h"
|
16 | 18 | #include "opentelemetry/sdk/version/version.h"
|
@@ -292,3 +294,48 @@ TEST(ResourceTest, DerivedResourceDetector)
|
292 | 294 | EXPECT_EQ(resource.GetSchemaURL(), detector.schema_url);
|
293 | 295 | EXPECT_TRUE(received_attributes.find("key") != received_attributes.end());
|
294 | 296 | }
|
| 297 | + |
| 298 | +TEST(ResourceTest, ExctractValidContainerId) |
| 299 | +{ |
| 300 | + std::string line = "13:name=systemd:/podruntime/docker/kubepods/ac679f8a8319c8cf7d38e1adf263bc08d23.aaaa"; |
| 301 | + std::string extracted_id = opentelemetry::sdk::common::ExtractContainerIDFromLine(line); |
| 302 | + EXPECT_EQ(std::string{"ac679f8a8319c8cf7d38e1adf263bc08d23"}, extracted_id); |
| 303 | +} |
| 304 | + |
| 305 | +TEST(ResourceTest, ExtractIdFromMockUpCGroupFile) |
| 306 | +{ |
| 307 | + const char *filename = "test_cgroup.txt"; |
| 308 | + |
| 309 | + { |
| 310 | + std::ofstream outfile(filename); |
| 311 | + outfile << "13:name=systemd:/kuberuntime/containerd/kubepods-pod872d2066_00ef_48ea_a7d8_51b18b72d739:cri-containerd:e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1\n"; |
| 312 | + outfile << "9:cpu:/not-a-container\n"; |
| 313 | + } |
| 314 | + |
| 315 | + std::string container_id = opentelemetry::sdk::common::GetContainerIDFromCgroup(filename); |
| 316 | + EXPECT_EQ(container_id, std::string{"e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1"}); |
| 317 | + |
| 318 | + std::remove(filename); |
| 319 | +} |
| 320 | + |
| 321 | +TEST(ResourceTest, DoesNotExtractInvalidLine) |
| 322 | +{ |
| 323 | + std::string line = "this line does not contain a container id"; |
| 324 | + std::string id = opentelemetry::sdk::common::ExtractContainerIDFromLine(line); |
| 325 | + EXPECT_EQ(id, std::string{""}); |
| 326 | +} |
| 327 | + |
| 328 | +TEST(ContainerIdDetectorTest, ReturnsEmptyOnNoMatch) |
| 329 | +{ |
| 330 | + const char *filename = "test_empty_cgroup.txt"; |
| 331 | + |
| 332 | + { |
| 333 | + std::ofstream outfile(filename); |
| 334 | + outfile << "no container id here\n"; |
| 335 | + } |
| 336 | + |
| 337 | + std::string id = opentelemetry::sdk::common::GetContainerIDFromCgroup(filename); |
| 338 | + EXPECT_EQ(id, std::string{""}); |
| 339 | + |
| 340 | + std::remove(filename); // cleanup |
| 341 | +} |
0 commit comments