mirror of
https://github.com/moby/moby.git
synced 2025-07-29 07:21:35 +03:00
Merge pull request #5839 from unclejack/improve_build_rm
add --force-rm to clean up after a failed build
This commit is contained in:
@ -264,6 +264,118 @@ func TestBuildWithInaccessibleFilesInContext(t *testing.T) {
|
||||
logDone("build - ADD from context with accessible links must work")
|
||||
}
|
||||
|
||||
func TestBuildForceRm(t *testing.T) {
|
||||
containerCountBefore, err := getContainerCount()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get the container count: %s", err)
|
||||
}
|
||||
|
||||
buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildForceRm")
|
||||
buildCmd := exec.Command(dockerBinary, "build", "--force-rm", ".")
|
||||
buildCmd.Dir = buildDirectory
|
||||
_, exitCode, err := runCommandWithOutput(buildCmd)
|
||||
|
||||
if err == nil || exitCode == 0 {
|
||||
t.Fatal("failed to build the image")
|
||||
}
|
||||
|
||||
containerCountAfter, err := getContainerCount()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get the container count: %s", err)
|
||||
}
|
||||
|
||||
if containerCountBefore != containerCountAfter {
|
||||
t.Fatalf("--force-rm shouldn't have left containers behind")
|
||||
}
|
||||
|
||||
logDone("build - ensure --force-rm doesn't leave containers behind")
|
||||
}
|
||||
|
||||
func TestBuildRm(t *testing.T) {
|
||||
{
|
||||
containerCountBefore, err := getContainerCount()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get the container count: %s", err)
|
||||
}
|
||||
|
||||
buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildRm")
|
||||
buildCmd := exec.Command(dockerBinary, "build", "--rm", "-t", "testbuildrm", ".")
|
||||
buildCmd.Dir = buildDirectory
|
||||
_, exitCode, err := runCommandWithOutput(buildCmd)
|
||||
|
||||
if err != nil || exitCode != 0 {
|
||||
t.Fatal("failed to build the image")
|
||||
}
|
||||
|
||||
containerCountAfter, err := getContainerCount()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get the container count: %s", err)
|
||||
}
|
||||
|
||||
if containerCountBefore != containerCountAfter {
|
||||
t.Fatalf("-rm shouldn't have left containers behind")
|
||||
}
|
||||
deleteImages("testbuildrm")
|
||||
}
|
||||
|
||||
{
|
||||
containerCountBefore, err := getContainerCount()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get the container count: %s", err)
|
||||
}
|
||||
|
||||
buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildRm")
|
||||
buildCmd := exec.Command(dockerBinary, "build", "-t", "testbuildrm", ".")
|
||||
buildCmd.Dir = buildDirectory
|
||||
_, exitCode, err := runCommandWithOutput(buildCmd)
|
||||
|
||||
if err != nil || exitCode != 0 {
|
||||
t.Fatal("failed to build the image")
|
||||
}
|
||||
|
||||
containerCountAfter, err := getContainerCount()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get the container count: %s", err)
|
||||
}
|
||||
|
||||
if containerCountBefore != containerCountAfter {
|
||||
t.Fatalf("--rm shouldn't have left containers behind")
|
||||
}
|
||||
deleteImages("testbuildrm")
|
||||
}
|
||||
|
||||
{
|
||||
containerCountBefore, err := getContainerCount()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get the container count: %s", err)
|
||||
}
|
||||
|
||||
buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildRm")
|
||||
buildCmd := exec.Command(dockerBinary, "build", "--rm=false", "-t", "testbuildrm", ".")
|
||||
buildCmd.Dir = buildDirectory
|
||||
_, exitCode, err := runCommandWithOutput(buildCmd)
|
||||
|
||||
if err != nil || exitCode != 0 {
|
||||
t.Fatal("failed to build the image")
|
||||
}
|
||||
|
||||
containerCountAfter, err := getContainerCount()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get the container count: %s", err)
|
||||
}
|
||||
|
||||
if containerCountBefore == containerCountAfter {
|
||||
t.Fatalf("--rm=false should have left containers behind")
|
||||
}
|
||||
deleteAllContainers()
|
||||
deleteImages("testbuildrm")
|
||||
|
||||
}
|
||||
|
||||
logDone("build - ensure --rm doesn't leave containers behind and that --rm=true is the default")
|
||||
logDone("build - ensure --rm=false overrides the default")
|
||||
}
|
||||
|
||||
// TODO: TestCaching
|
||||
|
||||
// TODO: TestADDCacheInvalidation
|
||||
|
Reference in New Issue
Block a user