github.com/coreos/mantle@v0.13.0/system/anonfile_linux_test.go (about)

     1  // Copyright 2015 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package system
    16  
    17  import (
    18  	"io/ioutil"
    19  	"os"
    20  	"path/filepath"
    21  	"syscall"
    22  	"testing"
    23  )
    24  
    25  func TestAnonymousFile(t *testing.T) {
    26  	tmp, err := ioutil.TempDir("", "")
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	defer os.RemoveAll(tmp)
    31  
    32  	anon, err := AnonymousFile(tmp)
    33  	if IsOpNotSupported(err) {
    34  		t.Skip("O_TMPFILE not supported")
    35  	} else if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	defer anon.Close()
    39  
    40  	info, err := ioutil.ReadDir(tmp)
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  	if len(info) != 0 {
    45  		t.Errorf("%s not empty: %v", tmp, info)
    46  	}
    47  
    48  	name := filepath.Join(tmp, "name")
    49  	if err := LinkFile(anon, name); err != nil {
    50  		t.Errorf("Link failed: %v", err)
    51  	}
    52  
    53  	info, err = ioutil.ReadDir(tmp)
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  	if len(info) != 1 || info[0].Name() != "name" {
    58  		t.Errorf("%s has unexpected contents: %v", tmp, info)
    59  	}
    60  }
    61  
    62  func TestLinkFile(t *testing.T) {
    63  	tmp, err := ioutil.TempDir("", "")
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	defer os.RemoveAll(tmp)
    68  
    69  	orig, err := ioutil.TempFile(tmp, "")
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  	defer orig.Close()
    74  
    75  	info, err := ioutil.ReadDir(tmp)
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  	if len(info) != 1 || info[0].Name() != filepath.Base(orig.Name()) {
    80  		t.Fatalf("%s has unexpected contents: %v", tmp, info)
    81  	}
    82  
    83  	// LinkFile while orig still exists should work
    84  	if err := LinkFile(orig, filepath.Join(tmp, "name1")); err != nil {
    85  		t.Errorf("Link failed: %v", err)
    86  	}
    87  
    88  	if err := os.Remove(orig.Name()); err != nil {
    89  		t.Fatal(err)
    90  	}
    91  
    92  	// name1 is keeping orig alive so this still works
    93  	if err := LinkFile(orig, filepath.Join(tmp, "name2")); err != nil {
    94  		t.Errorf("Link failed: %v", err)
    95  	}
    96  
    97  	if err := os.Remove(filepath.Join(tmp, "name1")); err != nil {
    98  		t.Fatal(err)
    99  	}
   100  	if err := os.Remove(filepath.Join(tmp, "name2")); err != nil {
   101  		t.Fatal(err)
   102  	}
   103  
   104  	// LinkFile after orig is removed doesn't work which is a
   105  	// difference between how normal files and O_TMPFILE works.
   106  	if err := LinkFile(orig, filepath.Join(tmp, "name3")); err == nil {
   107  		t.Error("Linking to removed file unexpectedly worked!")
   108  	}
   109  }
   110  
   111  func TestPrivateFile(t *testing.T) {
   112  	tmp, err := ioutil.TempDir("", "")
   113  	if err != nil {
   114  		t.Fatal(err)
   115  	}
   116  	defer os.RemoveAll(tmp)
   117  
   118  	priv, err := PrivateFile(tmp)
   119  	if err != nil {
   120  		// Travis is an unfun stick in the mud and gives us
   121  		// an ancient system lacking O_TMPFILE support.
   122  		if oserr, ok := err.(*os.PathError); ok {
   123  			if errno, ok := oserr.Err.(syscall.Errno); ok {
   124  				if errno == syscall.EOPNOTSUPP {
   125  					t.Skip("O_TMPFILE not supported")
   126  				}
   127  			}
   128  		}
   129  		t.Fatal(err)
   130  	}
   131  	defer priv.Close()
   132  
   133  	info, err := ioutil.ReadDir(tmp)
   134  	if err != nil {
   135  		t.Fatal(err)
   136  	}
   137  	if len(info) != 0 {
   138  		t.Errorf("%s not empty: %v", tmp, info)
   139  	}
   140  
   141  	if err := LinkFile(priv, filepath.Join(tmp, "name")); err == nil {
   142  		t.Error("Linking to private file unexpectedly worked!")
   143  	}
   144  }