github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/manifest/manifest_disk_repository_test.go (about) 1 package manifest_test 2 3 import ( 4 "path/filepath" 5 6 . "code.cloudfoundry.org/cli/cf/manifest" 7 . "github.com/onsi/ginkgo" 8 . "github.com/onsi/gomega" 9 ) 10 11 var _ = Describe("ManifestDiskRepository", func() { 12 var repo Repository 13 14 BeforeEach(func() { 15 repo = NewDiskRepository() 16 }) 17 18 Describe("given a directory containing a file called 'manifest.yml'", func() { 19 It("reads that file", func() { 20 m, err := repo.ReadManifest("../../fixtures/manifests") 21 22 Expect(err).NotTo(HaveOccurred()) 23 Expect(m.Path).To(Equal(filepath.Clean("../../fixtures/manifests/manifest.yml"))) 24 25 applications, err := m.Applications() 26 27 Expect(err).NotTo(HaveOccurred()) 28 Expect(*applications[0].Name).To(Equal("from-default-manifest")) 29 }) 30 }) 31 32 Describe("given a directory that doesn't contain a file called 'manifest.y{a}ml'", func() { 33 It("returns an error", func() { 34 m, err := repo.ReadManifest("../../fixtures") 35 36 Expect(err).To(HaveOccurred()) 37 Expect(m.Path).To(BeEmpty()) 38 }) 39 }) 40 41 Describe("given a directory that contains a file called 'manifest.yaml'", func() { 42 It("reads that file", func() { 43 m, err := repo.ReadManifest("../../fixtures/manifests/only_yaml") 44 45 Expect(err).NotTo(HaveOccurred()) 46 Expect(m.Path).To(Equal(filepath.Clean("../../fixtures/manifests/only_yaml/manifest.yaml"))) 47 48 applications, err := m.Applications() 49 Expect(err).NotTo(HaveOccurred()) 50 Expect(*applications[0].Name).To(Equal("from-default-manifest")) 51 }) 52 }) 53 54 Describe("given a directory contains files called 'manifest.yml' and 'manifest.yaml'", func() { 55 It("reads the file named 'manifest.yml'", func() { 56 m, err := repo.ReadManifest("../../fixtures/manifests/both_yaml_yml") 57 58 Expect(err).NotTo(HaveOccurred()) 59 Expect(m.Path).To(Equal(filepath.Clean("../../fixtures/manifests/both_yaml_yml/manifest.yml"))) 60 61 applications, err := m.Applications() 62 Expect(err).NotTo(HaveOccurred()) 63 Expect(*applications[0].Name).To(Equal("yml-extension")) 64 }) 65 }) 66 67 Describe("given a path to a file", func() { 68 var ( 69 inputPath string 70 m *Manifest 71 err error 72 ) 73 74 BeforeEach(func() { 75 inputPath = filepath.Clean("../../fixtures/manifests/different-manifest.yml") 76 m, err = repo.ReadManifest(inputPath) 77 }) 78 79 It("reads the file at that path", func() { 80 Expect(err).NotTo(HaveOccurred()) 81 Expect(m.Path).To(Equal(inputPath)) 82 83 applications, err := m.Applications() 84 Expect(err).NotTo(HaveOccurred()) 85 Expect(*applications[0].Name).To(Equal("from-different-manifest")) 86 }) 87 88 It("passes the base directory to the manifest file", func() { 89 applications, err := m.Applications() 90 Expect(err).NotTo(HaveOccurred()) 91 Expect(len(applications)).To(Equal(1)) 92 Expect(*applications[0].Name).To(Equal("from-different-manifest")) 93 94 appPath := filepath.Clean("../../fixtures/manifests") 95 Expect(*applications[0].Path).To(Equal(appPath)) 96 }) 97 }) 98 99 Describe("given a path to a file that doesn't exist", func() { 100 It("returns an error", func() { 101 _, err := repo.ReadManifest("some/path/that/doesnt/exist/manifest.yml") 102 Expect(err).To(HaveOccurred()) 103 }) 104 105 It("returns empty string for the manifest path", func() { 106 m, _ := repo.ReadManifest("some/path/that/doesnt/exist/manifest.yml") 107 Expect(m.Path).To(Equal("")) 108 }) 109 }) 110 111 Describe("when the manifest is empty", func() { 112 It("returns an error", func() { 113 _, err := repo.ReadManifest("../../fixtures/manifests/empty-manifest.yml") 114 Expect(err).To(HaveOccurred()) 115 }) 116 117 It("returns the path to the manifest", func() { 118 inputPath := filepath.Clean("../../fixtures/manifests/empty-manifest.yml") 119 m, _ := repo.ReadManifest(inputPath) 120 Expect(m.Path).To(Equal(inputPath)) 121 }) 122 }) 123 124 It("converts nested maps to generic maps", func() { 125 m, err := repo.ReadManifest("../../fixtures/manifests/different-manifest.yml") 126 Expect(err).NotTo(HaveOccurred()) 127 128 applications, err := m.Applications() 129 Expect(err).NotTo(HaveOccurred()) 130 Expect(*applications[0].EnvironmentVars).To(Equal(map[string]interface{}{ 131 "LD_LIBRARY_PATH": "/usr/lib/somewhere", 132 })) 133 }) 134 135 It("merges manifests with their 'inherited' manifests", func() { 136 m, err := repo.ReadManifest("../../fixtures/manifests/inherited-manifest.yml") 137 Expect(err).NotTo(HaveOccurred()) 138 139 applications, err := m.Applications() 140 Expect(err).NotTo(HaveOccurred()) 141 Expect(*applications[0].Name).To(Equal("base-app")) 142 Expect(applications[0].ServicesToBind).To(Equal([]string{"base-service"})) 143 Expect(*applications[0].EnvironmentVars).To(Equal(map[string]interface{}{ 144 "foo": "bar", 145 "will-be-overridden": "my-value", 146 })) 147 148 Expect(*applications[1].Name).To(Equal("my-app")) 149 150 env := *applications[1].EnvironmentVars 151 Expect(env["will-be-overridden"]).To(Equal("my-value")) 152 Expect(env["foo"]).To(Equal("bar")) 153 154 services := applications[1].ServicesToBind 155 Expect(services).To(Equal([]string{"base-service", "foo-service"})) 156 }) 157 158 It("supports yml merges", func() { 159 m, err := repo.ReadManifest("../../fixtures/manifests/merge-manifest.yml") 160 Expect(err).NotTo(HaveOccurred()) 161 162 applications, err := m.Applications() 163 Expect(err).NotTo(HaveOccurred()) 164 165 Expect(*applications[0].Name).To(Equal("blue")) 166 Expect(*applications[0].InstanceCount).To(Equal(1)) 167 Expect(*applications[0].Memory).To(Equal(int64(256))) 168 169 Expect(*applications[1].Name).To(Equal("green")) 170 Expect(*applications[1].InstanceCount).To(Equal(1)) 171 Expect(*applications[1].Memory).To(Equal(int64(256))) 172 173 Expect(*applications[2].Name).To(Equal("big-blue")) 174 Expect(*applications[2].InstanceCount).To(Equal(3)) 175 Expect(*applications[2].Memory).To(Equal(int64(256))) 176 }) 177 })