Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CI: parallize unit tests #5954

Merged
merged 5 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ cross_build_task:

unit_task:
name: 'Unit tests w/ $STORAGE_DRIVER'
gce_instance:
cpu: 4
alias: unit
skip: &not_build_docs >-
$CIRRUS_CHANGE_TITLE =~ '.*CI:DOCS.*' ||
Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ 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
RACEFLAGS := $(shell $(GO_TEST) -race ./pkg/dummy > /dev/null 2>&1 && echo -race)
export GO_TEST=$(GO) test -parallel=$(NPROCS)
RACEFLAGS ?= $(shell $(GO_TEST) -race ./pkg/dummy > /dev/null 2>&1 && echo -race)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you merge this one with previous

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


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})
Expand Down
6 changes: 6 additions & 0 deletions buildah_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ func TestMain(m *testing.M) {
}

func TestOpenBuilderCommonBuildOpts(t *testing.T) {
// 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How will we know when it's safe? Is there an open issue?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just created an issue #5967 and added in comment and commit message.

// Tracked here: https://github.com/containers/buildah/issues/5967
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
6 changes: 6 additions & 0 deletions commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ import (
)

func TestCommitLinkedLayers(t *testing.T) {
// 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()

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
8 changes: 7 additions & 1 deletion contrib/cirrus/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions convertcw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ func (d *dummyAttestationHandler) ServeHTTP(rw http.ResponseWriter, req *http.Re
}

func TestCWConvertImage(t *testing.T) {
// 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} {
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
Loading