github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/pkg/fileutil/fileutil_test.go (about)

     1  // Copyright 2015 The rkt Authors
     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 fileutil
    16  
    17  import (
    18  	"io/ioutil"
    19  	"os"
    20  	"path/filepath"
    21  	"testing"
    22  
    23  	"github.com/coreos/rkt/pkg/uid"
    24  )
    25  
    26  const tstprefix = "fileutil-test"
    27  
    28  func touch(t *testing.T, name string) {
    29  	f, err := os.Create(name)
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	if err := f.Close(); err != nil {
    34  		t.Fatal(err)
    35  	}
    36  }
    37  
    38  type tree struct {
    39  	path string
    40  	dir  bool
    41  }
    42  
    43  func createTree(t *testing.T, dir string, tr []tree) {
    44  	for _, f := range tr {
    45  		if f.dir {
    46  			if err := os.MkdirAll(filepath.Join(dir, f.path), 0755); err != nil {
    47  				t.Fatal(err)
    48  			}
    49  		} else {
    50  			touch(t, filepath.Join(dir, f.path))
    51  		}
    52  	}
    53  }
    54  
    55  func checkTree(t *testing.T, dir string, tr []tree) {
    56  	for _, f := range tr {
    57  		if _, err := os.Stat(filepath.Join(dir, f.path)); err != nil {
    58  			t.Fatal(err)
    59  		}
    60  	}
    61  }
    62  
    63  func TestCopyTree(t *testing.T) {
    64  	td, err := ioutil.TempDir("", tstprefix)
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  	defer os.RemoveAll(td)
    69  
    70  	src := filepath.Join(td, "src")
    71  	dst := filepath.Join(td, "dst")
    72  	if err := os.MkdirAll(filepath.Join(td, "src"), 0755); err != nil {
    73  		t.Fatal(err)
    74  	}
    75  
    76  	tr := []tree{
    77  		{
    78  			path: "dir1",
    79  			dir:  true,
    80  		},
    81  		{
    82  			path: "dir2",
    83  			dir:  true,
    84  		},
    85  		{
    86  			path: "dir1/foo",
    87  			dir:  false,
    88  		},
    89  		{
    90  			path: "dir1/bar",
    91  			dir:  false,
    92  		},
    93  	}
    94  
    95  	createTree(t, src, tr)
    96  
    97  	// absolute paths
    98  	if err := CopyTree(src, dst, uid.NewBlankUidRange()); err != nil {
    99  		t.Fatal(err)
   100  	}
   101  	checkTree(t, dst, tr)
   102  
   103  	// relative paths
   104  	if err := os.Chdir(td); err != nil {
   105  		t.Fatal(err)
   106  	}
   107  
   108  	dst = "dst-rel1"
   109  	if err := CopyTree("././src/", dst, uid.NewBlankUidRange()); err != nil {
   110  		t.Fatal(err)
   111  	}
   112  	checkTree(t, dst, tr)
   113  
   114  	dst = "./dst-rel2"
   115  	if err := CopyTree("./src", dst, uid.NewBlankUidRange()); err != nil {
   116  		t.Fatal(err)
   117  	}
   118  	checkTree(t, dst, tr)
   119  }