func TestMakeNodeSet(t *testing.T) { for _, testcase := range []struct { inputs []string wants []string }{ {inputs: nil, wants: nil}, { inputs: []string{"a"}, wants: []string{"a"}, }, { inputs: []string{"b", "c", "a"}, wants: []string{"a", "b", "c"}, }, { inputs: []string{"a", "a", "a"}, wants: []string{"a"}, }, } { var inputs []report.Node for _, id := range testcase.inputs { inputs = append(inputs, report.MakeNode(id)) } set := report.MakeNodeSet(inputs...) var have []string set.ForEach(func(node report.Node) { have = append(have, node.ID) }) if !reflect.DeepEqual(testcase.wants, have) { t.Errorf("%#v: want %#v, have %#v", testcase.inputs, testcase.wants, have) } } }
func TestNodeSetDelete(t *testing.T) { for _, testcase := range []struct { input report.NodeSet nodes []string want report.NodeSet }{ { input: report.NodeSet{}, nodes: []string{}, want: report.NodeSet{}, }, { input: report.EmptyNodeSet, nodes: []string{}, want: report.EmptyNodeSet, }, { input: report.MakeNodeSet(report.MakeNode("a")), nodes: []string{}, want: report.MakeNodeSet(report.MakeNode("a")), }, { input: report.EmptyNodeSet, nodes: []string{"a"}, want: report.EmptyNodeSet, }, { input: report.MakeNodeSet(report.MakeNode("a")), nodes: []string{"a"}, want: report.EmptyNodeSet, }, { input: report.MakeNodeSet(report.MakeNode("b")), nodes: []string{"a", "b"}, want: report.EmptyNodeSet, }, { input: report.MakeNodeSet(report.MakeNode("a")), nodes: []string{"c", "b"}, want: report.MakeNodeSet(report.MakeNode("a")), }, { input: report.MakeNodeSet(report.MakeNode("a"), report.MakeNode("c")), nodes: []string{"a", "a", "a"}, want: report.MakeNodeSet(report.MakeNode("c")), }, } { originalLen := testcase.input.Size() if want, have := testcase.want, testcase.input.Delete(testcase.nodes...); !reflect.DeepEqual(want, have) { t.Errorf("%v + %v: want %v, have %v", testcase.input, testcase.nodes, want, have) } if testcase.input.Size() != originalLen { t.Errorf("%v + %v: modified the original input!", testcase.input, testcase.nodes) } } }
// 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 BenchmarkMakeNodeSet(b *testing.B) { nodes := []report.Node{} for i := 1000; i >= 0; i-- { nodes = append(nodes, report.MakeNodeWith(fmt.Sprint(i), map[string]string{ "a": "1", "b": "2", })) } b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { benchmarkResult = report.MakeNodeSet(nodes...) } }
// MapX2Host maps any Nodes to host Nodes. // // If this function is given a node without a hostname // (including other pseudo nodes), it will drop the node. // // Otherwise, this function will produce a node with the correct ID // format for a container, but without any Major or Minor labels. // It does not have enough info to do that, and the resulting graph // must be merged with a container graph to get that info. func MapX2Host(n report.Node, _ report.Networks) report.Nodes { // Don't propagate all pseudo nodes - we do this in MapEndpoint2Host if n.Topology == Pseudo { return report.Nodes{} } hostNodeID, timestamp, ok := n.Latest.LookupEntry(report.HostNodeID) if !ok { return report.Nodes{} } id := report.MakeHostNodeID(report.ExtractHostID(n)) result := NewDerivedNode(id, n).WithTopology(report.Host) result.Latest = result.Latest.Set(report.HostNodeID, timestamp, hostNodeID) result.Counters = result.Counters.Add(n.Topology, 1) result.Children = report.MakeNodeSet(n) return report.Nodes{id: result} }
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) unknownPseudoNode1 = func(adjacent ...string) report.Node { return pseudo(UnknownPseudoNode1ID, adjacent...). WithChildren(report.MakeNodeSet( RenderedEndpoints[fixture.UnknownClient1NodeID], RenderedEndpoints[fixture.UnknownClient2NodeID], )) } unknownPseudoNode2 = func(adjacent ...string) report.Node { return pseudo(UnknownPseudoNode2ID, adjacent...). WithChildren(report.MakeNodeSet( RenderedEndpoints[fixture.UnknownClient3NodeID], )) } theIncomingInternetNode = func(adjacent ...string) report.Node { return pseudo(render.IncomingInternetID, adjacent...). WithChildren(report.MakeNodeSet( RenderedEndpoints[fixture.RandomClientNodeID], )) }
func TestNodeSetMerge(t *testing.T) { for _, testcase := range []struct { input report.NodeSet other report.NodeSet want report.NodeSet }{ {input: report.NodeSet{}, other: report.NodeSet{}, want: report.NodeSet{}}, {input: report.EmptyNodeSet, other: report.EmptyNodeSet, want: report.EmptyNodeSet}, { input: report.MakeNodeSet(report.MakeNode("a")), other: report.EmptyNodeSet, want: report.MakeNodeSet(report.MakeNode("a")), }, { input: report.EmptyNodeSet, other: report.MakeNodeSet(report.MakeNode("a")), want: report.MakeNodeSet(report.MakeNode("a")), }, { input: report.MakeNodeSet(report.MakeNode("a")), other: report.MakeNodeSet(report.MakeNode("b")), want: report.MakeNodeSet(report.MakeNode("a"), report.MakeNode("b")), }, { input: report.MakeNodeSet(report.MakeNode("b")), other: report.MakeNodeSet(report.MakeNode("a")), want: report.MakeNodeSet(report.MakeNode("a"), report.MakeNode("b")), }, { input: report.MakeNodeSet(report.MakeNode("a")), other: report.MakeNodeSet(report.MakeNode("a")), want: report.MakeNodeSet(report.MakeNode("a")), }, { input: report.MakeNodeSet(report.MakeNode("a"), report.MakeNode("c")), other: report.MakeNodeSet(report.MakeNode("a"), report.MakeNode("b")), want: report.MakeNodeSet(report.MakeNode("a"), report.MakeNode("b"), report.MakeNode("c")), }, { input: report.MakeNodeSet(report.MakeNode("b")), other: report.MakeNodeSet(report.MakeNode("a")), want: report.MakeNodeSet(report.MakeNode("a"), report.MakeNode("b")), }, } { originalLen := testcase.input.Size() if want, have := testcase.want, testcase.input.Merge(testcase.other); !reflect.DeepEqual(want, have) { t.Errorf("%v + %v: want %v, have %v", testcase.input, testcase.other, want, have) } if testcase.input.Size() != originalLen { t.Errorf("%v + %v: modified the original input!", testcase.input, testcase.other) } } }