mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
pkg/system: use t.TempDir(), remove some test-utils
With t.TempDir(), some of the test-utilities became so small that it was more transparent to inline them. This also helps separating concenrs, as we're in the process of thinning out and decoupling some packages. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
7bd051eeec
commit
ab7bc6b7d2
6 changed files with 36 additions and 39 deletions
|
@ -2,6 +2,7 @@ package system // import "github.com/docker/docker/pkg/system"
|
|||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
@ -9,8 +10,10 @@ import (
|
|||
|
||||
// TestChtimesLinux tests Chtimes access time on a tempfile on Linux
|
||||
func TestChtimesLinux(t *testing.T) {
|
||||
file, dir := prepareTempFile(t)
|
||||
defer os.RemoveAll(dir)
|
||||
file := filepath.Join(t.TempDir(), "exist")
|
||||
if err := os.WriteFile(file, []byte("hello"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
beforeUnixEpochTime := unixEpochTime.Add(-100 * time.Second)
|
||||
afterUnixEpochTime := unixEpochTime.Add(100 * time.Second)
|
||||
|
|
|
@ -7,24 +7,12 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
// prepareTempFile creates a temporary file in a temporary directory.
|
||||
func prepareTempFile(t *testing.T) (string, string) {
|
||||
dir, err := os.MkdirTemp("", "docker-system-test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
file := filepath.Join(dir, "exist")
|
||||
if err := os.WriteFile(file, []byte("hello"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return file, dir
|
||||
}
|
||||
|
||||
// TestChtimes tests Chtimes on a tempfile. Test only mTime, because aTime is OS dependent
|
||||
func TestChtimes(t *testing.T) {
|
||||
file, dir := prepareTempFile(t)
|
||||
defer os.RemoveAll(dir)
|
||||
file := filepath.Join(t.TempDir(), "exist")
|
||||
if err := os.WriteFile(file, []byte("hello"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
beforeUnixEpochTime := unixEpochTime.Add(-100 * time.Second)
|
||||
afterUnixEpochTime := unixEpochTime.Add(100 * time.Second)
|
||||
|
|
|
@ -5,6 +5,7 @@ package system // import "github.com/docker/docker/pkg/system"
|
|||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
@ -12,8 +13,10 @@ import (
|
|||
|
||||
// TestChtimesWindows tests Chtimes access time on a tempfile on Windows
|
||||
func TestChtimesWindows(t *testing.T) {
|
||||
file, dir := prepareTempFile(t)
|
||||
defer os.RemoveAll(dir)
|
||||
file := filepath.Join(t.TempDir(), "exist")
|
||||
if err := os.WriteFile(file, []byte("hello"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
beforeUnixEpochTime := unixEpochTime.Add(-100 * time.Second)
|
||||
afterUnixEpochTime := unixEpochTime.Add(100 * time.Second)
|
||||
|
|
|
@ -5,13 +5,17 @@ package system // import "github.com/docker/docker/pkg/system"
|
|||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestLstat tests Lstat for existing and non existing files
|
||||
func TestLstat(t *testing.T) {
|
||||
file, invalid, _, dir := prepareFiles(t)
|
||||
defer os.RemoveAll(dir)
|
||||
tmpDir := t.TempDir()
|
||||
file := filepath.Join(tmpDir, "exist")
|
||||
if err := os.WriteFile(file, []byte("hello"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
statFile, err := Lstat(file)
|
||||
if err != nil {
|
||||
|
@ -21,7 +25,7 @@ func TestLstat(t *testing.T) {
|
|||
t.Fatal("returned empty stat for existing file")
|
||||
}
|
||||
|
||||
statInvalid, err := Lstat(invalid)
|
||||
statInvalid, err := Lstat(filepath.Join(tmpDir, "nosuchfile"))
|
||||
if err == nil {
|
||||
t.Fatal("did not return error for non-existing file")
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ package system // import "github.com/docker/docker/pkg/system"
|
|||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
|
@ -13,8 +14,10 @@ import (
|
|||
|
||||
// TestFromStatT tests fromStatT for a tempfile
|
||||
func TestFromStatT(t *testing.T) {
|
||||
file, _, _, dir := prepareFiles(t)
|
||||
defer os.RemoveAll(dir)
|
||||
file := filepath.Join(t.TempDir(), "exist")
|
||||
if err := os.WriteFile(file, []byte("hello"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
stat := &syscall.Stat_t{}
|
||||
err := syscall.Lstat(file, stat)
|
||||
|
|
|
@ -11,30 +11,26 @@ import (
|
|||
)
|
||||
|
||||
// prepareFiles creates files for testing in the temp directory
|
||||
func prepareFiles(t *testing.T) (string, string, string, string) {
|
||||
dir, err := os.MkdirTemp("", "docker-system-test")
|
||||
if err != nil {
|
||||
func prepareFiles(t *testing.T) (file, invalid, symlink string) {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
|
||||
file = filepath.Join(dir, "exist")
|
||||
if err := os.WriteFile(file, []byte("hello"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
file := filepath.Join(dir, "exist")
|
||||
if err := os.WriteFile(file, []byte("hello"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
invalid := filepath.Join(dir, "doesnt-exist")
|
||||
|
||||
symlink := filepath.Join(dir, "symlink")
|
||||
invalid = filepath.Join(dir, "doesnt-exist")
|
||||
symlink = filepath.Join(dir, "symlink")
|
||||
if err := os.Symlink(file, symlink); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
return file, invalid, symlink, dir
|
||||
return file, invalid, symlink
|
||||
}
|
||||
|
||||
func TestLUtimesNano(t *testing.T) {
|
||||
file, invalid, symlink, dir := prepareFiles(t)
|
||||
defer os.RemoveAll(dir)
|
||||
file, invalid, symlink := prepareFiles(t)
|
||||
|
||||
before, err := os.Stat(file)
|
||||
if err != nil {
|
||||
|
|
Loading…
Add table
Reference in a new issue