Ensure pods are part of the set namespace when added

Signed-off-by: Matthew Heon <matthew.heon@gmail.com>
This commit is contained in:
Matthew Heon 2018-07-19 12:04:44 -04:00
parent 7b30659629
commit 84afa32493
2 changed files with 45 additions and 0 deletions

View File

@ -542,6 +542,10 @@ func (s *InMemoryState) AddPod(pod *Pod) error {
return errors.Wrapf(ErrPodRemoved, "pod %s is not valid and cannot be added", pod.ID())
}
if err := s.checkNSMatch(pod.ID(), pod.Namespace()); err != nil {
return err
}
if _, ok := s.pods[pod.ID()]; ok {
return errors.Wrapf(ErrPodExists, "pod with ID %s already exists in state", pod.ID())
}

View File

@ -1790,6 +1790,47 @@ func TestAddPodCtrNameConflictFails(t *testing.T) {
})
}
func TestAddPodSameNamespaceSucceeds(t *testing.T) {
runForAllStates(t, func(t *testing.T, state State, lockPath string) {
testPod, err := getTestPod1(lockPath)
assert.NoError(t, err)
testPod.config.Namespace = "test1"
state.SetNamespace("test1")
err = state.AddPod(testPod)
assert.NoError(t, err)
allPods, err := state.AllPods()
assert.NoError(t, err)
assert.Equal(t, 1, len(allPods))
testPodsEqual(t, testPod, allPods[0])
assert.Equal(t, testPod.valid, allPods[0].valid)
})
}
func TestAddPodDifferentNamespaceFails(t *testing.T) {
runForAllStates(t, func(t *testing.T, state State, lockPath string) {
testPod, err := getTestPod1(lockPath)
assert.NoError(t, err)
testPod.config.Namespace = "test1"
state.SetNamespace("test2")
err = state.AddPod(testPod)
assert.Error(t, err)
state.SetNamespace("")
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{config: &PodConfig{}})