github.com/cloudfoundry-attic/ltc@v0.0.0-20151123212628-098adc7919fc/config/persister/file_persister_test.go (about)

     1  package persister_test
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"reflect"
     9  
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  
    13  	"github.com/cloudfoundry-incubator/ltc/config/persister"
    14  )
    15  
    16  var _ = Describe("filePersister", func() {
    17  	type data struct {
    18  		Value string
    19  	}
    20  
    21  	var (
    22  		tmpDir  string
    23  		tmpFile *os.File
    24  		err     error
    25  	)
    26  
    27  	BeforeEach(func() {
    28  		tmpDir = os.TempDir()
    29  
    30  		tmpFile, err = ioutil.TempFile(tmpDir, "tmp_file")
    31  		Expect(err).ToNot(HaveOccurred())
    32  	})
    33  
    34  	AfterEach(func() {
    35  		tmpFile.Close()
    36  		Expect(os.RemoveAll(tmpFile.Name())).To(Succeed())
    37  	})
    38  
    39  	Describe("Load", func() {
    40  
    41  		var (
    42  			filePersister persister.Persister
    43  			dataToRead    *data
    44  		)
    45  
    46  		BeforeEach(func() {
    47  			dataToRead = &data{}
    48  			filePersister = persister.NewFilePersister(tmpFile.Name())
    49  		})
    50  
    51  		JustBeforeEach(func() {
    52  			err = filePersister.Load(dataToRead)
    53  		})
    54  
    55  		It("Loads empty data from an empty file", func() {
    56  			Expect(dataToRead.Value).To(BeEmpty())
    57  		})
    58  
    59  		Context("when the file already exists", func() {
    60  			BeforeEach(func() {
    61  				err := ioutil.WriteFile(tmpFile.Name(), []byte(`{"Value":"test value"}`), 0700)
    62  				Expect(err).ToNot(HaveOccurred())
    63  			})
    64  
    65  			It("Loads JSON from the file", func() {
    66  				Expect(dataToRead.Value).To(Equal("test value"))
    67  			})
    68  		})
    69  
    70  		Context("when the file has invalid json", func() {
    71  			BeforeEach(func() {
    72  				err := ioutil.WriteFile(tmpFile.Name(), []byte(`{"Value":"test value`), 0700)
    73  				Expect(err).ToNot(HaveOccurred())
    74  			})
    75  
    76  			It("returns errors from invalid JSON", func() {
    77  				Expect(reflect.TypeOf(err).String()).To(Equal("*json.SyntaxError"))
    78  			})
    79  		})
    80  
    81  		Context("when reading the file returns an error", func() {
    82  			BeforeEach(func() {
    83  				err := ioutil.WriteFile(tmpFile.Name(), []byte(""), 0000)
    84  				Expect(err).ToNot(HaveOccurred())
    85  			})
    86  
    87  			It("returns errors from reading the file", func() {
    88  				Expect(reflect.TypeOf(err).String()).To(Equal("*json.SyntaxError"))
    89  			})
    90  		})
    91  
    92  		Context("when reading nonexistant files", func() {
    93  			BeforeEach(func() {
    94  				nonExistantFile := fmt.Sprintf("%s/nonexistant/tmp_file", tmpDir)
    95  				Expect(os.RemoveAll(nonExistantFile)).To(Succeed())
    96  
    97  				filePersister = persister.NewFilePersister(nonExistantFile)
    98  			})
    99  
   100  			It("handles nonexistant files silently", func() {
   101  				Expect(err).ToNot(HaveOccurred())
   102  			})
   103  		})
   104  	})
   105  
   106  	Describe("Save", func() {
   107  
   108  		var (
   109  			filePersister persister.Persister
   110  			dataToSave    *data
   111  		)
   112  
   113  		BeforeEach(func() {
   114  			dataToSave = &data{Value: "Some Value to be written in json"}
   115  			filePersister = persister.NewFilePersister(tmpFile.Name())
   116  		})
   117  
   118  		JustBeforeEach(func() {
   119  			err = filePersister.Save(dataToSave)
   120  		})
   121  
   122  		It("Saves valid JSON to the filepath", func() {
   123  			jsonBytes, err := ioutil.ReadFile(tmpFile.Name())
   124  			Expect(err).ToNot(HaveOccurred())
   125  			Expect(jsonBytes).To(MatchJSON(`{"Value":"Some Value to be written in json"}`))
   126  		})
   127  
   128  		It("Returns an error rather than save invalid JSON", func() {
   129  			err := filePersister.Save(func() {})
   130  			Expect(reflect.TypeOf(err).String()).To(Equal("*json.UnsupportedTypeError"))
   131  		})
   132  
   133  		Context("when reading nonexistant files", func() {
   134  
   135  			var nonExistantFile string
   136  
   137  			BeforeEach(func() {
   138  				nonExistantFile = filepath.Join(tmpDir, "nonexistant", "tmp_file")
   139  				Expect(os.RemoveAll(filepath.Dir(nonExistantFile))).To(Succeed())
   140  
   141  				filePersister = persister.NewFilePersister(nonExistantFile)
   142  			})
   143  
   144  			AfterEach(func() {
   145  				Expect(os.RemoveAll(filepath.Dir(nonExistantFile))).To(Succeed())
   146  			})
   147  
   148  			It("writes to nonexistant directories", func() {
   149  				Expect(err).NotTo(HaveOccurred())
   150  			})
   151  		})
   152  
   153  		Context("when making the directory", func() {
   154  			BeforeEach(func() {
   155  				filePath := filepath.Join(tmpFile.Name(), "no_privs", "tmp_file")
   156  				if _, err := os.Stat(tmpFile.Name()); err != nil {
   157  					Expect(os.IsNotExist(err)).To(BeFalse())
   158  				}
   159  
   160  				filePersister = persister.NewFilePersister(filePath)
   161  			})
   162  
   163  			AfterEach(func() {
   164  				tmpFile.Close()
   165  				Expect(os.RemoveAll(tmpFile.Name())).To(Succeed())
   166  			})
   167  
   168  			It("returns errors from making the directory", func() {
   169  				Expect(reflect.TypeOf(err).String()).To(Equal("*os.PathError"))
   170  			})
   171  		})
   172  
   173  		Context("when writing the file returns errors", func() {
   174  			BeforeEach(func() {
   175  				filePersister = persister.NewFilePersister(tmpDir)
   176  			})
   177  
   178  			It("returns errors from writing the file", func() {
   179  				Expect(reflect.TypeOf(err).String()).To(Equal("*os.PathError"))
   180  			})
   181  		})
   182  
   183  	})
   184  })