Skip to content

Commit

Permalink
change tag
Browse files Browse the repository at this point in the history
  • Loading branch information
NameHaibinZhang committed Nov 14, 2024
1 parent ee6bb37 commit 51b4c53
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/metadata/K8sMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
}

bool K8sMetadata::FromInfoJson(const Json::Value& json, k8sContainerInfo& info) {
if (!json.isMember(TAG_IMAGES) || !json.isMember("labels") || !json.isMember(TAG_NAMESPACE)
if (!json.isMember(TAG_IMAGES) || !json.isMember(TAG_LABELS) || !json.isMember(TAG_NAMESPACE)
|| !json.isMember(TAG_WORKLOADKIND) || !json.isMember(TAG_WORKLOADNAME)) {
return false;
}
Expand Down
29 changes: 29 additions & 0 deletions core/unittest/metadata/K8sMetadataUnittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,34 @@ class k8sMetadataUnittest : public ::testing::Test {
}

public:

void TestFromInfoJson() {
// 创建Json测试数据
Json::Value testJson;
testJson["images"]["image1"] = "nginx:latest";
testJson["labels"]["app_id"] = "my_app_id";
testJson["namespace"] = "default";
testJson["workloadKind"] = "Deployment";
testJson["workloadName"] = "nginx-deployment";
testJson["serviceName"] = "nginx-service";

k8sContainerInfo info;
auto& k8sMetadata = K8sMetadata::GetInstance();

// 调用函数
bool result = k8sMetadata.FromInfoJson(testJson, info);

// 验证结果
EXPECT_TRUE(result);
EXPECT_EQ(info.images["image1"], "nginx:latest");
EXPECT_EQ(info.labels["app_id"], "my_app_id");
EXPECT_EQ(info.appId, "my_app_id");
EXPECT_EQ(info.k8sNamespace, "default");
EXPECT_EQ(info.workloadKind, "Deployment");
EXPECT_EQ(info.workloadName, "nginx-deployment");
EXPECT_EQ(info.serviceName, "nginx-service");
}

void TestGetByContainerIds() {
LOG_INFO(sLogger, ("TestGetByContainerIds() begin", time(NULL)));
const std::string jsonData = R"({"containerd://286effd2650c0689b779018e42e9ec7aa3d2cb843005e038204e85fc3d4f9144":{"namespace":"default","workloadName":"oneagent-demo-658648895b","workloadKind":"replicaset","serviceName":"","labels":{"app":"oneagent-demo","pod-template-hash":"658648895b"},"envs":{},""images"":{"oneagent-demo":"sls-opensource-registry.cn-shanghai.cr.aliyuncs.com/ilogtail-community-edition/centos7-cve-fix:1.0.0"}}})";
Expand Down Expand Up @@ -421,6 +449,7 @@ APSARA_UNIT_TEST_CASE(k8sMetadataUnittest, TestGetByContainerIds, 0);
APSARA_UNIT_TEST_CASE(k8sMetadataUnittest, TestGetByLocalHost, 1);
APSARA_UNIT_TEST_CASE(k8sMetadataUnittest, TestAddLabelToMetric, 2);
APSARA_UNIT_TEST_CASE(k8sMetadataUnittest, TestAddLabelToSpan, 3);
APSARA_UNIT_TEST_CASE(k8sMetadataUnittest, TestFromInfoJson, 4);



Expand Down

0 comments on commit 51b4c53

Please sign in to comment.