|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#include <gtest/gtest.h> |
| 5 | +#include <fstream> |
| 6 | +#include <string> |
| 7 | +#include <utility> |
| 8 | + |
| 9 | +#include "opentelemetry/nostd/string_view.h" |
| 10 | +#include "opentelemetry/nostd/variant.h" |
| 11 | +#include "opentelemetry/resource_detectors/container_detector.h" |
| 12 | +#include "opentelemetry/resource_detectors/container_detector_utils.h" |
| 13 | +#include "opentelemetry/sdk/resource/resource.h" |
| 14 | + |
| 15 | +TEST(ContainerIdDetectorTest, ExtractValidContainerIdFromLine) |
| 16 | +{ |
| 17 | + std::string line = |
| 18 | + "13:name=systemd:/podruntime/docker/kubepods/ac679f8a8319c8cf7d38e1adf263bc08d23.aaaa"; |
| 19 | + std::string extracted_id = |
| 20 | + opentelemetry::resource_detector::detail::ExtractContainerIDFromLine(line); |
| 21 | + EXPECT_EQ(std::string{"ac679f8a8319c8cf7d38e1adf263bc08d23"}, extracted_id); |
| 22 | +} |
| 23 | + |
| 24 | +TEST(ContainerIdDetectorTest, ExtractIdFromMockUpCGroupFile) |
| 25 | +{ |
| 26 | + const char *filename = "test_cgroup.txt"; |
| 27 | + |
| 28 | + { |
| 29 | + std::ofstream outfile(filename); |
| 30 | + outfile << "13:name=systemd:/kuberuntime/containerd" |
| 31 | + "/kubepods-pod872d2066_00ef_48ea_a7d8_51b18b72d739:cri-containerd:" |
| 32 | + "e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1\n"; |
| 33 | + outfile << "9:cpu:/not-a-container\n"; |
| 34 | + } |
| 35 | + |
| 36 | + std::string container_id = |
| 37 | + opentelemetry::resource_detector::detail::GetContainerIDFromCgroup(filename); |
| 38 | + EXPECT_EQ(container_id, |
| 39 | + std::string{"e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1"}); |
| 40 | + |
| 41 | + std::remove(filename); |
| 42 | +} |
| 43 | + |
| 44 | +TEST(ContainerIdDetectorTest, DoesNotExtractInvalidLine) |
| 45 | +{ |
| 46 | + std::string line = "this line does not contain a container id"; |
| 47 | + std::string id = opentelemetry::resource_detector::detail::ExtractContainerIDFromLine(line); |
| 48 | + EXPECT_EQ(id, std::string{""}); |
| 49 | +} |
| 50 | + |
| 51 | +TEST(ContainerIdDetectorTest, ReturnsEmptyOnNoMatch) |
| 52 | +{ |
| 53 | + const char *filename = "test_empty_cgroup.txt"; |
| 54 | + |
| 55 | + { |
| 56 | + std::ofstream outfile(filename); |
| 57 | + outfile << "no container id here\n"; |
| 58 | + } |
| 59 | + |
| 60 | + std::string id = opentelemetry::resource_detector::detail::GetContainerIDFromCgroup(filename); |
| 61 | + EXPECT_EQ(id, std::string{""}); |
| 62 | + |
| 63 | + std::remove(filename); // cleanup |
| 64 | +} |
| 65 | + |
| 66 | +TEST(ContainerIdDetectorTest, ReturnsEmptyOnFileFailingToOpen) |
| 67 | +{ |
| 68 | + const char *filename = "test_invalid_cgroup.txt"; |
| 69 | + |
| 70 | + std::string id = opentelemetry::resource_detector::detail::GetContainerIDFromCgroup(filename); |
| 71 | + EXPECT_EQ(id, std::string{""}); |
| 72 | +} |
0 commit comments