github.com/mfpierre/corectl@v0.5.6/Godeps/_workspace/src/golang.org/x/sys/unix/syscall_linux_test.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build linux
     6  
     7  package unix_test
     8  
     9  import (
    10  	"io/ioutil"
    11  	"os"
    12  	"testing"
    13  	"time"
    14  
    15  	"golang.org/x/sys/unix"
    16  )
    17  
    18  func TestTime(t *testing.T) {
    19  	var ut unix.Time_t
    20  	ut2, err := unix.Time(&ut)
    21  	if err != nil {
    22  		t.Fatalf("Time: %v", err)
    23  	}
    24  	if ut != ut2 {
    25  		t.Errorf("Time: return value %v should be equal to argument %v", ut2, ut)
    26  	}
    27  
    28  	var now time.Time
    29  
    30  	for i := 0; i < 10; i++ {
    31  		ut, err = unix.Time(nil)
    32  		if err != nil {
    33  			t.Fatalf("Time: %v", err)
    34  		}
    35  
    36  		now = time.Now()
    37  
    38  		if int64(ut) == now.Unix() {
    39  			return
    40  		}
    41  	}
    42  
    43  	t.Errorf("Time: return value %v should be nearly equal to time.Now().Unix() %v", ut, now.Unix())
    44  }
    45  
    46  func TestUtime(t *testing.T) {
    47  	defer chtmpdir(t)()
    48  
    49  	touch(t, "file1")
    50  
    51  	buf := &unix.Utimbuf{
    52  		Modtime: 12345,
    53  	}
    54  
    55  	err := unix.Utime("file1", buf)
    56  	if err != nil {
    57  		t.Fatalf("Utime: %v", err)
    58  	}
    59  
    60  	fi, err := os.Stat("file1")
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  
    65  	if fi.ModTime().Unix() != 12345 {
    66  		t.Errorf("Utime: failed to change modtime: expected %v, got %v", 12345, fi.ModTime().Unix())
    67  	}
    68  }
    69  
    70  func TestGetrlimit(t *testing.T) {
    71  	var rlim unix.Rlimit
    72  	err := unix.Getrlimit(unix.RLIMIT_AS, &rlim)
    73  	if err != nil {
    74  		t.Fatalf("Getrlimit: %v", err)
    75  	}
    76  }
    77  
    78  // utilities taken from os/os_test.go
    79  
    80  func touch(t *testing.T, name string) {
    81  	f, err := os.Create(name)
    82  	if err != nil {
    83  		t.Fatal(err)
    84  	}
    85  	if err := f.Close(); err != nil {
    86  		t.Fatal(err)
    87  	}
    88  }
    89  
    90  // chtmpdir changes the working directory to a new temporary directory and
    91  // provides a cleanup function. Used when PWD is read-only.
    92  func chtmpdir(t *testing.T) func() {
    93  	oldwd, err := os.Getwd()
    94  	if err != nil {
    95  		t.Fatalf("chtmpdir: %v", err)
    96  	}
    97  	d, err := ioutil.TempDir("", "test")
    98  	if err != nil {
    99  		t.Fatalf("chtmpdir: %v", err)
   100  	}
   101  	if err := os.Chdir(d); err != nil {
   102  		t.Fatalf("chtmpdir: %v", err)
   103  	}
   104  	return func() {
   105  		if err := os.Chdir(oldwd); err != nil {
   106  			t.Fatalf("chtmpdir: %v", err)
   107  		}
   108  		os.RemoveAll(d)
   109  	}
   110  }