Skip to content

Commit

Permalink
Merge pull request containerd#6681 from Juneezee/test/t.TempDir
Browse files Browse the repository at this point in the history
  • Loading branch information
fuweid committed Mar 16, 2022
2 parents df41bf8 + 52d307a commit 79d7df7
Show file tree
Hide file tree
Showing 40 changed files with 136 additions and 431 deletions.
7 changes: 1 addition & 6 deletions archive/compression/compression_test.go
Expand Up @@ -122,10 +122,7 @@ func TestCompressDecompressUncompressed(t *testing.T) {

func TestDetectPigz(t *testing.T) {
// Create fake PATH with unpigz executable, make sure detectPigz can find it
tempPath, err := os.MkdirTemp("", "containerd_temp_")
if err != nil {
t.Fatal(err)
}
tempPath := t.TempDir()

filename := "unpigz"
if runtime.GOOS == "windows" {
Expand All @@ -138,8 +135,6 @@ func TestDetectPigz(t *testing.T) {
t.Fatal(err)
}

defer os.RemoveAll(tempPath)

oldPath := os.Getenv("PATH")
os.Setenv("PATH", tempPath)
defer os.Setenv("PATH", oldPath)
Expand Down
6 changes: 1 addition & 5 deletions archive/issues_test.go
Expand Up @@ -36,11 +36,7 @@ func TestPrefixHeaderReadable(t *testing.T) {
// https://gist.github.com/stevvooe/e2a790ad4e97425896206c0816e1a882#file-out-go
var testFile = []byte("\x1f\x8b\x08\x08\x44\x21\x68\x59\x00\x03\x74\x2e\x74\x61\x72\x00\x4b\xcb\xcf\x67\xa0\x35\x30\x80\x00\x86\x06\x10\x47\x01\xc1\x37\x40\x00\x54\xb6\xb1\xa1\xa9\x99\x09\x48\x25\x1d\x40\x69\x71\x49\x62\x91\x02\xe5\x76\xa1\x79\x84\x21\x91\xd6\x80\x72\xaf\x8f\x82\x51\x30\x0a\x46\x36\x00\x00\xf0\x1c\x1e\x95\x00\x06\x00\x00")

tmpDir, err := os.MkdirTemp("", "prefix-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
tmpDir := t.TempDir()

r, err := compression.DecompressStream(bytes.NewReader(testFile))
if err != nil {
Expand Down
12 changes: 2 additions & 10 deletions archive/tar_linux_test.go
Expand Up @@ -36,11 +36,7 @@ import (
func TestOverlayApply(t *testing.T) {
testutil.RequiresRoot(t)

base, err := os.MkdirTemp("", "test-ovl-diff-apply-")
if err != nil {
t.Fatalf("unable to create temp dir: %+v", err)
}
defer os.RemoveAll(base)
base := t.TempDir()

if err := overlayutils.Supported(base); err != nil {
t.Skipf("skipping because overlay is not supported %v", err)
Expand All @@ -55,11 +51,7 @@ func TestOverlayApply(t *testing.T) {
func TestOverlayApplyNoParents(t *testing.T) {
testutil.RequiresRoot(t)

base, err := os.MkdirTemp("", "test-ovl-diff-apply-")
if err != nil {
t.Fatalf("unable to create temp dir: %+v", err)
}
defer os.RemoveAll(base)
base := t.TempDir()

if err := overlayutils.Supported(base); err != nil {
t.Skipf("skipping because overlay is not supported %v", err)
Expand Down
77 changes: 18 additions & 59 deletions archive/tar_test.go
Expand Up @@ -23,6 +23,7 @@ import (
"archive/tar"
"bytes"
"context"
_ "crypto/sha256"
"errors"
"fmt"
"io"
Expand All @@ -31,8 +32,6 @@ import (
"testing"
"time"

_ "crypto/sha256"

"github.com/containerd/containerd/archive/tartest"
"github.com/containerd/containerd/pkg/testutil"
"github.com/containerd/continuity/fs"
Expand All @@ -58,15 +57,15 @@ var baseApplier = fstest.Apply(
func TestUnpack(t *testing.T) {
requireTar(t)

if err := testApply(baseApplier); err != nil {
if err := testApply(t, baseApplier); err != nil {
t.Fatalf("Test apply failed: %+v", err)
}
}

func TestBaseDiff(t *testing.T) {
requireTar(t)

if err := testBaseDiff(baseApplier); err != nil {
if err := testBaseDiff(t, baseApplier); err != nil {
t.Fatalf("Test base diff failed: %+v", err)
}
}
Expand Down Expand Up @@ -102,7 +101,7 @@ func TestRelativeSymlinks(t *testing.T) {
}

for _, bo := range breakoutLinks {
if err := testDiffApply(bo); err != nil {
if err := testDiffApply(t, bo); err != nil {
t.Fatalf("Test apply failed: %+v", err)
}
}
Expand Down Expand Up @@ -179,7 +178,7 @@ func TestSymlinks(t *testing.T) {
}

for i, l := range links {
if err := testDiffApply(l[0], l[1]); err != nil {
if err := testDiffApply(t, l[0], l[1]); err != nil {
t.Fatalf("Test[%d] apply failed: %+v", i+1, err)
}
}
Expand Down Expand Up @@ -243,11 +242,7 @@ func TestBreakouts(t *testing.T) {
return nil
}
errFileDiff := errors.New("files differ")
td, err := os.MkdirTemp("", "test-breakouts-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(td)
td := t.TempDir()

isSymlinkFile := func(f string) func(string) error {
return func(root string) error {
Expand Down Expand Up @@ -841,17 +836,9 @@ func TestApplyTar(t *testing.T) {
}
}

func testApply(a fstest.Applier) error {
td, err := os.MkdirTemp("", "test-apply-")
if err != nil {
return fmt.Errorf("failed to create temp dir: %w", err)
}
defer os.RemoveAll(td)
dest, err := os.MkdirTemp("", "test-apply-dest-")
if err != nil {
return fmt.Errorf("failed to create temp dir: %w", err)
}
defer os.RemoveAll(dest)
func testApply(t *testing.T, a fstest.Applier) error {
td := t.TempDir()
dest := t.TempDir()

if err := a.Apply(td); err != nil {
return fmt.Errorf("failed to apply filesystem changes: %w", err)
Expand Down Expand Up @@ -882,17 +869,9 @@ func testApply(a fstest.Applier) error {
return fstest.CheckDirectoryEqual(td, dest)
}

func testBaseDiff(a fstest.Applier) error {
td, err := os.MkdirTemp("", "test-base-diff-")
if err != nil {
return fmt.Errorf("failed to create temp dir: %w", err)
}
defer os.RemoveAll(td)
dest, err := os.MkdirTemp("", "test-base-diff-dest-")
if err != nil {
return fmt.Errorf("failed to create temp dir: %w", err)
}
defer os.RemoveAll(dest)
func testBaseDiff(t *testing.T, a fstest.Applier) error {
td := t.TempDir()
dest := t.TempDir()

if err := a.Apply(td); err != nil {
return fmt.Errorf("failed to apply filesystem changes: %w", err)
Expand All @@ -909,17 +888,9 @@ func testBaseDiff(a fstest.Applier) error {
return fstest.CheckDirectoryEqual(td, dest)
}

func testDiffApply(appliers ...fstest.Applier) error {
td, err := os.MkdirTemp("", "test-diff-apply-")
if err != nil {
return fmt.Errorf("failed to create temp dir: %w", err)
}
defer os.RemoveAll(td)
dest, err := os.MkdirTemp("", "test-diff-apply-dest-")
if err != nil {
return fmt.Errorf("failed to create temp dir: %w", err)
}
defer os.RemoveAll(dest)
func testDiffApply(t *testing.T, appliers ...fstest.Applier) error {
td := t.TempDir()
dest := t.TempDir()

for _, a := range appliers {
if err := a.Apply(td); err != nil {
Expand Down Expand Up @@ -950,11 +921,7 @@ func testDiffApply(appliers ...fstest.Applier) error {

func makeWriterToTarTest(wt tartest.WriterToTar, a fstest.Applier, validate func(string) error, applyErr error) func(*testing.T) {
return func(t *testing.T) {
td, err := os.MkdirTemp("", "test-writer-to-tar-")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(td)
td := t.TempDir()

if a != nil {
if err := a.Apply(td); err != nil {
Expand Down Expand Up @@ -1254,20 +1221,12 @@ func whiteoutEntry(name string) tarEntryValidator {

func makeDiffTarTest(validators []tarEntryValidator, a, b fstest.Applier) func(*testing.T) {
return func(t *testing.T) {
ad, err := os.MkdirTemp("", "test-make-diff-tar-")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer os.RemoveAll(ad)
ad := t.TempDir()
if err := a.Apply(ad); err != nil {
t.Fatalf("failed to apply a: %v", err)
}

bd, err := os.MkdirTemp("", "test-make-diff-tar-")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer os.RemoveAll(bd)
bd := t.TempDir()
if err := fs.CopyDir(bd, ad); err != nil {
t.Fatalf("failed to copy dir: %v", err)
}
Expand Down
4 changes: 1 addition & 3 deletions cio/io_test.go
Expand Up @@ -49,9 +49,7 @@ func TestNewFIFOSetInDir(t *testing.T) {
t.Skip("NewFIFOSetInDir has different behaviour on windows")
}

root, err := os.MkdirTemp("", "test-new-fifo-set")
assert.NilError(t, err)
defer os.RemoveAll(root)
root := t.TempDir()

fifos, err := NewFIFOSetInDir(root, "theid", true)
assert.NilError(t, err)
Expand Down
8 changes: 1 addition & 7 deletions cio/io_unix_test.go
Expand Up @@ -21,8 +21,6 @@ package cio

import (
"context"
"fmt"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -65,11 +63,7 @@ func TestOpenFifosWithTerminal(t *testing.T) {
var ctx, cancel = context.WithCancel(context.Background())
defer cancel()

ioFifoDir, err := os.MkdirTemp("", fmt.Sprintf("cio-%s", t.Name()))
if err != nil {
t.Fatalf("unexpected error during creating temp dir: %v", err)
}
defer os.RemoveAll(ioFifoDir)
ioFifoDir := t.TempDir()

cfg := Config{
Stdout: filepath.Join(ioFifoDir, "test-stdout"),
Expand Down
27 changes: 4 additions & 23 deletions content/local/store_test.go
Expand Up @@ -291,28 +291,17 @@ func populateBlobStore(ctx context.Context, t checker, cs content.Store, nblobs,
return blobs
}

func contentStoreEnv(t checker) (context.Context, string, content.Store, func()) {
pc, _, _, ok := runtime.Caller(1)
if !ok {
t.Fatal("failed to resolve caller")
}
fn := runtime.FuncForPC(pc)

tmpdir, err := os.MkdirTemp("", filepath.Base(fn.Name())+"-")
if err != nil {
t.Fatal(err)
}
func contentStoreEnv(t testing.TB) (context.Context, string, content.Store, func()) {
tmpdir := t.TempDir()

cs, err := NewStore(tmpdir)
if err != nil {
os.RemoveAll(tmpdir)
t.Fatal(err)
}

ctx, cancel := context.WithCancel(context.Background())
return ctx, tmpdir, cs, func() {
cancel()
os.RemoveAll(tmpdir)
}
}

Expand Down Expand Up @@ -361,11 +350,7 @@ func checkWrite(ctx context.Context, t checker, cs content.Store, dgst digest.Di
}

func TestWriterTruncateRecoversFromIncompleteWrite(t *testing.T) {
tmpdir, err := os.MkdirTemp("", "test-local-content-store-recover")
assert.NilError(t, err)
defer os.RemoveAll(tmpdir)

cs, err := NewStore(tmpdir)
cs, err := NewStore(t.TempDir())
assert.NilError(t, err)

ctx, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -400,11 +385,7 @@ func setupIncompleteWrite(ctx context.Context, t *testing.T, cs content.Store, r
}

func TestWriteReadEmptyFileTimestamp(t *testing.T) {
root, err := os.MkdirTemp("", "test-write-read-file-timestamp")
if err != nil {
t.Errorf("failed to create a tmp dir: %v", err)
}
defer os.RemoveAll(root)
root := t.TempDir()

emptyFile := filepath.Join(root, "updatedat")
if err := writeTimestampFile(emptyFile, time.Time{}); err != nil {
Expand Down
4 changes: 1 addition & 3 deletions integration/addition_gids_test.go
Expand Up @@ -31,9 +31,7 @@ import (
)

func TestAdditionalGids(t *testing.T) {
testPodLogDir, err := os.MkdirTemp("", "additional-gids")
require.NoError(t, err)
defer os.RemoveAll(testPodLogDir)
testPodLogDir := t.TempDir()

t.Log("Create a sandbox with log directory")
sb, sbConfig := PodSandboxConfigWithCleanup(t, "sandbox", "additional-gids",
Expand Down
6 changes: 1 addition & 5 deletions integration/client/container_checkpoint_test.go
Expand Up @@ -455,11 +455,7 @@ func TestCheckpointRestoreWithImagePath(t *testing.T) {
}

// create image path store criu image files
crDir, err := os.MkdirTemp("", "test-cr")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(crDir)
crDir := t.TempDir()
imagePath := filepath.Join(crDir, "cr")
// checkpoint task
if _, err := task.Checkpoint(ctx, WithCheckpointImagePath(imagePath)); err != nil {
Expand Down
10 changes: 1 addition & 9 deletions integration/client/daemon_config_linux_test.go
Expand Up @@ -35,15 +35,7 @@ import (

// TestDaemonRuntimeRoot ensures plugin.linux.runtime_root is not ignored
func TestDaemonRuntimeRoot(t *testing.T) {
runtimeRoot, err := os.MkdirTemp("", "containerd-test-runtime-root")
if err != nil {
t.Fatal(err)
}
defer func() {
if err != nil {
os.RemoveAll(runtimeRoot)
}
}()
runtimeRoot := t.TempDir()
configTOML := `
version = 2
[plugins]
Expand Down
16 changes: 4 additions & 12 deletions integration/client/restart_monitor_test.go
Expand Up @@ -49,22 +49,14 @@ func newDaemonWithConfig(t *testing.T, configTOML string) (*Client, *daemon, fun
buf = bytes.NewBuffer(nil)
)

tempDir, err := os.MkdirTemp("", "containerd-test-new-daemon-with-config")
if err != nil {
t.Fatal(err)
}
defer func() {
if err != nil {
os.RemoveAll(tempDir)
}
}()
tempDir := t.TempDir()

configTOMLFile := filepath.Join(tempDir, "config.toml")
if err = os.WriteFile(configTOMLFile, []byte(configTOML), 0600); err != nil {
if err := os.WriteFile(configTOMLFile, []byte(configTOML), 0600); err != nil {
t.Fatal(err)
}

if err = srvconfig.LoadConfig(configTOMLFile, &configTOMLDecoded); err != nil {
if err := srvconfig.LoadConfig(configTOMLFile, &configTOMLDecoded); err != nil {
t.Fatal(err)
}

Expand All @@ -83,7 +75,7 @@ func newDaemonWithConfig(t *testing.T, configTOML string) (*Client, *daemon, fun
if configTOMLDecoded.State == "" {
args = append(args, "--state", filepath.Join(tempDir, "state"))
}
if err = ctrd.start("containerd", address, args, buf, buf); err != nil {
if err := ctrd.start("containerd", address, args, buf, buf); err != nil {
t.Fatalf("%v: %s", err, buf.String())
}

Expand Down

0 comments on commit 79d7df7

Please sign in to comment.