Use t.Cleanup in fileFromFixture

... to simplify users.

Should not change test behavior.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
This commit is contained in:
Miloslav Trmač 2025-07-08 21:36:51 +02:00
parent 4dafc99bd6
commit 5d2cb67fb1
2 changed files with 3 additions and 7 deletions

View File

@ -745,7 +745,6 @@ func (s *copySuite) TestCopySignatures() {
dirDest := "dir:" + dir dirDest := "dir:" + dir
policy := fileFromFixture(t, "fixtures/policy.json", map[string]string{"@keydir@": s.gpgHome}) policy := fileFromFixture(t, "fixtures/policy.json", map[string]string{"@keydir@": s.gpgHome})
defer os.Remove(policy)
// type: reject // type: reject
assertSkopeoFails(t, fmt.Sprintf(".*Source image rejected: Running image %s:latest is rejected by policy.*", testFQIN), assertSkopeoFails(t, fmt.Sprintf(".*Source image rejected: Running image %s:latest is rejected by policy.*", testFQIN),
@ -809,7 +808,6 @@ func (s *copySuite) TestCopyDirSignatures() {
// Note the "/@dirpath@": The value starts with a slash so that it is not rejected in other tests which do not replace it, // Note the "/@dirpath@": The value starts with a slash so that it is not rejected in other tests which do not replace it,
// but we must ensure that the result is a canonical path, not something starting with a "//". // but we must ensure that the result is a canonical path, not something starting with a "//".
policy := fileFromFixture(t, "fixtures/policy.json", map[string]string{"@keydir@": s.gpgHome, "/@dirpath@": topDir + "/restricted"}) policy := fileFromFixture(t, "fixtures/policy.json", map[string]string{"@keydir@": s.gpgHome, "/@dirpath@": topDir + "/restricted"})
defer os.Remove(policy)
// Get some images. // Get some images.
assertSkopeoSucceeds(t, "", "copy", "--retry-times", "3", testFQIN+":armfh", topDirDest+"/dir1") assertSkopeoSucceeds(t, "", "copy", "--retry-times", "3", testFQIN+":armfh", topDirDest+"/dir1")
@ -917,7 +915,6 @@ func (s *copySuite) TestCopyDockerLookaside() {
defer splitLookasideReadServer.Close() defer splitLookasideReadServer.Close()
policy := fileFromFixture(t, "fixtures/policy.json", map[string]string{"@keydir@": s.gpgHome}) policy := fileFromFixture(t, "fixtures/policy.json", map[string]string{"@keydir@": s.gpgHome})
defer os.Remove(policy)
registriesDir := filepath.Join(tmpDir, "registries.d") registriesDir := filepath.Join(tmpDir, "registries.d")
err = os.Mkdir(registriesDir, 0755) err = os.Mkdir(registriesDir, 0755)
require.NoError(t, err) require.NoError(t, err)
@ -978,7 +975,6 @@ func (s *copySuite) TestCopyAtomicExtension() {
registriesDir := filepath.Join(topDir, "registries.d") registriesDir := filepath.Join(topDir, "registries.d")
dirDest := "dir:" + topDir dirDest := "dir:" + topDir
policy := fileFromFixture(t, "fixtures/policy.json", map[string]string{"@keydir@": s.gpgHome}) policy := fileFromFixture(t, "fixtures/policy.json", map[string]string{"@keydir@": s.gpgHome})
defer os.Remove(policy)
// Get an image to work with to an atomic: destination. Also verifies that we can use Docker repositories without X-Registry-Supports-Signatures // Get an image to work with to an atomic: destination. Also verifies that we can use Docker repositories without X-Registry-Supports-Signatures
assertSkopeoSucceeds(t, "", "--tls-verify=false", "--registries.d", registriesDir, "copy", "--retry-times", "3", assertSkopeoSucceeds(t, "", "--tls-verify=false", "--registries.d", registriesDir, "copy", "--retry-times", "3",
@ -1036,7 +1032,6 @@ func (s *copySuite) TestCopyVerifyingMirroredSignatures() {
dirDest := "dir:" + filepath.Join(topDir, "unused-dest") dirDest := "dir:" + filepath.Join(topDir, "unused-dest")
policy := fileFromFixture(t, "fixtures/policy.json", map[string]string{"@keydir@": s.gpgHome}) policy := fileFromFixture(t, "fixtures/policy.json", map[string]string{"@keydir@": s.gpgHome})
defer os.Remove(policy)
// We use X-R-S-S for this testing to avoid having to deal with the lookasides. // We use X-R-S-S for this testing to avoid having to deal with the lookasides.
// A downside is that OpenShift records signatures per image, so the error messages below // A downside is that OpenShift records signatures per image, so the error messages below

View File

@ -174,8 +174,8 @@ func modifyEnviron(env []string, name, value string) []string {
return append(res, prefix+value) return append(res, prefix+value)
} }
// fileFromFixture applies edits to inputPath and returns a path to the temporary file. // fileFromFixture applies edits to inputPath and returns a path to the temporary file with the edits,
// Callers should defer os.Remove(the_returned_path) // which will be automatically removed when the test completes.
func fileFromFixture(t *testing.T, inputPath string, edits map[string]string) string { func fileFromFixture(t *testing.T, inputPath string, edits map[string]string) string {
contents, err := os.ReadFile(inputPath) contents, err := os.ReadFile(inputPath)
require.NoError(t, err) require.NoError(t, err)
@ -188,6 +188,7 @@ func fileFromFixture(t *testing.T, inputPath string, edits map[string]string) st
file, err := os.CreateTemp("", "policy.json") file, err := os.CreateTemp("", "policy.json")
require.NoError(t, err) require.NoError(t, err)
path := file.Name() path := file.Name()
t.Cleanup(func() { os.Remove(path) })
_, err = file.Write(contents) _, err = file.Write(contents)
require.NoError(t, err) require.NoError(t, err)