github.com/rawahars/moby@v24.0.4+incompatible/pkg/system/chtimes_test.go (about) 1 package system // import "github.com/docker/docker/pkg/system" 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 "time" 8 ) 9 10 // TestChtimesModTime tests Chtimes on a tempfile. Test only mTime, because 11 // aTime is OS dependent. 12 func TestChtimesModTime(t *testing.T) { 13 file := filepath.Join(t.TempDir(), "exist") 14 if err := os.WriteFile(file, []byte("hello"), 0o644); err != nil { 15 t.Fatal(err) 16 } 17 18 beforeUnixEpochTime := unixEpochTime.Add(-100 * time.Second) 19 afterUnixEpochTime := unixEpochTime.Add(100 * time.Second) 20 21 // Test both aTime and mTime set to Unix Epoch 22 t.Run("both aTime and mTime set to Unix Epoch", func(t *testing.T) { 23 if err := Chtimes(file, unixEpochTime, unixEpochTime); err != nil { 24 t.Error(err) 25 } 26 27 f, err := os.Stat(file) 28 if err != nil { 29 t.Fatal(err) 30 } 31 32 if f.ModTime() != unixEpochTime { 33 t.Fatalf("Expected: %s, got: %s", unixEpochTime, f.ModTime()) 34 } 35 }) 36 37 // Test aTime before Unix Epoch and mTime set to Unix Epoch 38 t.Run("aTime before Unix Epoch and mTime set to Unix Epoch", func(t *testing.T) { 39 if err := Chtimes(file, beforeUnixEpochTime, unixEpochTime); err != nil { 40 t.Error(err) 41 } 42 43 f, err := os.Stat(file) 44 if err != nil { 45 t.Fatal(err) 46 } 47 48 if f.ModTime() != unixEpochTime { 49 t.Fatalf("Expected: %s, got: %s", unixEpochTime, f.ModTime()) 50 } 51 }) 52 53 // Test aTime set to Unix Epoch and mTime before Unix Epoch 54 t.Run("aTime set to Unix Epoch and mTime before Unix Epoch", func(t *testing.T) { 55 if err := Chtimes(file, unixEpochTime, beforeUnixEpochTime); err != nil { 56 t.Error(err) 57 } 58 59 f, err := os.Stat(file) 60 if err != nil { 61 t.Fatal(err) 62 } 63 64 if f.ModTime() != unixEpochTime { 65 t.Fatalf("Expected: %s, got: %s", unixEpochTime, f.ModTime()) 66 } 67 }) 68 69 // Test both aTime and mTime set to after Unix Epoch (valid time) 70 t.Run("both aTime and mTime set to after Unix Epoch (valid time)", func(t *testing.T) { 71 if err := Chtimes(file, afterUnixEpochTime, afterUnixEpochTime); err != nil { 72 t.Error(err) 73 } 74 75 f, err := os.Stat(file) 76 if err != nil { 77 t.Fatal(err) 78 } 79 80 if f.ModTime() != afterUnixEpochTime { 81 t.Fatalf("Expected: %s, got: %s", afterUnixEpochTime, f.ModTime()) 82 } 83 }) 84 85 // Test both aTime and mTime set to Unix max time 86 t.Run("both aTime and mTime set to Unix max time", func(t *testing.T) { 87 if err := Chtimes(file, unixMaxTime, unixMaxTime); err != nil { 88 t.Error(err) 89 } 90 91 f, err := os.Stat(file) 92 if err != nil { 93 t.Fatal(err) 94 } 95 96 if f.ModTime().Truncate(time.Second) != unixMaxTime.Truncate(time.Second) { 97 t.Fatalf("Expected: %s, got: %s", unixMaxTime.Truncate(time.Second), f.ModTime().Truncate(time.Second)) 98 } 99 }) 100 }