stats time precesion affects the test results
This commit is contained in:
parent
6efeaf6b87
commit
115b132e5f
@ -37,7 +37,7 @@ func testGetJsonData(
|
|||||||
return fmt.Errorf("unable to retrieve data: %v", err)
|
return fmt.Errorf("unable to retrieve data: %v", err)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(reply, expected) {
|
if !reflect.DeepEqual(reply, expected) {
|
||||||
return fmt.Errorf("retrieved wrong data: %v != %v", pretty.Sprintf("%# v", reply), pretty.Sprintf("% #v", expected))
|
return pretty.Errorf("retrieved wrong data: %# v != %# v", reply, expected)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -82,12 +82,13 @@ func TestGetMachineinfo(t *testing.T) {
|
|||||||
t.Fatalf("unable to get a client %v", err)
|
t.Fatalf("unable to get a client %v", err)
|
||||||
}
|
}
|
||||||
defer server.Close()
|
defer server.Close()
|
||||||
err = testGetJsonData(minfo, func() (interface{}, error) {
|
returned, err := client.MachineInfo()
|
||||||
return client.MachineInfo()
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
if !reflect.DeepEqual(returned, minfo) {
|
||||||
|
t.Fatalf("received unexpected machine info")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetContainerInfo(t *testing.T) {
|
func TestGetContainerInfo(t *testing.T) {
|
||||||
@ -104,10 +105,37 @@ func TestGetContainerInfo(t *testing.T) {
|
|||||||
t.Fatalf("unable to get a client %v", err)
|
t.Fatalf("unable to get a client %v", err)
|
||||||
}
|
}
|
||||||
defer server.Close()
|
defer server.Close()
|
||||||
err = testGetJsonData(cinfo, func() (interface{}, error) {
|
returned, err := client.ContainerInfo(containerName, query)
|
||||||
return client.ContainerInfo(containerName, query)
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We cannot use DeepEqual() to compare them directly,
|
||||||
|
// because json en/decoded time may have preceision issues.
|
||||||
|
if !reflect.DeepEqual(returned.ContainerReference, cinfo.ContainerReference) {
|
||||||
|
t.Errorf("received unexpected container ref")
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(returned.Subcontainers, cinfo.Subcontainers) {
|
||||||
|
t.Errorf("received unexpected subcontainers")
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(returned.Spec, cinfo.Spec) {
|
||||||
|
t.Errorf("received unexpected spec")
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(returned.StatsPercentiles, cinfo.StatsPercentiles) {
|
||||||
|
t.Errorf("received unexpected spec")
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, expectedStats := range cinfo.Stats {
|
||||||
|
returnedStats := returned.Stats[i]
|
||||||
|
if !expectedStats.Eq(returnedStats) {
|
||||||
|
t.Errorf("received unexpected stats")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, expectedSample := range cinfo.Samples {
|
||||||
|
returnedSample := returned.Samples[i]
|
||||||
|
if !expectedSample.Eq(returnedSample) {
|
||||||
|
t.Errorf("received unexpected sample")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ package info
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -247,6 +248,67 @@ type ContainerStatsSample struct {
|
|||||||
} `json:"memory"`
|
} `json:"memory"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func timeEq(t1, t2 time.Time, tolerance time.Duration) bool {
|
||||||
|
// t1 should not be later than t2
|
||||||
|
if t1.After(t2) {
|
||||||
|
t1, t2 = t2, t1
|
||||||
|
}
|
||||||
|
diff := t2.Sub(t1)
|
||||||
|
if diff <= tolerance {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func durationEq(a, b time.Duration, tolerance time.Duration) bool {
|
||||||
|
if a > b {
|
||||||
|
a, b = b, a
|
||||||
|
}
|
||||||
|
diff := a - b
|
||||||
|
if diff <= tolerance {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
// 10ms, i.e. 0.01s
|
||||||
|
timePrecision time.Duration = 10 * time.Millisecond
|
||||||
|
)
|
||||||
|
|
||||||
|
// This function is useful because we do not require precise time
|
||||||
|
// representation.
|
||||||
|
func (a *ContainerStats) Eq(b *ContainerStats) bool {
|
||||||
|
if !timeEq(a.Timestamp, b.Timestamp, timePrecision) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(a.Cpu, b.Cpu) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(a.Memory, b.Memory) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// This function is useful because we do not require precise time
|
||||||
|
// representation.
|
||||||
|
func (a *ContainerStatsSample) Eq(b *ContainerStatsSample) bool {
|
||||||
|
if !timeEq(a.Timestamp, b.Timestamp, timePrecision) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !durationEq(a.Duration, b.Duration, timePrecision) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(a.Cpu, b.Cpu) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(a.Memory, b.Memory) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
type Percentile struct {
|
type Percentile struct {
|
||||||
Percentage int `json:"percentage"`
|
Percentage int `json:"percentage"`
|
||||||
Value uint64 `json:"value"`
|
Value uint64 `json:"value"`
|
||||||
|
Loading…
Reference in New Issue
Block a user