github.com/lmars/docker@v1.6.0-rc2/pkg/system/utimes_test.go (about)

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