From 9934175840af10656363494ee3bff1e0414e490e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Graber?= Date: Fri, 17 Nov 2023 20:00:46 -0500 Subject: [PATCH] image: Use t.Cleanup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Graber --- image/incus_test.go | 18 +++++------------- image/lxc_test.go | 26 ++++++++++++-------------- 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/image/incus_test.go b/image/incus_test.go index 34660573..6008ec9f 100644 --- a/image/incus_test.go +++ b/image/incus_test.go @@ -45,13 +45,6 @@ func setupIncus(t *testing.T) *IncusImage { image := NewIncusImage(context.TODO(), cacheDir, "", cacheDir, incusDef) - fail := true - defer func() { - if fail { - teardownIncus(t) - } - }() - // Check cache directory require.Equal(t, cacheDir, image.cacheDir) require.Equal(t, incusDef, image.definition) @@ -61,7 +54,6 @@ func setupIncus(t *testing.T) *IncusImage { err = incusDef.Validate() require.NoError(t, err) - fail = false return image } @@ -71,7 +63,7 @@ func teardownIncus(t *testing.T) { func TestIncusBuild(t *testing.T) { image := setupIncus(t) - defer teardownIncus(t) + t.Cleanup(func() { teardownIncus(t) }) testIncusBuildSplitImage(t, image) testIncusBuildUnifiedImage(t, image) @@ -104,13 +96,13 @@ func testIncusBuildUnifiedImage(t *testing.T, image *IncusImage) { // Create unified tarball with custom name. _, _, err := image.Build(true, "xz", false) require.NoError(t, err) - defer os.Remove("ubuntu-17.10-x86_64-testing.tar.xz") + t.Cleanup(func() { os.Remove("ubuntu-17.10-x86_64-testing.tar.xz") }) require.FileExists(t, "ubuntu-17.10-x86_64-testing.tar.xz") _, _, err = image.Build(true, "gzip", false) require.NoError(t, err) - defer os.Remove("ubuntu-17.10-x86_64-testing.tar.gz") + t.Cleanup(func() { os.Remove("ubuntu-17.10-x86_64-testing.tar.gz") }) require.FileExists(t, "ubuntu-17.10-x86_64-testing.tar.gz") @@ -118,14 +110,14 @@ func testIncusBuildUnifiedImage(t *testing.T, image *IncusImage) { image.definition.Image.Name = "" _, _, err = image.Build(true, "xz", false) require.NoError(t, err) - defer os.Remove("incus.tar.xz") + t.Cleanup(func() { os.Remove("incus.tar.xz") }) require.FileExists(t, "incus.tar.xz") } func TestIncusCreateMetadata(t *testing.T) { image := setupIncus(t) - defer teardownIncus(t) + t.Cleanup(func() { teardownIncus(t) }) err := image.createMetadata() require.NoError(t, err) diff --git a/image/lxc_test.go b/image/lxc_test.go index 2fffd83a..3b802433 100644 --- a/image/lxc_test.go +++ b/image/lxc_test.go @@ -96,7 +96,7 @@ func teardownLXC() { func TestNewLXCImage(t *testing.T) { image := NewLXCImage(context.TODO(), lxcCacheDir(), "", lxcCacheDir(), lxcDef) - defer teardownLXC() + t.Cleanup(teardownLXC) require.Equal(t, lxcCacheDir(), image.cacheDir) require.Equal(t, lxcDef, image.definition) @@ -104,7 +104,7 @@ func TestNewLXCImage(t *testing.T) { func TestLXCAddTemplate(t *testing.T) { image := setupLXC() - defer teardownLXC() + t.Cleanup(teardownLXC) // Make sure templates file is empty. _, err := os.Stat(filepath.Join(lxcCacheDir(), "metadata", "templates")) @@ -142,29 +142,29 @@ func TestLXCAddTemplate(t *testing.T) { func TestLXCBuild(t *testing.T) { image := setupLXC() - defer teardownLXC() + t.Cleanup(teardownLXC) err := os.MkdirAll(filepath.Join(lxcCacheDir(), "rootfs"), 0755) require.NoError(t, err) err = image.Build("xz") require.NoError(t, err) - defer func() { + t.Cleanup(func() { os.Remove("meta.tar.xz") os.Remove("rootfs.tar.xz") - }() + }) err = image.Build("gzip") require.NoError(t, err) - defer func() { + t.Cleanup(func() { os.Remove("meta.tar.gz") os.Remove("rootfs.tar.gz") - }() + }) } func TestLXCCreateMetadataBasic(t *testing.T) { defaultImage := setupLXC() - defer teardownLXC() + t.Cleanup(teardownLXC) tests := []struct { name string @@ -244,7 +244,7 @@ func TestLXCCreateMetadataBasic(t *testing.T) { func TestLXCCreateMetadataConfig(t *testing.T) { image := setupLXC() - defer teardownLXC() + t.Cleanup(teardownLXC) tests := []struct { configFile string @@ -310,10 +310,8 @@ func TestLXCCreateMetadataConfig(t *testing.T) { func TestLXCPackMetadata(t *testing.T) { image := setupLXC() - defer func() { - teardownLXC() - os.Remove("meta.tar.xz") - }() + t.Cleanup(teardownLXC) + t.Cleanup(func() { os.Remove("meta.tar.xz") }) err := image.createMetadata() require.NoError(t, err) @@ -335,7 +333,7 @@ func TestLXCPackMetadata(t *testing.T) { func TestLXCWriteMetadata(t *testing.T) { image := setupLXC() - defer teardownLXC() + t.Cleanup(teardownLXC) // Should fail due to invalid path err := image.writeMetadata("/path/file", "", false)