Skip to content

Commit

Permalink
Simplify tests by testing.TempDir instead of os.MkdirTemp
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksandr Redko <[email protected]>
  • Loading branch information
alexandear committed Jan 5, 2024
1 parent 8a44f84 commit 7bd1b77
Show file tree
Hide file tree
Showing 23 changed files with 135 additions and 322 deletions.
3 changes: 1 addition & 2 deletions libimage/corrupted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import (

func TestCorruptedLayers(t *testing.T) {
// Regression tests for https://bugzilla.redhat.com/show_bug.cgi?id=1966872.
runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()
pullOptions := &PullOptions{}
pullOptions.Writer = os.Stdout
Expand Down
18 changes: 6 additions & 12 deletions libimage/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ func TestFilterReference(t *testing.T) {
busyboxLatest := "quay.io/libpod/busybox:latest"
alpineLatest := "quay.io/libpod/alpine:latest"

runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()

pullOptions := &PullOptions{}
Expand Down Expand Up @@ -93,8 +92,7 @@ func TestFilterDigest(t *testing.T) {
busyboxLatest := "quay.io/libpod/busybox:latest"
alpineLatest := "quay.io/libpod/alpine:latest"

runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()

pullOptions := &PullOptions{}
Expand Down Expand Up @@ -136,8 +134,7 @@ func TestFilterID(t *testing.T) {
busyboxLatest := "quay.io/libpod/busybox:latest"
alpineLatest := "quay.io/libpod/alpine:latest"

runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()

pullOptions := &PullOptions{}
Expand Down Expand Up @@ -175,8 +172,7 @@ func TestFilterManifest(t *testing.T) {
busyboxLatest := "quay.io/libpod/busybox:latest"
alpineLatest := "quay.io/libpod/alpine:latest"

runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()

pullOptions := &PullOptions{}
Expand Down Expand Up @@ -229,8 +225,7 @@ func TestFilterAfterSinceBeforeUntil(t *testing.T) {
busyboxLatest := "quay.io/libpod/busybox:latest"
alpineLatest := "quay.io/libpod/alpine:latest"

runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()

pullOptions := &PullOptions{}
Expand Down Expand Up @@ -282,8 +277,7 @@ func TestFilterIdLabel(t *testing.T) {
busyboxLatest := "quay.io/libpod/busybox:latest"
alpineLatest := "quay.io/libpod/alpine:latest"

runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()

pullOptions := &PullOptions{}
Expand Down
3 changes: 1 addition & 2 deletions libimage/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
)

func TestHistory(t *testing.T) {
runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()

name := "quay.io/libpod/alpine:3.10.2"
Expand Down
24 changes: 9 additions & 15 deletions libimage/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ func TestImageFunctions(t *testing.T) {
busyboxLatest := busybox + ":latest"
busyboxDigest := busybox + "@"

runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()

// Looking up image by invalid sha.
Expand Down Expand Up @@ -190,8 +189,7 @@ func TestLookupImage(t *testing.T) {
alpineNoTag := "quay.io/libpod/alpine"
alpineLatest := alpineNoTag + ":latest"

runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()

pullOptions := &PullOptions{}
Expand Down Expand Up @@ -245,8 +243,7 @@ func TestLookupImage(t *testing.T) {
}

func TestInspectHealthcheck(t *testing.T) {
runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()

imageName := "quay.io/libpod/healthcheck:config-only"
Expand All @@ -265,8 +262,7 @@ func TestInspectHealthcheck(t *testing.T) {
}

func TestTag(t *testing.T) {
runtime, image, cleanup := getImageAndRuntime(t)
defer cleanup()
runtime, image := getImageAndRuntime(t)

digest := "sha256:adab3844f497ab9171f070d4cae4114b5aec565ac772e2f2579405b78be67c96"

Expand Down Expand Up @@ -303,8 +299,7 @@ func TestTag(t *testing.T) {
}

func TestTagAndUntagParallel(t *testing.T) {
runtime, image, cleanup := getImageAndRuntime(t)
defer cleanup()
runtime, image := getImageAndRuntime(t)

tagCount := 10
wg := sync.WaitGroup{}
Expand Down Expand Up @@ -354,8 +349,7 @@ func TestTagAndUntagParallel(t *testing.T) {
}

func TestUntag(t *testing.T) {
runtime, image, cleanup := getImageAndRuntime(t)
defer cleanup()
runtime, image := getImageAndRuntime(t)

digest := "sha256:adab3844f497ab9171f070d4cae4114b5aec565ac772e2f2579405b78be67c96"

Expand Down Expand Up @@ -394,12 +388,12 @@ func TestUntag(t *testing.T) {
require.ErrorIs(t, err, errUntagDigest, "check for specific digest error")
}

func getImageAndRuntime(t *testing.T) (*Runtime, *Image, func()) {
func getImageAndRuntime(t *testing.T) (*Runtime, *Image) {
// Note: this will resolve pull from the GCR registry (see
// testdata/registries.conf).
busyboxLatest := "docker.io/library/busybox:latest"

runtime, cleanup := testNewRuntime(t)
runtime := testNewRuntime(t)
ctx := context.Background()

pullOptions := &PullOptions{}
Expand All @@ -410,5 +404,5 @@ func getImageAndRuntime(t *testing.T) (*Runtime, *Image, func()) {

image := pulledImages[0]

return runtime, image, cleanup
return runtime, image
}
3 changes: 1 addition & 2 deletions libimage/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
)

func TestImport(t *testing.T) {
runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()

importOptions := &ImportOptions{}
Expand Down
3 changes: 1 addition & 2 deletions libimage/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ func TestLoad(t *testing.T) {
os.Unsetenv("TMPDIR")
}()

runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()
loadOptions := &LoadOptions{}
loadOptions.Writer = os.Stdout
Expand Down
12 changes: 4 additions & 8 deletions libimage/manifest_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import (
)

func TestCreateManifestList(t *testing.T) {
runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()

list, err := runtime.CreateManifestList("mylist")
Expand Down Expand Up @@ -44,8 +43,7 @@ func TestCreateManifestList(t *testing.T) {
// Inspect must contain both formats i.e OCIv1 and docker
func TestInspectManifestListWithAnnotations(t *testing.T) {
listName := "testinspect"
runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()

list, err := runtime.CreateManifestList(listName)
Expand Down Expand Up @@ -84,8 +82,7 @@ func TestInspectManifestListWithAnnotations(t *testing.T) {
func TestCreateAndTagManifestList(t *testing.T) {
tagName := "testlisttagged"
listName := "testlist"
runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()

list, err := runtime.CreateManifestList(listName)
Expand Down Expand Up @@ -121,8 +118,7 @@ func TestCreateAndTagManifestList(t *testing.T) {
func TestCreateAndRemoveManifestList(t *testing.T) {
tagName := "manifestlisttagged"
listName := "manifestlist"
runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()

list, err := runtime.CreateManifestList(listName)
Expand Down
22 changes: 4 additions & 18 deletions libimage/manifests/manifests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"testing"
"time"
Expand Down Expand Up @@ -50,10 +49,7 @@ func TestSaveLoad(t *testing.T) {
t.Skip("Test can only run as root")
}

dir, err := os.MkdirTemp("", "manifests")
assert.Nilf(t, err, "error creating temporary directory")
defer os.RemoveAll(dir)

dir := t.TempDir()
storeOptions := storage.StoreOptions{
GraphRoot: filepath.Join(dir, "root"),
RunRoot: filepath.Join(dir, "runroot"),
Expand Down Expand Up @@ -168,10 +164,7 @@ func TestReference(t *testing.T) {
}
ctx := context.Background()

dir, err := os.MkdirTemp("", "manifests")
assert.Nilf(t, err, "error creating temporary directory")
defer os.RemoveAll(dir)

dir := t.TempDir()
storeOptions := storage.StoreOptions{
GraphRoot: filepath.Join(dir, "root"),
RunRoot: filepath.Join(dir, "runroot"),
Expand Down Expand Up @@ -268,10 +261,7 @@ func TestPushManifest(t *testing.T) {
}
ctx := context.Background()

dir, err := os.MkdirTemp("", "manifests")
assert.Nilf(t, err, "error creating temporary directory")
defer os.RemoveAll(dir)

dir := t.TempDir()
storeOptions := storage.StoreOptions{
GraphRoot: filepath.Join(dir, "root"),
RunRoot: filepath.Join(dir, "runroot"),
Expand All @@ -288,11 +278,7 @@ func TestPushManifest(t *testing.T) {
}
}()

dest, err := os.MkdirTemp("", "manifests")
assert.Nilf(t, err, "error creating temporary directory")
defer os.RemoveAll(dest)

destRef, err := alltransports.ParseImageName(fmt.Sprintf("dir:%s", dest))
destRef, err := alltransports.ParseImageName(fmt.Sprintf("dir:%s", t.TempDir()))
assert.Nilf(t, err, "ParseImageName()")

ref, err := alltransports.ParseImageName(otherListImage)
Expand Down
19 changes: 7 additions & 12 deletions libimage/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import (
)

func TestPull(t *testing.T) {
runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()
pullOptions := &PullOptions{}
pullOptions.Writer = os.Stdout
Expand Down Expand Up @@ -81,8 +80,8 @@ func TestPull(t *testing.T) {
}

func TestPullPlatforms(t *testing.T) {
runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)

ctx := context.Background()
pullOptions := &PullOptions{}
pullOptions.Writer = os.Stdout
Expand Down Expand Up @@ -141,8 +140,7 @@ func TestPullPlatforms(t *testing.T) {
}

func TestPullPlatformsWithEmptyRegistriesConf(t *testing.T) {
runtime, cleanup := testNewRuntime(t, testNewRuntimeOptions{registriesConfPath: "/dev/null"})
defer cleanup()
runtime := testNewRuntime(t, testNewRuntimeOptions{registriesConfPath: "/dev/null"})
ctx := context.Background()
pullOptions := &PullOptions{}
pullOptions.Writer = os.Stdout
Expand Down Expand Up @@ -171,8 +169,7 @@ func TestPullPlatformsWithEmptyRegistriesConf(t *testing.T) {
}

func TestPullPolicy(t *testing.T) {
runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()
pullOptions := &PullOptions{}

Expand All @@ -191,8 +188,7 @@ func TestPullPolicy(t *testing.T) {

func TestShortNameAndIDconflict(t *testing.T) {
// Regression test for https://github.com/containers/podman/issues/12761
runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()
pullOptions := &PullOptions{}
pullOptions.Writer = os.Stdout
Expand Down Expand Up @@ -228,8 +224,7 @@ func TestPullOCINoReference(t *testing.T) {
// specified reference is preserved in the image name.

busybox := "docker.io/library/busybox:latest"
runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()
pullOptions := &PullOptions{}
pullOptions.Writer = os.Stdout
Expand Down
13 changes: 4 additions & 9 deletions libimage/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import (
)

func TestPush(t *testing.T) {
runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()

// Prefetch alpine.
Expand All @@ -29,9 +28,7 @@ func TestPush(t *testing.T) {
pushOptions := &PushOptions{}
pushOptions.Writer = os.Stdout

workdir, err := os.MkdirTemp("", "libimagepush")
require.NoError(t, err)
defer os.RemoveAll(workdir)
workdir := t.TempDir()

for _, test := range []struct {
source string
Expand Down Expand Up @@ -77,8 +74,7 @@ func TestPush(t *testing.T) {
}

func TestPushOtherPlatform(t *testing.T) {
runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()

// Prefetch alpine.
Expand All @@ -104,8 +100,7 @@ func TestPushOtherPlatform(t *testing.T) {
}

func TestPushWithForceCompression(t *testing.T) {
runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()

// Prefetch alpine.
Expand Down
3 changes: 1 addition & 2 deletions libimage/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ func TestRemoveImages(t *testing.T) {
// testdata/registries.conf).
busyboxLatest := "docker.io/library/busybox:latest"

runtime, cleanup := testNewRuntime(t)
defer cleanup()
runtime := testNewRuntime(t)
ctx := context.Background()

pullOptions := &PullOptions{}
Expand Down
Loading

0 comments on commit 7bd1b77

Please sign in to comment.