From a8525ddbcfa6c629a6487077f2de45821c8f4efc Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Fri, 22 Apr 2016 16:18:17 +0100 Subject: [PATCH] Adding tests for ContainerHostnameRenderer and PodServiceRenderer with filters --- render/container_test.go | 36 +++++++++++++++++++++-- render/expected/expected.go | 52 +++++++++++++++++++++++++++------- render/pod_test.go | 29 +++++++++++++++++++ test/fixture/report_fixture.go | 8 +++++- 4 files changed, 112 insertions(+), 13 deletions(-) diff --git a/render/container_test.go b/render/container_test.go index 828114ba3a..d833b171d5 100644 --- a/render/container_test.go +++ b/render/container_test.go @@ -91,6 +91,40 @@ func TestContainerWithHostIPsRenderer(t *testing.T) { } } +func TestContainerHostnameRenderer(t *testing.T) { + have := Prune(render.ContainerHostnameRenderer(render.FilterNoop).Render(fixture.Report)) + want := Prune(expected.RenderedContainerHostnames) + if !reflect.DeepEqual(want, have) { + t.Error(test.Diff(want, have)) + } +} + +func TestContainerHostnameFilterRenderer(t *testing.T) { + // add a system container into the topology and ensure + // it is filtered out correctly. + input := fixture.Report.Copy() + + clientContainer2ID := "f6g7h8i9j1" + clientContainer2NodeID := report.MakeContainerNodeID(clientContainer2ID) + input.Container.AddNode(report.MakeNodeWith(clientContainer2NodeID, map[string]string{ + docker.LabelPrefix + "works.weave.role": "system", + docker.ContainerHostname: fixture.ClientContainerHostname, + report.HostNodeID: fixture.ClientHostNodeID, + }). + WithParents(report.EmptySets. + Add("host", report.MakeStringSet(fixture.ClientHostNodeID)), + ).WithTopology(report.Container)) + + have := Prune(render.ContainerHostnameRenderer(render.FilterSystem).Render(input)) + want := Prune(expected.RenderedContainerHostnames) + // Test works by virtue of the RenderedContainerHostname only having a container + // counter == 1 + + if !reflect.DeepEqual(want, have) { + t.Error(test.Diff(want, have)) + } +} + func TestContainerImageRenderer(t *testing.T) { have := Prune(render.ContainerImageRenderer(render.FilterNoop).Render(fixture.Report)) want := Prune(expected.RenderedContainerImages) @@ -104,8 +138,6 @@ func TestContainerImageFilterRenderer(t *testing.T) { // it is filtered out correctly. input := fixture.Report.Copy() - // TODO: Add a process and endpoint here to make this test fail, so we can fix it. - clientContainer2ID := "f6g7h8i9j1" clientContainer2NodeID := report.MakeContainerNodeID(clientContainer2ID) input.Container.AddNode(report.MakeNodeWith(clientContainer2NodeID, map[string]string{ diff --git a/render/expected/expected.go b/render/expected/expected.go index 88a69fc308..9e58d32938 100644 --- a/render/expected/expected.go +++ b/render/expected/expected.go @@ -27,15 +27,16 @@ var ( return n } } - pseudo = node(render.Pseudo) - endpoint = node(report.Endpoint) - processNode = node(report.Process) - processNameNode = node(render.MakeGroupNodeTopology(report.Process, process.Name)) - container = node(report.Container) - containerImage = node(report.ContainerImage) - pod = node(report.Pod) - service = node(report.Service) - hostNode = node(report.Host) + pseudo = node(render.Pseudo) + endpoint = node(report.Endpoint) + processNode = node(report.Process) + processNameNode = node(render.MakeGroupNodeTopology(report.Process, process.Name)) + container = node(report.Container) + containerHostnameNode = node(render.MakeGroupNodeTopology(report.Container, docker.ContainerHostname)) + containerImage = node(report.ContainerImage) + pod = node(report.Pod) + service = node(report.Service) + hostNode = node(report.Host) UnknownPseudoNode1ID = render.MakePseudoNodeID(fixture.UnknownClient1IP) UnknownPseudoNode2ID = render.MakePseudoNodeID(fixture.UnknownClient3IP) @@ -176,6 +177,37 @@ var ( render.OutgoingInternetID: theOutgoingInternetNode, } + RenderedContainerHostnames = report.Nodes{ + fixture.ClientContainerHostname: containerHostnameNode(fixture.ClientContainerHostname, fixture.ServerContainerHostname). + WithLatests(map[string]string{ + docker.ContainerHostname: fixture.ClientContainerHostname, + }). + WithCounters(map[string]int{ + report.Container: 1, + }). + WithChildren(report.MakeNodeSet( + RenderedEndpoints[fixture.Client54001NodeID], + RenderedEndpoints[fixture.Client54002NodeID], + RenderedProcesses[fixture.ClientProcess1NodeID], + RenderedProcesses[fixture.ClientProcess2NodeID], + RenderedContainers[fixture.ClientContainerNodeID], + )), + + fixture.ServerContainerHostname: containerHostnameNode(fixture.ServerContainerHostname). + WithLatests(map[string]string{ + docker.ContainerHostname: fixture.ServerContainerHostname, + }). + WithChildren(report.MakeNodeSet( + RenderedEndpoints[fixture.Server80NodeID], + RenderedProcesses[fixture.ServerProcessNodeID], + RenderedContainers[fixture.ServerContainerNodeID], + )), + + uncontainedServerID: uncontainedServerNode, + render.IncomingInternetID: theIncomingInternetNode(fixture.ServerContainerHostname), + render.OutgoingInternetID: theOutgoingInternetNode, + } + RenderedContainerImages = report.Nodes{ fixture.ClientContainerImageNodeID: containerImage(fixture.ClientContainerImageNodeID, fixture.ServerContainerImageNodeID). WithLatests(map[string]string{ @@ -236,7 +268,7 @@ var ( } RenderedPodServices = report.Nodes{ - fixture.ServiceNodeID: service(fixture.ServiceNodeID, fixture.ServiceNodeID). + fixture.ServiceNodeID: service(fixture.ServiceNodeID). WithChildren(report.MakeNodeSet( RenderedEndpoints[fixture.Client54001NodeID], RenderedEndpoints[fixture.Client54002NodeID], diff --git a/render/pod_test.go b/render/pod_test.go index 16bde1b32d..9cd5cba441 100644 --- a/render/pod_test.go +++ b/render/pod_test.go @@ -7,6 +7,7 @@ import ( "github.com/weaveworks/scope/probe/kubernetes" "github.com/weaveworks/scope/render" "github.com/weaveworks/scope/render/expected" + "github.com/weaveworks/scope/report" "github.com/weaveworks/scope/test" "github.com/weaveworks/scope/test/fixture" "github.com/weaveworks/scope/test/reflect" @@ -48,3 +49,31 @@ func TestPodServiceRenderer(t *testing.T) { t.Error(test.Diff(want, have)) } } + +func TestPodServiceFilterRenderer(t *testing.T) { + // tag on containers or pod namespace in the topology and ensure + // it is filtered out correctly. + input := fixture.Report.Copy() + input.Pod.Nodes[fixture.ClientPodNodeID] = input.Pod.Nodes[fixture.ClientPodNodeID].WithLatests(map[string]string{ + kubernetes.PodID: "pod:kube-system/foo", + kubernetes.Namespace: "kube-system", + kubernetes.PodName: "foo", + }) + input.Container.Nodes[fixture.ClientContainerNodeID] = input.Container.Nodes[fixture.ClientContainerNodeID].WithLatests(map[string]string{ + docker.LabelPrefix + "io.kubernetes.pod.name": "kube-system/foo", + }) + have := Prune(render.PodServiceRenderer(render.FilterSystem).Render(input)) + want := Prune(expected.RenderedPodServices.Copy()) + wantNode := want[fixture.ServiceNodeID] + wantNode.Adjacency = nil + wantNode.Children = report.MakeNodeSet( + expected.RenderedEndpoints[fixture.Server80NodeID], + expected.RenderedProcesses[fixture.ServerProcessNodeID], + expected.RenderedContainers[fixture.ServerContainerNodeID], + expected.RenderedPods[fixture.ServerPodNodeID], + ) + want[fixture.ServiceNodeID] = wantNode + if !reflect.DeepEqual(want, have) { + t.Error(test.Diff(want, have)) + } +} diff --git a/test/fixture/report_fixture.go b/test/fixture/report_fixture.go index 7427e465cd..f6dc2f9beb 100644 --- a/test/fixture/report_fixture.go +++ b/test/fixture/report_fixture.go @@ -75,9 +75,13 @@ var ( ClientContainerID = "a1b2c3d4e5" ClientContainerName = "client" ServerContainerID = "5e4d3c2b1a" + ServerContainerName = "task-name-5-server-aceb93e2f2b797caba01" ClientContainerNodeID = report.MakeContainerNodeID(ClientContainerID) ServerContainerNodeID = report.MakeContainerNodeID(ServerContainerID) + ClientContainerHostname = ClientContainerName + ".hostname.com" + ServerContainerHostname = ServerContainerName + ".hostname.com" + ClientContainerImageID = "imageid123" ServerContainerImageID = "imageid456" ClientContainerImageNodeID = report.MakeContainerImageNodeID(ClientContainerImageID) @@ -256,6 +260,7 @@ var ( ClientContainerNodeID, map[string]string{ docker.ContainerID: ClientContainerID, docker.ContainerName: ClientContainerName, + docker.ContainerHostname: ClientContainerHostname, docker.ImageID: ClientContainerImageID, report.HostNodeID: ClientHostNodeID, docker.LabelPrefix + "io.kubernetes.pod.name": ClientPodID, @@ -276,7 +281,8 @@ var ( ServerContainerNodeID, map[string]string{ docker.ContainerID: ServerContainerID, - docker.ContainerName: "task-name-5-server-aceb93e2f2b797caba01", + docker.ContainerName: ServerContainerName, + docker.ContainerHostname: ServerContainerHostname, docker.ContainerState: docker.StateRunning, docker.ContainerStateHuman: docker.StateRunning, docker.ImageID: ServerContainerImageID,