Skip to content

Commit

Permalink
unit_test: use Parallel test where possible
Browse files Browse the repository at this point in the history
Add `t.Parallel()` to unit tests whereever its possible without race.

Signed-off-by: flouthoc <[email protected]>
  • Loading branch information
flouthoc committed Feb 4, 2025
1 parent ae5e123 commit aadfc5c
Show file tree
Hide file tree
Showing 35 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions buildah_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
15 changes: 15 additions & 0 deletions chroot/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,15 @@ 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")
}
testMinimal(t, nil, nil)
}

func TestMinimalSkeleton(t *testing.T) {
t.Parallel()
if unix.Getuid() != 0 {
t.Skip("tests need to be run as root")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down
5 changes: 5 additions & 0 deletions cmd/buildah/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down
1 change: 1 addition & 0 deletions commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
)

func TestCommitLinkedLayers(t *testing.T) {
t.Parallel()
ctx := context.TODO()
now := time.Now()

Expand Down
1 change: 1 addition & 0 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions convertcw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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} {
Expand Down
1 change: 1 addition & 0 deletions copier/xattrs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
1 change: 1 addition & 0 deletions define/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

func TestPullPolicy(t *testing.T) {
t.Parallel()
for name, val := range PolicyMap {
assert.Equal(t, name, val.String())
}
Expand Down
1 change: 1 addition & 0 deletions define/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions digester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (c *CompositeDigester) isOpen() bool {
}

func TestCompositeDigester(t *testing.T) {
t.Parallel()
tests := []struct {
name string
itemTypes []string
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions imagebuildah/stage_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

func TestHistoryEntriesEqual(t *testing.T) {
t.Parallel()
testCases := []struct {
a, b string
equal bool
Expand Down
1 change: 1 addition & 0 deletions imagebuildah/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

func TestGeneratePathChecksum(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()

tempFile, err := os.CreateTemp(tempDir, "testfile")
Expand Down
2 changes: 2 additions & 0 deletions internal/config/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions internal/mkcw/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
)

func TestSlop(t *testing.T) {
t.Parallel()
testCases := []struct {
input int64
slop string
Expand Down Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions internal/mkcw/luks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

func TestCheckLUKSPassphrase(t *testing.T) {
t.Parallel()
passphrase, err := GenerateDiskEncryptionPassphrase()
require.NoError(t, err)
secondPassphrase, err := GenerateDiskEncryptionPassphrase()
Expand Down
1 change: 1 addition & 0 deletions internal/mkcw/workload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions internal/open/open_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions internal/open/open_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
9 changes: 9 additions & 0 deletions internal/sbom/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

func TestEncodeJSON(t *testing.T) {
t.Parallel()
tmp := t.TempDir()
map1 := map[string]any{
"string": "yeah",
Expand Down Expand Up @@ -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(`
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -123,6 +126,7 @@ func TestGetComponentNameVersionPurl(t *testing.T) {
}

func TestGetLicenseID(t *testing.T) {
t.Parallel()
input := map[string]any{
"licenseId": "driver",
}
Expand All @@ -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",
Expand Down Expand Up @@ -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},
Expand All @@ -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},
Expand Down Expand Up @@ -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},
Expand All @@ -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},
Expand Down
1 change: 1 addition & 0 deletions internal/sbom/presets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

func TestPreset(t *testing.T) {
t.Parallel()
for presetName, expectToFind := range map[string]bool{
"": true,
"syft": true,
Expand Down
1 change: 1 addition & 0 deletions internal/tmpdir/tmpdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

func TestGetTempDir(t *testing.T) {
t.Parallel()
// test default
err := os.Unsetenv("TMPDIR")
require.NoError(t, err)
Expand Down
Loading

0 comments on commit aadfc5c

Please sign in to comment.