Skip to content

Commit

Permalink
image: Use t.Cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Graber <[email protected]>
  • Loading branch information
stgraber committed Nov 18, 2023
1 parent 1ee669b commit 9934175
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 27 deletions.
18 changes: 5 additions & 13 deletions image/incus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -61,7 +54,6 @@ func setupIncus(t *testing.T) *IncusImage {
err = incusDef.Validate()
require.NoError(t, err)

fail = false
return image
}

Expand All @@ -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)
Expand Down Expand Up @@ -104,28 +96,28 @@ 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")

// Create unified tarball with default name.
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)
Expand Down
26 changes: 12 additions & 14 deletions image/lxc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ 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)
}

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"))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 9934175

Please sign in to comment.