github.com/damirazo/docker@v1.9.0/pkg/system/chtimes_test.go (about) 1 package system 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "testing" 8 "time" 9 ) 10 11 // prepareTempFile creates a temporary file in a temporary directory. 12 func prepareTempFile(t *testing.T) (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 return file, dir 23 } 24 25 // TestChtimes tests Chtimes on a tempfile. Test only mTime, because aTime is OS dependent 26 func TestChtimes(t *testing.T) { 27 file, dir := prepareTempFile(t) 28 defer os.RemoveAll(dir) 29 30 beforeUnixEpochTime := time.Unix(0, 0).Add(-100 * time.Second) 31 unixEpochTime := time.Unix(0, 0) 32 afterUnixEpochTime := time.Unix(100, 0) 33 // The max Unix time is 33 bits set 34 unixMaxTime := unixEpochTime.Add((1<<33 - 1) * time.Second) 35 afterUnixMaxTime := unixMaxTime.Add(100 * time.Second) 36 37 // Test both aTime and mTime set to Unix Epoch 38 Chtimes(file, unixEpochTime, unixEpochTime) 39 40 f, err := os.Stat(file) 41 if err != nil { 42 t.Fatal(err) 43 } 44 45 if f.ModTime() != unixEpochTime { 46 t.Fatalf("Expected: %s, got: %s", unixEpochTime, f.ModTime()) 47 } 48 49 // Test aTime before Unix Epoch and mTime set to Unix Epoch 50 Chtimes(file, beforeUnixEpochTime, unixEpochTime) 51 52 f, err = os.Stat(file) 53 if err != nil { 54 t.Fatal(err) 55 } 56 57 if f.ModTime() != unixEpochTime { 58 t.Fatalf("Expected: %s, got: %s", unixEpochTime, f.ModTime()) 59 } 60 61 // Test aTime set to Unix Epoch and mTime before Unix Epoch 62 Chtimes(file, unixEpochTime, beforeUnixEpochTime) 63 64 f, err = os.Stat(file) 65 if err != nil { 66 t.Fatal(err) 67 } 68 69 if f.ModTime() != unixEpochTime { 70 t.Fatalf("Expected: %s, got: %s", unixEpochTime, f.ModTime()) 71 } 72 73 // Test both aTime and mTime set to after Unix Epoch (valid time) 74 Chtimes(file, afterUnixEpochTime, afterUnixEpochTime) 75 76 f, err = os.Stat(file) 77 if err != nil { 78 t.Fatal(err) 79 } 80 81 if f.ModTime() != afterUnixEpochTime { 82 t.Fatalf("Expected: %s, got: %s", afterUnixEpochTime, f.ModTime()) 83 } 84 85 // Test both aTime and mTime set to Unix max time 86 Chtimes(file, unixMaxTime, unixMaxTime) 87 88 f, err = os.Stat(file) 89 if err != nil { 90 t.Fatal(err) 91 } 92 93 if f.ModTime() != unixMaxTime { 94 t.Fatalf("Expected: %s, got: %s", unixMaxTime, f.ModTime()) 95 } 96 97 // Test aTime after Unix max time and mTime set to Unix max time 98 Chtimes(file, afterUnixMaxTime, unixMaxTime) 99 100 f, err = os.Stat(file) 101 if err != nil { 102 t.Fatal(err) 103 } 104 105 if f.ModTime() != unixMaxTime { 106 t.Fatalf("Expected: %s, got: %s", unixMaxTime, f.ModTime()) 107 } 108 109 // Test aTime set to Unix Epoch and mTime before Unix Epoch 110 Chtimes(file, unixMaxTime, afterUnixMaxTime) 111 112 f, err = os.Stat(file) 113 if err != nil { 114 t.Fatal(err) 115 } 116 117 if f.ModTime() != unixEpochTime { 118 t.Fatalf("Expected: %s, got: %s", unixEpochTime, f.ModTime()) 119 } 120 }