diff options
author | Matthew Heon <matthew.heon@gmail.com> | 2018-02-07 15:05:31 -0500 |
---|---|---|
committer | Atomic Bot <atomic-devel@projectatomic.io> | 2018-02-09 15:01:34 +0000 |
commit | 19840e0b3d3f3e4389152c8e96cfe92f68d024eb (patch) | |
tree | 5a1579abf0002881679acc12e5a79c17149830b5 /libpod | |
parent | cb28a1d284c8e9ec3539a11e472590686ccbd5f8 (diff) | |
download | podman-19840e0b3d3f3e4389152c8e96cfe92f68d024eb.tar.gz podman-19840e0b3d3f3e4389152c8e96cfe92f68d024eb.tar.bz2 podman-19840e0b3d3f3e4389152c8e96cfe92f68d024eb.zip |
Finish unit tests for pods
Signed-off-by: Matthew Heon <matthew.heon@gmail.com>
Closes: #268
Approved by: rhatdan
Diffstat (limited to 'libpod')
-rw-r--r-- | libpod/state_test.go | 1435 |
1 files changed, 1435 insertions, 0 deletions
diff --git a/libpod/state_test.go b/libpod/state_test.go index 6c4ee30af..f18393e23 100644 --- a/libpod/state_test.go +++ b/libpod/state_test.go @@ -197,6 +197,10 @@ func TestAddDuplicateCtrIDFails(t *testing.T) { err = state.AddContainer(testCtr2) assert.Error(t, err) + + ctrs, err := state.AllContainers() + assert.NoError(t, err) + assert.Equal(t, 1, len(ctrs)) }) } @@ -212,6 +216,10 @@ func TestAddDuplicateCtrNameFails(t *testing.T) { err = state.AddContainer(testCtr2) assert.Error(t, err) + + ctrs, err := state.AllContainers() + assert.NoError(t, err) + assert.Equal(t, 1, len(ctrs)) }) } @@ -227,6 +235,10 @@ func TestAddCtrPodDupIDFails(t *testing.T) { err = state.AddContainer(testCtr) assert.Error(t, err) + + ctrs, err := state.AllContainers() + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs)) }) } @@ -242,6 +254,32 @@ func TestAddCtrPodDupNameFails(t *testing.T) { err = state.AddContainer(testCtr) assert.Error(t, err) + + ctrs, err := state.AllContainers() + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs)) + }) +} + +func TestAddCtrInPodFails(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr, err := getTestCtr2(lockPath) + assert.NoError(t, err) + + testCtr.config.Pod = testPod.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainer(testCtr) + assert.Error(t, err) + + ctrs, err := state.AllContainers() + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs)) }) } @@ -510,6 +548,7 @@ func TestRemoveNonexistantContainerFails(t *testing.T) { err = state.RemoveContainer(testCtr) assert.Error(t, err) + assert.False(t, testCtr.valid) }) } @@ -656,6 +695,10 @@ func TestCannotRemoveContainerWithDependency(t *testing.T) { err = state.RemoveContainer(testCtr1) assert.Error(t, err) + + ctrs, err := state.AllContainers() + assert.NoError(t, err) + assert.Equal(t, 2, len(ctrs)) }) } @@ -679,6 +722,10 @@ func TestCanRemoveContainerAfterDependencyRemoved(t *testing.T) { err = state.RemoveContainer(testCtr1) assert.NoError(t, err) + + ctrs, err := state.AllContainers() + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs)) }) } @@ -703,6 +750,48 @@ func TestCanRemoveContainerAfterDependencyRemovedDuplicate(t *testing.T) { err = state.RemoveContainer(testCtr1) assert.NoError(t, err) + + ctrs, err := state.AllContainers() + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs)) + }) +} + +func TestCannotUsePodAsDependency(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testCtr, err := getTestCtr1(lockPath) + assert.NoError(t, err) + + testPod, err := getTestPod2(lockPath) + assert.NoError(t, err) + + testCtr.config.UserNsCtr = testPod.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainer(testCtr) + assert.Error(t, err) + + ctrs, err := state.AllContainers() + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs)) + }) +} + +func TestCannotUseBadIDAsDependency(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testCtr, err := getTestCtr1(lockPath) + assert.NoError(t, err) + + testCtr.config.UserNsCtr = strings.Repeat("5", 32) + + err = state.AddContainer(testCtr) + assert.Error(t, err) + + ctrs, err := state.AllContainers() + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs)) }) } @@ -911,3 +1000,1349 @@ func TestLookupPodByCtrName(t *testing.T) { assert.Error(t, err) }) } + +func TestHasPodEmptyIDErrors(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + _, err := state.HasPod("") + assert.Error(t, err) + }) +} + +func TestHasPodNoSuchPod(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + exist, err := state.HasPod("notexist") + assert.NoError(t, err) + assert.False(t, exist) + }) +} + +func TestHasPodWrongIDFalse(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + err = state.AddPod(testPod) + assert.NoError(t, err) + + exist, err := state.HasPod(strings.Repeat("a", 32)) + assert.NoError(t, err) + assert.False(t, exist) + }) +} + +func TestHasPodRightIDTrue(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + err = state.AddPod(testPod) + assert.NoError(t, err) + + exist, err := state.HasPod(testPod.ID()) + assert.NoError(t, err) + assert.True(t, exist) + }) +} + +func TestHasPodCtrIDFalse(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testCtr, err := getTestCtr1(lockPath) + assert.NoError(t, err) + + err = state.AddContainer(testCtr) + assert.NoError(t, err) + + exist, err := state.HasPod(testCtr.ID()) + assert.NoError(t, err) + assert.False(t, exist) + }) +} + +func TestAddPodInvalidPodErrors(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + err := state.AddPod(&Pod{}) + assert.Error(t, err) + }) +} + +func TestAddPodValidPodSucceeds(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + err = state.AddPod(testPod) + assert.NoError(t, err) + + allPods, err := state.AllPods() + assert.NoError(t, err) + assert.Equal(t, 1, len(allPods)) + + // Use assert.EqualValues if the test fails to pretty print diff + // between actual and expected + if !testPodsEqual(testPod, allPods[0]) { + assert.EqualValues(t, testPod, allPods[0]) + } + }) +} + +func TestAddPodDuplicateIDFails(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod1, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testPod2, err := getTestPod(testPod1.ID(), "testpod2", lockPath) + assert.NoError(t, err) + + err = state.AddPod(testPod1) + assert.NoError(t, err) + + err = state.AddPod(testPod2) + assert.Error(t, err) + + allPods, err := state.AllPods() + assert.NoError(t, err) + assert.Equal(t, 1, len(allPods)) + }) +} + +func TestAddPodDuplicateNameFails(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod1, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testPod2, err := getTestPod(strings.Repeat("2", 32), testPod1.Name(), lockPath) + assert.NoError(t, err) + + err = state.AddPod(testPod1) + assert.NoError(t, err) + + err = state.AddPod(testPod2) + assert.Error(t, err) + + + allPods, err := state.AllPods() + assert.NoError(t, err) + assert.Equal(t, 1, len(allPods)) + }) +} + +func TestAddPodNonDuplicateSucceeds(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod1, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testPod2, err := getTestPod2(lockPath) + assert.NoError(t, err) + + err = state.AddPod(testPod1) + assert.NoError(t, err) + + err = state.AddPod(testPod2) + assert.NoError(t, err) + + allPods, err := state.AllPods() + assert.NoError(t, err) + assert.Equal(t, 2, len(allPods)) + }) +} + +func TestAddPodCtrIDConflictFails(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testCtr, err := getTestCtr1(lockPath) + assert.NoError(t, err) + + testPod, err := getTestPod(testCtr.ID(), "testpod1", lockPath) + assert.NoError(t, err) + + err = state.AddContainer(testCtr) + assert.NoError(t, err) + + err = state.AddPod(testPod) + assert.Error(t, err) + + + allPods, err := state.AllPods() + assert.NoError(t, err) + assert.Equal(t, 0, len(allPods)) + }) +} + +func TestAddPodCtrNameConflictFails(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testCtr, err := getTestCtr1(lockPath) + assert.NoError(t, err) + + testPod, err := getTestPod(strings.Repeat("3", 32), testCtr.Name(), lockPath) + assert.NoError(t, err) + + err = state.AddContainer(testCtr) + assert.NoError(t, err) + + err = state.AddPod(testPod) + assert.Error(t, err) + + + allPods, err := state.AllPods() + assert.NoError(t, err) + assert.Equal(t, 0, len(allPods)) + }) +} + +func TestRemovePodInvalidPodErrors(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + err := state.RemovePod(&Pod{}) + assert.Error(t, err) + }) +} + +func TestRemovePodNotInStateFails(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + err = state.RemovePod(testPod) + assert.Error(t, err) + assert.False(t, testPod.valid) + }) +} + +func TestRemovePodSucceeds(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.RemovePod(testPod) + assert.NoError(t, err) + + allPods, err := state.AllPods() + assert.NoError(t, err) + assert.Equal(t, 0, len(allPods)) + }) +} + +func TestRemovePodFromPods(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod1, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testPod2, err := getTestPod2(lockPath) + assert.NoError(t, err) + + err = state.AddPod(testPod1) + assert.NoError(t, err) + + err = state.AddPod(testPod2) + assert.NoError(t, err) + + err = state.RemovePod(testPod1) + assert.NoError(t, err) + + allPods, err := state.AllPods() + assert.NoError(t, err) + assert.Equal(t, 1, len(allPods)) + + // Use assert.EqualValues if the test fails to pretty print diff + // between actual and expected + if !testPodsEqual(testPod2, allPods[0]) { + assert.EqualValues(t, testPod2, allPods[0]) + } + }) +} + +func TestRemovePodNotEmptyFails(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr, err := getTestCtr2(lockPath) + assert.NoError(t, err) + testCtr.config.Pod = testPod.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr) + assert.NoError(t, err) + + err = state.RemovePod(testPod) + assert.Error(t, err) + + + allPods, err := state.AllPods() + assert.NoError(t, err) + assert.Equal(t, 1, len(allPods)) + }) +} + +func TestRemovePodAfterEmptySucceeds(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr, err := getTestCtr2(lockPath) + assert.NoError(t, err) + testCtr.config.Pod = testPod.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr) + assert.NoError(t, err) + + err = state.RemoveContainerFromPod(testPod, testCtr) + assert.NoError(t, err) + + err = state.RemovePod(testPod) + assert.NoError(t, err) + + allPods, err := state.AllPods() + assert.NoError(t, err) + assert.Equal(t, 0, len(allPods)) + }) +} + +func TestAllPodsEmptyOnEmptyState(t *testing.T) { + runForAllStates(t , func(t *testing.T, state State, lockPath string) { + allPods, err := state.AllPods() + assert.NoError(t, err) + assert.Equal(t, 0, len(allPods)) + }) +} + +func TestAllPodsFindsPod(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + err = state.AddPod(testPod) + assert.NoError(t, err) + + allPods, err := state.AllPods() + assert.NoError(t, err) + assert.Equal(t, 1, len(allPods)) + + // Use assert.EqualValues if the test fails to pretty print diff + // between actual and expected + if !testPodsEqual(testPod, allPods[0]) { + assert.EqualValues(t, testPod, allPods[0]) + } + }) +} + +func TestAllPodsMultiplePods(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod1, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testPod2, err := getTestPod2(lockPath) + assert.NoError(t, err) + + testPod3, err := getTestPodN("3", lockPath) + assert.NoError(t, err) + + allPods1, err := state.AllPods() + assert.NoError(t, err) + assert.Equal(t, 0, len(allPods1)) + + err = state.AddPod(testPod1) + assert.NoError(t, err) + + allPods2, err := state.AllPods() + assert.NoError(t, err) + assert.Equal(t, 1, len(allPods2)) + + err = state.AddPod(testPod2) + assert.NoError(t, err) + + allPods3, err := state.AllPods() + assert.NoError(t, err) + assert.Equal(t, 2, len(allPods3)) + + err = state.AddPod(testPod3) + assert.NoError(t, err) + + allPods4, err := state.AllPods() + assert.NoError(t, err) + assert.Equal(t, 3, len(allPods4)) + }) +} + +func TestPodHasContainerNoSuchPod(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + _, err := state.PodHasContainer(&Pod{}, strings.Repeat("0", 32)) + assert.Error(t, err) + }) +} + +func TestPodHasContainerEmptyCtrID(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + err = state.AddPod(testPod) + assert.NoError(t, err) + + _, err = state.PodHasContainer(testPod, "") + assert.Error(t, err) + }) +} + +func TestPodHasContainerNoSuchCtr(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + err = state.AddPod(testPod) + assert.NoError(t, err) + + exist, err := state.PodHasContainer(testPod, strings.Repeat("2", 32)) + assert.NoError(t, err) + assert.False(t, exist) + }) +} + +func TestPodHasContainerCtrNotInPod(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr, err := getTestCtr2(lockPath) + assert.NoError(t, err) + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainer(testCtr) + assert.NoError(t, err) + + exist, err := state.PodHasContainer(testPod, testCtr.ID()) + assert.NoError(t, err) + assert.False(t, exist) + }) +} + +func TestPodHasContainerSucceeds(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr, err := getTestCtr2(lockPath) + assert.NoError(t, err) + + testCtr.config.Pod = testPod.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr) + assert.NoError(t, err) + + exist, err := state.PodHasContainer(testPod, testCtr.ID()) + assert.NoError(t, err) + assert.True(t, exist) + }) +} + +func TestPodContainersByIDInvalidPod(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + _, err := state.PodContainersByID(&Pod{}) + assert.Error(t, err) + }) +} + +func TestPodContainerdByIDPodNotInState(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + _, err = state.PodContainersByID(testPod) + assert.Error(t, err) + assert.False(t, testPod.valid) + }) +} + +func TestPodContainersByIDEmptyPod(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + err = state.AddPod(testPod) + assert.NoError(t, err) + + ctrs, err := state.PodContainersByID(testPod) + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs)) + }) +} + +func TestPodContainersByIDOneContainer(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr, err := getTestCtr2(lockPath) + assert.NoError(t, err) + + testCtr.config.Pod = testPod.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr) + assert.NoError(t, err) + + ctrs, err := state.PodContainersByID(testPod) + assert.NoError(t, err) + assert.Equal(t, 1, len(ctrs)) + assert.Equal(t, testCtr.ID(), ctrs[0]) + }) +} + +func TestPodContainersByIDMultipleContainers(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr1, err := getTestCtr2(lockPath) + assert.NoError(t, err) + testCtr1.config.Pod = testPod.ID() + + testCtr2, err := getTestCtrN("3", lockPath) + assert.NoError(t, err) + testCtr2.config.Pod = testPod.ID() + + testCtr3, err := getTestCtrN("4", lockPath) + assert.NoError(t, err) + testCtr3.config.Pod = testPod.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + ctrs0, err := state.PodContainersByID(testPod) + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs0)) + + err = state.AddContainerToPod(testPod, testCtr1) + assert.NoError(t, err) + + ctrs1, err := state.PodContainersByID(testPod) + assert.NoError(t, err) + assert.Equal(t, 1, len(ctrs1)) + + err = state.AddContainerToPod(testPod, testCtr2) + assert.NoError(t, err) + + ctrs2, err := state.PodContainersByID(testPod) + assert.NoError(t, err) + assert.Equal(t, 2, len(ctrs2)) + + err = state.AddContainerToPod(testPod, testCtr3) + assert.NoError(t, err) + + ctrs3, err := state.PodContainersByID(testPod) + assert.NoError(t, err) + assert.Equal(t, 3, len(ctrs3)) + }) +} + +func TestPodContainersInvalidPod(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + _, err := state.PodContainers(&Pod{}) + assert.Error(t, err) + }) +} + +func TestPodContainerdPodNotInState(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + _, err = state.PodContainers(testPod) + assert.Error(t, err) + assert.False(t, testPod.valid) + }) +} + +func TestPodContainersEmptyPod(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + err = state.AddPod(testPod) + assert.NoError(t, err) + + ctrs, err := state.PodContainers(testPod) + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs)) + }) +} + +func TestPodContainersOneContainer(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr, err := getTestCtr2(lockPath) + assert.NoError(t, err) + + testCtr.config.Pod = testPod.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr) + assert.NoError(t, err) + + ctrs, err := state.PodContainers(testPod) + assert.NoError(t, err) + assert.Equal(t, 1, len(ctrs)) + + // Use assert.EqualValues if the test fails to pretty print diff + // between actual and expected + if !testContainersEqual(testCtr, ctrs[0]) { + assert.EqualValues(t, testCtr, ctrs[0]) + } + }) +} + +func TestPodContainersMultipleContainers(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr1, err := getTestCtr2(lockPath) + assert.NoError(t, err) + testCtr1.config.Pod = testPod.ID() + + testCtr2, err := getTestCtrN("3", lockPath) + assert.NoError(t, err) + testCtr2.config.Pod = testPod.ID() + + testCtr3, err := getTestCtrN("4", lockPath) + assert.NoError(t, err) + testCtr3.config.Pod = testPod.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + ctrs0, err := state.PodContainers(testPod) + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs0)) + + err = state.AddContainerToPod(testPod, testCtr1) + assert.NoError(t, err) + + ctrs1, err := state.PodContainers(testPod) + assert.NoError(t, err) + assert.Equal(t, 1, len(ctrs1)) + + err = state.AddContainerToPod(testPod, testCtr2) + assert.NoError(t, err) + + ctrs2, err := state.PodContainers(testPod) + assert.NoError(t, err) + assert.Equal(t, 2, len(ctrs2)) + + err = state.AddContainerToPod(testPod, testCtr3) + assert.NoError(t, err) + + ctrs3, err := state.PodContainers(testPod) + assert.NoError(t, err) + assert.Equal(t, 3, len(ctrs3)) + }) +} + +func TestRemovePodContainersInvalidPod(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + err := state.RemovePodContainers(&Pod{}) + assert.Error(t, err) + }) +} + +func TestRemovePodContainersPodNotInState(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + err = state.RemovePodContainers(testPod) + assert.Error(t, err) + assert.False(t, testPod.valid) + }) +} + +func TestRemovePodContainersNoContainers(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.RemovePodContainers(testPod) + assert.NoError(t, err) + + ctrs, err := state.PodContainersByID(testPod) + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs)) + }) +} + +func TestRemovePodContainersOneContainer(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr, err := getTestCtr2(lockPath) + assert.NoError(t, err) + testCtr.config.Pod = testPod.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr) + assert.NoError(t, err) + + err = state.RemovePodContainers(testPod) + assert.NoError(t, err) + + ctrs, err := state.PodContainersByID(testPod) + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs)) + }) +} + +func TestRemovePodContainersPreservesCtrOutsidePod(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr1, err := getTestCtr2(lockPath) + assert.NoError(t, err) + testCtr1.config.Pod = testPod.ID() + + testCtr2, err := getTestCtrN("3", lockPath) + assert.NoError(t, err) + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr1) + assert.NoError(t, err) + + err = state.AddContainer(testCtr2) + assert.NoError(t, err) + + err = state.RemovePodContainers(testPod) + assert.NoError(t, err) + + ctrs, err := state.PodContainersByID(testPod) + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs)) + + allCtrs, err := state.AllContainers() + assert.NoError(t, err) + assert.Equal(t, 1, len(allCtrs)) + }) +} + +func TestRemovePodContainersTwoContainers(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr1, err := getTestCtr2(lockPath) + assert.NoError(t, err) + testCtr1.config.Pod = testPod.ID() + + testCtr2, err := getTestCtrN("3", lockPath) + assert.NoError(t, err) + testCtr2.config.Pod = testPod.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr1) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr2) + assert.NoError(t, err) + + err = state.RemovePodContainers(testPod) + assert.NoError(t, err) + + ctrs, err := state.PodContainersByID(testPod) + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs)) + }) +} + +func TestRemovePodContainerDependencyInPod(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr1, err := getTestCtr2(lockPath) + assert.NoError(t, err) + testCtr1.config.Pod = testPod.ID() + + testCtr2, err := getTestCtrN("3", lockPath) + assert.NoError(t, err) + testCtr2.config.Pod = testPod.ID() + testCtr2.config.IPCNsCtr = testCtr1.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr1) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr2) + assert.NoError(t, err) + + err = state.RemovePodContainers(testPod) + assert.NoError(t, err) + + ctrs, err := state.PodContainersByID(testPod) + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs)) + }) +} + +func TestRemovePodContainerDependencyNotInPod(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr1, err := getTestCtr2(lockPath) + assert.NoError(t, err) + testCtr1.config.Pod = testPod.ID() + + testCtr2, err := getTestCtrN("3", lockPath) + assert.NoError(t, err) + testCtr2.config.IPCNsCtr = testCtr1.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr1) + assert.NoError(t, err) + + err = state.AddContainer(testCtr2) + assert.NoError(t, err) + + err = state.RemovePodContainers(testPod) + t.Logf("Err %v", err) + assert.Error(t, err) + + ctrs, err := state.PodContainersByID(testPod) + assert.NoError(t, err) + assert.Equal(t, 1, len(ctrs)) + }) +} + +func TestAddContainerToPodInvalidPod(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testCtr, err := getTestCtr1(lockPath) + assert.NoError(t, err) + + err = state.AddContainerToPod(&Pod{}, testCtr) + assert.Error(t, err) + }) +} + +func TestAddContainerToPodInvalidCtr(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, &Container{config: &ContainerConfig{ID: "1234"}}) + assert.Error(t, err) + + ctrs, err := state.PodContainersByID(testPod) + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs)) + }) +} + +func TestAddContainerToPodPodNotInState(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr, err := getTestCtr2(lockPath) + assert.NoError(t, err) + testCtr.config.Pod = testPod.ID() + + err = state.AddContainerToPod(testPod, testCtr) + assert.Error(t, err) + assert.False(t, testPod.valid) + }) +} + +func TestAddContainerToPodSucceeds(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr, err := getTestCtr2(lockPath) + assert.NoError(t, err) + testCtr.config.Pod = testPod.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr) + assert.NoError(t, err) + + ctrs, err := state.PodContainers(testPod) + assert.NoError(t, err) + assert.Equal(t, 1, len(ctrs)) + + allCtrs, err := state.AllContainers() + assert.NoError(t, err) + assert.Equal(t, 1, len(allCtrs)) + + // Use assert.EqualValues if the test fails to pretty print diff + // between actual and expected + if !testContainersEqual(testCtr, ctrs[0]) { + assert.EqualValues(t, testCtr, ctrs[0]) + } + if !testContainersEqual(allCtrs[0], ctrs[0]) { + assert.EqualValues(t, allCtrs[0], ctrs[0]) + } + }) +} + +func TestAddContainerToPodTwoContainers(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr1, err := getTestCtr2(lockPath) + assert.NoError(t, err) + testCtr1.config.Pod = testPod.ID() + + testCtr2, err := getTestCtrN("3", lockPath) + assert.NoError(t, err) + testCtr2.config.Pod = testPod.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr1) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr2) + assert.NoError(t, err) + + ctrs, err := state.PodContainers(testPod) + assert.NoError(t, err) + assert.Equal(t, 2, len(ctrs)) + + allCtrs, err := state.AllContainers() + assert.NoError(t, err) + assert.Equal(t, 2, len(allCtrs)) + }) +} + +func TestAddContainerToPodWithAddContainer(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr1, err := getTestCtr2(lockPath) + assert.NoError(t, err) + testCtr1.config.Pod = testPod.ID() + + testCtr2, err := getTestCtrN("3", lockPath) + assert.NoError(t, err) + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr1) + assert.NoError(t, err) + + err = state.AddContainer(testCtr2) + assert.NoError(t, err) + + ctrs, err := state.PodContainers(testPod) + assert.NoError(t, err) + assert.Equal(t, 1, len(ctrs)) + + allCtrs, err := state.AllContainers() + assert.NoError(t, err) + assert.Equal(t, 2, len(allCtrs)) + + // Use assert.EqualValues if the test fails to pretty print diff + // between actual and expected + if !testContainersEqual(testCtr1, ctrs[0]) { + assert.EqualValues(t, testCtr1, ctrs[0]) + } + }) +} + +func TestAddContainerToPodCtrIDConflict(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr1, err := getTestCtr2(lockPath) + assert.NoError(t, err) + + testCtr2, err := getTestContainer(testCtr1.ID(), "testCtr3", lockPath) + assert.NoError(t, err) + testCtr2.config.Pod = testPod.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainer(testCtr1) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr2) + assert.Error(t, err) + + ctrs, err := state.PodContainers(testPod) + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs)) + + allCtrs, err := state.AllContainers() + assert.NoError(t, err) + assert.Equal(t, 1, len(allCtrs)) + }) +} + +func TestAddContainerToPodCtrNameConflict(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr1, err := getTestCtr2(lockPath) + assert.NoError(t, err) + + testCtr2, err := getTestContainer(strings.Repeat("4", 32), testCtr1.Name(), lockPath) + assert.NoError(t, err) + testCtr2.config.Pod = testPod.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainer(testCtr1) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr2) + assert.Error(t, err) + + ctrs, err := state.PodContainers(testPod) + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs)) + + allCtrs, err := state.AllContainers() + assert.NoError(t, err) + assert.Equal(t, 1, len(allCtrs)) + }) +} + +func TestAddContainerToPodPodIDConflict(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr, err := getTestContainer(testPod.ID(), "testCtr", lockPath) + assert.NoError(t, err) + testCtr.config.Pod = testPod.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr) + assert.Error(t, err) + + ctrs, err := state.PodContainers(testPod) + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs)) + + allCtrs, err := state.AllContainers() + assert.NoError(t, err) + assert.Equal(t, 0, len(allCtrs)) + }) +} + +func TestAddContainerToPodPodNameConflict(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr, err := getTestContainer(strings.Repeat("2", 32), testPod.Name(), lockPath) + assert.NoError(t, err) + testCtr.config.Pod = testPod.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr) + assert.Error(t, err) + + ctrs, err := state.PodContainers(testPod) + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs)) + + allCtrs, err := state.AllContainers() + assert.NoError(t, err) + assert.Equal(t, 0, len(allCtrs)) + }) +} + +func TestAddContainerToPodAddsDependencies(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr1, err := getTestCtr2(lockPath) + assert.NoError(t, err) + testCtr1.config.Pod = testPod.ID() + + testCtr2, err := getTestCtrN("3", lockPath) + assert.NoError(t, err) + testCtr2.config.Pod = testPod.ID() + testCtr2.config.IPCNsCtr = testCtr1.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr1) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr2) + assert.NoError(t, err) + + deps, err := state.ContainerInUse(testCtr1) + assert.NoError(t, err) + assert.Equal(t, 1, len(deps)) + assert.Equal(t, testCtr2.ID(), deps[0]) + }) +} + +func TestAddContainerToPodPodDependencyFails(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr, err := getTestCtr2(lockPath) + assert.NoError(t, err) + testCtr.config.Pod = testPod.ID() + testCtr.config.IPCNsCtr = testPod.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr) + assert.Error(t, err) + + ctrs, err := state.PodContainers(testPod) + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs)) + }) +} + +func TestAddContainerToPodBadDependencyFails(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr, err := getTestCtr2(lockPath) + assert.NoError(t, err) + testCtr.config.Pod = testPod.ID() + testCtr.config.IPCNsCtr = strings.Repeat("8", 32) + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr) + assert.Error(t, err) + + ctrs, err := state.PodContainers(testPod) + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs)) + }) +} + +func TestRemoveContainerFromPodBadPodFails(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testCtr, err := getTestCtr1(lockPath) + assert.NoError(t, err) + + err = state.RemoveContainerFromPod(&Pod{}, testCtr) + assert.Error(t, err) + }) +} + +func TestRemoveContainerFromPodPodNotInStateFails(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr, err := getTestCtr2(lockPath) + assert.NoError(t, err) + testCtr.config.Pod = testPod.ID() + + err = state.RemoveContainerFromPod(testPod, testCtr) + assert.Error(t, err) + + assert.False(t, testPod.valid) + }) +} + +func TestRemoveContainerFromPodCtrNotInStateFails(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr, err := getTestCtr2(lockPath) + assert.NoError(t, err) + testCtr.config.Pod = testPod.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.RemoveContainerFromPod(testPod, testCtr) + assert.Error(t, err) + + assert.False(t, testCtr.valid) + }) +} + +func TestRemoveContainerFromPodCtrNotInPodFails(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr, err := getTestCtr2(lockPath) + assert.NoError(t, err) + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainer(testCtr) + assert.NoError(t, err) + + err = state.RemoveContainerFromPod(testPod, testCtr) + assert.Error(t, err) + + assert.True(t, testCtr.valid) + + ctrs, err := state.AllContainers() + assert.NoError(t, err) + assert.Equal(t, 1, len(ctrs)) + }) +} + +func TestRemoveContainerFromPodSucceeds(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr, err := getTestCtr2(lockPath) + assert.NoError(t, err) + testCtr.config.Pod = testPod.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr) + assert.NoError(t, err) + + err = state.RemoveContainerFromPod(testPod, testCtr) + assert.NoError(t, err) + + ctrs, err := state.PodContainers(testPod) + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs)) + + allCtrs, err := state.AllContainers() + assert.NoError(t, err) + assert.Equal(t, 0, len(allCtrs)) + }) +} + +func TestRemoveContainerFromPodWithDependencyFails(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr1, err := getTestCtr2(lockPath) + assert.NoError(t, err) + testCtr1.config.Pod = testPod.ID() + + testCtr2, err := getTestCtrN("3", lockPath) + assert.NoError(t, err) + testCtr2.config.Pod = testPod.ID() + testCtr2.config.IPCNsCtr = testCtr1.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr1) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr2) + assert.NoError(t, err) + + err = state.RemoveContainerFromPod(testPod, testCtr1) + assert.Error(t, err) + + ctrs, err := state.PodContainers(testPod) + assert.NoError(t, err) + assert.Equal(t, 2, len(ctrs)) + + allCtrs, err := state.AllContainers() + assert.NoError(t, err) + assert.Equal(t, 2, len(allCtrs)) + }) +} + +func TestRemoveContainerFromPodWithDependencySucceedsAfterDepRemoved(t *testing.T) { + runForAllStates(t, func(t *testing.T, state State, lockPath string) { + testPod, err := getTestPod1(lockPath) + assert.NoError(t, err) + + testCtr1, err := getTestCtr2(lockPath) + assert.NoError(t, err) + testCtr1.config.Pod = testPod.ID() + + testCtr2, err := getTestCtrN("3", lockPath) + assert.NoError(t, err) + testCtr2.config.Pod = testPod.ID() + testCtr2.config.IPCNsCtr = testCtr1.ID() + + err = state.AddPod(testPod) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr1) + assert.NoError(t, err) + + err = state.AddContainerToPod(testPod, testCtr2) + assert.NoError(t, err) + + err = state.RemoveContainerFromPod(testPod, testCtr2) + assert.NoError(t, err) + + err = state.RemoveContainerFromPod(testPod, testCtr1) + assert.NoError(t, err) + + ctrs, err := state.PodContainers(testPod) + assert.NoError(t, err) + assert.Equal(t, 0, len(ctrs)) + + allCtrs, err := state.AllContainers() + assert.NoError(t, err) + assert.Equal(t, 0, len(allCtrs)) + }) +} |