From aadfc5cf301e9eed02cb2aa08d9de654ba11f992 Mon Sep 17 00:00:00 2001 From: flouthoc Date: Mon, 3 Feb 2025 21:36:03 -0800 Subject: [PATCH 1/5] unit_test: use Parallel test where possible Add `t.Parallel()` to unit tests whereever its possible without race. Signed-off-by: flouthoc --- buildah_test.go | 1 + chroot/run_test.go | 15 +++++++++++++++ cmd/buildah/images_test.go | 5 +++++ commit_test.go | 1 + common_test.go | 1 + convertcw_test.go | 1 + copier/xattrs_test.go | 1 + define/pull_test.go | 1 + define/types_test.go | 1 + digester_test.go | 2 ++ imagebuildah/stage_executor_test.go | 1 + imagebuildah/util_test.go | 1 + internal/config/convert_test.go | 2 ++ internal/mkcw/archive_test.go | 2 ++ internal/mkcw/luks_test.go | 1 + internal/mkcw/workload_test.go | 1 + internal/open/open_linux_test.go | 1 + internal/open/open_test.go | 1 + internal/sbom/merge_test.go | 9 +++++++++ internal/sbom/presets_test.go | 1 + internal/tmpdir/tmpdir_test.go | 1 + internal/volumes/bind_linux_test.go | 1 + internal/volumes/volumes_test.go | 1 + new_test.go | 2 ++ pkg/chrootuser/user_test.go | 2 ++ pkg/cli/common_test.go | 8 ++++++++ pkg/formats/formats_test.go | 1 + pkg/jail/jail_test.go | 1 + pkg/parse/parse_test.go | 11 +++++++++++ pkg/rusage/rusage_test.go | 1 + pkg/sshagent/sshagent_test.go | 1 + pkg/util/resource_unix_test.go | 1 + pkg/util/util_test.go | 1 + run_test.go | 1 + util/util_test.go | 3 +++ 35 files changed, 85 insertions(+) diff --git a/buildah_test.go b/buildah_test.go index d9cad2dbf4e..ee906e80f2d 100644 --- a/buildah_test.go +++ b/buildah_test.go @@ -40,6 +40,7 @@ func TestMain(m *testing.M) { } func TestOpenBuilderCommonBuildOpts(t *testing.T) { + t.Parallel() ctx := context.TODO() store, err := storage.GetStore(types.StoreOptions{ RunRoot: t.TempDir(), diff --git a/chroot/run_test.go b/chroot/run_test.go index 7f3cee59c4c..4be51e8670b 100644 --- a/chroot/run_test.go +++ b/chroot/run_test.go @@ -123,6 +123,7 @@ func testMinimal(t *testing.T, modify func(g *generate.Generator, rootDir, bundl } func TestNoop(t *testing.T) { + t.Parallel() if unix.Getuid() != 0 { t.Skip("tests need to be run as root") } @@ -130,6 +131,7 @@ func TestNoop(t *testing.T) { } func TestMinimalSkeleton(t *testing.T) { + t.Parallel() if unix.Getuid() != 0 { t.Skip("tests need to be run as root") } @@ -142,6 +144,7 @@ func TestMinimalSkeleton(t *testing.T) { } func TestProcessTerminal(t *testing.T) { + t.Parallel() if unix.Getuid() != 0 { t.Skip("tests need to be run as root") } @@ -160,6 +163,7 @@ func TestProcessTerminal(t *testing.T) { } func TestProcessConsoleSize(t *testing.T) { + t.Parallel() if unix.Getuid() != 0 { t.Skip("tests need to be run as root") } @@ -182,6 +186,7 @@ func TestProcessConsoleSize(t *testing.T) { } func TestProcessUser(t *testing.T) { + t.Parallel() if unix.Getuid() != 0 { t.Skip("tests need to be run as root") } @@ -205,6 +210,7 @@ func TestProcessUser(t *testing.T) { } func TestProcessEnv(t *testing.T) { + t.Parallel() if unix.Getuid() != 0 { t.Skip("tests need to be run as root") } @@ -226,6 +232,7 @@ func TestProcessEnv(t *testing.T) { } func TestProcessCwd(t *testing.T) { + t.Parallel() if unix.Getuid() != 0 { t.Skip("tests need to be run as root") } @@ -245,6 +252,7 @@ func TestProcessCwd(t *testing.T) { } func TestProcessCapabilities(t *testing.T) { + t.Parallel() if unix.Getuid() != 0 { t.Skip("tests need to be run as root") } @@ -289,6 +297,7 @@ func TestProcessCapabilities(t *testing.T) { } func TestProcessRlimits(t *testing.T) { + t.Parallel() if unix.Getuid() != 0 { t.Skip("tests need to be run as root") } @@ -327,6 +336,7 @@ func TestProcessRlimits(t *testing.T) { } func TestProcessNoNewPrivileges(t *testing.T) { + t.Parallel() if unix.Getuid() != 0 { t.Skip("tests need to be run as root") } @@ -348,6 +358,7 @@ func TestProcessNoNewPrivileges(t *testing.T) { } func TestProcessOOMScoreAdj(t *testing.T) { + t.Parallel() if unix.Getuid() != 0 { t.Skip("tests need to be run as root") } @@ -370,6 +381,7 @@ func TestProcessOOMScoreAdj(t *testing.T) { } func TestHostname(t *testing.T) { + t.Parallel() if unix.Getuid() != 0 { t.Skip("tests need to be run as root") } @@ -387,6 +399,7 @@ func TestHostname(t *testing.T) { } func TestMounts(t *testing.T) { + t.Parallel() if unix.Getuid() != 0 { t.Skip("tests need to be run as root") } @@ -547,6 +560,7 @@ func TestMounts(t *testing.T) { } func TestLinuxIDMapping(t *testing.T) { + t.Parallel() if unix.Getuid() != 0 { t.Skip("tests need to be run as root") } @@ -584,6 +598,7 @@ func TestLinuxIDMapping(t *testing.T) { } func TestLinuxIDMappingShift(t *testing.T) { + t.Parallel() if unix.Getuid() != 0 { t.Skip("tests need to be run as root") } diff --git a/cmd/buildah/images_test.go b/cmd/buildah/images_test.go index bfd3f8475bc..07d19defd72 100644 --- a/cmd/buildah/images_test.go +++ b/cmd/buildah/images_test.go @@ -5,6 +5,7 @@ import ( ) func TestSizeFormatting(t *testing.T) { + t.Parallel() size := formattedSize(0) if size != "0 B" { t.Errorf("Error formatting size: expected '%s' got '%s'", "0 B", size) @@ -22,6 +23,7 @@ func TestSizeFormatting(t *testing.T) { } func TestMatchWithTag(t *testing.T) { + t.Parallel() isMatch := matchesReference("gcr.io/pause:latest", "pause:latest") if !isMatch { t.Error("expected match, got not match") @@ -34,6 +36,7 @@ func TestMatchWithTag(t *testing.T) { } func TestNoMatchesReferenceWithTag(t *testing.T) { + t.Parallel() isMatch := matchesReference("gcr.io/pause:latest", "redis:latest") if isMatch { t.Error("expected no match, got match") @@ -46,6 +49,7 @@ func TestNoMatchesReferenceWithTag(t *testing.T) { } func TestMatchesReferenceWithoutTag(t *testing.T) { + t.Parallel() isMatch := matchesReference("gcr.io/pause:latest", "pause") if !isMatch { t.Error("expected match, got not match") @@ -58,6 +62,7 @@ func TestMatchesReferenceWithoutTag(t *testing.T) { } func TestNoMatchesReferenceWithoutTag(t *testing.T) { + t.Parallel() isMatch := matchesReference("gcr.io/pause:latest", "redis") if isMatch { t.Error("expected no match, got match") diff --git a/commit_test.go b/commit_test.go index 96f540e509a..9fac7797df1 100644 --- a/commit_test.go +++ b/commit_test.go @@ -21,6 +21,7 @@ import ( ) func TestCommitLinkedLayers(t *testing.T) { + t.Parallel() ctx := context.TODO() now := time.Now() diff --git a/common_test.go b/common_test.go index e4ba17960d1..d5565b924f0 100644 --- a/common_test.go +++ b/common_test.go @@ -41,6 +41,7 @@ func (ts *testRetryCopyImageWrappedStore) CreateImage(id string, names []string, } func TestRetryCopyImage(t *testing.T) { + t.Parallel() ctx := context.TODO() graphDriverName := os.Getenv("STORAGE_DRIVER") diff --git a/convertcw_test.go b/convertcw_test.go index 32f1fb61893..a5dcb4d8036 100644 --- a/convertcw_test.go +++ b/convertcw_test.go @@ -66,6 +66,7 @@ func (d *dummyAttestationHandler) ServeHTTP(rw http.ResponseWriter, req *http.Re } func TestCWConvertImage(t *testing.T) { + t.Parallel() ctx := context.TODO() for _, status := range []int{http.StatusOK, http.StatusInternalServerError} { for _, ignoreChainRetrievalErrors := range []bool{false, true} { diff --git a/copier/xattrs_test.go b/copier/xattrs_test.go index 7f3e2803281..4b015f4c7dc 100644 --- a/copier/xattrs_test.go +++ b/copier/xattrs_test.go @@ -17,6 +17,7 @@ func init() { } func TestXattrs(t *testing.T) { + t.Parallel() if !xattrsSupported { t.Skipf("xattrs are not supported on this platform, skipping") } diff --git a/define/pull_test.go b/define/pull_test.go index fbaf8319f65..f906643564f 100644 --- a/define/pull_test.go +++ b/define/pull_test.go @@ -7,6 +7,7 @@ import ( ) func TestPullPolicy(t *testing.T) { + t.Parallel() for name, val := range PolicyMap { assert.Equal(t, name, val.String()) } diff --git a/define/types_test.go b/define/types_test.go index 9ca2fa6beb3..7318e0935b8 100644 --- a/define/types_test.go +++ b/define/types_test.go @@ -7,6 +7,7 @@ import ( ) func TestParseGitBuildContext(t *testing.T) { + t.Parallel() // Tests with only repo repo, subdir, branch := parseGitBuildContext("https://github.com/containers/repo.git") assert.Equal(t, repo, "https://github.com/containers/repo.git") diff --git a/digester_test.go b/digester_test.go index 91b7516fc9e..7f1d734a0bd 100644 --- a/digester_test.go +++ b/digester_test.go @@ -25,6 +25,7 @@ func (c *CompositeDigester) isOpen() bool { } func TestCompositeDigester(t *testing.T) { + t.Parallel() tests := []struct { name string itemTypes []string @@ -186,6 +187,7 @@ func TestCompositeDigester(t *testing.T) { } func TestTarFilterer(t *testing.T) { + t.Parallel() tests := []struct { name string input, output map[string]string diff --git a/imagebuildah/stage_executor_test.go b/imagebuildah/stage_executor_test.go index d65174d0e97..57cf7da706a 100644 --- a/imagebuildah/stage_executor_test.go +++ b/imagebuildah/stage_executor_test.go @@ -11,6 +11,7 @@ import ( ) func TestHistoryEntriesEqual(t *testing.T) { + t.Parallel() testCases := []struct { a, b string equal bool diff --git a/imagebuildah/util_test.go b/imagebuildah/util_test.go index 2263d20fa94..abe28608fd2 100644 --- a/imagebuildah/util_test.go +++ b/imagebuildah/util_test.go @@ -12,6 +12,7 @@ import ( ) func TestGeneratePathChecksum(t *testing.T) { + t.Parallel() tempDir := t.TempDir() tempFile, err := os.CreateTemp(tempDir, "testfile") diff --git a/internal/config/convert_test.go b/internal/config/convert_test.go index 5710964852b..8ac2779dbcf 100644 --- a/internal/config/convert_test.go +++ b/internal/config/convert_test.go @@ -148,6 +148,7 @@ func checkValue(t *testing.T, value reflect.Value, name string, kind reflect.Kin } func TestGoDockerclientConfigFromSchema2Config(t *testing.T) { + t.Parallel() var input manifest.Schema2Config fillAllFields(t, &input) output := GoDockerclientConfigFromSchema2Config(&input) @@ -157,6 +158,7 @@ func TestGoDockerclientConfigFromSchema2Config(t *testing.T) { } func TestSchema2ConfigFromGoDockerclientConfig(t *testing.T) { + t.Parallel() var input dockerclient.Config fillAllFields(t, &input) output := Schema2ConfigFromGoDockerclientConfig(&input) diff --git a/internal/mkcw/archive_test.go b/internal/mkcw/archive_test.go index c2e06fc3af7..57de56071ce 100644 --- a/internal/mkcw/archive_test.go +++ b/internal/mkcw/archive_test.go @@ -20,6 +20,7 @@ import ( ) func TestSlop(t *testing.T) { + t.Parallel() testCases := []struct { input int64 slop string @@ -84,6 +85,7 @@ func (d *dummyAttestationHandler) ServeHTTP(rw http.ResponseWriter, req *http.Re } func TestArchive(t *testing.T) { + t.Parallel() ociConfig := &v1.Image{ Config: v1.ImageConfig{ User: "root", diff --git a/internal/mkcw/luks_test.go b/internal/mkcw/luks_test.go index 3df723ffb86..8302ce52e70 100644 --- a/internal/mkcw/luks_test.go +++ b/internal/mkcw/luks_test.go @@ -11,6 +11,7 @@ import ( ) func TestCheckLUKSPassphrase(t *testing.T) { + t.Parallel() passphrase, err := GenerateDiskEncryptionPassphrase() require.NoError(t, err) secondPassphrase, err := GenerateDiskEncryptionPassphrase() diff --git a/internal/mkcw/workload_test.go b/internal/mkcw/workload_test.go index 2de766f97a0..1d84ba4146f 100644 --- a/internal/mkcw/workload_test.go +++ b/internal/mkcw/workload_test.go @@ -10,6 +10,7 @@ import ( ) func TestReadWriteWorkloadConfig(t *testing.T) { + t.Parallel() // Create a temporary file to stand in for a disk image. temp := filepath.Join(t.TempDir(), "disk.img") f, err := os.OpenFile(temp, os.O_CREATE|os.O_RDWR, 0o600) diff --git a/internal/open/open_linux_test.go b/internal/open/open_linux_test.go index e89f5fad789..e64ec6fa645 100644 --- a/internal/open/open_linux_test.go +++ b/internal/open/open_linux_test.go @@ -10,6 +10,7 @@ import ( ) func TestBindFdToPath(t *testing.T) { + t.Parallel() first := t.TempDir() sampleData := []byte("sample data") err := os.WriteFile(filepath.Join(first, "testfile"), sampleData, 0o600) diff --git a/internal/open/open_test.go b/internal/open/open_test.go index c349a10c99d..0a24647b452 100644 --- a/internal/open/open_test.go +++ b/internal/open/open_test.go @@ -19,6 +19,7 @@ func TestMain(m *testing.M) { } func TestOpenInChroot(t *testing.T) { + t.Parallel() tmpdir := t.TempDir() firstContents := []byte{0, 1, 2, 3} secondContents := []byte{4, 5, 6, 7} diff --git a/internal/sbom/merge_test.go b/internal/sbom/merge_test.go index 4a21f98621d..a851f0b4ade 100644 --- a/internal/sbom/merge_test.go +++ b/internal/sbom/merge_test.go @@ -10,6 +10,7 @@ import ( ) func TestEncodeJSON(t *testing.T) { + t.Parallel() tmp := t.TempDir() map1 := map[string]any{ "string": "yeah", @@ -56,6 +57,7 @@ func TestEncodeJSON(t *testing.T) { } func TestDecodeJSON(t *testing.T) { + t.Parallel() tmp := t.TempDir() var map1, map2, map3 map[string]any err := os.WriteFile(filepath.Join(tmp, "1.json"), []byte(` @@ -93,6 +95,7 @@ func TestDecodeJSON(t *testing.T) { } func TestGetComponentNameVersionPurl(t *testing.T) { + t.Parallel() input := map[string]any{ "name": "alice", "version": "1.0", @@ -123,6 +126,7 @@ func TestGetComponentNameVersionPurl(t *testing.T) { } func TestGetLicenseID(t *testing.T) { + t.Parallel() input := map[string]any{ "licenseId": "driver", } @@ -132,6 +136,7 @@ func TestGetLicenseID(t *testing.T) { } func TestGetPackageNameVersionInfoPurl(t *testing.T) { + t.Parallel() input := map[string]any{ "name": "alice", "versionInfo": "1.0", @@ -194,6 +199,7 @@ func TestGetPackageNameVersionInfoPurl(t *testing.T) { } func TestMergeSlicesWithoutDuplicatesFixed(t *testing.T) { + t.Parallel() base := map[string]any{ "array": []any{ map[string]any{"first": 1}, @@ -217,6 +223,7 @@ func TestMergeSlicesWithoutDuplicatesFixed(t *testing.T) { } func TestMergeSlicesWithoutDuplicatesDynamic(t *testing.T) { + t.Parallel() base := map[string]any{ "array": []any{ map[string]any{"first": 1}, @@ -246,6 +253,7 @@ func TestMergeSlicesWithoutDuplicatesDynamic(t *testing.T) { } func TestMergeSlicesWithoutDuplicatesNoop(t *testing.T) { + t.Parallel() base := map[string]any{ "array": []any{ map[string]any{"first": 1}, @@ -269,6 +277,7 @@ func TestMergeSlicesWithoutDuplicatesNoop(t *testing.T) { } func TestMergeSlicesWithoutDuplicatesMissing(t *testing.T) { + t.Parallel() base := map[string]any{ "array": []any{ map[string]any{"first": 1}, diff --git a/internal/sbom/presets_test.go b/internal/sbom/presets_test.go index 8dfb15a7299..a1909b198a9 100644 --- a/internal/sbom/presets_test.go +++ b/internal/sbom/presets_test.go @@ -8,6 +8,7 @@ import ( ) func TestPreset(t *testing.T) { + t.Parallel() for presetName, expectToFind := range map[string]bool{ "": true, "syft": true, diff --git a/internal/tmpdir/tmpdir_test.go b/internal/tmpdir/tmpdir_test.go index 3dd335d15ef..07c374ad11a 100644 --- a/internal/tmpdir/tmpdir_test.go +++ b/internal/tmpdir/tmpdir_test.go @@ -11,6 +11,7 @@ import ( ) func TestGetTempDir(t *testing.T) { + t.Parallel() // test default err := os.Unsetenv("TMPDIR") require.NoError(t, err) diff --git a/internal/volumes/bind_linux_test.go b/internal/volumes/bind_linux_test.go index 6e21a8fee02..cd71c682b7a 100644 --- a/internal/volumes/bind_linux_test.go +++ b/internal/volumes/bind_linux_test.go @@ -10,6 +10,7 @@ import ( ) func TestBindFromChroot(t *testing.T) { + t.Parallel() if os.Getuid() != 0 { t.Skip("not running as root, assuming we can't mount or chroot") } diff --git a/internal/volumes/volumes_test.go b/internal/volumes/volumes_test.go index fb3b517377d..be2843a33e4 100644 --- a/internal/volumes/volumes_test.go +++ b/internal/volumes/volumes_test.go @@ -12,6 +12,7 @@ import ( ) func TestGetMount(t *testing.T) { + t.Parallel() tempDir := t.TempDir() rootDir := t.TempDir() runDir := t.TempDir() diff --git a/new_test.go b/new_test.go index ae78a2633e1..ea9acb15405 100644 --- a/new_test.go +++ b/new_test.go @@ -9,6 +9,7 @@ import ( ) func TestGetImageName(t *testing.T) { + t.Parallel() tt := []struct { caseName string name string @@ -30,6 +31,7 @@ func TestGetImageName(t *testing.T) { } func TestNoBaseImageSpecifierIsScratch(t *testing.T) { + t.Parallel() assert.Equal(t, "scratch", imagebuilder.NoBaseImageSpecifier) // juuuuust in case assert.Equal(t, "scratch", BaseImageFakeName) } diff --git a/pkg/chrootuser/user_test.go b/pkg/chrootuser/user_test.go index fadbb18dada..97ca47481bf 100644 --- a/pkg/chrootuser/user_test.go +++ b/pkg/chrootuser/user_test.go @@ -16,6 +16,7 @@ kmem:*:2: ` func TestParseStripComments(t *testing.T) { + t.Parallel() // Test reading group file, ignoring comment lines rc := bufio.NewScanner(strings.NewReader(testGroupData)) line, ok := scanWithoutComments(rc) @@ -24,6 +25,7 @@ func TestParseStripComments(t *testing.T) { } func TestParseNextGroup(t *testing.T) { + t.Parallel() // Test parsing group file rc := bufio.NewScanner(strings.NewReader(testGroupData)) expected := []lookupGroupEntry{ diff --git a/pkg/cli/common_test.go b/pkg/cli/common_test.go index 112b2664fe1..a8d8c74ac64 100644 --- a/pkg/cli/common_test.go +++ b/pkg/cli/common_test.go @@ -35,24 +35,28 @@ func testFlagCompletion(t *testing.T, flags pflag.FlagSet, flagCompletions compl } func TestUserNsFlagsCompletion(t *testing.T) { + t.Parallel() flags := GetUserNSFlags(&UserNSResults{}) flagCompletions := GetUserNSFlagsCompletions() testFlagCompletion(t, flags, flagCompletions) } func TestNameSpaceFlagsCompletion(t *testing.T) { + t.Parallel() flags := GetNameSpaceFlags(&NameSpaceResults{}) flagCompletions := GetNameSpaceFlagsCompletions() testFlagCompletion(t, flags, flagCompletions) } func TestBudFlagsCompletion(t *testing.T) { + t.Parallel() flags := GetBudFlags(&BudResults{}) flagCompletions := GetBudFlagsCompletions() testFlagCompletion(t, flags, flagCompletions) } func TestFromAndBudFlagsCompletions(t *testing.T) { + t.Parallel() flags, err := GetFromAndBudFlags(&FromAndBudResults{}, &UserNSResults{}, &NameSpaceResults{}) if err != nil { t.Error("Could load the from and build flags.") @@ -62,6 +66,7 @@ func TestFromAndBudFlagsCompletions(t *testing.T) { } func TestLookupEnvVarReferences(t *testing.T) { + t.Parallel() t.Run("EmptyInput", func(t *testing.T) { assert.Empty(t, LookupEnvVarReferences(nil, nil)) assert.Empty(t, LookupEnvVarReferences([]string{}, nil)) @@ -114,6 +119,7 @@ func TestLookupEnvVarReferences(t *testing.T) { } func TestDecryptConfig(t *testing.T) { + t.Parallel() // Just a smoke test for the default path. res, err := DecryptConfig(nil) assert.NoError(t, err) @@ -121,6 +127,7 @@ func TestDecryptConfig(t *testing.T) { } func TestEncryptConfig(t *testing.T) { + t.Parallel() // Just a smoke test for the default path. cfg, layers, err := EncryptConfig(nil, nil) assert.NoError(t, err) @@ -129,6 +136,7 @@ func TestEncryptConfig(t *testing.T) { } func TestGetFormat(t *testing.T) { + t.Parallel() _, err := GetFormat("bogus") assert.NotNil(t, err) diff --git a/pkg/formats/formats_test.go b/pkg/formats/formats_test.go index 628da0121c3..9aaccfe6843 100644 --- a/pkg/formats/formats_test.go +++ b/pkg/formats/formats_test.go @@ -11,6 +11,7 @@ type ImageData struct { } func TestSetJSONFormatEncoder(t *testing.T) { + t.Parallel() tt := []struct { name string imageData *ImageData diff --git a/pkg/jail/jail_test.go b/pkg/jail/jail_test.go index 322d0044ff3..e4a11744f8e 100644 --- a/pkg/jail/jail_test.go +++ b/pkg/jail/jail_test.go @@ -10,6 +10,7 @@ import ( ) func TestParseVersion(t *testing.T) { + t.Parallel() tt := []struct { version string shouldFail bool diff --git a/pkg/parse/parse_test.go b/pkg/parse/parse_test.go index aeb842f7b2c..7b218d47351 100644 --- a/pkg/parse/parse_test.go +++ b/pkg/parse/parse_test.go @@ -14,6 +14,7 @@ import ( ) func TestCommonBuildOptionsFromFlagSet(t *testing.T) { + t.Parallel() fs := pflag.NewFlagSet("testme", pflag.PanicOnError) fs.String("memory", "1GB", "") fs.String("shm-size", "5TB", "") @@ -29,6 +30,7 @@ func TestCommonBuildOptionsFromFlagSet(t *testing.T) { // TestDeviceParser verifies the given device strings is parsed correctly func TestDeviceParser(t *testing.T) { + t.Parallel() if runtime.GOOS != "linux" { t.Skip("Devices is only supported on Linux") } @@ -76,6 +78,7 @@ func TestDeviceParser(t *testing.T) { } func TestIsValidDeviceMode(t *testing.T) { + t.Parallel() if runtime.GOOS != "linux" { t.Skip("Devices is only supported on Linux") } @@ -88,6 +91,7 @@ func TestIsValidDeviceMode(t *testing.T) { } func TestDeviceFromPath(t *testing.T) { + t.Parallel() if runtime.GOOS != "linux" { t.Skip("Devices is only supported on Linux") } @@ -115,6 +119,7 @@ func TestDeviceFromPath(t *testing.T) { } func TestIDMappingOptions(t *testing.T) { + t.Parallel() fs := pflag.NewFlagSet("testme", pflag.PanicOnError) pfs := pflag.NewFlagSet("persist", pflag.PanicOnError) fs.String("userns-uid-map-user", "", "") @@ -134,6 +139,7 @@ func TestIDMappingOptions(t *testing.T) { } func TestIsolation(t *testing.T) { + t.Parallel() def, err := defaultIsolation() if err != nil { assert.Error(t, err) @@ -162,6 +168,7 @@ func TestIsolation(t *testing.T) { } func TestNamespaceOptions(t *testing.T) { + t.Parallel() fs := pflag.NewFlagSet("testme", pflag.PanicOnError) fs.String("cgroupns", "", "") err := fs.Parse([]string{"--cgroupns", "private"}) @@ -176,6 +183,7 @@ func TestNamespaceOptions(t *testing.T) { } func TestParsePlatform(t *testing.T) { + t.Parallel() os, arch, variant, err := Platform("a/b/c") assert.NoError(t, err) assert.NoError(t, err) @@ -195,6 +203,7 @@ func TestParsePlatform(t *testing.T) { } func TestParsePullPolicy(t *testing.T) { + t.Parallel() testCases := map[string]bool{ "missing": true, "ifmissing": true, @@ -221,6 +230,7 @@ func TestParsePullPolicy(t *testing.T) { } func TestSplitStringWithColonEscape(t *testing.T) { + t.Parallel() tests := []struct { volume string expectedResult []string @@ -237,6 +247,7 @@ func TestSplitStringWithColonEscape(t *testing.T) { } func TestSystemContextFromFlagSet(t *testing.T) { + t.Parallel() fs := pflag.NewFlagSet("testme", pflag.PanicOnError) fs.Bool("tls-verify", false, "") err := fs.Parse([]string{"--tls-verify", "false"}) diff --git a/pkg/rusage/rusage_test.go b/pkg/rusage/rusage_test.go index 62ac573e579..2ff93e59304 100644 --- a/pkg/rusage/rusage_test.go +++ b/pkg/rusage/rusage_test.go @@ -33,6 +33,7 @@ func TestMain(m *testing.M) { } func TestRusage(t *testing.T) { + t.Parallel() if !Supported() { t.Skip("not supported on this platform") } diff --git a/pkg/sshagent/sshagent_test.go b/pkg/sshagent/sshagent_test.go index 5bb6de967f3..fc5d056e065 100644 --- a/pkg/sshagent/sshagent_test.go +++ b/pkg/sshagent/sshagent_test.go @@ -34,6 +34,7 @@ func testClient(path string) ([]*agent.Key, error) { } func TestAgentServer(t *testing.T) { + t.Parallel() src, err := testNewKeySource() require.NoError(t, err) ag, err := NewAgentServer(src) diff --git a/pkg/util/resource_unix_test.go b/pkg/util/resource_unix_test.go index 6ee95dcce40..0abd9426e52 100644 --- a/pkg/util/resource_unix_test.go +++ b/pkg/util/resource_unix_test.go @@ -8,6 +8,7 @@ import ( ) func TestParseUlimit(t *testing.T) { + t.Parallel() _, err := ParseUlimit("bogus") assert.NotNil(t, err) diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go index 8998abeda9b..e40bbd0c718 100644 --- a/pkg/util/util_test.go +++ b/pkg/util/util_test.go @@ -7,6 +7,7 @@ import ( ) func TestDiscoverContainerfile(t *testing.T) { + t.Parallel() _, err := DiscoverContainerfile("./bogus") assert.NotNil(t, err) diff --git a/run_test.go b/run_test.go index 5e1ec4e0701..418fea08b06 100644 --- a/run_test.go +++ b/run_test.go @@ -10,6 +10,7 @@ import ( ) func TestAddRlimits(t *testing.T) { + t.Parallel() tt := []struct { name string ulimit []string diff --git a/util/util_test.go b/util/util_test.go index 5ef05e029fc..53a80d89d94 100644 --- a/util/util_test.go +++ b/util/util_test.go @@ -11,6 +11,7 @@ import ( ) func TestMergeEnv(t *testing.T) { + t.Parallel() tests := [][3][]string{ { []string{"A=B", "B=C", "C=D"}, @@ -44,6 +45,7 @@ func TestMergeEnv(t *testing.T) { } func TestRuntime(t *testing.T) { + t.Parallel() os.Setenv("CONTAINERS_CONF", "/dev/null") conf, _ := config.Default() defaultRuntime := conf.Engine.OCIRuntime @@ -60,6 +62,7 @@ func TestRuntime(t *testing.T) { } func TestMountsSort(t *testing.T) { + t.Parallel() mounts1a := []specs.Mount{ { Source: "/a/bb/c", From 1879f3dc65d0fe3369c15a7eed577a4ccc32a8ea Mon Sep 17 00:00:00 2001 From: flouthoc Date: Mon, 3 Feb 2025 21:36:11 -0800 Subject: [PATCH 2/5] Makefile: use -parallel for go test Signed-off-by: flouthoc --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 1032ef9a70e..c36c13b0222 100644 --- a/Makefile +++ b/Makefile @@ -22,8 +22,9 @@ STRIP ?= strip GO := go GO_LDFLAGS := $(shell if $(GO) version|grep -q gccgo; then echo "-gccgoflags"; else echo "-ldflags"; fi) GO_GCFLAGS := $(shell if $(GO) version|grep -q gccgo; then echo "-gccgoflags"; else echo "-gcflags"; fi) +NPROCS := $(shell nproc) export GO_BUILD=$(GO) build -export GO_TEST=$(GO) test +export GO_TEST=$(GO) test -parallel=$(NPROCS) RACEFLAGS := $(shell $(GO_TEST) -race ./pkg/dummy > /dev/null 2>&1 && echo -race) COMMIT_NO ?= $(shell git rev-parse HEAD 2> /dev/null || true) From 81479b200c9a64e2e9f32d78d2a52a563ea583d8 Mon Sep 17 00:00:00 2001 From: flouthoc Date: Mon, 3 Feb 2025 21:36:14 -0800 Subject: [PATCH 3/5] .cirrus: use multiple cpu for unit tests Signed-off-by: flouthoc --- .cirrus.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.cirrus.yml b/.cirrus.yml index b08fe59ec20..19e05761bbf 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -154,6 +154,8 @@ cross_build_task: unit_task: name: 'Unit tests w/ $STORAGE_DRIVER' + gce_instance: + cpu: 4 alias: unit skip: ¬_build_docs >- $CIRRUS_CHANGE_TITLE =~ '.*CI:DOCS.*' || From de3c3baf096016df93cfee09e3d9d6054c06c9ba Mon Sep 17 00:00:00 2001 From: flouthoc Date: Mon, 3 Feb 2025 21:37:02 -0800 Subject: [PATCH 4/5] unit: deparallize some tests See issue: https://github.com/containers/buildah/issues/5967 Signed-off-by: flouthoc --- buildah_test.go | 7 ++++++- commit_test.go | 7 ++++++- convertcw_test.go | 7 ++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/buildah_test.go b/buildah_test.go index ee906e80f2d..f9387260a0c 100644 --- a/buildah_test.go +++ b/buildah_test.go @@ -40,7 +40,12 @@ func TestMain(m *testing.M) { } func TestOpenBuilderCommonBuildOpts(t *testing.T) { - t.Parallel() + // This test cannot be parallized as this uses NewBuilder() + // which eventually and indirectly accesses a global variable + // defined in `go-selinux`, this must be fixed at `go-selinux` + // or builder must enable sometime of locking mechanism i.e if + // routine is creating Builder other's must wait for it. + // Tracked here: https://github.com/containers/buildah/issues/5967 ctx := context.TODO() store, err := storage.GetStore(types.StoreOptions{ RunRoot: t.TempDir(), diff --git a/commit_test.go b/commit_test.go index 9fac7797df1..9eca89a666d 100644 --- a/commit_test.go +++ b/commit_test.go @@ -21,7 +21,12 @@ import ( ) func TestCommitLinkedLayers(t *testing.T) { - t.Parallel() + // This test cannot be parallized as this uses NewBuilder() + // which eventually and indirectly accesses a global variable + // defined in `go-selinux`, this must be fixed at `go-selinux` + // or builder must enable sometime of locking mechanism i.e if + // routine is creating Builder other's must wait for it. + // Tracked here: https://github.com/containers/buildah/issues/5967 ctx := context.TODO() now := time.Now() diff --git a/convertcw_test.go b/convertcw_test.go index a5dcb4d8036..42a0dba1ecd 100644 --- a/convertcw_test.go +++ b/convertcw_test.go @@ -66,7 +66,12 @@ func (d *dummyAttestationHandler) ServeHTTP(rw http.ResponseWriter, req *http.Re } func TestCWConvertImage(t *testing.T) { - t.Parallel() + // This test cannot be parallized as this uses NewBuilder() + // which eventually and indirectly accesses a global variable + // defined in `go-selinux`, this must be fixed at `go-selinux` + // or builder must enable sometime of locking mechanism i.e if + // routine is creating Builder other's must wait for it. + // Tracked here: https://github.com/containers/buildah/issues/5967 ctx := context.TODO() for _, status := range []int{http.StatusOK, http.StatusInternalServerError} { for _, ignoreChainRetrievalErrors := range []bool{false, true} { From 147a3ca91646258e47afde7ad598c6a12f73f563 Mon Sep 17 00:00:00 2001 From: flouthoc Date: Mon, 3 Feb 2025 21:37:08 -0800 Subject: [PATCH 5/5] .cirrus: run -race only on non-PR branch Signed-off-by: flouthoc --- Makefile | 2 +- contrib/cirrus/test.sh | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index c36c13b0222..a2aa24da905 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ GO_GCFLAGS := $(shell if $(GO) version|grep -q gccgo; then echo "-gccgoflags"; e NPROCS := $(shell nproc) export GO_BUILD=$(GO) build export GO_TEST=$(GO) test -parallel=$(NPROCS) -RACEFLAGS := $(shell $(GO_TEST) -race ./pkg/dummy > /dev/null 2>&1 && echo -race) +RACEFLAGS ?= $(shell $(GO_TEST) -race ./pkg/dummy > /dev/null 2>&1 && echo -race) COMMIT_NO ?= $(shell git rev-parse HEAD 2> /dev/null || true) GIT_COMMIT ?= $(if $(shell git status --porcelain --untracked-files=no),${COMMIT_NO}-dirty,${COMMIT_NO}) diff --git a/contrib/cirrus/test.sh b/contrib/cirrus/test.sh index 2ee945ec000..1f4fff42f5d 100755 --- a/contrib/cirrus/test.sh +++ b/contrib/cirrus/test.sh @@ -67,7 +67,13 @@ else showrun make validate ;; unit) - showrun make test-unit + race= + if [[ -z "$CIRRUS_PR" ]]; then + # If not running on a PR then run unit tests + # with appropriate `-race` flags. + race="-race" + fi + showrun make test-unit RACEFLAGS=$race ;; conformance) # Typically it's undesirable to install packages at runtime.