Skip to content

Commit

Permalink
fixing up some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbellamy committed Nov 5, 2015
1 parent 35069a0 commit b5d9396
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 63 deletions.
5 changes: 5 additions & 0 deletions probe/docker/container_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ func TestContainer(t *testing.T) {
defer c.StopGatheringStats()

// Send some stats to the docker container
now := time.Now()
stats := &client.Stats{}
stats.Read = now
stats.MemoryStats.Usage = 12345
if err = json.NewEncoder(writer).Encode(&stats); err != nil {
t.Error(err)
Expand All @@ -78,6 +80,9 @@ func TestContainer(t *testing.T) {
"docker_container_ports": report.MakeStringSet("1.2.3.4:80->80/tcp", "81/tcp"),
"docker_container_ips": report.MakeStringSet("1.2.3.4"),
"docker_container_ips_with_scopes": report.MakeStringSet("scope;1.2.3.4"),
}).WithMetrics(report.Metrics{
"cpu_total_usage": report.MakeMetric(),
"memory_usage": report.MakeMetric().Add(now, 12345),
})
test.Poll(t, 100*time.Millisecond, want, func() interface{} {
node := c.GetNode("scope", []net.IP{})
Expand Down
10 changes: 7 additions & 3 deletions probe/host/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ func TestGetKernelVersion(t *testing.T) {

func TestGetLoad(t *testing.T) {
have := host.GetLoad()
if have == nil {
t.Fatal(have)
if len(have) != 3 {
t.Fatalf("Expected 3 metrics, but got: %v", have)
}
for key, metric := range have {
if len(metric.Samples) != 1 {
t.Errorf("Expected metric %s to have 1 sample, but had: %d", key, len(metric.Samples))
}
}
t.Log(have)
}

func TestGetUptime(t *testing.T) {
Expand Down
119 changes: 63 additions & 56 deletions render/detailed_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ func TestOriginTable(t *testing.T) {
Numeric: false,
Rank: 1,
Rows: []render.Row{
{"Load", "0.01 0.01 0.01", "", false},
{"Operating system", "Linux", "", false},
{Key: "Load (1m)", ValueMajor: "0.01", Metric: fixture.LoadMetric, ValueType: "sparkline"},
{Key: "Load (5m)", ValueMajor: "0.01", Metric: fixture.LoadMetric, ValueType: "sparkline"},
{Key: "Load (15m)", ValueMajor: "0.01", Metric: fixture.LoadMetric, ValueType: "sparkline"},
{Key: "Operating system", ValueMajor: "Linux"},
},
},
} {
Expand All @@ -47,22 +49,22 @@ func TestOriginTable(t *testing.T) {
Numeric: false,
Rank: 2,
Rows: []render.Row{
{"Host", fixture.ServerHostID, "", false},
{"Container ID", fixture.ServerContainerID, "", false},
{Key: "Host", ValueMajor: fixture.ServerHostID},
{Key: "Container ID", ValueMajor: fixture.ServerContainerID},
},
},
fixture.ServerContainerNodeID: {
Title: `Container "server"`,
Numeric: false,
Rank: 3,
Rows: []render.Row{
{"Host", fixture.ServerHostID, "", false},
{"ID", fixture.ServerContainerID, "", false},
{"Image ID", fixture.ServerContainerImageID, "", false},
{fmt.Sprintf(`Label %q`, render.AmazonECSContainerNameLabel), `server`, "", false},
{`Label "foo1"`, `bar1`, "", false},
{`Label "foo2"`, `bar2`, "", false},
{`Label "io.kubernetes.pod.name"`, "ping/pong-b", "", false},
{Key: "Host", ValueMajor: fixture.ServerHostID},
{Key: "ID", ValueMajor: fixture.ServerContainerID},
{Key: "Image ID", ValueMajor: fixture.ServerContainerImageID},
{Key: fmt.Sprintf(`Label %q`, render.AmazonECSContainerNameLabel), ValueMajor: `server`},
{Key: `Label "foo1"`, ValueMajor: `bar1`},
{Key: `Label "foo2"`, ValueMajor: `bar2`},
{Key: `Label "io.kubernetes.pod.name"`, ValueMajor: "ping/pong-b"},
},
},
} {
Expand Down Expand Up @@ -93,14 +95,26 @@ func TestMakeDetailedHostNode(t *testing.T) {
Rank: 1,
Rows: []render.Row{
{
Key: "Load",
ValueMajor: "0.01 0.01 0.01",
ValueMinor: "",
Key: "Load (1m)",
ValueMajor: "0.01",
Metric: fixture.LoadMetric,
ValueType: "sparkline",
},
{
Key: "Load (5m)",
ValueMajor: "0.01",
Metric: fixture.LoadMetric,
ValueType: "sparkline",
},
{
Key: "Load (15m)",
ValueMajor: "0.01",
Metric: fixture.LoadMetric,
ValueType: "sparkline",
},
{
Key: "Operating system",
ValueMajor: "Linux",
ValueMinor: "",
},
},
},
Expand All @@ -112,18 +126,15 @@ func TestMakeDetailedHostNode(t *testing.T) {
{
Key: "TCP connections",
ValueMajor: "3",
ValueMinor: "",
},
{
Key: "Client",
ValueMajor: "Server",
ValueMinor: "",
Expandable: true,
},
{
Key: "10.10.10.20",
ValueMajor: "192.168.1.1",
ValueMinor: "",
Expandable: true,
},
},
Expand All @@ -149,22 +160,22 @@ func TestMakeDetailedContainerNode(t *testing.T) {
Numeric: false,
Rank: 4,
Rows: []render.Row{
{"Image ID", fixture.ServerContainerImageID, "", false},
{`Label "foo1"`, `bar1`, "", false},
{`Label "foo2"`, `bar2`, "", false},
{Key: "Image ID", ValueMajor: fixture.ServerContainerImageID},
{Key: `Label "foo1"`, ValueMajor: `bar1`},
{Key: `Label "foo2"`, ValueMajor: `bar2`},
},
},
{
Title: `Container "server"`,
Numeric: false,
Rank: 3,
Rows: []render.Row{
{"ID", fixture.ServerContainerID, "", false},
{"Image ID", fixture.ServerContainerImageID, "", false},
{fmt.Sprintf(`Label %q`, render.AmazonECSContainerNameLabel), `server`, "", false},
{`Label "foo1"`, `bar1`, "", false},
{`Label "foo2"`, `bar2`, "", false},
{`Label "io.kubernetes.pod.name"`, "ping/pong-b", "", false},
{Key: "ID", ValueMajor: fixture.ServerContainerID},
{Key: "Image ID", ValueMajor: fixture.ServerContainerImageID},
{Key: fmt.Sprintf(`Label %q`, render.AmazonECSContainerNameLabel), ValueMajor: `server`},
{Key: `Label "foo1"`, ValueMajor: `bar1`},
{Key: `Label "foo2"`, ValueMajor: `bar2`},
{Key: `Label "io.kubernetes.pod.name"`, ValueMajor: "ping/pong-b"},
},
},
{
Expand All @@ -178,53 +189,49 @@ func TestMakeDetailedContainerNode(t *testing.T) {
Numeric: false,
Rank: 1,
Rows: []render.Row{
{"Load", "0.01 0.01 0.01", "", false},
{"Operating system", "Linux", "", false},
{Key: "Load (1m)", ValueMajor: "0.01", Metric: fixture.LoadMetric, ValueType: "sparkline"},
{Key: "Load (5m)", ValueMajor: "0.01", Metric: fixture.LoadMetric, ValueType: "sparkline"},
{Key: "Load (15m)", ValueMajor: "0.01", Metric: fixture.LoadMetric, ValueType: "sparkline"},
{Key: "Operating system", ValueMajor: "Linux"},
},
},
{
Title: "Connections",
Numeric: false,
Rank: 0,
Rows: []render.Row{
{"Ingress packet rate", "105", "packets/sec", false},
{"Ingress byte rate", "1.0", "KBps", false},
{"Client", "Server", "", true},
{Key: "Ingress packet rate", ValueMajor: "105", ValueMinor: "packets/sec"},
{Key: "Ingress byte rate", ValueMajor: "1.0", ValueMinor: "KBps"},
{Key: "Client", ValueMajor: "Server", Expandable: true},
{
fmt.Sprintf("%s:%s", fixture.UnknownClient1IP, fixture.UnknownClient1Port),
fmt.Sprintf("%s:%s", fixture.ServerIP, fixture.ServerPort),
"",
true,
Key: fmt.Sprintf("%s:%s", fixture.UnknownClient1IP, fixture.UnknownClient1Port),
ValueMajor: fmt.Sprintf("%s:%s", fixture.ServerIP, fixture.ServerPort),
Expandable: true,
},
{
fmt.Sprintf("%s:%s", fixture.UnknownClient2IP, fixture.UnknownClient2Port),
fmt.Sprintf("%s:%s", fixture.ServerIP, fixture.ServerPort),
"",
true,
Key: fmt.Sprintf("%s:%s", fixture.UnknownClient2IP, fixture.UnknownClient2Port),
ValueMajor: fmt.Sprintf("%s:%s", fixture.ServerIP, fixture.ServerPort),
Expandable: true,
},
{
fmt.Sprintf("%s:%s", fixture.UnknownClient3IP, fixture.UnknownClient3Port),
fmt.Sprintf("%s:%s", fixture.ServerIP, fixture.ServerPort),
"",
true,
Key: fmt.Sprintf("%s:%s", fixture.UnknownClient3IP, fixture.UnknownClient3Port),
ValueMajor: fmt.Sprintf("%s:%s", fixture.ServerIP, fixture.ServerPort),
Expandable: true,
},
{
fmt.Sprintf("%s:%s", fixture.ClientIP, fixture.ClientPort54001),
fmt.Sprintf("%s:%s", fixture.ServerIP, fixture.ServerPort),
"",
true,
Key: fmt.Sprintf("%s:%s", fixture.ClientIP, fixture.ClientPort54001),
ValueMajor: fmt.Sprintf("%s:%s", fixture.ServerIP, fixture.ServerPort),
Expandable: true,
},
{
fmt.Sprintf("%s:%s", fixture.ClientIP, fixture.ClientPort54002),
fmt.Sprintf("%s:%s", fixture.ServerIP, fixture.ServerPort),
"",
true,
Key: fmt.Sprintf("%s:%s", fixture.ClientIP, fixture.ClientPort54002),
ValueMajor: fmt.Sprintf("%s:%s", fixture.ServerIP, fixture.ServerPort),
Expandable: true,
},
{
fmt.Sprintf("%s:%s", fixture.RandomClientIP, fixture.RandomClientPort),
fmt.Sprintf("%s:%s", fixture.ServerIP, fixture.ServerPort),
"",
true,
Key: fmt.Sprintf("%s:%s", fixture.RandomClientIP, fixture.RandomClientPort),
ValueMajor: fmt.Sprintf("%s:%s", fixture.ServerIP, fixture.ServerPort),
Expandable: true,
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions render/expected/expected.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ var (
fixture.ServerHostNodeID,
fixture.ServerAddressNodeID,
),
Node: report.MakeNode(),
Node: report.MakeNode().WithMetrics(fixture.LoadMetrics),
EdgeMetadata: report.EdgeMetadata{
MaxConnCountTCP: newu64(3),
},
Expand All @@ -338,7 +338,7 @@ var (
fixture.ClientHostNodeID,
fixture.ClientAddressNodeID,
),
Node: report.MakeNode().WithAdjacent(ServerHostRenderedID),
Node: report.MakeNode().WithAdjacent(ServerHostRenderedID).WithMetrics(fixture.LoadMetrics),
EdgeMetadata: report.EdgeMetadata{
MaxConnCountTCP: newu64(3),
},
Expand Down
5 changes: 5 additions & 0 deletions report/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,11 @@ func MakeMetric() Metric {
return Metric{}
}

func (m Metric) WithFirst(t time.Time) Metric {
m.First = t
return m
}

// Add adds the sample to the Metric. Add is the only valid way to grow a
// Metric. Add returns the Metric to enable chaining.
func (m Metric) Add(t time.Time, v float64) Metric {
Expand Down
19 changes: 17 additions & 2 deletions test/fixture/report_fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
// This is an example Report:
// 2 hosts with probes installed - client & server.
var (
Now = time.Now()

ClientHostID = "client.hostname.com"
ServerHostID = "server.hostname.com"
UnknownHostID = ""
Expand Down Expand Up @@ -96,6 +98,13 @@ var (
ServiceID = "ping/pongservice"
ServiceNodeID = report.MakeServiceNodeID("ping", "pongservice")

LoadMetric = report.MakeMetric().Add(Now, 0.01).WithFirst(Now.Add(-15 * time.Second))
LoadMetrics = report.Metrics{
host.Load1: LoadMetric,
host.Load5: LoadMetric,
host.Load15: LoadMetric,
}

Report = report.Report{
Endpoint: report.Topology{
Nodes: report.Nodes{
Expand Down Expand Up @@ -284,18 +293,24 @@ var (
ClientHostNodeID: report.MakeNodeWith(map[string]string{
"host_name": ClientHostName,
"os": "Linux",
"load": "0.01 0.01 0.01",
report.HostNodeID: ClientHostNodeID,
}).WithSets(report.Sets{
host.LocalNetworks: report.MakeStringSet("10.10.10.0/24"),
}).WithMetrics(report.Metrics{
host.Load1: LoadMetric,
host.Load5: LoadMetric,
host.Load15: LoadMetric,
}),
ServerHostNodeID: report.MakeNodeWith(map[string]string{
"host_name": ServerHostName,
"os": "Linux",
"load": "0.01 0.01 0.01",
report.HostNodeID: ServerHostNodeID,
}).WithSets(report.Sets{
host.LocalNetworks: report.MakeStringSet("10.10.10.0/24"),
}).WithMetrics(report.Metrics{
host.Load1: LoadMetric,
host.Load5: LoadMetric,
host.Load15: LoadMetric,
}),
},
},
Expand Down

0 comments on commit b5d9396

Please sign in to comment.