示例#1
0
func TestMakeRenderableNodes(t *testing.T) {

	var (
		newu64     = func(value uint64) *uint64 { return &value }
		srcNodeID  = "srcNode"
		dstNode1ID = "dstNode1"
		dstNode2ID = "dstNode2"
		srcNode    = report.MakeNode().
				WithEdge(dstNode1ID, report.EdgeMetadata{EgressPacketCount: newu64(100), EgressByteCount: newu64(1000)}).
				WithEdge(dstNode2ID, report.EdgeMetadata{EgressPacketCount: newu64(200), EgressByteCount: newu64(2000)})
		dstNode1 = report.MakeNode()
		dstNode2 = report.MakeNode()
		topology = report.MakeTopology().
				AddNode(srcNodeID, srcNode).
				AddNode(dstNode1ID, dstNode1).
				AddNode(dstNode2ID, dstNode2)
	)

	result := render.MakeRenderableNodes(topology)
	mustLookup := func(id string) render.RenderableNode {
		node, ok := result[id]
		if !ok {
			t.Fatalf("Expected result to contain node: %q, got: %v", id, result)
		}
		return node
	}

	// Source nodes should have the flattened edge metadata
	{
		have := mustLookup(srcNodeID).EdgeMetadata
		want := report.EdgeMetadata{EgressPacketCount: newu64(300), EgressByteCount: newu64(3000)}
		if !reflect.DeepEqual(want, have) {
			t.Errorf(test.Diff(want, have))
		}
	}

	// Result destination nodes should have the reverse of the source nodes
	{
		have := mustLookup(dstNode1ID).EdgeMetadata
		want := report.EdgeMetadata{IngressPacketCount: newu64(100), IngressByteCount: newu64(1000)}
		if !reflect.DeepEqual(want, have) {
			t.Errorf(test.Diff(want, have))
		}

		have = mustLookup(dstNode2ID).EdgeMetadata
		want = report.EdgeMetadata{IngressPacketCount: newu64(200), IngressByteCount: newu64(2000)}
		if !reflect.DeepEqual(want, have) {
			t.Errorf(test.Diff(want, have))
		}
	}
}
示例#2
0
func TestMapEdge(t *testing.T) {
	selector := render.TopologySelector(func(_ report.Report) render.RenderableNodes {
		return render.MakeRenderableNodes(report.Topology{
			Nodes: report.Nodes{
				"foo": report.MakeNode().WithMetadata(map[string]string{
					"id": "foo",
				}).WithEdge("bar", report.EdgeMetadata{
					EgressPacketCount: newu64(1),
					EgressByteCount:   newu64(2),
				}),

				"bar": report.MakeNode().WithMetadata(map[string]string{
					"id": "bar",
				}).WithEdge("foo", report.EdgeMetadata{
					EgressPacketCount: newu64(3),
					EgressByteCount:   newu64(4),
				}),
			},
		})
	})

	mapper := render.Map{
		MapFunc: func(node render.RenderableNode, _ report.Networks) render.RenderableNodes {
			id := "_" + node.ID
			return render.RenderableNodes{id: render.NewDerivedNode(id, node)}
		},
		Renderer: selector,
	}

	have := mapper.Render(report.MakeReport()).Prune()
	want := (render.RenderableNodes{
		"_foo": {
			ID:      "_foo",
			Origins: report.MakeIDList("foo"),
			Node:    report.MakeNode().WithAdjacent("_bar"),
			EdgeMetadata: report.EdgeMetadata{
				EgressPacketCount:  newu64(1),
				EgressByteCount:    newu64(2),
				IngressPacketCount: newu64(3),
				IngressByteCount:   newu64(4),
			},
		},
		"_bar": {
			ID:      "_bar",
			Origins: report.MakeIDList("bar"),
			Node:    report.MakeNode().WithAdjacent("_foo"),
			EdgeMetadata: report.EdgeMetadata{
				EgressPacketCount:  newu64(3),
				EgressByteCount:    newu64(4),
				IngressPacketCount: newu64(1),
				IngressByteCount:   newu64(2),
			},
		},
	}).Prune()
	if !reflect.DeepEqual(want, have) {
		t.Error(test.Diff(want, have))
	}

	if want, have := (report.EdgeMetadata{
		EgressPacketCount: newu64(1),
		EgressByteCount:   newu64(2),
	}), mapper.EdgeMetadata(report.MakeReport(), "_foo", "_bar"); !reflect.DeepEqual(want, have) {
		t.Error(test.Diff(want, have))
	}
}