Skip to content

Commit

Permalink
fixing up tests for Metadata -> Latest
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbellamy committed Jan 22, 2016
1 parent dd9fb78 commit 5bc7c31
Show file tree
Hide file tree
Showing 12 changed files with 339 additions and 238 deletions.
7 changes: 2 additions & 5 deletions probe/docker/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,11 @@ func TestContainer(t *testing.T) {

test.Poll(t, 100*time.Millisecond, want, func() interface{} {
node := c.GetNode("scope", []net.IP{})
keys := node.Latest.Keys()
sort.Strings(keys)
for _, k := range keys {
v, _ := node.Latest.Lookup(k)
node.Latest.ForEach(func(k, v string) {
if v == "0" || v == "" {
node.Latest = node.Latest.Delete(k)
}
}
})
return node
})

Expand Down
2 changes: 1 addition & 1 deletion probe/docker/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestLabels(t *testing.T) {
}
nmd := report.MakeNode()

docker.AddLabels(nmd, want)
nmd = docker.AddLabels(nmd, want)
have := docker.ExtractLabels(nmd)

if !reflect.DeepEqual(want, have) {
Expand Down
112 changes: 52 additions & 60 deletions probe/docker/reporter_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package docker_test

import (
"reflect"
"testing"

client "github.com/fsouza/go-dockerclient"

"github.com/weaveworks/scope/probe/docker"
"github.com/weaveworks/scope/report"
"github.com/weaveworks/scope/test"
)

type mockRegistry struct {
Expand Down Expand Up @@ -52,66 +50,60 @@ var (
)

func TestReporter(t *testing.T) {
want := report.MakeReport()
want.Container = report.Topology{
Nodes: report.Nodes{
report.MakeContainerNodeID("ping"): report.MakeNodeWith(map[string]string{
docker.ContainerID: "ping",
docker.ContainerName: "pong",
docker.ImageID: "baz",
}),
},
Controls: report.Controls{
docker.RestartContainer: report.Control{
ID: docker.RestartContainer,
Human: "Restart",
Icon: "fa-repeat",
},
docker.StartContainer: report.Control{
ID: docker.StartContainer,
Human: "Start",
Icon: "fa-play",
},
docker.StopContainer: report.Control{
ID: docker.StopContainer,
Human: "Stop",
Icon: "fa-stop",
},
docker.PauseContainer: report.Control{
ID: docker.PauseContainer,
Human: "Pause",
Icon: "fa-pause",
},
docker.UnpauseContainer: report.Control{
ID: docker.UnpauseContainer,
Human: "Unpause",
Icon: "fa-play",
},
docker.AttachContainer: report.Control{
ID: docker.AttachContainer,
Human: "Attach",
Icon: "fa-desktop",
},
docker.ExecContainer: report.Control{
ID: docker.ExecContainer,
Human: "Exec /bin/sh",
Icon: "fa-terminal",
},
},
containerImageNodeID := report.MakeContainerImageNodeID("baz")
rpt, err := docker.NewReporter(mockRegistryInstance, "host1", nil).Report()
if err != nil {
t.Fatal(err)
}
want.ContainerImage = report.Topology{
Nodes: report.Nodes{
report.MakeContainerImageNodeID("baz"): report.MakeNodeWith(map[string]string{
docker.ImageID: "baz",
docker.ImageName: "bang",
}),
},
Controls: report.Controls{},

// Reporter should add a container
{
containerNodeID := report.MakeContainerNodeID("ping")
node, ok := rpt.Container.Nodes[containerNodeID]
if !ok {
t.Fatalf("Expected report to have container image %q, but not found", containerNodeID)
}

for k, want := range map[string]string{
docker.ContainerID: "ping",
docker.ContainerName: "pong",
docker.ImageID: "baz",
} {
if have, ok := node.Latest.Lookup(k); !ok || have != want {
t.Errorf("Expected container %s latest %q: %q, got %q", containerNodeID, k, want, have)
}
}

// container should have controls
if len(rpt.Container.Controls) == 0 {
t.Errorf("Container should have some controls")
}

// container should have the image as a parent
if parents, ok := node.Parents.Lookup(report.ContainerImage); !ok || !parents.Contains(containerImageNodeID) {
t.Errorf("Expected container %s to have parent container image %q, got %q", containerNodeID, containerImageNodeID, parents)
}
}

reporter := docker.NewReporter(mockRegistryInstance, "host1", nil)
have, _ := reporter.Report()
if !reflect.DeepEqual(want, have) {
t.Errorf("%s", test.Diff(want, have))
// Reporter should add a container image
{
node, ok := rpt.ContainerImage.Nodes[containerImageNodeID]
if !ok {
t.Fatalf("Expected report to have container image %q, but not found", containerImageNodeID)
}

for k, want := range map[string]string{
docker.ImageID: "baz",
docker.ImageName: "bang",
} {
if have, ok := node.Latest.Lookup(k); !ok || have != want {
t.Errorf("Expected container image %s latest %q: %q, got %q", containerImageNodeID, k, want, have)
}
}

// container image should have no controls
if len(rpt.ContainerImage.Controls) != 0 {
t.Errorf("Container images should not have any controls")
}
}
}
39 changes: 23 additions & 16 deletions probe/docker/tagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package docker_test

import (
"fmt"
"reflect"
"testing"

"github.com/weaveworks/scope/probe/docker"
"github.com/weaveworks/scope/probe/process"
"github.com/weaveworks/scope/report"
"github.com/weaveworks/scope/test"
)

type mockProcessTree struct {
Expand Down Expand Up @@ -38,28 +36,37 @@ func TestTagger(t *testing.T) {
var (
pid1NodeID = report.MakeProcessNodeID("somehost.com", "2")
pid2NodeID = report.MakeProcessNodeID("somehost.com", "3")
wantNode = report.MakeNodeWith(map[string]string{
docker.ContainerID: "ping",
}).WithParents(report.EmptySets.
Add(report.Container, report.MakeStringSet(report.MakeContainerNodeID("ping"))).
Add(report.ContainerImage, report.MakeStringSet(report.MakeContainerImageNodeID("baz"))),
)
)

input := report.MakeReport()
input.Process.AddNode(pid1NodeID, report.MakeNodeWith(map[string]string{process.PID: "2"}))
input.Process.AddNode(pid2NodeID, report.MakeNodeWith(map[string]string{process.PID: "3"}))

want := report.MakeReport()
want.Process.AddNode(pid1NodeID, report.MakeNodeWith(map[string]string{process.PID: "2"}).Merge(wantNode))
want.Process.AddNode(pid2NodeID, report.MakeNodeWith(map[string]string{process.PID: "3"}).Merge(wantNode))

tagger := docker.NewTagger(mockRegistryInstance, nil)
have, err := tagger.Tag(input)
have, err := docker.NewTagger(mockRegistryInstance, nil).Tag(input)
if err != nil {
t.Errorf("%v", err)
}
if !reflect.DeepEqual(want, have) {
t.Errorf("%s", test.Diff(want, have))

// Processes should be tagged with their container ID, and parents
for _, nodeID := range []string{pid1NodeID, pid2NodeID} {
node, ok := have.Process.Nodes[nodeID]
if !ok {
t.Errorf("Expected process node %s, but not found", nodeID)
}

// node should have the container id added
if have, ok := node.Latest.Lookup(docker.ContainerID); !ok || have != "ping" {
t.Errorf("Expected process node %s to have container id %q, got %q", nodeID, "ping", have)
}

// node should have the container as a parent
if have, ok := node.Parents.Lookup(report.Container); !ok || !have.Contains(report.MakeContainerNodeID("ping")) {
t.Errorf("Expected process node %s to have container %q as a parent, got %q", nodeID, "ping", have)
}

// node should have the container image as a parent
if have, ok := node.Parents.Lookup(report.ContainerImage); !ok || !have.Contains(report.MakeContainerImageNodeID("baz")) {
t.Errorf("Expected process node %s to have container image %q as a parent, got %q", nodeID, "baz", have)
}
}
}
4 changes: 2 additions & 2 deletions probe/endpoint/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestSpyNoProcesses(t *testing.T) {
scopedRemote = report.MakeAddressNodeID(nodeID, fixRemoteAddress.String())
)

have, _ := r.Address.Nodes[scopedLocal].Metadata.Lookup(docker.Name)
have, _ := r.Address.Nodes[scopedLocal].Latest.Lookup(docker.Name)
if want, have := nodeName, have; want != have {
t.Fatalf("want %q, have %q", want, have)
}
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestSpyWithProcesses(t *testing.T) {
for key, want := range map[string]string{
"pid": strconv.FormatUint(uint64(fixProcessPID), 10),
} {
have := r.Endpoint.Nodes[scopedLocal].Metadata.Lookup(key)
have, _ := r.Endpoint.Nodes[scopedLocal].Latest.Lookup(key)
if want != have {
t.Errorf("Process.Nodes[%q][%q]: want %q, have %q", scopedLocal, key, want, have)
}
Expand Down
60 changes: 43 additions & 17 deletions probe/host/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package host_test

import (
"net"
"reflect"
"runtime"
"testing"
"time"

"github.com/weaveworks/scope/common/mtime"
"github.com/weaveworks/scope/probe/host"
"github.com/weaveworks/scope/report"
"github.com/weaveworks/scope/test"
)

func TestReporter(t *testing.T) {
Expand All @@ -21,7 +19,7 @@ func TestReporter(t *testing.T) {
hostID = "hostid"
hostname = "hostname"
timestamp = time.Now()
load = report.Metrics{
metrics = report.Metrics{
host.Load1: report.MakeMetric().Add(timestamp, 1.0),
host.Load5: report.MakeMetric().Add(timestamp, 5.0),
host.Load15: report.MakeMetric().Add(timestamp, 15.0),
Expand Down Expand Up @@ -52,23 +50,51 @@ func TestReporter(t *testing.T) {
host.GetMemoryUsageBytes = oldGetMemoryUsageBytes
}()
host.GetKernelVersion = func() (string, error) { return release + " " + version, nil }
host.GetLoad = func(time.Time) report.Metrics { return load }
host.GetLoad = func(time.Time) report.Metrics { return metrics }
host.GetUptime = func() (time.Duration, error) { return time.ParseDuration(uptime) }
host.GetCPUUsagePercent = func() (float64, float64) { return 30.0, 100.0 }
host.GetMemoryUsageBytes = func() (float64, float64) { return 60.0, 100.0 }

want := report.MakeReport()
want.Host.AddNode(report.MakeHostNodeID(hostID), report.MakeNodeWith(map[string]string{
host.Timestamp: timestamp.UTC().Format(time.RFC3339Nano),
host.HostName: hostname,
host.OS: runtime.GOOS,
host.Uptime: uptime,
host.KernelVersion: kernel,
}).WithSets(report.EmptySets.
Add(host.LocalNetworks, report.MakeStringSet(network)),
).WithMetrics(load))
have, _ := host.NewReporter(hostID, hostname, localNets).Report()
if !reflect.DeepEqual(want, have) {
t.Errorf("%s", test.Diff(want, have))
rpt, err := host.NewReporter(hostID, hostname, localNets).Report()
if err != nil {
t.Fatal(err)
}

nodeID := report.MakeHostNodeID(hostID)
node, ok := rpt.Host.Nodes[nodeID]
if !ok {
t.Errorf("Expected host node %q, but not found", nodeID)
}

// Should have a bunch of expected latest keys
for _, tuple := range []struct {
key, want string
}{
{host.Timestamp, timestamp.UTC().Format(time.RFC3339Nano)},
{host.HostName, hostname},
{host.OS, runtime.GOOS},
{host.Uptime, uptime},
{host.KernelVersion, kernel},
} {
if have, ok := node.Latest.Lookup(tuple.key); !ok || have != tuple.want {
t.Errorf("Expected %s %q, got %q", tuple.key, tuple.want, have)
}
}

// Should have the local network
if have, ok := node.Sets.Lookup(host.LocalNetworks); !ok || !have.Contains(network) {
t.Errorf("Expected host.LocalNetworks to include %q, got %q", network, have)
}

// Should have metrics
for key, want := range metrics {
wantSample := want.LastSample()
if metric, ok := node.Metrics[key]; !ok {
t.Errorf("Expected %s metric, but not found", key)
} else if sample := metric.LastSample(); sample == nil {
t.Errorf("Expected %s metric to have a sample, but there were none", key)
} else if sample.Value != wantSample.Value {
t.Errorf("Expected %s metric sample %f, got %f", key, wantSample, sample.Value)
}
}
}
32 changes: 22 additions & 10 deletions probe/host/tagger_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package host_test

import (
"reflect"
"testing"

"github.com/weaveworks/scope/probe/host"
"github.com/weaveworks/scope/report"
"github.com/weaveworks/scope/test"
)

func TestTagger(t *testing.T) {
Expand All @@ -19,15 +17,29 @@ func TestTagger(t *testing.T) {

r := report.MakeReport()
r.Process.AddNode(endpointNodeID, node)
want := node.Merge(report.MakeNodeWith(map[string]string{
report.HostNodeID: report.MakeHostNodeID(hostID),
report.ProbeID: probeID,
}).WithParents(report.EmptySets.
Add(report.Host, report.MakeStringSet(report.MakeHostNodeID(hostID))),
))
rpt, _ := host.NewTagger(hostID, probeID).Tag(r)
have := rpt.Process.Nodes[endpointNodeID].Copy()
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))

// It should now have the host ID
wantHostID := report.MakeHostNodeID(hostID)
if hostID, ok := have.Latest.Lookup(report.HostNodeID); !ok || hostID != wantHostID {
t.Errorf("Expected %q got %q", wantHostID, report.MakeHostNodeID(hostID))
}

// It should now have the probe ID
if haveProbeID, ok := have.Latest.Lookup(report.ProbeID); !ok || haveProbeID != probeID {
t.Errorf("Expected %q got %q", probeID, haveProbeID)
}

// It should still have the other keys
want := "bar"
if have, ok := have.Latest.Lookup("foo"); !ok || have != want {
t.Errorf("Expected %q got %q", want, have)
}

// It should have the host as a parent
wantParent := report.MakeHostNodeID(hostID)
if have, ok := have.Parents.Lookup(report.Host); !ok || len(have) != 1 || have[0] != wantParent {
t.Errorf("Expected %q got %q", report.MakeStringSet(wantParent), have)
}
}
Loading

0 comments on commit 5bc7c31

Please sign in to comment.