github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/daemon/logger/loggerutils/file_windows_test.go (about)

     1  package loggerutils
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"gotest.tools/v3/assert"
    10  )
    11  
    12  func TestOpenFileDelete(t *testing.T) {
    13  	tmpDir := t.TempDir()
    14  	f, err := openFile(filepath.Join(tmpDir, "test.txt"), os.O_CREATE|os.O_RDWR, 644)
    15  	assert.NilError(t, err)
    16  	defer f.Close()
    17  
    18  	assert.NilError(t, os.RemoveAll(f.Name()))
    19  }
    20  
    21  func TestOpenFileRename(t *testing.T) {
    22  	tmpDir := t.TempDir()
    23  	f, err := openFile(filepath.Join(tmpDir, "test.txt"), os.O_CREATE|os.O_RDWR, 0644)
    24  	assert.NilError(t, err)
    25  	defer f.Close()
    26  
    27  	assert.NilError(t, os.Rename(f.Name(), f.Name()+"renamed"))
    28  }
    29  
    30  func TestUnlinkOpenFile(t *testing.T) {
    31  	tmpDir := t.TempDir()
    32  	name := filepath.Join(tmpDir, "test.txt")
    33  	f, err := openFile(name, os.O_CREATE|os.O_RDWR, 0644)
    34  	assert.NilError(t, err)
    35  	defer func() { assert.NilError(t, f.Close()) }()
    36  
    37  	_, err = io.WriteString(f, "first")
    38  	assert.NilError(t, err)
    39  
    40  	assert.NilError(t, unlink(name))
    41  	f2, err := openFile(name, os.O_CREATE|os.O_RDWR, 0644)
    42  	assert.NilError(t, err)
    43  	defer func() { assert.NilError(t, f2.Close()) }()
    44  
    45  	_, err = io.WriteString(f2, "second")
    46  	assert.NilError(t, err)
    47  
    48  	_, err = f.Seek(0, io.SeekStart)
    49  	assert.NilError(t, err)
    50  	fdata, err := io.ReadAll(f)
    51  	assert.NilError(t, err)
    52  	assert.Check(t, "first" == string(fdata))
    53  
    54  	_, err = f2.Seek(0, io.SeekStart)
    55  	assert.NilError(t, err)
    56  	f2data, err := io.ReadAll(f2)
    57  	assert.NilError(t, err)
    58  	assert.Check(t, "second" == string(f2data))
    59  }