github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/pkg/system/chtimes_test.go (about)

     1  package system // import "github.com/demonoid81/moby/pkg/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  	unixMaxTime := maxTime
    34  
    35  	// Test both aTime and mTime set to Unix Epoch
    36  	Chtimes(file, unixEpochTime, unixEpochTime)
    37  
    38  	f, err := os.Stat(file)
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  
    43  	if f.ModTime() != unixEpochTime {
    44  		t.Fatalf("Expected: %s, got: %s", unixEpochTime, f.ModTime())
    45  	}
    46  
    47  	// Test aTime before Unix Epoch and mTime set to Unix Epoch
    48  	Chtimes(file, beforeUnixEpochTime, unixEpochTime)
    49  
    50  	f, err = os.Stat(file)
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  
    55  	if f.ModTime() != unixEpochTime {
    56  		t.Fatalf("Expected: %s, got: %s", unixEpochTime, f.ModTime())
    57  	}
    58  
    59  	// Test aTime set to Unix Epoch and mTime before Unix Epoch
    60  	Chtimes(file, unixEpochTime, beforeUnixEpochTime)
    61  
    62  	f, err = os.Stat(file)
    63  	if err != nil {
    64  		t.Fatal(err)
    65  	}
    66  
    67  	if f.ModTime() != unixEpochTime {
    68  		t.Fatalf("Expected: %s, got: %s", unixEpochTime, f.ModTime())
    69  	}
    70  
    71  	// Test both aTime and mTime set to after Unix Epoch (valid time)
    72  	Chtimes(file, afterUnixEpochTime, afterUnixEpochTime)
    73  
    74  	f, err = os.Stat(file)
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  
    79  	if f.ModTime() != afterUnixEpochTime {
    80  		t.Fatalf("Expected: %s, got: %s", afterUnixEpochTime, f.ModTime())
    81  	}
    82  
    83  	// Test both aTime and mTime set to Unix max time
    84  	Chtimes(file, unixMaxTime, unixMaxTime)
    85  
    86  	f, err = os.Stat(file)
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  
    91  	if f.ModTime().Truncate(time.Second) != unixMaxTime.Truncate(time.Second) {
    92  		t.Fatalf("Expected: %s, got: %s", unixMaxTime.Truncate(time.Second), f.ModTime().Truncate(time.Second))
    93  	}
    94  }