浏览代码

Merge pull request #32341 from ripcurld0/tt_rm_ut

Use a test table in the daemon delete unit tests
Vincent Demeester 8 年之前
父节点
当前提交
a3a109d956
共有 1 个文件被更改,包括 44 次插入56 次删除
  1. 44 56
      daemon/delete_test.go

+ 44 - 56
daemon/delete_test.go

@@ -24,77 +24,65 @@ func newDaemonWithTmpRoot(t *testing.T) (*Daemon, func()) {
 	return d, func() { os.RemoveAll(tmp) }
 }
 
-// TestContainerDeletePaused tests that a useful error message and instructions is given when attempting
-// to remove a paused container (#30842)
-func TestContainerDeletePaused(t *testing.T) {
-	c := &container.Container{
+func newContainerWithState(state *container.State) *container.Container {
+	return &container.Container{
 		CommonContainer: container.CommonContainer{
 			ID:     "test",
-			State:  &container.State{Paused: true, Running: true},
+			State:  state,
 			Config: &containertypes.Config{},
 		},
 	}
 
-	d, cleanup := newDaemonWithTmpRoot(t)
-	defer cleanup()
-	d.containers.Add(c.ID, c)
-
-	err := d.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: false})
-
-	testutil.ErrorContains(t, err, "cannot remove a paused container")
-	testutil.ErrorContains(t, err, "Unpause and then stop the container before attempting removal or force remove")
 }
 
-// TestContainerDeleteRestarting tests that a useful error message and instructions is given when attempting
-// to remove a container that is restarting (#30842)
-func TestContainerDeleteRestarting(t *testing.T) {
-	c := &container.Container{
-		CommonContainer: container.CommonContainer{
-			ID:     "test",
-			State:  container.NewState(),
-			Config: &containertypes.Config{},
-		},
+// TestContainerDelete tests that a useful error message and instructions is
+// given when attempting to remove a container (#30842)
+func TestContainerDelete(t *testing.T) {
+	tt := []struct {
+		errMsg        string
+		fixMsg        string
+		initContainer func() *container.Container
+	}{
+		// a paused container
+		{
+			errMsg: "cannot remove a paused container",
+			fixMsg: "Unpause and then stop the container before attempting removal or force remove",
+			initContainer: func() *container.Container {
+				return newContainerWithState(&container.State{Paused: true, Running: true})
+			}},
+		// a restarting container
+		{
+			errMsg: "cannot remove a restarting container",
+			fixMsg: "Stop the container before attempting removal or force remove",
+			initContainer: func() *container.Container {
+				c := newContainerWithState(container.NewState())
+				c.SetRunning(0, true)
+				c.SetRestarting(&container.ExitStatus{})
+				return c
+			}},
+		// a running container
+		{
+			errMsg: "cannot remove a running container",
+			fixMsg: "Stop the container before attempting removal or force remove",
+			initContainer: func() *container.Container {
+				return newContainerWithState(&container.State{Running: true})
+			}},
 	}
 
-	c.SetRunning(0, true)
-	c.SetRestarting(&container.ExitStatus{})
-
-	d, cleanup := newDaemonWithTmpRoot(t)
-	defer cleanup()
-	d.containers.Add(c.ID, c)
-
-	err := d.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: false})
-	testutil.ErrorContains(t, err, "cannot remove a restarting container")
-	testutil.ErrorContains(t, err, "Stop the container before attempting removal or force remove")
-}
+	for _, te := range tt {
+		c := te.initContainer()
+		d, cleanup := newDaemonWithTmpRoot(t)
+		defer cleanup()
+		d.containers.Add(c.ID, c)
 
-// TestContainerDeleteRunning tests that a useful error message and instructions is given when attempting
-// to remove a running container (#30842)
-func TestContainerDeleteRunning(t *testing.T) {
-	c := &container.Container{
-		CommonContainer: container.CommonContainer{
-			ID:     "test",
-			State:  &container.State{Running: true},
-			Config: &containertypes.Config{},
-		},
+		err := d.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: false})
+		testutil.ErrorContains(t, err, te.errMsg)
+		testutil.ErrorContains(t, err, te.fixMsg)
 	}
-
-	d, cleanup := newDaemonWithTmpRoot(t)
-	defer cleanup()
-	d.containers.Add(c.ID, c)
-
-	err := d.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: false})
-	testutil.ErrorContains(t, err, "cannot remove a running container")
 }
 
 func TestContainerDoubleDelete(t *testing.T) {
-	c := &container.Container{
-		CommonContainer: container.CommonContainer{
-			ID:     "test",
-			State:  container.NewState(),
-			Config: &containertypes.Config{},
-		},
-	}
+	c := newContainerWithState(container.NewState())
 
 	// Mark the container as having a delete in progress
 	c.SetRemovalInProgress()