Skip to content

Commit 8e6def4

Browse files
author
github-actions
committed
Merge branch 'main' into cras
2 parents 47619cc + 2fcb7ac commit 8e6def4

32 files changed

+117
-257
lines changed

.golangci.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ linters:
4444
- reassign
4545
- revive
4646
- staticcheck
47-
- tenv
4847
- typecheck
4948
- unparam
5049
- unused
50+
- usetesting
5151
- whitespace
5252

5353
linters-settings:
@@ -70,5 +70,13 @@ issues:
7070
# G204 disallows exec.Command() with a command/args stored in variables
7171
# G306 disallows creation of files with permissions greater than 0600
7272
text: "^(G107|G204|G306):"
73+
- path: 'e2e/'
74+
linters:
75+
# The usetesting linter shows a lot of lint for env var handling and
76+
# tempdir creation in e2e tests. It's not really valid lint - the e2e
77+
# tests were explicitly written to keep tempdirs in a per-run outer dir,
78+
# and env var handling is complicated by the e2e.SingularityCmd
79+
# execution flow etc.
80+
- usetesting
7381
exclude-files:
7482
- "internal/pkg/util/user/cgo_lookup_unix.go"

cmd/internal/cli/key_newpair_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2021-2022, Sylabs Inc. All rights reserved.
1+
// Copyright (c) 2021-2025, Sylabs Inc. All rights reserved.
22
// This software is licensed under a 3-clause BSD license. Please consult the
33
// LICENSE.md file distributed with the sources of this project regarding your
44
// rights to use or distribute this software.
@@ -61,14 +61,13 @@ func Test_collectInput_flags(t *testing.T) {
6161
}
6262

6363
func TestCollectInput(t *testing.T) {
64-
tf, err := os.CreateTemp("", "collect-test-")
64+
tf, err := os.CreateTemp(t.TempDir(), "collect-test-")
6565
assert.NilError(t, err)
6666
defer tf.Close()
6767

6868
oldStdin := os.Stdin
6969
defer func(ostdin *os.File) {
7070
os.Stdin = ostdin
71-
os.Remove(tf.Name())
7271
}(oldStdin)
7372
os.Stdin = tf
7473

internal/app/singularity/remote_add_test.go

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2019-2022, Sylabs Inc. All rights reserved.
1+
// Copyright (c) 2019-2025, Sylabs Inc. All rights reserved.
22
// This software is licensed under a 3-clause BSD license. Please consult the
33
// LICENSE.md file distributed with the sources of this project regarding your
44
// rights to use or distribute this software.
@@ -7,6 +7,7 @@ package singularity
77

88
import (
99
"os"
10+
"path/filepath"
1011
"testing"
1112

1213
"github.com/sylabs/singularity/v4/internal/pkg/remote"
@@ -23,12 +24,7 @@ const (
2324
)
2425

2526
func createInvalidCfgFile(t *testing.T) string {
26-
f, err := os.CreateTemp("", "")
27-
if err != nil {
28-
t.Fatalf("cannot create temporary configuration file for testing: %s\n", err)
29-
}
30-
31-
path := f.Name()
27+
path := filepath.Join(t.TempDir(), "invalid.yml")
3228

3329
// Set an invalid configuration
3430
type aDummyStruct struct {
@@ -40,24 +36,18 @@ func createInvalidCfgFile(t *testing.T) string {
4036

4137
yaml, err := yaml.Marshal(cfg)
4238
if err != nil {
43-
f.Close()
44-
os.Remove(path)
4539
t.Fatalf("cannot marshal YAML: %s\n", err)
4640
}
4741

48-
f.Write(yaml)
49-
f.Close()
42+
if err := os.WriteFile(path, yaml, 0o644); err != nil {
43+
t.Fatal(err)
44+
}
5045

5146
return path
5247
}
5348

5449
func createValidCfgFile(t *testing.T) string {
55-
f, err := os.CreateTemp("", "")
56-
if err != nil {
57-
t.Fatalf("cannot create temporary configuration file for testing: %s\n", err)
58-
}
59-
60-
path := f.Name()
50+
path := filepath.Join(t.TempDir(), "valid.yml")
6151

6252
// Set a valid configuration
6353
cfg := remote.Config{
@@ -76,13 +66,12 @@ func createValidCfgFile(t *testing.T) string {
7666

7767
yaml, err := yaml.Marshal(cfg)
7868
if err != nil {
79-
f.Close()
80-
os.Remove(path)
8169
t.Fatalf("cannot marshal YAML: %s\n", err)
8270
}
8371

84-
f.Write(yaml)
85-
f.Close()
72+
if err := os.WriteFile(path, yaml, 0o644); err != nil {
73+
t.Fatal(err)
74+
}
8675

8776
return path
8877
}

internal/pkg/build/sources/conveyorPacker_local_test.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2018-2024, Sylabs Inc. All rights reserved.
1+
// Copyright (c) 2018-2025, Sylabs Inc. All rights reserved.
22
// This software is licensed under a 3-clause BSD license. Please consult the
33
// LICENSE.md file distributed with the sources of this project regarding your
44
// rights to use or distribute this software.
@@ -20,12 +20,7 @@ import (
2020

2121
func TestLocalPackerSquashfs(t *testing.T) {
2222
require.Command(t, "mksquashfs")
23-
24-
tempDirPath, err := os.MkdirTemp("", "test-localpacker-squashfs")
25-
if err != nil {
26-
t.Fatalf("while creating temp dir: %v", err)
27-
}
28-
defer os.RemoveAll(tempDirPath)
23+
tempDirPath := t.TempDir()
2924

3025
// Image root directory
3126
rootfs := filepath.Join(tempDirPath, "issue_3084_rootfs")
@@ -65,9 +60,7 @@ func TestLocalPackerSquashfs(t *testing.T) {
6560
defer os.Remove(image)
6661

6762
// Creates bundle
68-
bundleTmp, _ := os.MkdirTemp("", "bundle-temp-*")
69-
defer os.RemoveAll(bundleTmp)
70-
63+
bundleTmp := t.TempDir()
7164
b, err := types.NewBundle(tempDirPath, bundleTmp)
7265
if err != nil {
7366
t.Fatalf("while creating bundle: %v", err)

internal/pkg/cgroups/manager_linux_v1_test.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2018-2022, Sylabs Inc. All rights reserved.
1+
// Copyright (c) 2018-2025, Sylabs Inc. All rights reserved.
22
// This software is licensed under a 3-clause BSD license. Please consult the
33
// LICENSE.md file distributed with the sources of this project regarding your
44
// rights to use or distribute this software.
@@ -118,20 +118,13 @@ func testNewUpdateV1(t *testing.T, systemd bool) {
118118

119119
// Write a new config with [pids] limit = 512
120120
content := []byte("[pids]\nlimit = 512")
121-
tmpfile, err := os.CreateTemp("", "cgroups")
122-
if err != nil {
123-
t.Fatalf("While creating update file: %v", err)
124-
}
125-
defer os.Remove(tmpfile.Name())
126-
if _, err := tmpfile.Write(content); err != nil {
121+
tmpfile := filepath.Join(t.TempDir(), "cgroups")
122+
if err := os.WriteFile(tmpfile, content, 0o644); err != nil {
127123
t.Fatalf("While writing update file: %v", err)
128124
}
129-
if err := tmpfile.Close(); err != nil {
130-
t.Fatalf("While closing update file: %v", err)
131-
}
132125

133126
// Update existing cgroup from new config
134-
if err := manager.UpdateFromFile(tmpfile.Name()); err != nil {
127+
if err := manager.UpdateFromFile(tmpfile); err != nil {
135128
t.Fatalf("While updating cgroup: %v", err)
136129
}
137130

internal/pkg/cgroups/manager_linux_v2_test.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2021-2022, Sylabs Inc. All rights reserved.
1+
// Copyright (c) 2021-2025, Sylabs Inc. All rights reserved.
22
// This software is licensed under a 3-clause BSD license. Please consult the
33
// LICENSE.md file distributed with the sources of this project regarding your
44
// rights to use or distribute this software.
@@ -109,20 +109,13 @@ func testNewUpdateV2(t *testing.T, systemd bool) {
109109

110110
// Write a new config with [pids] limit = 512
111111
content := []byte("[pids]\nlimit = 512")
112-
tmpfile, err := os.CreateTemp("", "cgroups")
113-
if err != nil {
114-
t.Fatalf("While creating update file: %v", err)
115-
}
116-
defer os.Remove(tmpfile.Name())
117-
if _, err := tmpfile.Write(content); err != nil {
112+
tmpfile := filepath.Join(t.TempDir(), "cgroups")
113+
if err := os.WriteFile(tmpfile, content, 0o644); err != nil {
118114
t.Fatalf("While writing update file: %v", err)
119115
}
120-
if err := tmpfile.Close(); err != nil {
121-
t.Fatalf("While closing update file: %v", err)
122-
}
123116

124117
// Update existing cgroup from new config
125-
if err := manager.UpdateFromFile(tmpfile.Name()); err != nil {
118+
if err := manager.UpdateFromFile(tmpfile); err != nil {
126119
t.Fatalf("While updating cgroup: %v", err)
127120
}
128121

internal/pkg/image/unpacker/squashfs_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2020, Control Command Inc. All rights reserved.
2-
// Copyright (c) 2019-2022, Sylabs Inc. All rights reserved.
2+
// Copyright (c) 2019-2025, Sylabs Inc. All rights reserved.
33
// This software is licensed under a 3-clause BSD license. Please consult the
44
// LICENSE.md file distributed with the sources of this project regarding your
55
// rights to use or distribute this software.
@@ -19,7 +19,7 @@ func createArchive(t *testing.T) *os.File {
1919
if err != nil {
2020
t.SkipNow()
2121
}
22-
f, err := os.CreateTemp("", "archive-")
22+
f, err := os.CreateTemp(t.TempDir(), "archive-")
2323
if err != nil {
2424
t.Fatal(err)
2525
}
@@ -56,15 +56,14 @@ func testSquashfs(t *testing.T, tmpParent string) {
5656
t.Skip("unsquashfs not found")
5757
}
5858

59-
dir, err := os.MkdirTemp(tmpParent, "test-squashfs-")
59+
dir, err := os.MkdirTemp(tmpParent, "test-squashfs-") //nolint:usetesting
6060
if err != nil {
6161
t.Fatalf("while creating tmpdir: %v", err)
6262
}
6363
defer os.RemoveAll(dir)
6464

6565
// create archive with files present in this directory
6666
archive := createArchive(t)
67-
defer os.Remove(archive.Name())
6867

6968
savedPath := s.UnsquashfsPath
7069

internal/pkg/instance/logger_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2019-2022, Sylabs Inc. All rights reserved.
1+
// Copyright (c) 2019-2025, Sylabs Inc. All rights reserved.
22
// This software is licensed under a 3-clause BSD license. Please consult the
33
// LICENSE.md file distributed with the sources of this project regarding your
44
// rights to use or distribute this software.
@@ -8,6 +8,7 @@ package instance
88
import (
99
"bytes"
1010
"os"
11+
"path/filepath"
1112
"testing"
1213

1314
"github.com/sylabs/singularity/v4/internal/pkg/test"
@@ -72,12 +73,7 @@ func TestLogger(t *testing.T) {
7273
},
7374
}
7475

75-
logfile, err := os.CreateTemp("", "log-")
76-
if err != nil {
77-
t.Errorf("failed to create temporary log file: %s", err)
78-
}
79-
filename := logfile.Name()
80-
logfile.Close()
76+
filename := filepath.Join(t.TempDir(), "log")
8177

8278
for _, f := range formatTest {
8379
logger, err := NewLogger(filename, f.formatter)

internal/pkg/runtime/launcher/oci/mounts_linux_test.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2022-2023, Sylabs Inc. All rights reserved.
1+
// Copyright (c) 2022-2025, Sylabs Inc. All rights reserved.
22
// This software is licensed under a 3-clause BSD license. Please consult the
33
// LICENSE.md file distributed with the sources of this project regarding your
44
// rights to use or distribute this software.
@@ -501,16 +501,7 @@ func ptsFlags(t *testing.T) []string {
501501
}
502502

503503
func TestLauncher_addLibrariesMounts(t *testing.T) {
504-
tmpDir, err := os.MkdirTemp("", "add-libraries-mounts")
505-
if err != nil {
506-
t.Fatal(err)
507-
}
508-
t.Cleanup(func() {
509-
if !t.Failed() {
510-
os.RemoveAll(tmpDir)
511-
}
512-
})
513-
504+
tmpDir := t.TempDir()
514505
lib1 := filepath.Join(tmpDir, "lib1.so")
515506
lib2 := filepath.Join(tmpDir, "lib2.so")
516507
libInvalid := filepath.Join(tmpDir, "invalid")

internal/pkg/security/seccomp/seccomp_supported_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2018-2022, Sylabs Inc. All rights reserved.
1+
// Copyright (c) 2018-2025, Sylabs Inc. All rights reserved.
22
// This software is licensed under a 3-clause BSD license. Please consult the
33
// LICENSE.md file distributed with the sources of this project regarding your
44
// rights to use or distribute this software.
@@ -41,13 +41,10 @@ func defaultProfile() *specs.LinuxSeccomp {
4141
}
4242

4343
func testFchmod(t *testing.T) {
44-
tmpfile, err := os.CreateTemp("", "chmod_file")
44+
tmpfile, err := os.CreateTemp(t.TempDir(), "chmod_file-")
4545
if err != nil {
4646
t.Fatal(err)
4747
}
48-
file := tmpfile.Name()
49-
50-
defer os.Remove(file)
5148
defer tmpfile.Close()
5249

5350
if hasConditionSupport() {

0 commit comments

Comments
 (0)