Files
weave-scope/report/node_set_test.go
Paul Bellamy 2c6b6e6707 Refactoring rendering to remove RenderableNode
Squash of:
- use detailed.Summaries to render topology nodes
- ban merging nodes of different topologies (they should be mapped)
- need to prune parents when mapping node types
- render container images by id if they have no name
- remove separate render ids and prune parents in NewDerived*
- don't render metrics/metadata for groups of nodes
- fixing up tests
- removing pending unit tests (for mapping.go, for now)
- updating experimental dir for RenderableNode removal
2016-03-29 14:13:03 +01:00

250 lines
6.5 KiB
Go

package report_test
import (
"fmt"
"testing"
"github.com/weaveworks/scope/report"
"github.com/weaveworks/scope/test/reflect"
)
var benchmarkResult report.NodeSet
type nodeSpec struct {
id string
}
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().WithID(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 BenchmarkMakeNodeSet(b *testing.B) {
nodes := []report.Node{}
for i := 1000; i >= 0; i-- {
nodes = append(nodes, report.MakeNode().WithID(fmt.Sprint(i)).WithLatests(map[string]string{
"a": "1",
"b": "2",
}))
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchmarkResult = report.MakeNodeSet(nodes...)
}
}
func TestNodeSetAdd(t *testing.T) {
for _, testcase := range []struct {
input report.NodeSet
nodes []report.Node
want report.NodeSet
}{
{
input: report.NodeSet{},
nodes: []report.Node{},
want: report.NodeSet{},
},
{
input: report.EmptyNodeSet,
nodes: []report.Node{},
want: report.EmptyNodeSet,
},
{
input: report.MakeNodeSet(report.MakeNode().WithID("a")),
nodes: []report.Node{},
want: report.MakeNodeSet(report.MakeNode().WithID("a")),
},
{
input: report.EmptyNodeSet,
nodes: []report.Node{report.MakeNode().WithID("a")},
want: report.MakeNodeSet(report.MakeNode().WithID("a")),
},
{
input: report.MakeNodeSet(report.MakeNode().WithID("a")),
nodes: []report.Node{report.MakeNode().WithID("a")},
want: report.MakeNodeSet(report.MakeNode().WithID("a")),
},
{
input: report.MakeNodeSet(report.MakeNode().WithID("b")),
nodes: []report.Node{
report.MakeNode().WithID("a"),
report.MakeNode().WithID("b"),
},
want: report.MakeNodeSet(
report.MakeNode().WithID("a"),
report.MakeNode().WithID("b"),
),
},
{
input: report.MakeNodeSet(report.MakeNode().WithID("a")),
nodes: []report.Node{
report.MakeNode().WithID("c"),
report.MakeNode().WithID("b"),
},
want: report.MakeNodeSet(
report.MakeNode().WithID("a"),
report.MakeNode().WithID("b"),
report.MakeNode().WithID("c"),
),
},
{
input: report.MakeNodeSet(
report.MakeNode().WithID("a"),
report.MakeNode().WithID("c"),
),
nodes: []report.Node{
report.MakeNode().WithID("b"),
report.MakeNode().WithID("b"),
report.MakeNode().WithID("b"),
},
want: report.MakeNodeSet(
report.MakeNode().WithID("a"),
report.MakeNode().WithID("b"),
report.MakeNode().WithID("c"),
),
},
} {
originalLen := testcase.input.Size()
if want, have := testcase.want, testcase.input.Add(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)
}
}
}
func BenchmarkNodeSetAdd(b *testing.B) {
n := report.EmptyNodeSet
for i := 0; i < 600; i++ {
n = n.Add(
report.MakeNode().WithID(fmt.Sprint(i)).WithLatests(map[string]string{
"a": "1",
"b": "2",
}),
)
}
node := report.MakeNode().WithID("401.5").WithLatests(map[string]string{
"a": "1",
"b": "2",
})
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchmarkResult = n.Add(node)
}
}
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().WithID("a")),
other: report.EmptyNodeSet,
want: report.MakeNodeSet(report.MakeNode().WithID("a")),
},
{
input: report.EmptyNodeSet,
other: report.MakeNodeSet(report.MakeNode().WithID("a")),
want: report.MakeNodeSet(report.MakeNode().WithID("a")),
},
{
input: report.MakeNodeSet(report.MakeNode().WithID("a")),
other: report.MakeNodeSet(report.MakeNode().WithID("b")),
want: report.MakeNodeSet(report.MakeNode().WithID("a"), report.MakeNode().WithID("b")),
},
{
input: report.MakeNodeSet(report.MakeNode().WithID("b")),
other: report.MakeNodeSet(report.MakeNode().WithID("a")),
want: report.MakeNodeSet(report.MakeNode().WithID("a"), report.MakeNode().WithID("b")),
},
{
input: report.MakeNodeSet(report.MakeNode().WithID("a")),
other: report.MakeNodeSet(report.MakeNode().WithID("a")),
want: report.MakeNodeSet(report.MakeNode().WithID("a")),
},
{
input: report.MakeNodeSet(report.MakeNode().WithID("a"), report.MakeNode().WithID("c")),
other: report.MakeNodeSet(report.MakeNode().WithID("a"), report.MakeNode().WithID("b")),
want: report.MakeNodeSet(report.MakeNode().WithID("a"), report.MakeNode().WithID("b"), report.MakeNode().WithID("c")),
},
{
input: report.MakeNodeSet(report.MakeNode().WithID("b")),
other: report.MakeNodeSet(report.MakeNode().WithID("a")),
want: report.MakeNodeSet(report.MakeNode().WithID("a"), report.MakeNode().WithID("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)
}
}
}
func BenchmarkNodeSetMerge(b *testing.B) {
n, other := report.NodeSet{}, report.NodeSet{}
for i := 0; i < 600; i++ {
n = n.Add(
report.MakeNode().WithID(fmt.Sprint(i)).WithLatests(map[string]string{
"a": "1",
"b": "2",
}),
)
}
for i := 400; i < 1000; i++ {
other = other.Add(
report.MakeNode().WithID(fmt.Sprint(i)).WithLatests(map[string]string{
"c": "1",
"d": "2",
}),
)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchmarkResult = n.Merge(other)
}
}