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