github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/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, 0600); 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  }