From 87af1d1049e73bdf701644db0b790dcb7d06385b Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 26 Nov 2017 02:21:18 -0800 Subject: [PATCH] validation/util/container: Remove bundle even if delete fails When the container failed in creation, delete will fail, but we still want to remove the test bundle directory. Signed-off-by: W. Trevor King --- validation/create.go | 2 +- validation/util/container.go | 19 +++++++++++-------- validation/util/test.go | 2 +- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/validation/create.go b/validation/create.go index 63ce3b589..9a86fe928 100644 --- a/validation/create.go +++ b/validation/create.go @@ -27,7 +27,7 @@ func main() { if err != nil { util.Fatal(err) } - defer r.Clean(true) + defer r.Clean(true, true) err = r.SetConfig(&g) if err != nil { diff --git a/validation/util/container.go b/validation/util/container.go index c30a14251..652c316f8 100644 --- a/validation/util/container.go +++ b/validation/util/container.go @@ -151,16 +151,19 @@ func (r *Runtime) Delete() error { return cmd.Run() } -// Clean deletes the container and removes the bundle file according to the input parameter -func (r *Runtime) Clean(removeBundle bool) error { +// Clean deletes the container. If removeBundle is set, the bundle +// directory is removed after the container is deleted succesfully or, if +// forceRemoveBundle is true, after the deletion attempt regardless of +// whether it was successful or not. +func (r *Runtime) Clean(removeBundle bool, forceRemoveBundle bool) error { err := r.Delete() - if err != nil { - return err - } - if removeBundle { - os.RemoveAll(r.BundleDir) + if removeBundle && (err == nil || forceRemoveBundle) { + err2 := os.RemoveAll(r.BundleDir) + if err2 != nil && err == nil { + err = err2 + } } - return nil + return err } diff --git a/validation/util/test.go b/validation/util/test.go index c0f120f28..3b544197c 100644 --- a/validation/util/test.go +++ b/validation/util/test.go @@ -92,7 +92,7 @@ func RuntimeInsideValidate(g *generate.Generator, f PreFunc) (err error) { os.RemoveAll(bundleDir) return err } - defer r.Clean(true) + defer r.Clean(true, true) err = r.SetConfig(g) if err != nil { return err