Prune is only used in tests, so let's move it there

This commit is contained in:
Paul Bellamy
2016-04-21 14:48:33 +01:00
parent 1539e668e7
commit e7ebb83e23
8 changed files with 46 additions and 46 deletions

View File

@@ -47,8 +47,8 @@ func testMap(t *testing.T, f render.MapFunc, input testcase) {
}
func TestContainerRenderer(t *testing.T) {
have := render.ContainerRenderer.Render(fixture.Report).Prune()
want := expected.RenderedContainers.Prune()
have := Prune(render.ContainerRenderer.Render(fixture.Report))
want := Prune(expected.RenderedContainers)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}
@@ -61,8 +61,8 @@ func TestContainerFilterRenderer(t *testing.T) {
input.Container.Nodes[fixture.ClientContainerNodeID] = input.Container.Nodes[fixture.ClientContainerNodeID].WithLatests(map[string]string{
docker.LabelPrefix + "works.weave.role": "system",
})
have := render.FilterSystem(render.ContainerRenderer).Render(input).Prune()
want := expected.RenderedContainers.Copy().Prune()
have := Prune(render.FilterSystem(render.ContainerRenderer).Render(input))
want := Prune(expected.RenderedContainers.Copy())
delete(want, fixture.ClientContainerNodeID)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
@@ -92,8 +92,8 @@ func TestContainerWithHostIPsRenderer(t *testing.T) {
}
func TestContainerImageRenderer(t *testing.T) {
have := render.ContainerImageRenderer.Render(fixture.Report).Prune()
want := expected.RenderedContainerImages.Prune()
have := Prune(render.ContainerImageRenderer.Render(fixture.Report))
want := Prune(expected.RenderedContainerImages)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}

View File

@@ -11,8 +11,8 @@ import (
)
func TestHostRenderer(t *testing.T) {
have := render.HostRenderer.Render(fixture.Report).Prune()
want := expected.RenderedHosts.Prune()
have := Prune(render.HostRenderer.Render(fixture.Report))
want := Prune(expected.RenderedHosts)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}

View File

@@ -13,8 +13,8 @@ import (
)
func TestPodRenderer(t *testing.T) {
have := render.PodRenderer.Render(fixture.Report).Prune()
want := expected.RenderedPods.Prune()
have := Prune(render.PodRenderer.Render(fixture.Report))
want := Prune(expected.RenderedPods)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}
@@ -32,8 +32,8 @@ func TestPodFilterRenderer(t *testing.T) {
input.Container.Nodes[fixture.ClientContainerNodeID] = input.Container.Nodes[fixture.ClientContainerNodeID].WithLatests(map[string]string{
docker.LabelPrefix + "io.kubernetes.pod.name": "kube-system/foo",
})
have := render.FilterSystem(render.PodRenderer).Render(input).Prune()
want := expected.RenderedPods.Copy().Prune()
have := Prune(render.FilterSystem(render.PodRenderer).Render(input))
want := Prune(expected.RenderedPods.Copy())
delete(want, fixture.ClientPodNodeID)
delete(want, fixture.ClientContainerNodeID)
if !reflect.DeepEqual(want, have) {
@@ -42,8 +42,8 @@ func TestPodFilterRenderer(t *testing.T) {
}
func TestPodServiceRenderer(t *testing.T) {
have := render.PodServiceRenderer.Render(fixture.Report).Prune()
want := expected.RenderedPodServices.Prune()
have := Prune(render.PodServiceRenderer.Render(fixture.Report))
want := Prune(expected.RenderedPodServices)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}

View File

@@ -11,24 +11,24 @@ import (
)
func TestEndpointRenderer(t *testing.T) {
have := render.EndpointRenderer.Render(fixture.Report).Prune()
want := expected.RenderedEndpoints.Prune()
have := Prune(render.EndpointRenderer.Render(fixture.Report))
want := Prune(expected.RenderedEndpoints)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}
}
func TestProcessRenderer(t *testing.T) {
have := render.ProcessRenderer.Render(fixture.Report).Prune()
want := expected.RenderedProcesses.Prune()
have := Prune(render.ProcessRenderer.Render(fixture.Report))
want := Prune(expected.RenderedProcesses)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}
}
func TestProcessNameRenderer(t *testing.T) {
have := render.ProcessNameRenderer.Render(fixture.Report).Prune()
want := expected.RenderedProcessNames.Prune()
have := Prune(render.ProcessNameRenderer.Render(fixture.Report))
want := Prune(expected.RenderedProcessNames)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}

View File

@@ -16,6 +16,31 @@ type mockRenderer struct {
func (m mockRenderer) Render(rpt report.Report) report.Nodes { return m.Nodes }
func (m mockRenderer) Stats(rpt report.Report) render.Stats { return render.Stats{} }
// Prune returns a copy of the Nodes with all information not strictly
// necessary for rendering nodes and edges in the UI cut away.
func Prune(nodes report.Nodes) report.Nodes {
result := report.Nodes{}
for id, node := range nodes {
result[id] = PruneNode(node)
}
return result
}
// PruneNode returns a copy of the Node with all information not strictly
// necessary for rendering nodes and edges stripped away. Specifically, that
// means cutting out parts of the Node.
func PruneNode(node report.Node) report.Node {
prunedChildren := report.MakeNodeSet()
node.Children.ForEach(func(child report.Node) {
prunedChildren = prunedChildren.Add(PruneNode(child))
})
return report.MakeNode(
node.ID).
WithTopology(node.Topology).
WithAdjacent(node.Adjacency.Copy()...).
WithChildren(prunedChildren)
}
func TestReduceRender(t *testing.T) {
renderer := render.Reduce([]render.Renderer{
mockRenderer{Nodes: report.Nodes{"foo": report.MakeNode("foo")}},

View File

@@ -73,7 +73,7 @@ var (
)
func TestShortLivedInternetNodeConnections(t *testing.T) {
have := render.ContainerWithImageNameRenderer.Render(rpt).Prune()
have := Prune(render.ContainerWithImageNameRenderer.Render(rpt))
// Conntracked-only connections from the internet should be assigned to the internet pseudonode
internet, ok := have[render.IncomingInternetID]

View File

@@ -214,18 +214,3 @@ func (n Node) Merge(other Node) Node {
cp.Children = cp.Children.Merge(other.Children)
return cp
}
// Prune returns a copy of the Node with all information not strictly necessary
// for rendering nodes and edges stripped away. Specifically, that means
// cutting out parts of the Node.
func (n Node) Prune() Node {
prunedChildren := MakeNodeSet()
n.Children.ForEach(func(child Node) {
prunedChildren = prunedChildren.Add(child.Prune())
})
return MakeNode(
n.ID).
WithTopology(n.Topology).
WithAdjacent(n.Adjacency.Copy()...).
WithChildren(prunedChildren)
}

View File

@@ -185,16 +185,6 @@ func (n Nodes) Merge(other Nodes) Nodes {
return cp
}
// Prune returns a copy of the Nodes with all information not strictly
// necessary for rendering nodes and edges in the UI cut away.
func (n Nodes) Prune() Nodes {
result := Nodes{}
for id, node := range n {
result[id] = node.Prune()
}
return result
}
// Validate checks the topology for various inconsistencies.
func (t Topology) Validate() error {
errs := []string{}