github.com/schwarzm/garden-linux@v0.0.0-20150507151835-33bca2147c47/old/rootfs_provider/volume_creator_test.go (about)

     1  package rootfs_provider_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/cloudfoundry-incubator/garden-linux/old/rootfs_provider"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("SimpleVolumeCreator", func() {
    14  	var creator rootfs_provider.SimpleVolumeCreator
    15  
    16  	Context("the directory does not already exist in the image", func() {
    17  		It("creates all directories recursively", func() {
    18  			tmpdir, err := ioutil.TempDir("", "volumecreator")
    19  			Expect(err).ToNot(HaveOccurred())
    20  			defer os.RemoveAll(tmpdir)
    21  
    22  			Expect(creator.Create(tmpdir, filepath.Join("foo", "bar"))).To(Succeed())
    23  
    24  			_, err = os.Stat(filepath.Join(tmpdir, "foo", "bar"))
    25  			Expect(err).ToNot(HaveOccurred())
    26  		})
    27  
    28  		It("gives it the right permissions (0755)", func() {
    29  			tmpdir, err := ioutil.TempDir("", "volumecreator")
    30  			Expect(err).ToNot(HaveOccurred())
    31  			defer os.RemoveAll(tmpdir)
    32  
    33  			Expect(creator.Create(tmpdir, filepath.Join("foo", "bar"))).To(Succeed())
    34  
    35  			info, err := os.Stat(filepath.Join(tmpdir, "foo", "bar"))
    36  			Expect(err).ToNot(HaveOccurred())
    37  			Expect(info.Mode().Perm()).To(Equal(os.FileMode(0755)))
    38  		})
    39  	})
    40  
    41  	Context("a file with the same name as the directory already exists", func() {
    42  		It("returns an error", func() {
    43  			tmpdir, err := ioutil.TempDir("", "volumecreator")
    44  			Expect(err).ToNot(HaveOccurred())
    45  			defer os.RemoveAll(tmpdir)
    46  
    47  			volumepath := filepath.Join(tmpdir, "the", "volume")
    48  			Expect(os.MkdirAll(filepath.Join(tmpdir, "the"), 0755)).To(Succeed())
    49  			Expect(ioutil.WriteFile(volumepath, []byte("beans"), 0755)).To(Succeed())
    50  
    51  			Expect(creator.Create(tmpdir, filepath.Join("/the", "volume"))).ToNot(Succeed())
    52  		})
    53  	})
    54  
    55  	Context("the directory already exists in the image", func() {
    56  		It("leaves it alone", func() {
    57  			tmpdir, err := ioutil.TempDir("", "volumecreator")
    58  			Expect(err).ToNot(HaveOccurred())
    59  			defer os.RemoveAll(tmpdir)
    60  
    61  			volumepath := filepath.Join(tmpdir, "the", "volume")
    62  			Expect(os.MkdirAll(volumepath, 0755)).To(Succeed())
    63  
    64  			Expect(ioutil.WriteFile(filepath.Join(volumepath, "foo.txt"), []byte("beans"), 0755)).To(Succeed())
    65  
    66  			Expect(creator.Create(tmpdir, filepath.Join("the", "volume"))).To(Succeed())
    67  
    68  			_, err = os.Stat(volumepath)
    69  			Expect(err).ToNot(HaveOccurred())
    70  
    71  			Expect(filepath.Glob(filepath.Join(volumepath, "*"))).To(ContainElement(filepath.Join(volumepath, "foo.txt")))
    72  		})
    73  	})
    74  })