Skip to content
This repository was archived by the owner on Aug 19, 2019. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions test/kubernetes_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ class KubernetesTest : public ::testing::Test {
throw(json::Exception) {
return reader.GetNodeMetadata(node, collected_at, is_deleted);
}

json::value ComputePodAssociations(const KubernetesReader& reader,
const json::Object* pod) {
return reader.ComputePodAssociations(pod);
}

void UpdateOwnersCache(KubernetesReader* reader, const std::string& key,
const json::value& value) {
reader->owners_[key] = value->Clone();
}
};

TEST_F(KubernetesTest, GetNodeMetadata) {
Expand Down Expand Up @@ -68,4 +78,63 @@ TEST_F(KubernetesTest, GetNodeMetadata) {
});
EXPECT_EQ(big->ToString(), m.metadata.metadata->ToString());
}

TEST_F(KubernetesTest, ComputePodAssociations) {
Configuration config(std::stringstream(
"KubernetesClusterName: TestClusterName\n"
"KubernetesClusterLocation: TestClusterLocation\n"
"MetadataIngestionRawContentVersion: TestVersion\n"
"InstanceZone: TestZone\n"
"InstanceId: TestID\n"
));
Environment environment(config);
KubernetesReader reader(config, nullptr); // Don't need HealthChecker.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The statement in the comment is kind of obvious from you setting it to nullptr, but if you choose to keep it, please update the other place where you do the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should already be commented in all other locations.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be the only place, FWIW.

json::value controller = json::object({
{"controller", json::boolean(true)},
{"apiVersion", json::string("1.2.3")},
{"kind", json::string("TestKind")},
{"name", json::string("TestName")},
{"uid", json::string("TestUID1")},
{"metadata", json::object({
{"name", json::string("InnerTestName")},
{"kind", json::string("InnerTestKind")},
{"uid", json::string("InnerTestUID1")},
})},
});
UpdateOwnersCache(&reader, "1.2.3/TestKind/TestUID1", controller);
json::value pod = json::object({
{"metadata", json::object({
{"namespace", json::string("TestNamespace")},
{"uid", json::string("TestUID0")},
{"ownerReferences", json::array({
json::object({{"no_controller", json::boolean(true)}}),
std::move(controller),
})},
})},
{"spec", json::object({
{"nodeName", json::string("TestSpecNodeName")},
})},
});

json::value expected_associations = json::object({
{"raw", json::object({
{"controllers", json::object({
{"topLevelControllerName", json::string("InnerTestName")},
{"topLevelControllerType", json::string("TestKind")},
})},
{"infrastructureResource", json::object({
{"labels", json::object({
{"instance_id", json::string("TestID")},
{"zone", json::string("TestZone")},
})},
{"type", json::string("gce_instance")},
})},
{"nodeName", json::string("TestSpecNodeName")},
})},
{"version", json::string("TestVersion")},
});
const auto associations =
ComputePodAssociations(reader, pod->As<json::Object>());
EXPECT_EQ(expected_associations->ToString(), associations->ToString());
}
} // namespace google