mirror of
https://github.com/weaveworks/scope.git
synced 2026-03-03 02:00:43 +00:00
Merge pull request #903 from weaveworks/immutable-renderablenodes
Add some tests, and a zero-value for report.Sets
This commit is contained in:
62
render/selectors_test.go
Normal file
62
render/selectors_test.go
Normal file
@@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user