github.com/cloudfoundry/libcfbuildpack@v1.91.23/packager/cnbpackager/packager_test.go (about) 1 /* 2 * Copyright 2018-2020 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package cnbpackager 18 19 import ( 20 "fmt" 21 "io/ioutil" 22 "os" 23 "path/filepath" 24 "testing" 25 26 "github.com/cloudfoundry/libcfbuildpack/buildpack" 27 "github.com/cloudfoundry/libcfbuildpack/test" 28 29 "github.com/onsi/gomega" 30 "github.com/sclevine/spec" 31 "github.com/sclevine/spec/report" 32 ) 33 34 func TestUnitPackager(t *testing.T) { 35 spec.Run(t, "Package", testPackager, spec.Report(report.Terminal{})) 36 } 37 38 func testPackager(t *testing.T, when spec.G, it spec.S) { 39 var ( 40 cnbDir, outputDir, cacheDir, tempDir, depSHA, tarball, newVersion, buildpackTOML string 41 pkgr Packager 42 err error 43 ) 44 45 it.Before(func() { 46 gomega.RegisterTestingT(t) 47 48 tempDir, err = ioutil.TempDir("", "") 49 gomega.Expect(err).NotTo(gomega.HaveOccurred()) 50 51 depFile, err := filepath.Abs(filepath.Join("testdata", "hello.tgz")) 52 gomega.Expect(err).NotTo(gomega.HaveOccurred()) 53 depSHA = "9299d8c43e7af6797cd7fb5c12d986a90b864daa3c23ee50dab629ef844c1231" 54 55 buildpackTOML = fmt.Sprintf(` 56 [buildpack] 57 id = "buildpack-id" 58 name = "buildpack-name" 59 version = "buildpack-version" 60 61 [metadata] 62 include_files = ["buildpack.toml"] 63 64 [[metadata.dependencies]] 65 id = "dependency-id" 66 name = "dependency-name" 67 sha256 = "%s" 68 stacks = ["stack-id"] 69 uri = "file://%s" 70 version = "1.0.0" 71 72 [[stacks]] 73 id = 'stack-id'`, depSHA, depFile) 74 75 cnbDir = filepath.Join(tempDir, "cnb") 76 gomega.Expect(os.MkdirAll(cnbDir, 0777)) 77 gomega.Expect(ioutil.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), []byte(buildpackTOML), 0666)).To(gomega.Succeed()) 78 79 outputDir = filepath.Join(tempDir, "output") 80 cacheDir = filepath.Join(tempDir, "cache") 81 newVersion = "newVersion" 82 }) 83 84 it.After(func() { 85 os.RemoveAll(tempDir) 86 if tarball != "" { 87 os.RemoveAll(tarball) 88 } 89 }) 90 91 when("insertTemplateVersion", func() { 92 when("buildpack.toml doesn't have a templated version", func() { 93 it("doesn't replace anything in the buildpack", func() { 94 buildpackTomlPath := filepath.Join(cnbDir, "buildpack.toml") 95 gomega.Expect(insertTemplateVersion(cnbDir, newVersion)).To(gomega.Succeed()) 96 97 output, err := ioutil.ReadFile(buildpackTomlPath) 98 gomega.Expect(err).NotTo(gomega.HaveOccurred()) 99 gomega.Expect(string(output)).To(gomega.Equal(buildpackTOML)) 100 }) 101 }) 102 103 when("buildpack.toml has a templated version", func() { 104 it("replaces the version in the buildpack", func() { 105 buildpackTomlPath := filepath.Join(cnbDir, "buildpack.toml") 106 baseBuildpackTOML := `[buildpack] 107 id = "buildpack-id" 108 name = "buildpack-name" 109 version = "%s"` 110 oldBPToml := fmt.Sprintf(baseBuildpackTOML, "{{ .Version }}") 111 newBPToml := fmt.Sprintf(baseBuildpackTOML, newVersion) 112 gomega.Expect(ioutil.WriteFile(buildpackTomlPath, []byte(oldBPToml), 0666)).To(gomega.Succeed()) 113 114 gomega.Expect(insertTemplateVersion(cnbDir, newVersion)).To(gomega.Succeed()) 115 116 output, err := ioutil.ReadFile(buildpackTomlPath) 117 gomega.Expect(err).NotTo(gomega.HaveOccurred()) 118 gomega.Expect(string(output)).To(gomega.Equal(newBPToml)) 119 }) 120 }) 121 }) 122 123 when("cached", func() { 124 it.Before(func() { 125 outputDir += "-cached" 126 pkgr, err = New(cnbDir, outputDir, "", cacheDir) 127 gomega.Expect(err).ToNot(gomega.HaveOccurred()) 128 }) 129 130 it("Create makes a cached buildpack", func() { 131 gomega.Expect(pkgr.Create(true)).To(gomega.Succeed()) 132 cacheRoot := filepath.Join(cacheDir, buildpack.CacheRoot) 133 134 gomega.Expect(filepath.Join(cacheRoot, depSHA+".toml")).To(gomega.BeAnExistingFile()) 135 gomega.Expect(filepath.Join(cacheRoot, depSHA, "hello.tgz")).To(gomega.BeAnExistingFile()) 136 gomega.Expect(filepath.Join(outputDir, "buildpack.toml")).To(gomega.BeAnExistingFile()) 137 gomega.Expect(filepath.Join(outputDir, buildpack.CacheRoot, depSHA+".toml")).To(gomega.BeAnExistingFile()) 138 gomega.Expect(filepath.Join(outputDir, buildpack.CacheRoot, depSHA, "hello.tgz")).To(gomega.BeAnExistingFile()) 139 }) 140 }) 141 142 when("uncached", func() { 143 it.Before(func() { 144 pkgr, err = New(cnbDir, outputDir, "", cacheDir) 145 gomega.Expect(err).ToNot(gomega.HaveOccurred()) 146 }) 147 148 it("Create makes an uncached buildpack", func() { 149 gomega.Expect(pkgr.Create(false)).To(gomega.Succeed()) 150 gomega.Expect(filepath.Join(outputDir, "buildpack.toml")).To(gomega.BeAnExistingFile()) 151 gomega.Expect(filepath.Join(outputDir, buildpack.CacheRoot)).NotTo(gomega.BeAnExistingFile()) 152 }) 153 }) 154 155 when("archiving", func() { 156 it.Before(func() { 157 fakeCnbDir := filepath.Join("testdata", "archive-testdata", "fake-cnb") 158 pkgr, err = New(fakeCnbDir, outputDir, "", cacheDir) 159 gomega.Expect(err).ToNot(gomega.HaveOccurred()) 160 }) 161 162 it("Archive can make a tarred up buildpack", func() { 163 gomega.Expect(pkgr.Create(false)).To(gomega.Succeed()) 164 gomega.Expect(pkgr.Archive()).To(gomega.Succeed()) 165 166 tarball = filepath.Join(filepath.Dir(outputDir), filepath.Base(outputDir)+".tgz") 167 gomega.Expect(tarball).To(gomega.BeAnExistingFile()) 168 gomega.Expect(outputDir).NotTo(gomega.BeAnExistingFile()) 169 }) 170 171 it("includes the parent directories of included files", func() { 172 gomega.Expect(pkgr.Create(false)).To(gomega.Succeed()) 173 gomega.Expect(pkgr.Archive()).To(gomega.Succeed()) 174 175 tarball = filepath.Join(filepath.Dir(outputDir), filepath.Base(outputDir)+".tgz") 176 gomega.Expect(tarball).To(gomega.BeAnExistingFile()) 177 gomega.Expect(outputDir).NotTo(gomega.BeAnExistingFile()) 178 179 gomega.Expect(tarball).NotTo(test.HaveArchiveEntry("")) 180 gomega.Expect(tarball).To(test.HaveArchiveEntry("bin")) 181 gomega.Expect(tarball).To(test.HaveArchiveEntry("bin/detect")) 182 gomega.Expect(tarball).To(test.HaveArchiveEntry("bin/build")) 183 gomega.Expect(tarball).To(test.HaveArchiveEntry("buildpack.toml")) 184 }) 185 }) 186 187 when("summary", func() { 188 it("Returns a package Summary of the CNB directory", func() { 189 fakeCnbDir := filepath.Join("testdata", "summary-testdata", "fake-cnb") 190 pkgr, err = New(fakeCnbDir, "", "", "") 191 gomega.Expect(err).ToNot(gomega.HaveOccurred()) 192 solution := ` 193 Packaged binaries: 194 195 | name | version | stacks | 196 |-|-|-| 197 | dep1 | 7.8.9 | stack1 | 198 | dep1 | 4.5.6 | stack1, stack2 | 199 | dep2 | 7.8.9 | stack2 | 200 201 Default binary versions: 202 203 | name | version | 204 |-|-| 205 | dep1 | 4.5.x | 206 207 Supported stacks: 208 209 | name | 210 |-| 211 | stack1 | 212 | stack2 | 213 ` 214 215 summary, err := pkgr.Summary() 216 gomega.Expect(err).ToNot(gomega.HaveOccurred()) 217 gomega.Expect(summary).To(gomega.Equal(solution)) 218 }) 219 220 it("does not have default versions", func() { 221 fakeCnbDir := filepath.Join("testdata", "summary-testdata", "fake-cnb-without-defaults") 222 pkgr, err = New(fakeCnbDir, "", "", "") 223 gomega.Expect(err).ToNot(gomega.HaveOccurred()) 224 solution := ` 225 Packaged binaries: 226 227 | name | version | stacks | 228 |-|-|-| 229 | dep1 | 4.5.6 | stack1 | 230 | dep2 | 7.8.9 | stack2 | 231 232 Supported stacks: 233 234 | name | 235 |-| 236 | stack1 | 237 | stack2 | 238 ` 239 240 summary, err := pkgr.Summary() 241 gomega.Expect(err).ToNot(gomega.HaveOccurred()) 242 gomega.Expect(summary).To(gomega.Equal(solution)) 243 }) 244 245 it("does not have any dependencies", func() { 246 fakeCnbDir := filepath.Join("testdata", "summary-testdata", "fake-cnb-without-dependencies") 247 pkgr, err = New(fakeCnbDir, "", "", "") 248 gomega.Expect(err).ToNot(gomega.HaveOccurred()) 249 solution := ` 250 Supported stacks: 251 252 | name | 253 |-| 254 | stack1 | 255 | stack2 | 256 ` 257 258 summary, err := pkgr.Summary() 259 gomega.Expect(err).ToNot(gomega.HaveOccurred()) 260 gomega.Expect(summary).To(gomega.Equal(solution)) 261 }) 262 }, spec.Report(report.Terminal{})) 263 }