2018-02-05 16:05:59 -05:00
|
|
|
package ioutils // import "github.com/docker/docker/pkg/ioutils"
|
2016-04-20 20:08:47 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-08-09 14:55:17 -04:00
|
|
|
"runtime"
|
2016-04-20 20:08:47 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-08-09 14:55:17 -04:00
|
|
|
var (
|
|
|
|
testMode os.FileMode = 0640
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Windows does not support full Linux file mode
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
testMode = 0666
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-20 20:08:47 -04:00
|
|
|
func TestAtomicWriteToFile(t *testing.T) {
|
2021-08-24 06:10:50 -04:00
|
|
|
tmpDir, err := os.MkdirTemp("", "atomic-writers-test")
|
2016-04-20 20:08:47 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when creating temporary directory: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
expected := []byte("barbaz")
|
2016-08-09 14:55:17 -04:00
|
|
|
if err := AtomicWriteFile(filepath.Join(tmpDir, "foo"), expected, testMode); err != nil {
|
2016-04-20 20:08:47 -04:00
|
|
|
t.Fatalf("Error writing to file: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
actual, err := os.ReadFile(filepath.Join(tmpDir, "foo"))
|
2016-04-20 20:08:47 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error reading from file: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-03-30 05:26:16 -04:00
|
|
|
if !bytes.Equal(actual, expected) {
|
2016-04-20 20:08:47 -04:00
|
|
|
t.Fatalf("Data mismatch, expected %q, got %q", expected, actual)
|
|
|
|
}
|
2016-06-29 16:09:13 -04:00
|
|
|
|
|
|
|
st, err := os.Stat(filepath.Join(tmpDir, "foo"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error statting file: %v", err)
|
|
|
|
}
|
2019-08-06 20:38:51 -04:00
|
|
|
if expected := testMode; st.Mode() != expected {
|
2016-08-09 14:55:17 -04:00
|
|
|
t.Fatalf("Mode mismatched, expected %o, got %o", expected, st.Mode())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAtomicWriteSetCommit(t *testing.T) {
|
2021-08-24 06:10:50 -04:00
|
|
|
tmpDir, err := os.MkdirTemp("", "atomic-writerset-test")
|
2016-08-09 14:55:17 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when creating temporary directory: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
if err := os.Mkdir(filepath.Join(tmpDir, "tmp"), 0700); err != nil {
|
|
|
|
t.Fatalf("Error creating tmp directory: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
targetDir := filepath.Join(tmpDir, "target")
|
|
|
|
ws, err := NewAtomicWriteSet(filepath.Join(tmpDir, "tmp"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating atomic write set: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []byte("barbaz")
|
|
|
|
if err := ws.WriteFile("foo", expected, testMode); err != nil {
|
|
|
|
t.Fatalf("Error writing to file: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
if _, err := os.ReadFile(filepath.Join(targetDir, "foo")); err == nil {
|
2016-08-09 14:55:17 -04:00
|
|
|
t.Fatalf("Expected error reading file where should not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ws.Commit(targetDir); err != nil {
|
|
|
|
t.Fatalf("Error committing file: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
actual, err := os.ReadFile(filepath.Join(targetDir, "foo"))
|
2016-08-09 14:55:17 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error reading from file: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-03-30 05:26:16 -04:00
|
|
|
if !bytes.Equal(actual, expected) {
|
2016-08-09 14:55:17 -04:00
|
|
|
t.Fatalf("Data mismatch, expected %q, got %q", expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
st, err := os.Stat(filepath.Join(targetDir, "foo"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error statting file: %v", err)
|
|
|
|
}
|
2019-08-06 20:38:51 -04:00
|
|
|
if expected := testMode; st.Mode() != expected {
|
2016-06-29 16:09:13 -04:00
|
|
|
t.Fatalf("Mode mismatched, expected %o, got %o", expected, st.Mode())
|
|
|
|
}
|
2016-08-09 14:55:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAtomicWriteSetCancel(t *testing.T) {
|
2021-08-24 06:10:50 -04:00
|
|
|
tmpDir, err := os.MkdirTemp("", "atomic-writerset-test")
|
2016-08-09 14:55:17 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when creating temporary directory: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
if err := os.Mkdir(filepath.Join(tmpDir, "tmp"), 0700); err != nil {
|
|
|
|
t.Fatalf("Error creating tmp directory: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ws, err := NewAtomicWriteSet(filepath.Join(tmpDir, "tmp"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating atomic write set: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []byte("barbaz")
|
|
|
|
if err := ws.WriteFile("foo", expected, testMode); err != nil {
|
|
|
|
t.Fatalf("Error writing to file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ws.Cancel(); err != nil {
|
|
|
|
t.Fatalf("Error committing file: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
if _, err := os.ReadFile(filepath.Join(tmpDir, "target", "foo")); err == nil {
|
2016-08-09 14:55:17 -04:00
|
|
|
t.Fatalf("Expected error reading file where should not exist")
|
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
t.Fatalf("Unexpected error reading file: %s", err)
|
|
|
|
}
|
2016-04-20 20:08:47 -04:00
|
|
|
}
|