const-izing NodeMetadata fields

This commit is contained in:
Paul Bellamy
2015-07-02 14:33:17 +01:00
parent 129e529a0e
commit 95f941ff2d
6 changed files with 54 additions and 45 deletions

View File

@@ -7,6 +7,7 @@ import (
"strconv"
"time"
"github.com/weaveworks/scope/probe/docker"
"github.com/weaveworks/scope/report"
)
@@ -70,17 +71,17 @@ func DemoReport(nodeCount int) report.Report {
// Endpoint topology
if _, ok := r.Endpoint.NodeMetadatas[srcPortID]; !ok {
r.Endpoint.NodeMetadatas[srcPortID] = report.NodeMetadata{
"pid": "4000",
"name": c.srcProc,
"domain": "node-" + src,
docker.PID: "4000",
docker.Name: c.srcProc,
docker.Domain: "node-" + src,
}
}
r.Endpoint.Adjacency[srcID] = r.Endpoint.Adjacency[srcID].Add(dstPortID)
if _, ok := r.Endpoint.NodeMetadatas[dstPortID]; !ok {
r.Endpoint.NodeMetadatas[dstPortID] = report.NodeMetadata{
"pid": "4000",
"name": c.dstProc,
"domain": "node-" + dst,
docker.PID: "4000",
docker.Name: c.dstProc,
docker.Domain: "node-" + dst,
}
}
r.Endpoint.Adjacency[dstID] = r.Endpoint.Adjacency[dstID].Add(srcPortID)
@@ -100,13 +101,13 @@ func DemoReport(nodeCount int) report.Report {
// Address topology
if _, ok := r.Address.NodeMetadatas[srcAddressID]; !ok {
r.Address.NodeMetadatas[srcAddressID] = report.NodeMetadata{
"name": src,
docker.Name: src,
}
}
r.Address.Adjacency[nodeSrcAddressID] = r.Address.Adjacency[nodeSrcAddressID].Add(dstAddressID)
if _, ok := r.Address.NodeMetadatas[dstAddressID]; !ok {
r.Address.NodeMetadatas[dstAddressID] = report.NodeMetadata{
"name": dst,
docker.Name: dst,
}
}
r.Address.Adjacency[nodeDstAddressID] = r.Address.Adjacency[nodeDstAddressID].Add(srcAddressID)

View File

@@ -10,7 +10,12 @@ import (
// These constants are keys used in node metadata
// TODO: use these constants in report/{mapping.go, detailed_node.go} - pending some circular references
const (
Addr = "addr"
ContainerID = "docker_container_id"
Domain = "domain"
Name = "name"
PID = "pid"
Port = "port"
)
// These vars are exported for testing.

View File

@@ -8,6 +8,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/weaveworks/procspy"
"github.com/weaveworks/scope/probe/docker"
"github.com/weaveworks/scope/report"
)
@@ -80,8 +81,8 @@ func (r *Reporter) addConnection(rpt *report.Report, c *procspy.Connection) {
if _, ok := rpt.Address.NodeMetadatas[scopedLocal]; !ok {
rpt.Address.NodeMetadatas[scopedLocal] = report.NodeMetadata{
"name": r.hostName,
"addr": c.LocalAddress.String(),
docker.Name: r.hostName,
docker.Addr: c.LocalAddress.String(),
}
}

View File

@@ -6,6 +6,7 @@ import (
"testing"
"github.com/weaveworks/procspy"
"github.com/weaveworks/scope/probe/docker"
"github.com/weaveworks/scope/probe/endpoint"
"github.com/weaveworks/scope/report"
)
@@ -95,7 +96,7 @@ func TestSpyNoProcesses(t *testing.T) {
t.Fatalf("want %q, have %q", want, have)
}
if want, have := nodeName, r.Address.NodeMetadatas[scopedLocal]["name"]; want != have {
if want, have := nodeName, r.Address.NodeMetadatas[scopedLocal][docker.Name]; want != have {
t.Fatalf("want %q, have %q", want, have)
}
}

View File

@@ -125,7 +125,7 @@ func OriginTable(r report.Report, originID string) (Table, bool) {
func connectionDetailsRows(endpointTopology report.Topology, originID string, nmd report.NodeMetadata) []Row {
rows := []Row{}
local := fmt.Sprintf("%s:%s", nmd["addr"], nmd["port"])
local := fmt.Sprintf("%s:%s", nmd[docker.Addr], nmd[docker.Port])
adjacencies := endpointTopology.Adjacency[report.MakeAdjacencyID(originID)]
sort.Strings(adjacencies)
for _, adj := range adjacencies {

View File

@@ -4,6 +4,7 @@ import (
"reflect"
"testing"
"github.com/weaveworks/scope/probe/docker"
"github.com/weaveworks/scope/report"
)
@@ -223,84 +224,84 @@ func TestMergeNodeMetadatas(t *testing.T) {
a: report.NodeMetadatas{},
b: report.NodeMetadatas{
":192.168.1.1:12345": report.NodeMetadata{
"pid": "23128",
"name": "curl",
"domain": "node-a.local",
docker.PID: "23128",
docker.Name: "curl",
docker.Domain: "node-a.local",
},
},
want: report.NodeMetadatas{
":192.168.1.1:12345": report.NodeMetadata{
"pid": "23128",
"name": "curl",
"domain": "node-a.local",
docker.PID: "23128",
docker.Name: "curl",
docker.Domain: "node-a.local",
},
},
},
"Empty b": {
a: report.NodeMetadatas{
":192.168.1.1:12345": report.NodeMetadata{
"pid": "23128",
"name": "curl",
"domain": "node-a.local",
docker.PID: "23128",
docker.Name: "curl",
docker.Domain: "node-a.local",
},
},
b: report.NodeMetadatas{},
want: report.NodeMetadatas{
":192.168.1.1:12345": report.NodeMetadata{
"pid": "23128",
"name": "curl",
"domain": "node-a.local",
docker.PID: "23128",
docker.Name: "curl",
docker.Domain: "node-a.local",
},
},
},
"Simple merge": {
a: report.NodeMetadatas{
":192.168.1.1:12345": report.NodeMetadata{
"pid": "23128",
"name": "curl",
"domain": "node-a.local",
docker.PID: "23128",
docker.Name: "curl",
docker.Domain: "node-a.local",
},
},
b: report.NodeMetadatas{
":192.168.1.2:12345": report.NodeMetadata{
"pid": "42",
"name": "curl",
"domain": "node-a.local",
docker.PID: "42",
docker.Name: "curl",
docker.Domain: "node-a.local",
},
},
want: report.NodeMetadatas{
":192.168.1.1:12345": report.NodeMetadata{
"pid": "23128",
"name": "curl",
"domain": "node-a.local",
docker.PID: "23128",
docker.Name: "curl",
docker.Domain: "node-a.local",
},
":192.168.1.2:12345": report.NodeMetadata{
"pid": "42",
"name": "curl",
"domain": "node-a.local",
docker.PID: "42",
docker.Name: "curl",
docker.Domain: "node-a.local",
},
},
},
"Merge conflict": {
a: report.NodeMetadatas{
":192.168.1.1:12345": report.NodeMetadata{
"pid": "23128",
"name": "curl",
"domain": "node-a.local",
docker.PID: "23128",
docker.Name: "curl",
docker.Domain: "node-a.local",
},
},
b: report.NodeMetadatas{
":192.168.1.1:12345": report.NodeMetadata{ // <-- same ID
"pid": "0",
"name": "curl",
"domain": "node-a.local",
docker.PID: "0",
docker.Name: "curl",
docker.Domain: "node-a.local",
},
},
want: report.NodeMetadatas{
":192.168.1.1:12345": report.NodeMetadata{
"pid": "23128",
"name": "curl",
"domain": "node-a.local",
docker.PID: "23128",
docker.Name: "curl",
docker.Domain: "node-a.local",
},
},
},