From 0162b1f59d394e2bf08bafd89a8b7ceb111354a5 Mon Sep 17 00:00:00 2001 From: Victor Marmol Date: Wed, 17 Dec 2014 15:52:37 -0800 Subject: [PATCH 1/2] Add testify/require to godeps --- Godeps/Godeps.json | 8 +- .../stretchr/testify/require/doc.go | 77 +++++ .../stretchr/testify/require/requirements.go | 267 ++++++++++++++++++ .../testify/require/requirements_test.go | 266 +++++++++++++++++ 4 files changed, 616 insertions(+), 2 deletions(-) create mode 100644 Godeps/_workspace/src/github.com/stretchr/testify/require/doc.go create mode 100644 Godeps/_workspace/src/github.com/stretchr/testify/require/requirements.go create mode 100644 Godeps/_workspace/src/github.com/stretchr/testify/require/requirements_test.go diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 15d6e736..11f2322b 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -33,6 +33,10 @@ "ImportPath": "github.com/SeanDolphin/bqschema", "Rev": "a713d26df274e999ec10e9bbc7e73a1c4791053c" }, + { + "ImportPath": "github.com/abbot/go-http-auth", + "Rev": "c0ef4539dfab4d21c8ef20ba2924f9fc6f186d35" + }, { "ImportPath": "github.com/coreos/go-systemd/dbus", "Comment": "v2-27-g97e243d", @@ -99,8 +103,8 @@ "Rev": "8ce79b9f0b77745113f82c17d0756771456ccbd3" }, { - "ImportPath": "github.com/abbot/go-http-auth", - "Rev": "c0ef4539dfab4d21c8ef20ba2924f9fc6f186d35" + "ImportPath": "github.com/stretchr/testify/require", + "Rev": "8ce79b9f0b77745113f82c17d0756771456ccbd3" } ] } diff --git a/Godeps/_workspace/src/github.com/stretchr/testify/require/doc.go b/Godeps/_workspace/src/github.com/stretchr/testify/require/doc.go new file mode 100644 index 00000000..7b38438f --- /dev/null +++ b/Godeps/_workspace/src/github.com/stretchr/testify/require/doc.go @@ -0,0 +1,77 @@ +// Alternative testing tools which stop test execution if test failed. +// +// Example Usage +// +// The following is a complete example using require in a standard test function: +// import ( +// "testing" +// "github.com/stretchr/testify/require" +// ) +// +// func TestSomething(t *testing.T) { +// +// var a string = "Hello" +// var b string = "Hello" +// +// require.Equal(t, a, b, "The two words should be the same.") +// +// } +// +// Assertions +// +// The `require` package have same global functions as in the `assert` package, +// but instead of returning a boolean result they call `t.FailNow()`. +// +// Every assertion function also takes an optional string message as the final argument, +// allowing custom error messages to be appended to the message the assertion method outputs. +// +// Here is an overview of the assert functions: +// +// require.Equal(t, expected, actual [, message [, format-args]) +// +// require.NotEqual(t, notExpected, actual [, message [, format-args]]) +// +// require.True(t, actualBool [, message [, format-args]]) +// +// require.False(t, actualBool [, message [, format-args]]) +// +// require.Nil(t, actualObject [, message [, format-args]]) +// +// require.NotNil(t, actualObject [, message [, format-args]]) +// +// require.Empty(t, actualObject [, message [, format-args]]) +// +// require.NotEmpty(t, actualObject [, message [, format-args]]) +// +// require.Error(t, errorObject [, message [, format-args]]) +// +// require.NoError(t, errorObject [, message [, format-args]]) +// +// require.EqualError(t, theError, errString [, message [, format-args]]) +// +// require.Implements(t, (*MyInterface)(nil), new(MyObject) [,message [, format-args]]) +// +// require.IsType(t, expectedObject, actualObject [, message [, format-args]]) +// +// require.Contains(t, string, substring [, message [, format-args]]) +// +// require.NotContains(t, string, substring [, message [, format-args]]) +// +// require.Panics(t, func(){ +// +// // call code that should panic +// +// } [, message [, format-args]]) +// +// require.NotPanics(t, func(){ +// +// // call code that should not panic +// +// } [, message [, format-args]]) +// +// require.WithinDuration(t, timeA, timeB, deltaTime, [, message [, format-args]]) +// +// require.InDelta(t, numA, numB, delta, [, message [, format-args]]) +// +// require.InEpsilon(t, numA, numB, epsilon, [, message [, format-args]]) +package require diff --git a/Godeps/_workspace/src/github.com/stretchr/testify/require/requirements.go b/Godeps/_workspace/src/github.com/stretchr/testify/require/requirements.go new file mode 100644 index 00000000..6744d8b9 --- /dev/null +++ b/Godeps/_workspace/src/github.com/stretchr/testify/require/requirements.go @@ -0,0 +1,267 @@ +package require + +import ( + "github.com/stretchr/testify/assert" + "time" +) + +type TestingT interface { + Errorf(format string, args ...interface{}) + FailNow() +} + +// Fail reports a failure through +func FailNow(t TestingT, failureMessage string, msgAndArgs ...interface{}) { + assert.Fail(t, failureMessage, msgAndArgs...) + t.FailNow() +} + +// Implements asserts that an object is implemented by the specified interface. +// +// require.Implements(t, (*MyInterface)(nil), new(MyObject), "MyObject") +func Implements(t TestingT, interfaceObject interface{}, object interface{}, msgAndArgs ...interface{}) { + if !assert.Implements(t, interfaceObject, object, msgAndArgs...) { + t.FailNow() + } +} + +// IsType asserts that the specified objects are of the same type. +func IsType(t TestingT, expectedType interface{}, object interface{}, msgAndArgs ...interface{}) { + if !assert.IsType(t, expectedType, object, msgAndArgs...) { + t.FailNow() + } +} + +// Equal asserts that two objects are equal. +// +// require.Equal(t, 123, 123, "123 and 123 should be equal") +// +// Returns whether the assertion was successful (true) or not (false). +func Equal(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) { + if !assert.Equal(t, expected, actual, msgAndArgs...) { + t.FailNow() + } +} + +// Exactly asserts that two objects are equal is value and type. +// +// require.Exactly(t, int32(123), int64(123), "123 and 123 should NOT be equal") +// +// Returns whether the assertion was successful (true) or not (false). +func Exactly(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) { + if !assert.Exactly(t, expected, actual, msgAndArgs...) { + t.FailNow() + } +} + +// NotNil asserts that the specified object is not nil. +// +// require.NotNil(t, err, "err should be something") +// +// Returns whether the assertion was successful (true) or not (false). +func NotNil(t TestingT, object interface{}, msgAndArgs ...interface{}) { + if !assert.NotNil(t, object, msgAndArgs...) { + t.FailNow() + } +} + +// Nil asserts that the specified object is nil. +// +// require.Nil(t, err, "err should be nothing") +// +// Returns whether the assertion was successful (true) or not (false). +func Nil(t TestingT, object interface{}, msgAndArgs ...interface{}) { + if !assert.Nil(t, object, msgAndArgs...) { + t.FailNow() + } +} + +// Empty asserts that the specified object is empty. I.e. nil, "", false, 0 or either +// a slice or a channel with len == 0. +// +// require.Empty(t, obj) +// +// Returns whether the assertion was successful (true) or not (false). +func Empty(t TestingT, object interface{}, msgAndArgs ...interface{}) { + if !assert.Empty(t, object, msgAndArgs...) { + t.FailNow() + } +} + +// NotEmpty asserts that the specified object is NOT empty. I.e. not nil, "", false, 0 or either +// a slice or a channel with len == 0. +// +// require.NotEmpty(t, obj) +// require.Equal(t, "one", obj[0]) +// +// Returns whether the assertion was successful (true) or not (false). +func NotEmpty(t TestingT, object interface{}, msgAndArgs ...interface{}) { + if !assert.NotEmpty(t, object, msgAndArgs...) { + t.FailNow() + } +} + +// True asserts that the specified value is true. +// +// require.True(t, myBool, "myBool should be true") +// +// Returns whether the assertion was successful (true) or not (false). +func True(t TestingT, value bool, msgAndArgs ...interface{}) { + if !assert.True(t, value, msgAndArgs...) { + t.FailNow() + } +} + +// False asserts that the specified value is true. +// +// require.False(t, myBool, "myBool should be false") +// +// Returns whether the assertion was successful (true) or not (false). +func False(t TestingT, value bool, msgAndArgs ...interface{}) { + if !assert.False(t, value, msgAndArgs...) { + t.FailNow() + } +} + +// NotEqual asserts that the specified values are NOT equal. +// +// require.NotEqual(t, obj1, obj2, "two objects shouldn't be equal") +// +// Returns whether the assertion was successful (true) or not (false). +func NotEqual(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) { + if !assert.NotEqual(t, expected, actual, msgAndArgs...) { + t.FailNow() + } +} + +// Contains asserts that the specified string contains the specified substring. +// +// require.Contains(t, "Hello World", "World", "But 'Hello World' does contain 'World'") +// +// Returns whether the assertion was successful (true) or not (false). +func Contains(t TestingT, s, contains string, msgAndArgs ...interface{}) { + if !assert.Contains(t, s, contains, msgAndArgs...) { + t.FailNow() + } +} + +// NotContains asserts that the specified string does NOT contain the specified substring. +// +// require.NotContains(t, "Hello World", "Earth", "But 'Hello World' does NOT contain 'Earth'") +// +// Returns whether the assertion was successful (true) or not (false). +func NotContains(t TestingT, s, contains string, msgAndArgs ...interface{}) { + if !assert.NotContains(t, s, contains, msgAndArgs...) { + t.FailNow() + } +} + +// Condition uses a Comparison to assert a complex condition. +func Condition(t TestingT, comp assert.Comparison, msgAndArgs ...interface{}) { + if !assert.Condition(t, comp, msgAndArgs...) { + t.FailNow() + } +} + +// Panics asserts that the code inside the specified PanicTestFunc panics. +// +// require.Panics(t, func(){ +// GoCrazy() +// }, "Calling GoCrazy() should panic") +// +// Returns whether the assertion was successful (true) or not (false). +func Panics(t TestingT, f assert.PanicTestFunc, msgAndArgs ...interface{}) { + if !assert.Panics(t, f, msgAndArgs...) { + t.FailNow() + } +} + +// NotPanics asserts that the code inside the specified PanicTestFunc does NOT panic. +// +// require.NotPanics(t, func(){ +// RemainCalm() +// }, "Calling RemainCalm() should NOT panic") +// +// Returns whether the assertion was successful (true) or not (false). +func NotPanics(t TestingT, f assert.PanicTestFunc, msgAndArgs ...interface{}) { + if !assert.NotPanics(t, f, msgAndArgs...) { + t.FailNow() + } +} + +// WithinDuration asserts that the two times are within duration delta of each other. +// +// require.WithinDuration(t, time.Now(), time.Now(), 10*time.Second, "The difference should not be more than 10s") +// +// Returns whether the assertion was successful (true) or not (false). +func WithinDuration(t TestingT, expected, actual time.Time, delta time.Duration, msgAndArgs ...interface{}) { + if !assert.WithinDuration(t, expected, actual, delta, msgAndArgs...) { + t.FailNow() + } +} + +// InDelta asserts that the two numerals are within delta of each other. +// +// require.InDelta(t, math.Pi, (22 / 7.0), 0.01) +// +// Returns whether the assertion was successful (true) or not (false). +func InDelta(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{}) { + if !assert.InDelta(t, expected, actual, delta, msgAndArgs...) { + t.FailNow() + } +} + +// InEpsilon asserts that expected and actual have a relative error less than epsilon +// +// Returns whether the assertion was successful (true) or not (false). +func InEpsilon(t TestingT, expected, actual interface{}, epsilon float64, msgAndArgs ...interface{}) { + if !assert.InEpsilon(t, expected, actual, epsilon, msgAndArgs...) { + t.FailNow() + } +} + +/* + Errors +*/ + +// NoError asserts that a function returned no error (i.e. `nil`). +// +// actualObj, err := SomeFunction() +// require.NoError(t, err) +// require.Equal(t, actualObj, expectedObj) +// +// Returns whether the assertion was successful (true) or not (false). +func NoError(t TestingT, err error, msgAndArgs ...interface{}) { + if !assert.NoError(t, err, msgAndArgs...) { + t.FailNow() + } +} + +// Error asserts that a function returned an error (i.e. not `nil`). +// +// actualObj, err := SomeFunction() +// require.Error(t, err, "An error was expected") +// require.Equal(t, err, expectedError) +// } +// +// Returns whether the assertion was successful (true) or not (false). +func Error(t TestingT, err error, msgAndArgs ...interface{}) { + if !assert.Error(t, err, msgAndArgs...) { + t.FailNow() + } +} + +// EqualError asserts that a function returned an error (i.e. not `nil`) +// and that it is equal to the provided error. +// +// actualObj, err := SomeFunction() +// require.Error(t, err, "An error was expected") +// require.Equal(t, err, expectedError) +// } +// +// Returns whether the assertion was successful (true) or not (false). +func EqualError(t TestingT, theError error, errString string, msgAndArgs ...interface{}) { + if !assert.EqualError(t, theError, errString, msgAndArgs...) { + t.FailNow() + } +} diff --git a/Godeps/_workspace/src/github.com/stretchr/testify/require/requirements_test.go b/Godeps/_workspace/src/github.com/stretchr/testify/require/requirements_test.go new file mode 100644 index 00000000..9131b2f4 --- /dev/null +++ b/Godeps/_workspace/src/github.com/stretchr/testify/require/requirements_test.go @@ -0,0 +1,266 @@ +package require + +import ( + "errors" + "testing" + "time" +) + +// AssertionTesterInterface defines an interface to be used for testing assertion methods +type AssertionTesterInterface interface { + TestMethod() +} + +// AssertionTesterConformingObject is an object that conforms to the AssertionTesterInterface interface +type AssertionTesterConformingObject struct { +} + +func (a *AssertionTesterConformingObject) TestMethod() { +} + +// AssertionTesterNonConformingObject is an object that does not conform to the AssertionTesterInterface interface +type AssertionTesterNonConformingObject struct { +} + +type MockT struct { + Failed bool +} + +func (t *MockT) FailNow() { + t.Failed = true +} + +func (t *MockT) Errorf(format string, args ...interface{}) { + _, _ = format, args +} + +func TestImplements(t *testing.T) { + + Implements(t, (*AssertionTesterInterface)(nil), new(AssertionTesterConformingObject)) + + mockT := new(MockT) + Implements(mockT, (*AssertionTesterInterface)(nil), new(AssertionTesterNonConformingObject)) + if !mockT.Failed { + t.Error("Check should fail") + } +} + +func TestIsType(t *testing.T) { + + IsType(t, new(AssertionTesterConformingObject), new(AssertionTesterConformingObject)) + + mockT := new(MockT) + IsType(mockT, new(AssertionTesterConformingObject), new(AssertionTesterNonConformingObject)) + if !mockT.Failed { + t.Error("Check should fail") + } +} + +func TestEqual(t *testing.T) { + + Equal(t, 1, 1) + + mockT := new(MockT) + Equal(mockT, 1, 2) + if !mockT.Failed { + t.Error("Check should fail") + } + +} + +func TestNotEqual(t *testing.T) { + + NotEqual(t, 1, 2) + mockT := new(MockT) + NotEqual(mockT, 2, 2) + if !mockT.Failed { + t.Error("Check should fail") + } +} + +func TestExactly(t *testing.T) { + + a := float32(1) + b := float32(1) + c := float64(1) + + Exactly(t, a, b) + + mockT := new(MockT) + Exactly(mockT, a, c) + if !mockT.Failed { + t.Error("Check should fail") + } +} + +func TestNotNil(t *testing.T) { + + NotNil(t, new(AssertionTesterConformingObject)) + + mockT := new(MockT) + NotNil(mockT, nil) + if !mockT.Failed { + t.Error("Check should fail") + } +} + +func TestNil(t *testing.T) { + + Nil(t, nil) + + mockT := new(MockT) + Nil(mockT, new(AssertionTesterConformingObject)) + if !mockT.Failed { + t.Error("Check should fail") + } +} + +func TestTrue(t *testing.T) { + + True(t, true) + + mockT := new(MockT) + True(mockT, false) + if !mockT.Failed { + t.Error("Check should fail") + } +} + +func TestFalse(t *testing.T) { + + False(t, false) + + mockT := new(MockT) + False(mockT, true) + if !mockT.Failed { + t.Error("Check should fail") + } +} + +func TestContains(t *testing.T) { + + Contains(t, "Hello World", "Hello") + + mockT := new(MockT) + Contains(mockT, "Hello World", "Salut") + if !mockT.Failed { + t.Error("Check should fail") + } +} + +func TestNotContains(t *testing.T) { + + NotContains(t, "Hello World", "Hello!") + + mockT := new(MockT) + NotContains(mockT, "Hello World", "Hello") + if !mockT.Failed { + t.Error("Check should fail") + } +} + +func TestPanics(t *testing.T) { + + Panics(t, func() { + panic("Panic!") + }) + + mockT := new(MockT) + Panics(mockT, func() {}) + if !mockT.Failed { + t.Error("Check should fail") + } +} + +func TestNotPanics(t *testing.T) { + + NotPanics(t, func() {}) + + mockT := new(MockT) + NotPanics(mockT, func() { + panic("Panic!") + }) + if !mockT.Failed { + t.Error("Check should fail") + } +} + +func TestNoError(t *testing.T) { + + NoError(t, nil) + + mockT := new(MockT) + NoError(mockT, errors.New("some error")) + if !mockT.Failed { + t.Error("Check should fail") + } +} + +func TestError(t *testing.T) { + + Error(t, errors.New("some error")) + + mockT := new(MockT) + Error(mockT, nil) + if !mockT.Failed { + t.Error("Check should fail") + } +} + +func TestEqualError(t *testing.T) { + + EqualError(t, errors.New("some error"), "some error") + + mockT := new(MockT) + EqualError(mockT, errors.New("some error"), "Not some error") + if !mockT.Failed { + t.Error("Check should fail") + } +} + +func TestEmpty(t *testing.T) { + + Empty(t, "") + + mockT := new(MockT) + Empty(mockT, "x") + if !mockT.Failed { + t.Error("Check should fail") + } +} + +func TestNotEmpty(t *testing.T) { + + NotEmpty(t, "x") + + mockT := new(MockT) + NotEmpty(mockT, "") + if !mockT.Failed { + t.Error("Check should fail") + } +} + +func TestWithinDuration(t *testing.T) { + + a := time.Now() + b := a.Add(10 * time.Second) + + WithinDuration(t, a, b, 15*time.Second) + + mockT := new(MockT) + WithinDuration(mockT, a, b, 5*time.Second) + if !mockT.Failed { + t.Error("Check should fail") + } +} + +func TestInDelta(t *testing.T) { + + InDelta(t, 1.001, 1, 0.01) + + mockT := new(MockT) + InDelta(mockT, 1, 2, 0.5) + if !mockT.Failed { + t.Error("Check should fail") + } +} From 024c3770a34821b7336c6524a086a9c572ef62f5 Mon Sep 17 00:00:00 2001 From: Victor Marmol Date: Sat, 22 Nov 2014 13:40:49 +0800 Subject: [PATCH 2/2] Refactoring Docker integration test. Uses assert and require. Moves common components to a common file for reuse. --- integration/tests/api/docker_test.go | 163 +++++++-------------------- integration/tests/api/test_utils.go | 48 ++++++++ 2 files changed, 88 insertions(+), 123 deletions(-) create mode 100644 integration/tests/api/test_utils.go diff --git a/integration/tests/api/docker_test.go b/integration/tests/api/docker_test.go index 452dedc4..f18fd833 100644 --- a/integration/tests/api/docker_test.go +++ b/integration/tests/api/docker_test.go @@ -9,43 +9,34 @@ import ( "github.com/google/cadvisor/info" "github.com/google/cadvisor/integration/framework" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) -// Checks whether el is in vals. -func contains(el string, vals []string) bool { - for _, val := range vals { - if el == val { - return true - } - } - return false -} - // Sanity check the container by: // - Checking that the specified alias is a valid one for this container. // - Verifying that stats are not empty. func sanityCheck(alias string, containerInfo info.ContainerInfo, t *testing.T) { - if !contains(alias, containerInfo.Aliases) { - t.Errorf("Failed to find container alias %q in aliases %v", alias, containerInfo.Aliases) - } - if len(containerInfo.Stats) == 0 { - t.Errorf("No container stats found: %+v", containerInfo) - } + assert.Contains(t, containerInfo.Aliases, alias, "Alias %q should be in list of aliases %v", alias, containerInfo.Aliases) + assert.NotEmpty(t, containerInfo.Stats, "Expected container to have stats") } // Waits up to 5s for a container with the specified alias to appear. func waitForContainer(alias string, fm framework.Framework) { err := framework.RetryForDuration(func() error { - _, err := fm.Cadvisor().Client().DockerContainer(alias, &info.ContainerInfoRequest{}) + ret, err := fm.Cadvisor().Client().DockerContainer(alias, &info.ContainerInfoRequest{ + NumStats: 1, + }) if err != nil { return err } + if len(ret.Stats) != 1 { + return fmt.Errorf("no stats returned for container %q", alias) + } return nil }, 5*time.Second) - if err != nil { - fm.T().Fatalf("Timed out waiting for container %q to be available in cAdvisor: %v", alias, err) - } + require.NoError(fm.T(), err, "Timed out waiting for container %q to be available in cAdvisor: %v", alias, err) } // A Docker container in /docker/ @@ -62,9 +53,7 @@ func TestDockerContainerById(t *testing.T) { NumStats: 1, } containerInfo, err := fm.Cadvisor().Client().DockerContainer(containerId, request) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) sanityCheck(containerId, containerInfo, t) } @@ -78,7 +67,7 @@ func TestDockerContainerByName(t *testing.T) { fm.Docker().Run(framework.DockerRunArgs{ Image: "kubernetes/pause", Args: []string{"--name", containerName}, - }, "sleep", "inf") + }) // Wait for the container to show up. waitForContainer(containerName, fm) @@ -87,9 +76,7 @@ func TestDockerContainerByName(t *testing.T) { NumStats: 1, } containerInfo, err := fm.Cadvisor().Client().DockerContainer(containerName, request) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) sanityCheck(containerName, containerInfo, t) } @@ -97,8 +84,10 @@ func TestDockerContainerByName(t *testing.T) { // Find the first container with the specified alias in containers. func findContainer(alias string, containers []info.ContainerInfo, t *testing.T) info.ContainerInfo { for _, cont := range containers { - if contains(alias, cont.Aliases) { - return cont + for _, a := range cont.Aliases { + if alias == a { + return cont + } } } t.Fatalf("Failed to find container %q in %+v", alias, containers) @@ -120,9 +109,7 @@ func TestGetAllDockerContainers(t *testing.T) { NumStats: 1, } containersInfo, err := fm.Cadvisor().Client().AllDockerContainers(request) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) if len(containersInfo) < 2 { t.Fatalf("At least 2 Docker containers should exist, received %d: %+v", len(containersInfo), containersInfo) @@ -142,7 +129,7 @@ func TestBasicDockerContainer(t *testing.T) { Args: []string{ "--name", containerName, }, - }, "sleep", "inf") + }) // Wait for the container to show up. waitForContainer(containerId, fm) @@ -151,29 +138,14 @@ func TestBasicDockerContainer(t *testing.T) { NumStats: 1, } containerInfo, err := fm.Cadvisor().Client().DockerContainer(containerId, request) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) // Check that the contianer is known by both its name and ID. sanityCheck(containerId, containerInfo, t) sanityCheck(containerName, containerInfo, t) - if len(containerInfo.Subcontainers) != 0 { - t.Errorf("Container has subcontainers: %+v", containerInfo) - } - - if len(containerInfo.Stats) != 1 { - t.Fatalf("Container has more than 1 stat, has %d: %+v", len(containerInfo.Stats), containerInfo.Stats) - } -} - -// Returns the difference between a and b. -func difference(a, b uint64) uint64 { - if a > b { - return a - b - } - return b - a + assert.Empty(t, containerInfo.Subcontainers, "Should not have subcontainers") + assert.Len(t, containerInfo.Stats, 1, "Should have exactly one stat") } // TODO(vmarmol): Handle if CPU or memory is not isolated on this system. @@ -192,7 +164,7 @@ func TestDockerContainerSpec(t *testing.T) { "--cpuset", cpuMask, "--memory", strconv.FormatUint(memoryLimit, 10), }, - }, "sleep", "inf") + }) // Wait for the container to show up. waitForContainer(containerId, fm) @@ -201,45 +173,18 @@ func TestDockerContainerSpec(t *testing.T) { NumStats: 1, } containerInfo, err := fm.Cadvisor().Client().DockerContainer(containerId, request) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) sanityCheck(containerId, containerInfo, t) - if !containerInfo.Spec.HasCpu { - t.Errorf("CPU should be isolated: %+v", containerInfo) - } - if containerInfo.Spec.Cpu.Limit != cpuShares { - t.Errorf("Container should have %d shares, has %d", cpuShares, containerInfo.Spec.Cpu.Limit) - } - if containerInfo.Spec.Cpu.Mask != cpuMask { - t.Errorf("Cpu mask should be %q, but is %q", cpuMask, containerInfo.Spec.Cpu.Mask) - } - if !containerInfo.Spec.HasMemory { - t.Errorf("Memory should be isolated: %+v", containerInfo) - } - if containerInfo.Spec.Memory.Limit != memoryLimit { - t.Errorf("Container should have memory limit of %d, has %d", memoryLimit, containerInfo.Spec.Memory.Limit) - } - if !containerInfo.Spec.HasNetwork { - t.Errorf("Network should be isolated: %+v", containerInfo) - } - if !containerInfo.Spec.HasFilesystem { - t.Errorf("Filesystem should be isolated: %+v", containerInfo) - } -} + assert := assert.New(t) -// Expect the specified value to be non-zero. -func expectNonZero(val int, description string, t *testing.T) { - if val < 0 { - t.Errorf("%s should be posiive", description) - } - expectNonZeroU(uint64(val), description, t) -} -func expectNonZeroU(val uint64, description string, t *testing.T) { - if val == 0 { - t.Errorf("%s should be non-zero", description) - } + assert.True(containerInfo.Spec.HasCpu, "CPU should be isolated") + assert.Equal(containerInfo.Spec.Cpu.Limit, cpuShares, "Container should have %d shares, has %d", cpuShares, containerInfo.Spec.Cpu.Limit) + assert.Equal(containerInfo.Spec.Cpu.Mask, cpuMask, "Cpu mask should be %q, but is %q", cpuMask, containerInfo.Spec.Cpu.Mask) + assert.True(containerInfo.Spec.HasMemory, "Memory should be isolated") + assert.Equal(containerInfo.Spec.Memory.Limit, memoryLimit, "Container should have memory limit of %d, has %d", memoryLimit, containerInfo.Spec.Memory.Limit) + assert.True(containerInfo.Spec.HasNetwork, "Network should be isolated") + assert.True(containerInfo.Spec.HasFilesystem, "Filesystem should be isolated") } // Check the CPU ContainerStats. @@ -259,27 +204,9 @@ func TestDockerContainerCpuStats(t *testing.T) { t.Fatal(err) } sanityCheck(containerId, containerInfo, t) - stat := containerInfo.Stats[0] // Checks for CpuStats. - expectNonZeroU(stat.Cpu.Usage.Total, "CPU total usage", t) - expectNonZero(len(stat.Cpu.Usage.PerCpu), "per-core CPU usage", t) - totalUsage := uint64(0) - for _, usage := range stat.Cpu.Usage.PerCpu { - totalUsage += usage - } - dif := difference(totalUsage, stat.Cpu.Usage.Total) - if dif > uint64((5 * time.Millisecond).Nanoseconds()) { - t.Errorf("Per-core CPU usage (%d) and total usage (%d) are more than 1ms off", totalUsage, stat.Cpu.Usage.Total) - } - userPlusSystem := stat.Cpu.Usage.User + stat.Cpu.Usage.System - dif = difference(totalUsage, userPlusSystem) - if dif > uint64((25 * time.Millisecond).Nanoseconds()) { - t.Errorf("User + system CPU usage (%d) and total usage (%d) are more than 20ms off", userPlusSystem, stat.Cpu.Usage.Total) - } - if stat.Cpu.Load != 0 { - t.Errorf("Non-zero load is unexpected as it is currently unset. Do we need to update the test?") - } + checkCpuStats(t, containerInfo.Stats[0].Cpu) } // Check the memory ContainerStats. @@ -295,19 +222,11 @@ func TestDockerContainerMemoryStats(t *testing.T) { NumStats: 1, } containerInfo, err := fm.Cadvisor().Client().DockerContainer(containerId, request) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) sanityCheck(containerId, containerInfo, t) - stat := containerInfo.Stats[0] // Checks for MemoryStats. - expectNonZeroU(stat.Memory.Usage, "memory usage", t) - expectNonZeroU(stat.Memory.WorkingSet, "memory working set", t) - if stat.Memory.WorkingSet > stat.Memory.Usage { - t.Errorf("Memory working set (%d) should be at most equal to memory usage (%d)", stat.Memory.WorkingSet, stat.Memory.Usage) - } - // TODO(vmarmol): Add checks for ContainerData and HierarchicalData + checkMemoryStats(t, containerInfo.Stats[0].Memory) } // Check the network ContainerStats. @@ -323,14 +242,12 @@ func TestDockerContainerNetworkStats(t *testing.T) { NumStats: 1, } containerInfo, err := fm.Cadvisor().Client().DockerContainer(containerId, request) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) sanityCheck(containerId, containerInfo, t) - stat := containerInfo.Stats[0] // Checks for NetworkStats. - expectNonZeroU(stat.Network.TxBytes, "network tx bytes", t) - expectNonZeroU(stat.Network.TxPackets, "network tx packets", t) + stat := containerInfo.Stats[0] + assert.NotEqual(t, 0, stat.Network.TxBytes, "Network tx bytes should not bet zero") + assert.NotEqual(t, 0, stat.Network.TxPackets, "Network tx packets should not bet zero") // TODO(vmarmol): Can probably do a better test with two containers pinging each other. } diff --git a/integration/tests/api/test_utils.go b/integration/tests/api/test_utils.go new file mode 100644 index 00000000..112966d0 --- /dev/null +++ b/integration/tests/api/test_utils.go @@ -0,0 +1,48 @@ +package api + +import ( + "testing" + "time" + + "github.com/google/cadvisor/info" + "github.com/stretchr/testify/assert" +) + +// Checks that expected and actual are within delta of each other. +func inDelta(t *testing.T, expected, actual, delta uint64, description string) { + var diff uint64 + if expected > actual { + diff = expected - actual + } else { + diff = actual - expected + } + if diff > delta { + t.Errorf("%s (%d and %d) are not within %d of each other", description, expected, actual, delta) + } +} + +// Checks that CPU stats are valid. +func checkCpuStats(t *testing.T, stat info.CpuStats) { + assert := assert.New(t) + + assert.NotEqual(0, stat.Usage.Total, "Total CPU usage should not be zero") + assert.NotEmpty(stat.Usage.PerCpu, "Per-core usage should not be empty") + totalUsage := uint64(0) + for _, usage := range stat.Usage.PerCpu { + totalUsage += usage + } + inDelta(t, stat.Usage.Total, totalUsage, uint64((5 * time.Millisecond).Nanoseconds()), "Per-core CPU usage") + inDelta(t, stat.Usage.Total, stat.Usage.User+stat.Usage.System, uint64((25 * time.Millisecond).Nanoseconds()), "User + system CPU usage") + assert.Equal(0, stat.Load, "Non-zero load is unexpected as it is currently unset. Do we need to update the test?") +} + +func checkMemoryStats(t *testing.T, stat info.MemoryStats) { + assert := assert.New(t) + + assert.NotEqual(0, stat.Usage, "Memory usage should not be zero") + assert.NotEqual(0, stat.WorkingSet, "Memory working set should not be zero") + if stat.WorkingSet > stat.Usage { + t.Errorf("Memory working set (%d) should be at most equal to memory usage (%d)", stat.WorkingSet, stat.Usage) + } + // TODO(vmarmol): Add checks for ContainerData and HierarchicalData +}