Skip to content

Commit

Permalink
Merge pull request #821 from monstermunchkin/818-test-race
Browse files Browse the repository at this point in the history
Remove race in tests
  • Loading branch information
stgraber authored Feb 14, 2024
2 parents 052af07 + 703b66d commit 3111761
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 54 deletions.
8 changes: 6 additions & 2 deletions generators/cloud-init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import (
)

func TestCloudInitGeneratorRunLXC(t *testing.T) {
cacheDir := filepath.Join(os.TempDir(), "distrobuilder-test")
cacheDir, err := os.MkdirTemp(os.TempDir(), "distrobuilder-test-")
require.NoError(t, err)

rootfsDir := filepath.Join(cacheDir, "rootfs")

setup(t, cacheDir)
Expand Down Expand Up @@ -63,7 +65,9 @@ func TestCloudInitGeneratorRunLXC(t *testing.T) {
}

func TestCloudInitGeneratorRunIncus(t *testing.T) {
cacheDir := filepath.Join(os.TempDir(), "distrobuilder-test")
cacheDir, err := os.MkdirTemp(os.TempDir(), "distrobuilder-test-")
require.NoError(t, err)

rootfsDir := filepath.Join(cacheDir, "rootfs")

setup(t, cacheDir)
Expand Down
4 changes: 3 additions & 1 deletion generators/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
)

func TestCopyGeneratorRun(t *testing.T) {
cacheDir := filepath.Join(os.TempDir(), "distrobuilder-test")
cacheDir, err := os.MkdirTemp(os.TempDir(), "distrobuilder-test-")
require.NoError(t, err)

rootfsDir := filepath.Join(cacheDir, "rootfs")

setup(t, cacheDir)
Expand Down
8 changes: 6 additions & 2 deletions generators/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
)

func TestDumpGeneratorRunLXC(t *testing.T) {
cacheDir := filepath.Join(os.TempDir(), "distrobuilder-test")
cacheDir, err := os.MkdirTemp(os.TempDir(), "distrobuilder-test-")
require.NoError(t, err)

rootfsDir := filepath.Join(cacheDir, "rootfs")

setup(t, cacheDir)
Expand Down Expand Up @@ -78,7 +80,9 @@ func TestDumpGeneratorRunLXC(t *testing.T) {
}

func TestDumpGeneratorRunIncus(t *testing.T) {
cacheDir := filepath.Join(os.TempDir(), "distrobuilder-test")
cacheDir, err := os.MkdirTemp(os.TempDir(), "distrobuilder-test-")
require.NoError(t, err)

rootfsDir := filepath.Join(cacheDir, "rootfs")

setup(t, cacheDir)
Expand Down
8 changes: 6 additions & 2 deletions generators/hostname_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
)

func TestHostnameGeneratorRunLXC(t *testing.T) {
cacheDir := filepath.Join(os.TempDir(), "distrobuilder-test")
cacheDir, err := os.MkdirTemp(os.TempDir(), "distrobuilder-test-")
require.NoError(t, err)

rootfsDir := filepath.Join(cacheDir, "rootfs")

setup(t, cacheDir)
Expand Down Expand Up @@ -44,7 +46,9 @@ func TestHostnameGeneratorRunLXC(t *testing.T) {
}

func TestHostnameGeneratorRunIncus(t *testing.T) {
cacheDir := filepath.Join(os.TempDir(), "distrobuilder-test")
cacheDir, err := os.MkdirTemp(os.TempDir(), "distrobuilder-test-")
require.NoError(t, err)

rootfsDir := filepath.Join(cacheDir, "rootfs")

setup(t, cacheDir)
Expand Down
8 changes: 6 additions & 2 deletions generators/hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
)

func TestHostsGeneratorRunLXC(t *testing.T) {
cacheDir := filepath.Join(os.TempDir(), "distrobuilder-test")
cacheDir, err := os.MkdirTemp(os.TempDir(), "distrobuilder-test-")
require.NoError(t, err)

rootfsDir := filepath.Join(cacheDir, "rootfs")

setup(t, cacheDir)
Expand Down Expand Up @@ -46,7 +48,9 @@ func TestHostsGeneratorRunLXC(t *testing.T) {
}

func TestHostsGeneratorRunIncus(t *testing.T) {
cacheDir := filepath.Join(os.TempDir(), "distrobuilder-test")
cacheDir, err := os.MkdirTemp(os.TempDir(), "distrobuilder-test-")
require.NoError(t, err)

rootfsDir := filepath.Join(cacheDir, "rootfs")

setup(t, cacheDir)
Expand Down
8 changes: 6 additions & 2 deletions generators/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
)

func TestTemplateGeneratorRunIncus(t *testing.T) {
cacheDir := filepath.Join(os.TempDir(), "distrobuilder-test")
cacheDir, err := os.MkdirTemp(os.TempDir(), "distrobuilder-test-")
require.NoError(t, err)

rootfsDir := filepath.Join(cacheDir, "rootfs")

setup(t, cacheDir)
Expand Down Expand Up @@ -50,7 +52,9 @@ func TestTemplateGeneratorRunIncus(t *testing.T) {
}

func TestTemplateGeneratorRunIncusDefaultWhen(t *testing.T) {
cacheDir := filepath.Join(os.TempDir(), "distrobuilder-test")
cacheDir, err := os.MkdirTemp(os.TempDir(), "distrobuilder-test-")
require.NoError(t, err)

rootfsDir := filepath.Join(cacheDir, "rootfs")

setup(t, cacheDir)
Expand Down
23 changes: 10 additions & 13 deletions image/incus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ var incusDef = shared.Definition{
},
}

func setupIncus(t *testing.T) *IncusImage {
cacheDir := filepath.Join(os.TempDir(), "distrobuilder-test-incus")
func setupIncus(t *testing.T) (*IncusImage, string) {
cacheDir, err := os.MkdirTemp(os.TempDir(), "distrobuilder-test-")
require.NoError(t, err)

err := os.MkdirAll(filepath.Join(cacheDir, "rootfs"), 0755)
err = os.MkdirAll(filepath.Join(cacheDir, "rootfs"), 0755)
require.NoError(t, err)

err = os.MkdirAll(filepath.Join(cacheDir, "templates"), 0755)
Expand All @@ -48,7 +49,7 @@ func setupIncus(t *testing.T) *IncusImage {
fail := true
defer func() {
if fail {
teardownIncus(t)
os.RemoveAll(cacheDir)
}
}()

Expand All @@ -62,16 +63,12 @@ func setupIncus(t *testing.T) *IncusImage {
require.NoError(t, err)

fail = false
return image
}

func teardownIncus(t *testing.T) {
os.RemoveAll(filepath.Join(os.TempDir(), "distrobuilder-test-incus"))
return image, cacheDir
}

func TestIncusBuild(t *testing.T) {
image := setupIncus(t)
defer teardownIncus(t)
image, cacheDir := setupIncus(t)
defer os.RemoveAll(cacheDir)

testIncusBuildSplitImage(t, image)
testIncusBuildUnifiedImage(t, image)
Expand Down Expand Up @@ -124,8 +121,8 @@ func testIncusBuildUnifiedImage(t *testing.T, image *IncusImage) {
}

func TestIncusCreateMetadata(t *testing.T) {
image := setupIncus(t)
defer teardownIncus(t)
image, cacheDir := setupIncus(t)
defer os.RemoveAll(cacheDir)

err := image.createMetadata()
require.NoError(t, err)
Expand Down
60 changes: 30 additions & 30 deletions image/lxc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,35 +86,35 @@ func lxcCacheDir() string {
return filepath.Join(wd, "distrobuilder-test-lxc")
}

func setupLXC() *LXCImage {
return NewLXCImage(context.TODO(), lxcCacheDir(), "", lxcCacheDir(), lxcDef)
}
func setupLXC() (*LXCImage, string) {
cacheDir := lxcCacheDir()

func teardownLXC() {
os.RemoveAll(lxcCacheDir())
return NewLXCImage(context.TODO(), cacheDir, "", cacheDir, lxcDef), cacheDir
}

func TestNewLXCImage(t *testing.T) {
image := NewLXCImage(context.TODO(), lxcCacheDir(), "", lxcCacheDir(), lxcDef)
defer teardownLXC()
cacheDir := lxcCacheDir()

image := NewLXCImage(context.TODO(), cacheDir, "", cacheDir, lxcDef)
defer os.RemoveAll(cacheDir)

require.Equal(t, lxcCacheDir(), image.cacheDir)
require.Equal(t, cacheDir, image.cacheDir)
require.Equal(t, lxcDef, image.definition)
}

func TestLXCAddTemplate(t *testing.T) {
image := setupLXC()
defer teardownLXC()
image, cacheDir := setupLXC()
defer os.RemoveAll(cacheDir)

// Make sure templates file is empty.
_, err := os.Stat(filepath.Join(lxcCacheDir(), "metadata", "templates"))
_, err := os.Stat(filepath.Join(cacheDir, "metadata", "templates"))
require.EqualError(t, err, fmt.Sprintf("stat %s: no such file or directory",
filepath.Join(lxcCacheDir(), "metadata", "templates")))
filepath.Join(cacheDir, "metadata", "templates")))

// Add first template entry.
err = image.AddTemplate("/path/file1")
require.NoError(t, err)
file, err := os.Open(filepath.Join(lxcCacheDir(), "metadata", "templates"))
file, err := os.Open(filepath.Join(cacheDir, "metadata", "templates"))
require.NoError(t, err)

// Copy file content to buffer.
Expand All @@ -128,7 +128,7 @@ func TestLXCAddTemplate(t *testing.T) {
// Add second template entry.
err = image.AddTemplate("/path/file2")
require.NoError(t, err)
file, err = os.Open(filepath.Join(lxcCacheDir(), "metadata", "templates"))
file, err = os.Open(filepath.Join(cacheDir, "metadata", "templates"))
require.NoError(t, err)

// Copy file content to buffer.
Expand All @@ -141,10 +141,10 @@ func TestLXCAddTemplate(t *testing.T) {
}

func TestLXCBuild(t *testing.T) {
image := setupLXC()
defer teardownLXC()
image, cacheDir := setupLXC()
defer os.RemoveAll(cacheDir)

err := os.MkdirAll(filepath.Join(lxcCacheDir(), "rootfs"), 0755)
err := os.MkdirAll(filepath.Join(cacheDir, "rootfs"), 0755)
require.NoError(t, err)

err = image.Build("xz")
Expand All @@ -163,8 +163,8 @@ func TestLXCBuild(t *testing.T) {
}

func TestLXCCreateMetadataBasic(t *testing.T) {
defaultImage := setupLXC()
defer teardownLXC()
defaultImage, cacheDir := setupLXC()
defer os.RemoveAll(cacheDir)

tests := []struct {
name string
Expand Down Expand Up @@ -209,9 +209,9 @@ func TestLXCCreateMetadataBasic(t *testing.T) {
"",
func(l LXCImage) *LXCImage {
// Create /dev and device file.
err := os.MkdirAll(filepath.Join(lxcCacheDir(), "rootfs", "dev"), 0755)
err := os.MkdirAll(filepath.Join(cacheDir, "rootfs", "dev"), 0755)
require.NoError(t, err)
err = unix.Mknod(filepath.Join(lxcCacheDir(), "rootfs", "dev", "null"), unix.S_IFCHR, 0)
err = unix.Mknod(filepath.Join(cacheDir, "rootfs", "dev", "null"), unix.S_IFCHR, 0)
require.NoError(t, err)
return &l
},
Expand All @@ -230,7 +230,7 @@ func TestLXCCreateMetadataBasic(t *testing.T) {
}

// Verify create-message template
f, err := os.Open(filepath.Join(lxcCacheDir(), "metadata", "create-message"))
f, err := os.Open(filepath.Join(cacheDir, "metadata", "create-message"))
require.NoError(t, err)
defer f.Close()

Expand All @@ -243,8 +243,8 @@ func TestLXCCreateMetadataBasic(t *testing.T) {
}

func TestLXCCreateMetadataConfig(t *testing.T) {
image := setupLXC()
defer teardownLXC()
image, cacheDir := setupLXC()
defer os.RemoveAll(cacheDir)

tests := []struct {
configFile string
Expand Down Expand Up @@ -297,7 +297,7 @@ func TestLXCCreateMetadataConfig(t *testing.T) {

for _, tt := range tests {
log.Printf("Checking '%s'", tt.configFile)
file, err := os.Open(filepath.Join(lxcCacheDir(), "metadata", tt.configFile))
file, err := os.Open(filepath.Join(cacheDir, "metadata", tt.configFile))
require.NoError(t, err)

var buffer bytes.Buffer
Expand All @@ -309,9 +309,9 @@ func TestLXCCreateMetadataConfig(t *testing.T) {
}

func TestLXCPackMetadata(t *testing.T) {
image := setupLXC()
image, cacheDir := setupLXC()
defer func() {
teardownLXC()
os.RemoveAll(cacheDir)
os.Remove("meta.tar.xz")
}()

Expand All @@ -328,14 +328,14 @@ func TestLXCPackMetadata(t *testing.T) {
require.NoError(t, err)

// Provoke error by removing the metadata directory
os.RemoveAll(filepath.Join(lxcCacheDir(), "metadata"))
os.RemoveAll(filepath.Join(cacheDir, "metadata"))
err = image.packMetadata()
require.Error(t, err)
}

func TestLXCWriteMetadata(t *testing.T) {
image := setupLXC()
defer teardownLXC()
image, cacheDir := setupLXC()
defer os.RemoveAll(cacheDir)

// Should fail due to invalid path
err := image.writeMetadata("/path/file", "", false)
Expand Down

0 comments on commit 3111761

Please sign in to comment.