Refactor container gc tests

pull/6/head
feisky 2015-10-03 23:40:00 +08:00
parent 4c8a836260
commit 69867fb502
1 changed files with 13 additions and 19 deletions

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package kubelet package dockertools
import ( import (
"fmt" "fmt"
@ -25,23 +25,17 @@ import (
docker "github.com/fsouza/go-dockerclient" docker "github.com/fsouza/go-dockerclient"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/kubernetes/pkg/kubelet/dockertools"
) )
func newTestContainerGC(t *testing.T, MinAge time.Duration, MaxPerPodContainer, MaxContainers int) (containerGC, *dockertools.FakeDockerClient) { func newTestContainerGC(t *testing.T) (*containerGC, *FakeDockerClient) {
fakeDocker := new(dockertools.FakeDockerClient) fakeDocker := new(FakeDockerClient)
gc, err := newContainerGC(fakeDocker, ContainerGCPolicy{ gc := NewContainerGC(fakeDocker, "")
MinAge: MinAge,
MaxPerPodContainer: MaxPerPodContainer,
MaxContainers: MaxContainers,
})
require.Nil(t, err)
return gc, fakeDocker return gc, fakeDocker
} }
// Makes a stable time object, lower id is earlier time. // Makes a stable time object, lower id is earlier time.
func makeTime(id int) time.Time { func makeTime(id int) time.Time {
var zero time.Time
return zero.Add(time.Duration(id) * time.Second) return zero.Add(time.Duration(id) * time.Second)
} }
@ -90,7 +84,7 @@ func verifyStringArrayEqualsAnyOrder(t *testing.T, actual, expected []string) {
} }
func TestGarbageCollectZeroMaxContainers(t *testing.T) { func TestGarbageCollectZeroMaxContainers(t *testing.T) {
gc, fakeDocker := newTestContainerGC(t, time.Minute, 1, 0) gc, fakeDocker := newTestContainerGC(t)
fakeDocker.ContainerList = []docker.APIContainers{ fakeDocker.ContainerList = []docker.APIContainers{
makeAPIContainer("foo", "POD", "1876"), makeAPIContainer("foo", "POD", "1876"),
} }
@ -98,12 +92,12 @@ func TestGarbageCollectZeroMaxContainers(t *testing.T) {
makeContainerDetail("1876", false, makeTime(0)), makeContainerDetail("1876", false, makeTime(0)),
) )
assert.Nil(t, gc.GarbageCollect()) assert.Nil(t, gc.GarbageCollect(1, 0, time.Minute))
assert.Len(t, fakeDocker.Removed, 1) assert.Len(t, fakeDocker.Removed, 1)
} }
func TestGarbageCollectNoMaxPerPodContainerLimit(t *testing.T) { func TestGarbageCollectNoMaxPerPodContainerLimit(t *testing.T) {
gc, fakeDocker := newTestContainerGC(t, time.Minute, -1, 4) gc, fakeDocker := newTestContainerGC(t)
fakeDocker.ContainerList = []docker.APIContainers{ fakeDocker.ContainerList = []docker.APIContainers{
makeAPIContainer("foo", "POD", "1876"), makeAPIContainer("foo", "POD", "1876"),
makeAPIContainer("foo1", "POD", "2876"), makeAPIContainer("foo1", "POD", "2876"),
@ -119,12 +113,12 @@ func TestGarbageCollectNoMaxPerPodContainerLimit(t *testing.T) {
makeContainerDetail("5876", false, makeTime(4)), makeContainerDetail("5876", false, makeTime(4)),
) )
assert.Nil(t, gc.GarbageCollect()) assert.Nil(t, gc.GarbageCollect(-1, 4, time.Minute))
assert.Len(t, fakeDocker.Removed, 1) assert.Len(t, fakeDocker.Removed, 1)
} }
func TestGarbageCollectNoMaxLimit(t *testing.T) { func TestGarbageCollectNoMaxLimit(t *testing.T) {
gc, fakeDocker := newTestContainerGC(t, time.Minute, 1, -1) gc, fakeDocker := newTestContainerGC(t)
fakeDocker.ContainerList = []docker.APIContainers{ fakeDocker.ContainerList = []docker.APIContainers{
makeAPIContainer("foo", "POD", "1876"), makeAPIContainer("foo", "POD", "1876"),
makeAPIContainer("foo1", "POD", "2876"), makeAPIContainer("foo1", "POD", "2876"),
@ -140,7 +134,7 @@ func TestGarbageCollectNoMaxLimit(t *testing.T) {
makeContainerDetail("5876", false, makeTime(0)), makeContainerDetail("5876", false, makeTime(0)),
) )
assert.Nil(t, gc.GarbageCollect()) assert.Nil(t, gc.GarbageCollect(1, -1, time.Minute))
assert.Len(t, fakeDocker.Removed, 0) assert.Len(t, fakeDocker.Removed, 0)
} }
@ -309,10 +303,10 @@ func TestGarbageCollect(t *testing.T) {
} }
for i, test := range tests { for i, test := range tests {
t.Logf("Running test case with index %d", i) t.Logf("Running test case with index %d", i)
gc, fakeDocker := newTestContainerGC(t, time.Hour, 2, 6) gc, fakeDocker := newTestContainerGC(t)
fakeDocker.ContainerList = test.containers fakeDocker.ContainerList = test.containers
fakeDocker.ContainerMap = test.containerDetails fakeDocker.ContainerMap = test.containerDetails
assert.Nil(t, gc.GarbageCollect()) assert.Nil(t, gc.GarbageCollect(2, 6, time.Hour))
verifyStringArrayEqualsAnyOrder(t, fakeDocker.Removed, test.expectedRemoved) verifyStringArrayEqualsAnyOrder(t, fakeDocker.Removed, test.expectedRemoved)
} }
} }