diff --git a/test/kubernetes_unittest.cc b/test/kubernetes_unittest.cc index 79e4ac94..23777536 100644 --- a/test/kubernetes_unittest.cc +++ b/test/kubernetes_unittest.cc @@ -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) { @@ -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. + 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()); + EXPECT_EQ(expected_associations->ToString(), associations->ToString()); +} } // namespace google