diff --git a/render/selectors_test.go b/render/selectors_test.go new file mode 100644 index 000000000..ee01e983b --- /dev/null +++ b/render/selectors_test.go @@ -0,0 +1,62 @@ +package render_test + +import ( + "testing" + + "github.com/weaveworks/scope/render" + "github.com/weaveworks/scope/report" + "github.com/weaveworks/scope/test" + "github.com/weaveworks/scope/test/reflect" +) + +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)) + } + } +} diff --git a/report/edge_metadatas_internal_test.go b/report/edge_metadatas_internal_test.go index b6277a50a..758398939 100644 --- a/report/edge_metadatas_internal_test.go +++ b/report/edge_metadatas_internal_test.go @@ -282,3 +282,34 @@ func TestEdgeMetadatasEncodingNil(t *testing.T) { } func newu64(value uint64) *uint64 { return &value } + +func TestEdgeMetadataDeepEquals(t *testing.T) { + for _, c := range []struct { + name string + a, b interface{} + want bool + }{ + { + name: "zero values", + a: EdgeMetadata{}, + b: EdgeMetadata{}, + want: true, + }, + { + name: "matching, but different pointers", + a: EdgeMetadata{EgressPacketCount: newu64(3)}, + b: EdgeMetadata{EgressPacketCount: newu64(3)}, + want: true, + }, + { + name: "mismatching", + a: EdgeMetadata{EgressPacketCount: newu64(3)}, + b: EdgeMetadata{EgressPacketCount: newu64(4)}, + want: false, + }, + } { + if have := reflect.DeepEqual(c.a, c.b); have != c.want { + t.Errorf("reflect.DeepEqual(%v, %v) != %v", c.a, c.b, c.want) + } + } +} diff --git a/report/sets.go b/report/sets.go index 77067f9df..96d8cabec 100644 --- a/report/sets.go +++ b/report/sets.go @@ -27,11 +27,17 @@ func MakeSets() Sets { // Keys returns the keys for this set func (s Sets) Keys() []string { + if s.psMap == nil { + return nil + } return s.psMap.Keys() } // Add the given value to the Sets. func (s Sets) Add(key string, value StringSet) Sets { + if s.psMap == nil { + s = EmptySets + } if existingValue, ok := s.psMap.Lookup(key); ok { value = value.Merge(existingValue.(StringSet)) } @@ -42,6 +48,9 @@ func (s Sets) Add(key string, value StringSet) Sets { // Lookup returns the sets stored under key. func (s Sets) Lookup(key string) (StringSet, bool) { + if s.psMap == nil { + return EmptyStringSet, false + } if value, ok := s.psMap.Lookup(key); ok { return value.(StringSet), true } @@ -50,6 +59,9 @@ func (s Sets) Lookup(key string) (StringSet, bool) { // Size returns the number of elements func (s Sets) Size() int { + if s.psMap == nil { + return 0 + } return s.psMap.Size() } @@ -88,6 +100,9 @@ func (s Sets) Copy() Sets { } func (s Sets) String() string { + if s.psMap == nil { + s = EmptySets + } keys := []string{} for _, k := range s.psMap.Keys() { keys = append(keys, k) @@ -105,9 +120,12 @@ func (s Sets) String() string { // DeepEqual tests equality with other Sets func (s Sets) DeepEqual(t Sets) bool { - if s.psMap.Size() != t.psMap.Size() { + if s.Size() != t.Size() { return false } + if s.Size() == 0 { + return true + } equal := true s.psMap.ForEach(func(k string, val interface{}) { @@ -122,9 +140,11 @@ func (s Sets) DeepEqual(t Sets) bool { func (s Sets) toIntermediate() map[string]StringSet { intermediate := map[string]StringSet{} - s.psMap.ForEach(func(key string, val interface{}) { - intermediate[key] = val.(StringSet) - }) + if s.psMap != nil { + s.psMap.ForEach(func(key string, val interface{}) { + intermediate[key] = val.(StringSet) + }) + } return intermediate }