github.com/brahmaroutu/docker@v1.2.1-0.20160809185609-eb28dde01f16/pkg/ioutils/fswriters_test.go (about)

     1  package ioutils
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  )
    10  
    11  func TestAtomicWriteToFile(t *testing.T) {
    12  	tmpDir, err := ioutil.TempDir("", "atomic-writers-test")
    13  	if err != nil {
    14  		t.Fatalf("Error when creating temporary directory: %s", err)
    15  	}
    16  	defer os.RemoveAll(tmpDir)
    17  
    18  	expected := []byte("barbaz")
    19  	if err := AtomicWriteFile(filepath.Join(tmpDir, "foo"), expected, 0666); err != nil {
    20  		t.Fatalf("Error writing to file: %v", err)
    21  	}
    22  
    23  	actual, err := ioutil.ReadFile(filepath.Join(tmpDir, "foo"))
    24  	if err != nil {
    25  		t.Fatalf("Error reading from file: %v", err)
    26  	}
    27  
    28  	if bytes.Compare(actual, expected) != 0 {
    29  		t.Fatalf("Data mismatch, expected %q, got %q", expected, actual)
    30  	}
    31  
    32  	st, err := os.Stat(filepath.Join(tmpDir, "foo"))
    33  	if err != nil {
    34  		t.Fatalf("Error statting file: %v", err)
    35  	}
    36  	if expected := os.FileMode(0666); st.Mode() != expected {
    37  		t.Fatalf("Mode mismatched, expected %o, got %o", expected, st.Mode())
    38  	}
    39  }