Skip to content

Commit

Permalink
Adding tests for ContainerHostnameRenderer and PodServiceRenderer with
Browse files Browse the repository at this point in the history
filters
  • Loading branch information
paulbellamy committed Apr 22, 2016
1 parent 396c3d3 commit 465aeb0
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 12 deletions.
36 changes: 34 additions & 2 deletions render/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{
Expand Down
50 changes: 41 additions & 9 deletions render/expected/expected.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand Down
29 changes: 29 additions & 0 deletions render/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
}
}
8 changes: 7 additions & 1 deletion test/fixture/report_fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 465aeb0

Please sign in to comment.