Skip to content

Commit

Permalink
Use testing.T.TempDir()
Browse files Browse the repository at this point in the history
... to simplify tests.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
  • Loading branch information
mtrmac committed Mar 16, 2022
1 parent bb920c1 commit ef44aae
Show file tree
Hide file tree
Showing 22 changed files with 97 additions and 314 deletions.
4 changes: 1 addition & 3 deletions copy/sign_test.go
Expand Up @@ -39,9 +39,7 @@ func TestCreateSignature(t *testing.T) {
defer os.Unsetenv("GNUPGHOME")

// Signing a directory: reference, which does not have a DockerReference(), fails.
tempDir, err := ioutil.TempDir("", "signature-dir-dest")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()
dirRef, err := directory.NewReference(tempDir)
require.NoError(t, err)
dirDest, err := dirRef.NewImageDestination(context.Background(), nil)
Expand Down
14 changes: 4 additions & 10 deletions directory/directory_test.go
Expand Up @@ -18,7 +18,6 @@ import (

func TestDestinationReference(t *testing.T) {
ref, tmpDir := refToTempDir(t)
defer os.RemoveAll(tmpDir)

dest, err := ref.NewImageDestination(context.Background(), nil)
require.NoError(t, err)
Expand All @@ -28,8 +27,7 @@ func TestDestinationReference(t *testing.T) {
}

func TestGetPutManifest(t *testing.T) {
ref, tmpDir := refToTempDir(t)
defer os.RemoveAll(tmpDir)
ref, _ := refToTempDir(t)

man := []byte("test-manifest")
list := []byte("test-manifest-list")
Expand Down Expand Up @@ -64,8 +62,7 @@ func TestGetPutBlob(t *testing.T) {
providedBlob := []byte("provided-blob")
providedDigest := digest.Digest("sha256:provided-test-digest")

ref, tmpDir := refToTempDir(t)
defer os.RemoveAll(tmpDir)
ref, _ := refToTempDir(t)
cache := memory.New()

dest, err := ref.NewImageDestination(context.Background(), nil)
Expand Down Expand Up @@ -114,8 +111,7 @@ func TestPutBlobDigestFailure(t *testing.T) {
const digestErrorString = "Simulated digest error"
const blobDigest = digest.Digest("sha256:test-digest")

ref, tmpDir := refToTempDir(t)
defer os.RemoveAll(tmpDir)
ref, _ := refToTempDir(t)
dirRef, ok := ref.(dirReference)
require.True(t, ok)
blobPath := dirRef.layerPath(blobDigest)
Expand Down Expand Up @@ -153,8 +149,7 @@ func TestPutBlobDigestFailure(t *testing.T) {
}

func TestGetPutSignatures(t *testing.T) {
ref, tmpDir := refToTempDir(t)
defer os.RemoveAll(tmpDir)
ref, _ := refToTempDir(t)

man := []byte("test-manifest")
list := []byte("test-manifest-list")
Expand Down Expand Up @@ -201,7 +196,6 @@ func TestGetPutSignatures(t *testing.T) {

func TestSourceReference(t *testing.T) {
ref, tmpDir := refToTempDir(t)
defer os.RemoveAll(tmpDir)

src, err := ref.NewImageSource(context.Background(), nil)
require.NoError(t, err)
Expand Down
43 changes: 12 additions & 31 deletions directory/directory_transport_test.go
Expand Up @@ -50,9 +50,7 @@ func TestNewReference(t *testing.T) {

// testNewReference is a test shared for Transport.ParseReference and NewReference.
func testNewReference(t *testing.T, fn func(string) (types.ImageReference, error)) {
tmpDir, err := ioutil.TempDir("", "dir-transport-test")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
tmpDir := t.TempDir()

for _, path := range []string{
"/",
Expand All @@ -68,42 +66,35 @@ func testNewReference(t *testing.T, fn func(string) (types.ImageReference, error
assert.Equal(t, path, dirRef.path, path)
}

_, err = fn(tmpDir + "/thisparentdoesnotexist/something")
_, err := fn(tmpDir + "/thisparentdoesnotexist/something")
assert.Error(t, err)
}

// refToTempDir creates a temporary directory and returns a reference to it.
// The caller should
// defer os.RemoveAll(tmpDir)
func refToTempDir(t *testing.T) (ref types.ImageReference, tmpDir string) {
tmpDir, err := ioutil.TempDir("", "dir-transport-test")
require.NoError(t, err)
ref, err = NewReference(tmpDir)
func refToTempDir(t *testing.T) (types.ImageReference, string) {
tmpDir := t.TempDir()
ref, err := NewReference(tmpDir)
require.NoError(t, err)
return ref, tmpDir
}

func TestReferenceTransport(t *testing.T) {
ref, tmpDir := refToTempDir(t)
defer os.RemoveAll(tmpDir)
ref, _ := refToTempDir(t)
assert.Equal(t, Transport, ref.Transport())
}

func TestReferenceStringWithinTransport(t *testing.T) {
ref, tmpDir := refToTempDir(t)
defer os.RemoveAll(tmpDir)
assert.Equal(t, tmpDir, ref.StringWithinTransport())
}

func TestReferenceDockerReference(t *testing.T) {
ref, tmpDir := refToTempDir(t)
defer os.RemoveAll(tmpDir)
ref, _ := refToTempDir(t)
assert.Nil(t, ref.DockerReference())
}

func TestReferencePolicyConfigurationIdentity(t *testing.T) {
ref, tmpDir := refToTempDir(t)
defer os.RemoveAll(tmpDir)

assert.Equal(t, tmpDir, ref.PolicyConfigurationIdentity())
// A non-canonical path. Test just one, the various other cases are
Expand All @@ -120,7 +111,6 @@ func TestReferencePolicyConfigurationIdentity(t *testing.T) {

func TestReferencePolicyConfigurationNamespaces(t *testing.T) {
ref, tmpDir := refToTempDir(t)
defer os.RemoveAll(tmpDir)
// We don't really know enough to make a full equality test here.
ns := ref.PolicyConfigurationNamespaces()
require.NotNil(t, ns)
Expand Down Expand Up @@ -150,8 +140,7 @@ func TestReferencePolicyConfigurationNamespaces(t *testing.T) {
}

func TestReferenceNewImage(t *testing.T) {
ref, tmpDir := refToTempDir(t)
defer os.RemoveAll(tmpDir)
ref, _ := refToTempDir(t)

dest, err := ref.NewImageDestination(context.Background(), nil)
require.NoError(t, err)
Expand All @@ -169,8 +158,7 @@ func TestReferenceNewImage(t *testing.T) {
}

func TestReferenceNewImageNoValidManifest(t *testing.T) {
ref, tmpDir := refToTempDir(t)
defer os.RemoveAll(tmpDir)
ref, _ := refToTempDir(t)

dest, err := ref.NewImageDestination(context.Background(), nil)
require.NoError(t, err)
Expand All @@ -185,24 +173,21 @@ func TestReferenceNewImageNoValidManifest(t *testing.T) {
}

func TestReferenceNewImageSource(t *testing.T) {
ref, tmpDir := refToTempDir(t)
defer os.RemoveAll(tmpDir)
ref, _ := refToTempDir(t)
src, err := ref.NewImageSource(context.Background(), nil)
assert.NoError(t, err)
defer src.Close()
}

func TestReferenceNewImageDestination(t *testing.T) {
ref, tmpDir := refToTempDir(t)
defer os.RemoveAll(tmpDir)
ref, _ := refToTempDir(t)
dest, err := ref.NewImageDestination(context.Background(), nil)
assert.NoError(t, err)
defer dest.Close()
}

func TestReferenceDeleteImage(t *testing.T) {
ref, tmpDir := refToTempDir(t)
defer os.RemoveAll(tmpDir)
ref, _ := refToTempDir(t)
err := ref.DeleteImage(context.Background(), nil)
assert.Error(t, err)
}
Expand All @@ -211,7 +196,6 @@ func TestReferenceManifestPath(t *testing.T) {
dhex := digest.Digest("sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")

ref, tmpDir := refToTempDir(t)
defer os.RemoveAll(tmpDir)
dirRef, ok := ref.(dirReference)
require.True(t, ok)
assert.Equal(t, tmpDir+"/manifest.json", dirRef.manifestPath(nil))
Expand All @@ -222,7 +206,6 @@ func TestReferenceLayerPath(t *testing.T) {
const hex = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"

ref, tmpDir := refToTempDir(t)
defer os.RemoveAll(tmpDir)
dirRef, ok := ref.(dirReference)
require.True(t, ok)
assert.Equal(t, tmpDir+"/"+hex, dirRef.layerPath("sha256:"+hex))
Expand All @@ -232,7 +215,6 @@ func TestReferenceSignaturePath(t *testing.T) {
dhex := digest.Digest("sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")

ref, tmpDir := refToTempDir(t)
defer os.RemoveAll(tmpDir)
dirRef, ok := ref.(dirReference)
require.True(t, ok)
assert.Equal(t, tmpDir+"/signature-1", dirRef.signaturePath(0, nil))
Expand All @@ -243,7 +225,6 @@ func TestReferenceSignaturePath(t *testing.T) {

func TestReferenceVersionPath(t *testing.T) {
ref, tmpDir := refToTempDir(t)
defer os.RemoveAll(tmpDir)
dirRef, ok := ref.(dirReference)
require.True(t, ok)
assert.Equal(t, tmpDir+"/version", dirRef.versionPath())
Expand Down
7 changes: 2 additions & 5 deletions directory/explicitfilepath/path_test.go
Expand Up @@ -2,7 +2,6 @@ package explicitfilepath

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -125,12 +124,10 @@ func testPathsAreSameFile(t *testing.T, path1, path2, description string) {
}

func runPathResolvingTestCase(t *testing.T, f func(string) (string, error), c pathResolvingTestCase, suffix string) {
topDir, err := ioutil.TempDir("", "pathResolving")
require.NoError(t, err)
topDir := t.TempDir()
defer func() {
// Clean up after the "Unreadable directory" case; os.RemoveAll just fails.
// Clean up after the "Unreadable directory" case; os.RemoveAll just fails without this.
_ = os.Chmod(filepath.Join(topDir, "unreadable"), 0755) // Ignore errors, especially if this does not exist.
os.RemoveAll(topDir)
}()

input := c.setup(t, topDir) + suffix // Do not call filepath.Join() on input, it calls filepath.Clean() internally!
Expand Down
8 changes: 2 additions & 6 deletions docker/archive/transport_test.go
Expand Up @@ -251,9 +251,7 @@ func TestReferenceNewImageSource(t *testing.T) {
}

func TestReferenceNewImageDestination(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "docker-archive-test")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
tmpDir := t.TempDir()

ref, err := ParseReference(filepath.Join(tmpDir, "no-reference"))
require.NoError(t, err)
Expand All @@ -269,9 +267,7 @@ func TestReferenceNewImageDestination(t *testing.T) {
}

func TestReferenceDeleteImage(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "docker-archive-test")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
tmpDir := t.TempDir()

for i, suffix := range []string{"", ":some-reference", ":@0"} {
testFile := filepath.Join(tmpDir, fmt.Sprintf("file%d.tar", i))
Expand Down
12 changes: 3 additions & 9 deletions docker/lookaside_test.go
Expand Up @@ -29,9 +29,7 @@ func TestSignatureStorageBaseURL(t *testing.T) {

// No match found
// expect default user storage base
emptyDir, err := ioutil.TempDir("", "empty-dir")
require.NoError(t, err)
defer os.RemoveAll(emptyDir)
emptyDir := t.TempDir()
base, err := SignatureStorageBaseURL(&types.SystemContext{RegistriesDirPath: emptyDir},
dockerRefFromString(t, "//this/is/not/in/the:configuration"), false)
assert.NoError(t, err)
Expand All @@ -55,9 +53,7 @@ func TestRegistriesDirPath(t *testing.T) {
const nondefaultPath = "/this/is/not/the/default/registries.d"
const variableReference = "$HOME"
const rootPrefix = "/root/prefix"
tempHome, err := ioutil.TempDir("", "tempHome")
require.NoError(t, err)
defer os.RemoveAll(tempHome)
tempHome := t.TempDir()
var userRegistriesDir = filepath.FromSlash(".config/containers/registries.d")
userRegistriesDirPath := filepath.Join(tempHome, userRegistriesDir)
for _, c := range []struct {
Expand Down Expand Up @@ -115,9 +111,7 @@ func TestRegistriesDirPath(t *testing.T) {
}

func TestLoadAndMergeConfig(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "merge-config")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
tmpDir := t.TempDir()

// No registries.d exists
config, err := loadAndMergeConfig(filepath.Join(tmpDir, "thisdoesnotexist"))
Expand Down

0 comments on commit ef44aae

Please sign in to comment.