Skip to content

Commit 18842d4

Browse files
committed
tests: Avoid races when building/using the NSS library
* Lock the target directory to avoid multiple tests running in parallel trying to build the library at the same time (possibly with different set of features or other options). * Copy the resulting library to a temporary directory only used by the current test, to allow other tests to reuse the target directory (so that they don't have to rebuild it from scratch).
1 parent 6ea092b commit 18842d4

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

internal/testutils/rust.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/stretchr/testify/assert"
1515
"github.com/stretchr/testify/require"
16+
"github.com/ubuntu/authd/internal/fileutils"
1617
)
1718

1819
func getCargoPath() (path string, isNightly bool, err error) {
@@ -76,6 +77,9 @@ func BuildRustNSSLib(t *testing.T, disableCoverage bool, features ...string) (li
7677

7778
// Store the build artifacts in a common temp directory, so that they can be reused between tests.
7879
target := filepath.Join(os.TempDir(), "authd-tests-rust-build-artifacts")
80+
// Ensure the target directory exists.
81+
err = os.MkdirAll(target, 0700)
82+
require.NoError(t, err, "Setup: could not create Rust target dir")
7983

8084
rustDir := filepath.Join(projectRoot, "nss")
8185
if !disableCoverage {
@@ -84,6 +88,12 @@ func BuildRustNSSLib(t *testing.T, disableCoverage bool, features ...string) (li
8488

8589
features = append([]string{"integration_tests", "custom_socket"}, features...)
8690

91+
unlock, err := fileutils.LockDir(target)
92+
require.NoError(t, err, "Setup: could not lock Rust target dir")
93+
defer func() {
94+
require.NoError(t, unlock(), "Setup: could not unlock Rust target dir")
95+
}()
96+
8797
// Builds the nss library.
8898
// #nosec:G204 - we control the command arguments in tests
8999
cmd := exec.Command(cargo, "build", "--features", strings.Join(features, ","), "--target-dir", target)
@@ -106,9 +116,10 @@ func BuildRustNSSLib(t *testing.T, disableCoverage bool, features ...string) (li
106116
// If the env is not set, the target stays the same.
107117
target = filepath.Join(target, os.Getenv("DEB_HOST_RUST_TYPE"))
108118

109-
// Creates a symlink for the compiled library with the expected versioned name.
110-
libPath = filepath.Join(target, "libnss_authd.so.2")
111-
if err = os.Symlink(filepath.Join(target, "debug", "libnss_authd.so"), libPath); err != nil {
119+
// Copy the library to a temporary directory, so that we can safely use it from there after unlocking
120+
// the target directory, which allows other tests to rebuild the library.
121+
libPath = filepath.Join(t.TempDir(), "libnss_authd.so.2")
122+
if err = fileutils.CopyFile(filepath.Join(target, "debug", "libnss_authd.so"), libPath); err != nil {
112123
require.ErrorIs(t, err, os.ErrExist, "Setup: failed to create versioned link to the library")
113124
}
114125

0 commit comments

Comments
 (0)