github.com/ttys3/engine@v17.12.1-ce-rc2+incompatible/pkg/system/utimes_unix_test.go (about)

     1  // +build linux freebsd
     2  
     3  package system
     4  
     5  import (
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"syscall"
    10  	"testing"
    11  )
    12  
    13  // prepareFiles creates files for testing in the temp directory
    14  func prepareFiles(t *testing.T) (string, string, string, string) {
    15  	dir, err := ioutil.TempDir("", "docker-system-test")
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  
    20  	file := filepath.Join(dir, "exist")
    21  	if err := ioutil.WriteFile(file, []byte("hello"), 0644); err != nil {
    22  		t.Fatal(err)
    23  	}
    24  
    25  	invalid := filepath.Join(dir, "doesnt-exist")
    26  
    27  	symlink := filepath.Join(dir, "symlink")
    28  	if err := os.Symlink(file, symlink); err != nil {
    29  		t.Fatal(err)
    30  	}
    31  
    32  	return file, invalid, symlink, dir
    33  }
    34  
    35  func TestLUtimesNano(t *testing.T) {
    36  	file, invalid, symlink, dir := prepareFiles(t)
    37  	defer os.RemoveAll(dir)
    38  
    39  	before, err := os.Stat(file)
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  
    44  	ts := []syscall.Timespec{{Sec: 0, Nsec: 0}, {Sec: 0, Nsec: 0}}
    45  	if err := LUtimesNano(symlink, ts); err != nil {
    46  		t.Fatal(err)
    47  	}
    48  
    49  	symlinkInfo, err := os.Lstat(symlink)
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  	if before.ModTime().Unix() == symlinkInfo.ModTime().Unix() {
    54  		t.Fatal("The modification time of the symlink should be different")
    55  	}
    56  
    57  	fileInfo, err := os.Stat(file)
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  	if before.ModTime().Unix() != fileInfo.ModTime().Unix() {
    62  		t.Fatal("The modification time of the file should be same")
    63  	}
    64  
    65  	if err := LUtimesNano(invalid, ts); err == nil {
    66  		t.Fatal("Doesn't return an error on a non-existing file")
    67  	}
    68  }