github.com/coreos/mantle@v0.13.0/system/copy_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  	"bytes"
    19  	"io/ioutil"
    20  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  )
    24  
    25  func checkFile(t *testing.T, path string, data []byte, mode os.FileMode) {
    26  	file, err := os.Open(path)
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	defer file.Close()
    31  
    32  	info, err := file.Stat()
    33  	if info.Mode() != mode {
    34  		t.Fatalf("Unexpected mode: %s %s", info.Mode(), path)
    35  	}
    36  
    37  	newData, err := ioutil.ReadAll(file)
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	if !bytes.Equal(data, newData) {
    42  		t.Fatalf("Unexpected data: %q %s", string(newData), path)
    43  	}
    44  }
    45  
    46  func TestCopyRegularFile(t *testing.T) {
    47  	data := []byte("test")
    48  	tmp, err := ioutil.TempDir("", "")
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  	defer os.RemoveAll(tmp)
    53  
    54  	src := filepath.Join(tmp, "src")
    55  	if err := ioutil.WriteFile(src, data, 0600); err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	checkFile(t, src, data, 0600)
    59  
    60  	copy1 := filepath.Join(tmp, "copy1")
    61  	if err := CopyRegularFile(src, copy1); err != nil {
    62  		t.Fatal(err)
    63  	}
    64  	checkFile(t, copy1, data, 0600)
    65  
    66  	if err := os.Chmod(src, 0640); err != nil {
    67  		t.Fatal(err)
    68  	}
    69  	copy2 := filepath.Join(tmp, "copy2")
    70  	if err := CopyRegularFile(src, copy2); err != nil {
    71  		t.Fatal(err)
    72  	}
    73  	checkFile(t, copy2, data, 0640)
    74  }
    75  
    76  func TestInstallRegularFile(t *testing.T) {
    77  	data := []byte("test")
    78  	tmp, err := ioutil.TempDir("", "")
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  	defer os.RemoveAll(tmp)
    83  
    84  	src := filepath.Join(tmp, "src")
    85  	if err := ioutil.WriteFile(src, data, 0600); err != nil {
    86  		t.Fatal(err)
    87  	}
    88  	checkFile(t, src, data, 0600)
    89  
    90  	copy1 := filepath.Join(tmp, "subdir", "copy1")
    91  	if err := InstallRegularFile(src, copy1); err != nil {
    92  		t.Fatal(err)
    93  	}
    94  	checkFile(t, copy1, data, 0600)
    95  }