refactor: hide "empty set" constants

They are an implementation detail.
This commit is contained in:
Matthias Radestock
2017-07-03 00:01:36 +01:00
parent c5b2c9dc3d
commit 9dc50b5202
32 changed files with 157 additions and 165 deletions

View File

@@ -35,7 +35,7 @@ function generate_latest_map() {
local lowercase_data_type="${data_type,}"
local entry_type="${lowercase_data_type}LatestEntry"
local latest_map_type="${uppercase_data_type}LatestMap"
local empty_latest_map_variable="Empty${latest_map_type}"
local empty_latest_map_variable="empty${latest_map_type}"
local make_function="Make${latest_map_type}"
# shellcheck disable=SC2016
@@ -63,7 +63,6 @@ function generate_latest_map() {
// ${latest_map_type} holds latest ${data_type} instances.
type ${latest_map_type} struct { ps.Map }
// ${empty_latest_map_variable} is an empty ${latest_map_type}. Start with this.
var ${empty_latest_map_variable} = ${latest_map_type}{ps.NewMap()}
// ${make_function} makes an empty ${latest_map_type}.

View File

@@ -303,7 +303,7 @@ func (c *container) NetworkInfo(localAddrs []net.IP) report.Sets {
// Treat all Docker IPs as local scoped.
ipsWithScopes := addScopeToIPs(c.hostID, ipv4s)
return report.EmptySets.
return report.MakeSets().
Add(ContainerNetworks, report.MakeStringSet(networks...)).
Add(ContainerPorts, c.ports(localAddrs)).
Add(ContainerIPs, report.MakeStringSet(ipv4s...)).
@@ -388,7 +388,7 @@ func (c *container) getBaseNode() report.Node {
ContainerCommand: c.getSanitizedCommand(),
ImageID: c.Image(),
ContainerHostname: c.Hostname(),
}).WithParents(report.EmptySets.
}).WithParents(report.MakeSets().
Add(report.ContainerImage, report.MakeStringSet(report.MakeContainerImageNodeID(c.Image()))),
)
result = result.AddPrefixPropertyList(LabelPrefix, c.container.Config.Labels)

View File

@@ -86,7 +86,7 @@ func TestContainer(t *testing.T) {
).WithMetrics(report.Metrics{
"docker_cpu_total_usage": report.MakeMetric(nil),
"docker_memory_usage": report.MakeSingletonMetric(now, 12345).WithMax(45678),
}).WithParents(report.EmptySets.
}).WithParents(report.MakeSets().
Add(report.ContainerImage, report.MakeStringSet(report.MakeContainerImageNodeID("baz"))),
)
@@ -102,7 +102,7 @@ func TestContainer(t *testing.T) {
}
{
want := report.EmptySets.
want := report.MakeSets().
Add("docker_container_ports", report.MakeStringSet("1.2.3.4:80->80/tcp", "81/tcp")).
Add("docker_container_networks", nil).
Add("docker_container_ips", report.MakeStringSet("1.2.3.4")).

View File

@@ -71,7 +71,7 @@ func (c *mockContainer) GetNode() report.Node {
docker.ContainerID: c.c.ID,
docker.ContainerName: c.c.Name,
docker.ImageID: c.c.Image,
}).WithParents(report.EmptySets.
}).WithParents(report.MakeSets().
Add(report.ContainerImage, report.MakeStringSet(report.MakeContainerImageNodeID(c.c.Image))),
)
}
@@ -80,7 +80,7 @@ func (c *mockContainer) NetworkMode() (string, bool) {
return "", false
}
func (c *mockContainer) NetworkInfo([]net.IP) report.Sets {
return report.EmptySets
return report.MakeSets()
}
func (c *mockContainer) Container() *client.Container {

View File

@@ -218,7 +218,7 @@ func (r *Reporter) containerTopology(localAddrs []net.IP) report.Topology {
// namespaces & deal with containers in the host net namespace. This
// is recursive to deal with people who decide to be clever.
{
hostNetworkInfo := report.EmptySets
hostNetworkInfo := report.MakeSets()
if hostIPs, err := getLocalIPs(); err == nil {
hostIPsWithScopes := addScopeToIPs(r.hostID, hostIPs)
hostNetworkInfo = hostNetworkInfo.
@@ -230,7 +230,7 @@ func (r *Reporter) containerTopology(localAddrs []net.IP) report.Topology {
networkInfo = func(prefix string) (ips report.Sets, isInHostNamespace bool) {
container, ok := r.registry.GetContainerByPrefix(prefix)
if !ok {
return report.EmptySets, false
return report.MakeSets(), false
}
networkMode, ok := container.NetworkMode()

View File

@@ -117,7 +117,7 @@ func (t *Tagger) tag(tree process.Tree, topology *report.Topology) {
node := report.MakeNodeWith(nodeID, map[string]string{
ContainerID: c.ID(),
}).WithParents(report.EmptySets.
}).WithParents(report.MakeSets().
Add(report.Container, report.MakeStringSet(report.MakeContainerNodeID(c.ID()))),
)
@@ -125,7 +125,7 @@ func (t *Tagger) tag(tree process.Tree, topology *report.Topology) {
image, ok := t.registry.GetContainerImage(c.Image())
if ok && len(image.RepoTags) > 0 {
imageName := ImageNameWithoutVersion(image.RepoTags[0])
node = node.WithParents(report.EmptySets.
node = node.WithParents(report.MakeSets().
Add(report.ContainerImage, report.MakeStringSet(report.MakeContainerImageNodeID(imageName))),
)
}

View File

@@ -133,7 +133,7 @@ func (r *Reporter) Report() (report.Report, error) {
Uptime: uptime.String(),
ScopeVersion: r.version,
}).
WithSets(report.EmptySets.
WithSets(report.MakeSets().
Add(LocalNetworks, report.MakeStringSet(localCIDRs...)),
).
WithMetrics(metrics).

View File

@@ -26,7 +26,7 @@ func (Tagger) Name() string { return "Host" }
func (t Tagger) Tag(r report.Report) (report.Report, error) {
var (
metadata = map[string]string{report.HostNodeID: t.hostNodeID}
parents = report.EmptySets.Add(report.Host, report.MakeStringSet(t.hostNodeID))
parents = report.MakeSets().Add(report.Host, report.MakeStringSet(t.hostNodeID))
)
// Explicitly don't tag Endpoints, Addresses and Overlay nodes - These topologies include pseudo nodes,

View File

@@ -197,9 +197,9 @@ func (r *Reporter) Tag(rpt report.Report) (report.Report, error) {
n = n.WithLatest(report.DoesNotMakeConnections, mtime.Now(), "")
}
rpt.Container.Nodes[id] = n.WithParents(report.EmptySets.Add(
rpt.Container.Nodes[id] = n.WithParents(report.MakeSets().Add(
report.Pod,
report.EmptyStringSet.Add(report.MakePodNodeID(uid)),
report.MakeStringSet().Add(report.MakePodNodeID(uid)),
))
}
return rpt, nil
@@ -266,12 +266,12 @@ func (r *Reporter) serviceTopology() (report.Topology, []Service, error) {
// connections for which we don't have a robust solution
// (see https://github.com/weaveworks/scope/issues/1491)
func (r *Reporter) hostTopology(services []Service) report.Topology {
localNetworks := report.EmptyStringSet
localNetworks := report.MakeStringSet()
for _, service := range services {
localNetworks = localNetworks.Add(service.ClusterIP() + "/32")
}
node := report.MakeNode(report.MakeHostNodeID(r.hostID))
node = node.WithSets(report.EmptySets.
node = node.WithSets(report.MakeSets().
Add(host.LocalNetworks, localNetworks))
return report.MakeTopology().AddNode(node)
}

View File

@@ -264,7 +264,7 @@ func TestTagger(t *testing.T) {
}
have, ok := rpt.Container.Nodes["container1"].Parents.Lookup(report.Pod)
want := report.EmptyStringSet.Add(report.MakePodNodeID("123456"))
want := report.MakeStringSet().Add(report.MakePodNodeID("123456"))
if !ok || !reflect.DeepEqual(have, want) {
t.Errorf("Expected container to have pod parent %v %v", have, want)
}

View File

@@ -472,7 +472,7 @@ func (w *Weave) addCurrentPeerInfo(latests map[string]string, node report.Node)
latests[WeavePluginDriver] = "weave"
}
node = node.AddPrefixMulticolumnTable(WeaveConnectionsMulticolumnTablePrefix, getConnectionsTable(w.statusCache.Router))
node = node.WithParents(report.EmptySets.Add(report.Host, report.MakeStringSet(w.hostID)))
node = node.WithParents(report.MakeSets().Add(report.Host, report.MakeStringSet(w.hostID)))
return latests, node
}

View File

@@ -365,7 +365,7 @@ func MapContainerImage2Name(n report.Node, _ report.Networks) report.Nodes {
n.ID = report.MakeContainerImageNodeID(imageNameWithoutVersion)
if imageID, ok := report.ParseContainerImageNodeID(n.ID); ok {
n.Sets = n.Sets.Add(docker.ImageID, report.EmptyStringSet.Add(imageID))
n.Sets = n.Sets.Add(docker.ImageID, report.MakeStringSet().Add(imageID))
}
return report.Nodes{n.ID: n}

View File

@@ -23,7 +23,7 @@ func TestNodeMetadata(t *testing.T) {
docker.ContainerID: fixture.ClientContainerID,
docker.LabelPrefix + "label1": "label1value",
docker.ContainerStateHuman: docker.StateRunning,
}).WithTopology(report.Container).WithSets(report.EmptySets.
}).WithTopology(report.Container).WithSets(report.MakeSets().
Add(docker.ContainerIPs, report.MakeStringSet("10.10.10.0/24", "10.10.10.1/24")),
),
want: []report.MetadataRow{

View File

@@ -22,7 +22,7 @@ func TestParents(t *testing.T) {
{
name: "Node accidentally tagged with itself",
node: render.HostRenderer.Render(fixture.Report, nil)[fixture.ClientHostNodeID].WithParents(
report.EmptySets.Add(report.Host, report.MakeStringSet(fixture.ClientHostNodeID)),
report.MakeSets().Add(report.Host, report.MakeStringSet(fixture.ClientHostNodeID)),
),
want: nil,
},

View File

@@ -28,7 +28,7 @@ func TestNodeTables(t *testing.T) {
docker.ContainerID: fixture.ClientContainerID,
docker.LabelPrefix + "label1": "label1value",
docker.ContainerState: docker.StateRunning,
}).WithTopology(report.Container).WithSets(report.EmptySets.
}).WithTopology(report.Container).WithSets(report.MakeSets().
Add(docker.ContainerIPs, report.MakeStringSet("10.10.10.0/24", "10.10.10.1/24")),
),
want: []report.Table{

View File

@@ -79,7 +79,7 @@ var (
docker.ContainerName: container1Name,
report.HostNodeID: serverHostNodeID,
}).
WithSets(report.EmptySets.
WithSets(report.MakeSets().
Add(docker.ContainerIPs, report.MakeStringSet(container1IP)).
Add(docker.ContainerIPsWithScopes, report.MakeStringSet(report.MakeAddressNodeID("", container1IP))).
Add(docker.ContainerPorts, report.MakeStringSet(fmt.Sprintf("%s:%s->%s/tcp", serverIP, serverPort, serverPort))),
@@ -89,7 +89,7 @@ var (
docker.ContainerName: container2Name,
report.HostNodeID: serverHostNodeID,
}).
WithSets(report.EmptySets.
WithSets(report.MakeSets().
Add(docker.ContainerIPs, report.MakeStringSet(container2IP)).
Add(docker.ContainerIPsWithScopes, report.MakeStringSet(report.MakeAddressNodeID("", container2IP))),
).WithTopology(report.Container),
@@ -98,7 +98,7 @@ var (
docker.ContainerName: pauseContainerName,
report.HostNodeID: serverHostNodeID,
}).
WithSets(report.EmptySets.
WithSets(report.MakeSets().
Add(docker.ContainerIPs, report.MakeStringSet(pauseContainerIP)).
Add(docker.ContainerIPsWithScopes, report.MakeStringSet(report.MakeAddressNodeID("", pauseContainerIP))),
).WithTopology(report.Container).WithLatest(report.DoesNotMakeConnections, mtime.Now(), ""),
@@ -109,7 +109,7 @@ var (
serverHostNodeID: report.MakeNodeWith(serverHostNodeID, map[string]string{
report.HostNodeID: serverHostNodeID,
}).
WithSets(report.EmptySets.
WithSets(report.MakeSets().
Add(host.LocalNetworks, report.MakeStringSet("192.168.0.0/16")),
).WithTopology(report.Host),
},

View File

@@ -15,7 +15,7 @@ func TestReportLocalNetworks(t *testing.T) {
Host: report.Topology{
Nodes: report.Nodes{
"nonets": report.MakeNode("nonets"),
"foo": report.MakeNode("foo").WithSets(report.EmptySets.
"foo": report.MakeNode("foo").WithSets(report.MakeSets().
Add(host.LocalNetworks, report.MakeStringSet(
"10.0.0.1/8", "192.168.1.1/24", "10.0.0.1/8", "badnet/33")),
),
@@ -23,7 +23,7 @@ func TestReportLocalNetworks(t *testing.T) {
},
Overlay: report.Topology{
Nodes: report.Nodes{
"router": report.MakeNode("router").WithSets(report.EmptySets.
"router": report.MakeNode("router").WithSets(report.MakeSets().
Add(host.LocalNetworks, report.MakeStringSet("10.32.0.1/12")),
),
},

View File

@@ -14,12 +14,11 @@ type Counters struct {
psMap ps.Map
}
// EmptyCounters is the set of empty counters.
var EmptyCounters = Counters{ps.NewMap()}
var emptyCounters = Counters{ps.NewMap()}
// MakeCounters returns EmptyCounters
func MakeCounters() Counters {
return EmptyCounters
return emptyCounters
}
// Copy is a noop
@@ -30,7 +29,7 @@ func (c Counters) Copy() Counters {
// Add value to the counter 'key'
func (c Counters) Add(key string, value int) Counters {
if c.psMap == nil {
c = EmptyCounters
c = emptyCounters
}
if existingValue, ok := c.psMap.Lookup(key); ok {
value += existingValue.(int)

View File

@@ -11,7 +11,7 @@ import (
)
func TestCountersAdd(t *testing.T) {
have := EmptyCounters.
have := MakeCounters().
Add("foo", 1).
Add("foo", 2)
if v, ok := have.Lookup("foo"); !ok || v != 3 {
@@ -23,14 +23,14 @@ func TestCountersAdd(t *testing.T) {
}
func TestCountersDeepEquals(t *testing.T) {
want := EmptyCounters.
want := MakeCounters().
Add("foo", 3)
have := EmptyCounters.
have := MakeCounters().
Add("foo", 3)
if !reflect.DeepEqual(want, have) {
t.Errorf(test.Diff(want, have))
}
notequal := EmptyCounters.
notequal := MakeCounters().
Add("foo", 4)
if reflect.DeepEqual(want, notequal) {
t.Errorf(test.Diff(want, have))
@@ -59,34 +59,34 @@ func TestCountersMerge(t *testing.T) {
a, b, want Counters
}{
"Empty a": {
a: EmptyCounters,
b: EmptyCounters.
a: MakeCounters(),
b: MakeCounters().
Add("foo", 1),
want: EmptyCounters.
want: MakeCounters().
Add("foo", 1),
},
"Empty b": {
a: EmptyCounters.
a: MakeCounters().
Add("foo", 1),
b: EmptyCounters,
want: EmptyCounters.
b: MakeCounters(),
want: MakeCounters().
Add("foo", 1),
},
"Disjoin a & b": {
a: EmptyCounters.
a: MakeCounters().
Add("foo", 1),
b: EmptyCounters.
b: MakeCounters().
Add("bar", 2),
want: EmptyCounters.
want: MakeCounters().
Add("foo", 1).
Add("bar", 2),
},
"Overlapping a & b": {
a: EmptyCounters.
a: MakeCounters().
Add("foo", 1),
b: EmptyCounters.
b: MakeCounters().
Add("foo", 2),
want: EmptyCounters.
want: MakeCounters().
Add("foo", 3),
},
} {
@@ -97,7 +97,7 @@ func TestCountersMerge(t *testing.T) {
}
func TestCountersEncoding(t *testing.T) {
want := EmptyCounters.
want := MakeCounters().
Add("foo", 1).
Add("bar", 2)
@@ -111,7 +111,7 @@ func TestCountersEncoding(t *testing.T) {
encoder := codec.NewEncoder(buf, h)
want.CodecEncodeSelf(encoder)
decoder := codec.NewDecoder(buf, h)
have := EmptyCounters
have := MakeCounters()
have.CodecDecodeSelf(decoder)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
@@ -132,7 +132,7 @@ func TestCountersString(t *testing.T) {
}
{
have := EmptyCounters.String()
have := MakeCounters().String()
want := `{}`
if want != have {
t.Errorf("Expected: %s, Got %s", want, have)
@@ -140,7 +140,7 @@ func TestCountersString(t *testing.T) {
}
{
have := EmptyCounters.
have := MakeCounters().
Add("foo", 1).
Add("bar", 2).String()

View File

@@ -15,12 +15,11 @@ type EdgeMetadatas struct {
psMap ps.Map
}
// EmptyEdgeMetadatas is the set of empty EdgeMetadatas.
var EmptyEdgeMetadatas = EdgeMetadatas{ps.NewMap()}
var emptyEdgeMetadatas = EdgeMetadatas{ps.NewMap()}
// MakeEdgeMetadatas returns EmptyEdgeMetadatas
func MakeEdgeMetadatas() EdgeMetadatas {
return EmptyEdgeMetadatas
return emptyEdgeMetadatas
}
// Copy is a noop
@@ -31,7 +30,7 @@ func (c EdgeMetadatas) Copy() EdgeMetadatas {
// Add value to the counter 'key'
func (c EdgeMetadatas) Add(key string, value EdgeMetadata) EdgeMetadatas {
if c.psMap == nil {
c = EmptyEdgeMetadatas
c = emptyEdgeMetadatas
}
if existingValue, ok := c.psMap.Lookup(key); ok {
value = value.Merge(existingValue.(EdgeMetadata))

View File

@@ -11,7 +11,7 @@ import (
)
func TestEdgeMetadatasAdd(t *testing.T) {
have := EmptyEdgeMetadatas.
have := MakeEdgeMetadatas().
Add("foo",
EdgeMetadata{
EgressPacketCount: newu64(1),
@@ -41,12 +41,12 @@ func TestEdgeMetadatasAddNil(t *testing.T) {
}
func TestEdgeMetadatasDeepEquals(t *testing.T) {
want := EmptyEdgeMetadatas.
want := MakeEdgeMetadatas().
Add("foo",
EdgeMetadata{
EgressPacketCount: newu64(3),
})
have := EmptyEdgeMetadatas.
have := MakeEdgeMetadatas().
Add("foo",
EdgeMetadata{
EgressPacketCount: newu64(3),
@@ -66,27 +66,27 @@ func TestEdgeMetadatasMerge(t *testing.T) {
want: EdgeMetadatas{},
},
"Empty a": {
a: EmptyEdgeMetadatas,
b: EmptyEdgeMetadatas.
a: MakeEdgeMetadatas(),
b: MakeEdgeMetadatas().
Add("hostA|:192.168.1.1:12345|:192.168.1.2:80",
EdgeMetadata{
EgressPacketCount: newu64(1),
}),
want: EmptyEdgeMetadatas.
want: MakeEdgeMetadatas().
Add("hostA|:192.168.1.1:12345|:192.168.1.2:80",
EdgeMetadata{
EgressPacketCount: newu64(1),
}),
},
"Empty b": {
a: EmptyEdgeMetadatas.
a: MakeEdgeMetadatas().
Add("hostA|:192.168.1.1:12345|:192.168.1.2:80",
EdgeMetadata{
EgressPacketCount: newu64(12),
EgressByteCount: newu64(999),
}),
b: EmptyEdgeMetadatas,
want: EmptyEdgeMetadatas.
b: MakeEdgeMetadatas(),
want: MakeEdgeMetadatas().
Add("hostA|:192.168.1.1:12345|:192.168.1.2:80",
EdgeMetadata{
EgressPacketCount: newu64(12),
@@ -94,19 +94,19 @@ func TestEdgeMetadatasMerge(t *testing.T) {
}),
},
"Disjoint a & b": {
a: EmptyEdgeMetadatas.
a: MakeEdgeMetadatas().
Add("hostA|:192.168.1.1:12345|:192.168.1.2:80",
EdgeMetadata{
EgressPacketCount: newu64(12),
EgressByteCount: newu64(500),
}),
b: EmptyEdgeMetadatas.
b: MakeEdgeMetadatas().
Add("hostQ|:192.168.1.1:12345|:192.168.1.2:80",
EdgeMetadata{
EgressPacketCount: newu64(1),
EgressByteCount: newu64(2),
}),
want: EmptyEdgeMetadatas.
want: MakeEdgeMetadatas().
Add("hostA|:192.168.1.1:12345|:192.168.1.2:80",
EdgeMetadata{
EgressPacketCount: newu64(12),
@@ -119,20 +119,20 @@ func TestEdgeMetadatasMerge(t *testing.T) {
}),
},
"Overlapping a & b": {
a: EmptyEdgeMetadatas.
a: MakeEdgeMetadatas().
Add("hostA|:192.168.1.1:12345|:192.168.1.2:80",
EdgeMetadata{
EgressPacketCount: newu64(12),
EgressByteCount: newu64(1000),
}),
b: EmptyEdgeMetadatas.
b: MakeEdgeMetadatas().
Add("hostA|:192.168.1.1:12345|:192.168.1.2:80",
EdgeMetadata{
EgressPacketCount: newu64(1),
IngressByteCount: newu64(123),
EgressByteCount: newu64(2),
}),
want: EmptyEdgeMetadatas.
want: MakeEdgeMetadatas().
Add("hostA|:192.168.1.1:12345|:192.168.1.2:80",
EdgeMetadata{
EgressPacketCount: newu64(13),
@@ -168,7 +168,7 @@ func TestEdgeMetadataFlatten(t *testing.T) {
// Test an EdgeMetadatas flatten to the correct value (should
// just sum)
{
have := EmptyEdgeMetadatas.
have := MakeEdgeMetadatas().
Add("foo", EdgeMetadata{
EgressPacketCount: newu64(1),
}).
@@ -206,7 +206,7 @@ func TestEdgeMetadataReversed(t *testing.T) {
}
func TestEdgeMetadatasEncoding(t *testing.T) {
want := EmptyEdgeMetadatas.
want := MakeEdgeMetadatas().
Add("foo", EdgeMetadata{
EgressPacketCount: newu64(1),
}).
@@ -223,7 +223,7 @@ func TestEdgeMetadatasEncoding(t *testing.T) {
encoder := codec.NewEncoder(buf, h)
want.CodecEncodeSelf(encoder)
decoder := codec.NewDecoder(buf, h)
have := EmptyEdgeMetadatas
have := MakeEdgeMetadatas()
have.CodecDecodeSelf(decoder)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
@@ -245,7 +245,7 @@ func TestEdgeMetadatasEncodingNil(t *testing.T) {
encoder := codec.NewEncoder(buf, h)
want.CodecEncodeSelf(encoder)
decoder := codec.NewDecoder(buf, h)
have := EmptyEdgeMetadatas
have := MakeEdgeMetadatas()
have.CodecDecodeSelf(decoder)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))

View File

@@ -3,8 +3,7 @@ package report
// IDList is a list of string IDs, which are always sorted and unique.
type IDList StringSet
// EmptyIDList is an Empty ID List.
var EmptyIDList = IDList(EmptyStringSet)
var emptyIDList = IDList(MakeStringSet())
// MakeIDList makes a new IDList.
func MakeIDList(ids ...string) IDList {

View File

@@ -30,12 +30,11 @@ func (e *stringLatestEntry) Equal(e2 *stringLatestEntry) bool {
// StringLatestMap holds latest string instances.
type StringLatestMap struct{ ps.Map }
// EmptyStringLatestMap is an empty StringLatestMap. Start with this.
var EmptyStringLatestMap = StringLatestMap{ps.NewMap()}
var emptyStringLatestMap = StringLatestMap{ps.NewMap()}
// MakeStringLatestMap makes an empty StringLatestMap.
func MakeStringLatestMap() StringLatestMap {
return EmptyStringLatestMap
return emptyStringLatestMap
}
// Copy is a noop, as StringLatestMaps are immutable.
@@ -170,12 +169,11 @@ func (e *nodeControlDataLatestEntry) Equal(e2 *nodeControlDataLatestEntry) bool
// NodeControlDataLatestMap holds latest NodeControlData instances.
type NodeControlDataLatestMap struct{ ps.Map }
// EmptyNodeControlDataLatestMap is an empty NodeControlDataLatestMap. Start with this.
var EmptyNodeControlDataLatestMap = NodeControlDataLatestMap{ps.NewMap()}
var emptyNodeControlDataLatestMap = NodeControlDataLatestMap{ps.NewMap()}
// MakeNodeControlDataLatestMap makes an empty NodeControlDataLatestMap.
func MakeNodeControlDataLatestMap() NodeControlDataLatestMap {
return EmptyNodeControlDataLatestMap
return emptyNodeControlDataLatestMap
}
// Copy is a noop, as NodeControlDataLatestMaps are immutable.

View File

@@ -14,7 +14,7 @@ import (
func TestLatestMapAdd(t *testing.T) {
now := time.Now()
have := EmptyStringLatestMap.
have := MakeStringLatestMap().
Set("foo", now.Add(-1), "Baz").
Set("foo", now, "Bar")
if v, ok := have.Lookup("foo"); !ok || v != "Bar" {
@@ -37,7 +37,7 @@ func TestLatestMapLookupEntry(t *testing.T) {
Value interface{}
}
entry := LatestEntry{Timestamp: now, Value: "Bar"}
have := EmptyStringLatestMap.Set("foo", entry.Timestamp, entry.Value.(string))
have := MakeStringLatestMap().Set("foo", entry.Timestamp, entry.Value.(string))
if got, timestamp, ok := have.LookupEntry("foo"); !ok || got != entry.Value || !timestamp.Equal(entry.Timestamp) {
t.Errorf("got: %#v %v != expected %#v", got, timestamp, entry)
}
@@ -56,14 +56,14 @@ func TestLatestMapAddNil(t *testing.T) {
func TestLatestMapDeepEquals(t *testing.T) {
now := time.Now()
want := EmptyStringLatestMap.
want := MakeStringLatestMap().
Set("foo", now, "Bar")
have := EmptyStringLatestMap.
have := MakeStringLatestMap().
Set("foo", now, "Bar")
if !reflect.DeepEqual(want, have) {
t.Errorf(test.Diff(want, have))
}
notequal := EmptyStringLatestMap.
notequal := MakeStringLatestMap().
Set("foo", now, "Baz")
if reflect.DeepEqual(want, notequal) {
t.Errorf(test.Diff(want, have))
@@ -72,8 +72,8 @@ func TestLatestMapDeepEquals(t *testing.T) {
func TestLatestMapDelete(t *testing.T) {
now := time.Now()
want := EmptyStringLatestMap
have := EmptyStringLatestMap.
want := MakeStringLatestMap()
have := MakeStringLatestMap().
Set("foo", now, "Baz").
Delete("foo")
if !reflect.DeepEqual(want, have) {
@@ -90,7 +90,7 @@ func TestLatestMapDeleteNil(t *testing.T) {
}
func nilStringLatestMap() StringLatestMap {
m := EmptyStringLatestMap
m := MakeStringLatestMap()
m.Map = nil
return m
}
@@ -108,34 +108,34 @@ func TestLatestMapMerge(t *testing.T) {
want: nilStringLatestMap(),
},
"Empty a": {
a: EmptyStringLatestMap,
b: EmptyStringLatestMap.
a: MakeStringLatestMap(),
b: MakeStringLatestMap().
Set("foo", now, "bar"),
want: EmptyStringLatestMap.
want: MakeStringLatestMap().
Set("foo", now, "bar"),
},
"Empty b": {
a: EmptyStringLatestMap.
a: MakeStringLatestMap().
Set("foo", now, "bar"),
b: EmptyStringLatestMap,
want: EmptyStringLatestMap.
b: MakeStringLatestMap(),
want: MakeStringLatestMap().
Set("foo", now, "bar"),
},
"Disjoint a & b": {
a: EmptyStringLatestMap.
a: MakeStringLatestMap().
Set("foo", now, "bar"),
b: EmptyStringLatestMap.
b: MakeStringLatestMap().
Set("baz", now, "bop"),
want: EmptyStringLatestMap.
want: MakeStringLatestMap().
Set("foo", now, "bar").
Set("baz", now, "bop"),
},
"Common a & b": {
a: EmptyStringLatestMap.
a: MakeStringLatestMap().
Set("foo", now, "bar"),
b: EmptyStringLatestMap.
b: MakeStringLatestMap().
Set("foo", then, "baz"),
want: EmptyStringLatestMap.
want: MakeStringLatestMap().
Set("foo", now, "bar"),
},
} {
@@ -147,8 +147,8 @@ func TestLatestMapMerge(t *testing.T) {
func BenchmarkLatestMapMerge(b *testing.B) {
var (
left = EmptyStringLatestMap
right = EmptyStringLatestMap
left = MakeStringLatestMap()
right = MakeStringLatestMap()
now = time.Now()
)
@@ -169,7 +169,7 @@ func BenchmarkLatestMapMerge(b *testing.B) {
func TestLatestMapEncoding(t *testing.T) {
now := time.Now()
want := EmptyStringLatestMap.
want := MakeStringLatestMap().
Set("foo", now, "bar").
Set("bar", now, "baz")
@@ -181,7 +181,7 @@ func TestLatestMapEncoding(t *testing.T) {
encoder := codec.NewEncoder(buf, h)
want.CodecEncodeSelf(encoder)
decoder := codec.NewDecoder(buf, h)
have := EmptyStringLatestMap
have := MakeStringLatestMap()
have.CodecDecodeSelf(decoder)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
@@ -201,7 +201,7 @@ func TestLatestMapEncodingNil(t *testing.T) {
encoder := codec.NewEncoder(buf, h)
want.CodecEncodeSelf(encoder)
decoder := codec.NewDecoder(buf, h)
have := EmptyStringLatestMap
have := MakeStringLatestMap()
have.CodecDecodeSelf(decoder)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))

View File

@@ -28,15 +28,15 @@ type Node struct {
func MakeNode(id string) Node {
return Node{
ID: id,
Counters: EmptyCounters,
Sets: EmptySets,
Adjacency: EmptyIDList,
Edges: EmptyEdgeMetadatas,
Counters: MakeCounters(),
Sets: MakeSets(),
Adjacency: MakeIDList(),
Edges: MakeEdgeMetadatas(),
Controls: MakeNodeControls(),
LatestControls: EmptyNodeControlDataLatestMap,
Latest: EmptyStringLatestMap,
LatestControls: MakeNodeControlDataLatestMap(),
Latest: MakeStringLatestMap(),
Metrics: Metrics{},
Parents: EmptySets,
Parents: MakeSets(),
}
}
@@ -170,7 +170,7 @@ func (n Node) WithParents(parents Sets) Node {
// PruneParents returns a fresh copy of n, without any parents.
func (n Node) PruneParents() Node {
n.Parents = EmptySets
n.Parents = MakeSets()
return n
}

View File

@@ -17,12 +17,11 @@ type NodeSet struct {
psMap ps.Map
}
// EmptyNodeSet is the empty set of nodes.
var EmptyNodeSet = NodeSet{ps.NewMap()}
var emptyNodeSet = NodeSet{ps.NewMap()}
// MakeNodeSet makes a new NodeSet with the given nodes.
func MakeNodeSet(nodes ...Node) NodeSet {
return EmptyNodeSet.Add(nodes...)
return emptyNodeSet.Add(nodes...)
}
// Add adds the nodes to the NodeSet. Add is the only valid way to grow a
@@ -49,7 +48,7 @@ func (n NodeSet) Delete(ids ...string) NodeSet {
result = result.Delete(id)
}
if result.Size() == 0 {
return EmptyNodeSet
return emptyNodeSet
}
return NodeSet{result}
}

View File

@@ -76,9 +76,9 @@ func TestNodeSetAdd(t *testing.T) {
want: report.NodeSet{},
},
{
input: report.EmptyNodeSet,
input: report.MakeNodeSet(),
nodes: []report.Node{},
want: report.EmptyNodeSet,
want: report.MakeNodeSet(),
},
{
input: report.MakeNodeSet(report.MakeNode("a")),
@@ -86,7 +86,7 @@ func TestNodeSetAdd(t *testing.T) {
want: report.MakeNodeSet(report.MakeNode("a")),
},
{
input: report.EmptyNodeSet,
input: report.MakeNodeSet(),
nodes: []report.Node{report.MakeNode("a")},
want: report.MakeNodeSet(report.MakeNode("a")),
},
@@ -146,7 +146,7 @@ func TestNodeSetAdd(t *testing.T) {
}
func BenchmarkNodeSetAdd(b *testing.B) {
n := report.EmptyNodeSet
n := report.MakeNodeSet()
for i := 0; i < 600; i++ {
n = n.Add(
report.MakeNodeWith(fmt.Sprint(i), map[string]string{
@@ -181,9 +181,9 @@ func TestNodeSetDelete(t *testing.T) {
want: report.NodeSet{},
},
{
input: report.EmptyNodeSet,
input: report.MakeNodeSet(),
nodes: []string{},
want: report.EmptyNodeSet,
want: report.MakeNodeSet(),
},
{
input: report.MakeNodeSet(report.MakeNode("a")),
@@ -191,19 +191,19 @@ func TestNodeSetDelete(t *testing.T) {
want: report.MakeNodeSet(report.MakeNode("a")),
},
{
input: report.EmptyNodeSet,
input: report.MakeNodeSet(),
nodes: []string{"a"},
want: report.EmptyNodeSet,
want: report.MakeNodeSet(),
},
{
input: report.MakeNodeSet(report.MakeNode("a")),
nodes: []string{"a"},
want: report.EmptyNodeSet,
want: report.MakeNodeSet(),
},
{
input: report.MakeNodeSet(report.MakeNode("b")),
nodes: []string{"a", "b"},
want: report.EmptyNodeSet,
want: report.MakeNodeSet(),
},
{
input: report.MakeNodeSet(report.MakeNode("a")),
@@ -233,14 +233,14 @@ func TestNodeSetMerge(t *testing.T) {
want report.NodeSet
}{
{input: report.NodeSet{}, other: report.NodeSet{}, want: report.NodeSet{}},
{input: report.EmptyNodeSet, other: report.EmptyNodeSet, want: report.EmptyNodeSet},
{input: report.MakeNodeSet(), other: report.MakeNodeSet(), want: report.MakeNodeSet()},
{
input: report.MakeNodeSet(report.MakeNode("a")),
other: report.EmptyNodeSet,
other: report.MakeNodeSet(),
want: report.MakeNodeSet(report.MakeNode("a")),
},
{
input: report.EmptyNodeSet,
input: report.MakeNodeSet(),
other: report.MakeNodeSet(report.MakeNode("a")),
want: report.MakeNodeSet(report.MakeNode("a")),
},

View File

@@ -14,11 +14,11 @@ type Sets struct {
}
// EmptySets is an empty Sets. Starts with this.
var EmptySets = Sets{ps.NewMap()}
var emptySets = Sets{ps.NewMap()}
// MakeSets returns EmptySets
func MakeSets() Sets {
return EmptySets
return emptySets
}
// Keys returns the keys for this set
@@ -32,7 +32,7 @@ func (s Sets) Keys() []string {
// Add the given value to the Sets.
func (s Sets) Add(key string, value StringSet) Sets {
if s.psMap == nil {
s = EmptySets
s = emptySets
}
if existingValue, ok := s.psMap.Lookup(key); ok {
value = value.Merge(existingValue.(StringSet))
@@ -45,7 +45,7 @@ func (s Sets) Add(key string, value StringSet) Sets {
// Delete the given set from the Sets.
func (s Sets) Delete(key string) Sets {
if s.psMap == nil {
return EmptySets
return emptySets
}
return Sets{
psMap: s.psMap.Delete(key),
@@ -55,12 +55,12 @@ func (s Sets) Delete(key string) Sets {
// Lookup returns the sets stored under key.
func (s Sets) Lookup(key string) (StringSet, bool) {
if s.psMap == nil {
return EmptyStringSet, false
return MakeStringSet(), false
}
if value, ok := s.psMap.Lookup(key); ok {
return value.(StringSet), true
}
return EmptyStringSet, false
return MakeStringSet(), false
}
// Size returns the number of elements

View File

@@ -7,12 +7,12 @@ import (
)
func TestSets(t *testing.T) {
sets := EmptySets.Add("foo", MakeStringSet("bar"))
sets := MakeSets().Add("foo", MakeStringSet("bar"))
if v, _ := sets.Lookup("foo"); !reflect.DeepEqual(v, MakeStringSet("bar")) {
t.Fatal(v)
}
sets = sets.Merge(EmptySets.Add("foo", MakeStringSet("baz")))
sets = sets.Merge(MakeSets().Add("foo", MakeStringSet("baz")))
if v, _ := sets.Lookup("foo"); !reflect.DeepEqual(v, MakeStringSet("bar", "baz")) {
t.Fatal(v)
}

View File

@@ -12,20 +12,20 @@ func TestSetsMerge(t *testing.T) {
a, b report.Sets
want map[string][]string
}{
{report.EmptySets, report.EmptySets, map[string][]string{}},
{report.MakeSets(), report.MakeSets(), map[string][]string{}},
{
report.EmptySets,
report.EmptySets.Add("a", report.MakeStringSet("b")),
report.MakeSets(),
report.MakeSets().Add("a", report.MakeStringSet("b")),
map[string][]string{"a": {"b"}},
},
{
report.EmptySets,
report.EmptySets.Add("a", report.MakeStringSet("b", "c")),
report.MakeSets(),
report.MakeSets().Add("a", report.MakeStringSet("b", "c")),
map[string][]string{"a": {"b", "c"}},
},
{
report.EmptySets.Add("a", report.MakeStringSet("1")).Add("b", report.MakeStringSet("2")),
report.EmptySets.Add("c", report.MakeStringSet("3")).Add("b", report.MakeStringSet("3")),
report.MakeSets().Add("a", report.MakeStringSet("1")).Add("b", report.MakeStringSet("2")),
report.MakeSets().Add("c", report.MakeStringSet("3")).Add("b", report.MakeStringSet("3")),
map[string][]string{"a": {"1"}, "b": {"2", "3"}, "c": {"3"}},
},
} {

View File

@@ -8,8 +8,7 @@ import (
// method to add strings.
type StringSet []string
// EmptyStringSet is an empty string set.
var EmptyStringSet StringSet
var emptyStringSet StringSet
// MakeStringSet makes a new StringSet with the given strings.
func MakeStringSet(strs ...string) StringSet {
@@ -37,7 +36,7 @@ func (s StringSet) Contains(str string) bool {
// Intersection returns the intersections of a and b
func (s StringSet) Intersection(b StringSet) StringSet {
result, i, j := EmptyStringSet, 0, 0
result, i, j := emptyStringSet, 0, 0
for i < len(s) && j < len(b) {
if s[i] == b[j] {
result = result.Add(s[i])

View File

@@ -199,7 +199,7 @@ var (
docker.ContainerID: ClientContainerID,
report.HostNodeID: ClientHostNodeID,
}).
WithTopology(report.Process).WithParents(report.EmptySets.
WithTopology(report.Process).WithParents(report.MakeSets().
Add("host", report.MakeStringSet(ClientHostNodeID)).
Add("container", report.MakeStringSet(ClientContainerNodeID)),
).WithMetrics(report.Metrics{
@@ -212,7 +212,7 @@ var (
docker.ContainerID: ClientContainerID,
report.HostNodeID: ClientHostNodeID,
}).
WithTopology(report.Process).WithParents(report.EmptySets.
WithTopology(report.Process).WithParents(report.MakeSets().
Add("host", report.MakeStringSet(ClientHostNodeID)).
Add("container", report.MakeStringSet(ClientContainerNodeID)),
),
@@ -222,7 +222,7 @@ var (
docker.ContainerID: ServerContainerID,
report.HostNodeID: ServerHostNodeID,
}).
WithTopology(report.Process).WithParents(report.EmptySets.
WithTopology(report.Process).WithParents(report.MakeSets().
Add("host", report.MakeStringSet(ServerHostNodeID)).
Add("container", report.MakeStringSet(ServerContainerNodeID)),
),
@@ -231,7 +231,7 @@ var (
process.Name: NonContainerName,
report.HostNodeID: ServerHostNodeID,
}).
WithTopology(report.Process).WithParents(report.EmptySets.
WithTopology(report.Process).WithParents(report.MakeSets().
Add("host", report.MakeStringSet(ServerHostNodeID)),
),
},
@@ -254,7 +254,7 @@ var (
docker.ContainerState: docker.StateRunning,
docker.ContainerStateHuman: docker.StateRunning,
}).
WithTopology(report.Container).WithParents(report.EmptySets.
WithTopology(report.Container).WithParents(report.MakeSets().
Add("host", report.MakeStringSet(ClientHostNodeID)).
Add("container_image", report.MakeStringSet(ClientContainerImageNodeID)).
Add("pod", report.MakeStringSet(ClientPodNodeID)),
@@ -279,7 +279,7 @@ var (
docker.LabelPrefix + TestLabelKey2: ApplicationLabelValue2,
kubernetes.Namespace: KubernetesNamespace,
}).
WithTopology(report.Container).WithParents(report.EmptySets.
WithTopology(report.Container).WithParents(report.MakeSets().
Add("host", report.MakeStringSet(ServerHostNodeID)).
Add("container_image", report.MakeStringSet(ServerContainerImageNodeID)).
Add("pod", report.MakeStringSet(ServerPodNodeID)),
@@ -298,7 +298,7 @@ var (
docker.ImageName: ClientContainerImageName,
report.HostNodeID: ClientHostNodeID,
}).
WithParents(report.EmptySets.
WithParents(report.MakeSets().
Add("host", report.MakeStringSet(ClientHostNodeID)),
).WithTopology(report.ContainerImage),
ServerContainerImageNodeID: report.MakeNodeWith(ServerContainerImageNodeID, map[string]string{
@@ -308,7 +308,7 @@ var (
docker.LabelPrefix + "foo1": "bar1",
docker.LabelPrefix + "foo2": "bar2",
}).
WithParents(report.EmptySets.
WithParents(report.MakeSets().
Add("host", report.MakeStringSet(ServerHostNodeID)),
).WithTopology(report.ContainerImage),
},
@@ -323,7 +323,7 @@ var (
"os": "Linux",
report.HostNodeID: ClientHostNodeID,
}).
WithTopology(report.Host).WithSets(report.EmptySets.
WithTopology(report.Host).WithSets(report.MakeSets().
Add(host.LocalNetworks, report.MakeStringSet("10.10.10.0/24")),
).WithMetrics(report.Metrics{
host.CPUUsage: ClientHostCPUMetric,
@@ -337,7 +337,7 @@ var (
"os": "Linux",
report.HostNodeID: ServerHostNodeID,
}).
WithTopology(report.Host).WithSets(report.EmptySets.
WithTopology(report.Host).WithSets(report.MakeSets().
Add(host.LocalNetworks, report.MakeStringSet("10.10.10.0/24")),
).WithMetrics(report.Metrics{
host.CPUUsage: ServerHostCPUMetric,
@@ -356,7 +356,7 @@ var (
kubernetes.Namespace: KubernetesNamespace,
report.HostNodeID: ClientHostNodeID,
}).
WithTopology(report.Pod).WithParents(report.EmptySets.
WithTopology(report.Pod).WithParents(report.MakeSets().
Add("host", report.MakeStringSet(ClientHostNodeID)).
Add("service", report.MakeStringSet(ServiceNodeID)),
),
@@ -367,7 +367,7 @@ var (
kubernetes.State: "running",
report.HostNodeID: ServerHostNodeID,
}).
WithTopology(report.Pod).WithParents(report.EmptySets.
WithTopology(report.Pod).WithParents(report.MakeSets().
Add("host", report.MakeStringSet(ServerHostNodeID)).
Add("service", report.MakeStringSet(ServiceNodeID)),
),