mirror of
https://github.com/weaveworks/scope.git
synced 2026-05-04 00:10:26 +00:00
It is not a singleton anymore. Instead it is an object with a registry backend. The default registry backend is provided, which is equivalent to what used to be before. Custom backend can be provided for testing purposes. The registry also supports batch operations to remove and add handlers as an atomic step.
103 lines
3.2 KiB
Go
103 lines
3.2 KiB
Go
package host_test
|
|
|
|
import (
|
|
"net"
|
|
"runtime"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/weaveworks/scope/common/mtime"
|
|
"github.com/weaveworks/scope/probe/controls"
|
|
"github.com/weaveworks/scope/probe/host"
|
|
"github.com/weaveworks/scope/report"
|
|
)
|
|
|
|
func TestReporter(t *testing.T) {
|
|
var (
|
|
release = "release"
|
|
version = "version"
|
|
network = "192.168.0.0/16"
|
|
hostID = "hostid"
|
|
hostname = "hostname"
|
|
timestamp = time.Now()
|
|
metrics = report.Metrics{
|
|
host.Load1: report.MakeSingletonMetric(timestamp, 1.0),
|
|
host.CPUUsage: report.MakeSingletonMetric(timestamp, 30.0).WithMax(100.0),
|
|
host.MemoryUsage: report.MakeSingletonMetric(timestamp, 60.0).WithMax(100.0),
|
|
}
|
|
uptime = "278h55m43s"
|
|
kernel = "release version"
|
|
_, ipnet, _ = net.ParseCIDR(network)
|
|
)
|
|
|
|
mtime.NowForce(timestamp)
|
|
defer mtime.NowReset()
|
|
|
|
var (
|
|
oldGetKernelVersion = host.GetKernelVersion
|
|
oldGetLoad = host.GetLoad
|
|
oldGetUptime = host.GetUptime
|
|
oldGetCPUUsagePercent = host.GetCPUUsagePercent
|
|
oldGetMemoryUsageBytes = host.GetMemoryUsageBytes
|
|
oldGetLocalNetworks = host.GetLocalNetworks
|
|
)
|
|
defer func() {
|
|
host.GetKernelVersion = oldGetKernelVersion
|
|
host.GetLoad = oldGetLoad
|
|
host.GetUptime = oldGetUptime
|
|
host.GetCPUUsagePercent = oldGetCPUUsagePercent
|
|
host.GetMemoryUsageBytes = oldGetMemoryUsageBytes
|
|
host.GetLocalNetworks = oldGetLocalNetworks
|
|
}()
|
|
host.GetKernelVersion = func() (string, error) { return release + " " + version, nil }
|
|
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 }
|
|
host.GetLocalNetworks = func() ([]*net.IPNet, error) { return []*net.IPNet{ipnet}, nil }
|
|
|
|
hr := controls.NewDefaultHandlerRegistry()
|
|
rpt, err := host.NewReporter(hostID, hostname, "", "", nil, hr).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, ok := metric.LastSample(); !ok {
|
|
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)
|
|
}
|
|
}
|
|
}
|