github.com/paketoio/libpak@v1.3.1/build_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 libpak_test 18 19 import ( 20 "fmt" 21 "io/ioutil" 22 "os" 23 "path/filepath" 24 "testing" 25 26 "github.com/buildpacks/libcnb" 27 "github.com/buildpacks/libcnb/mocks" 28 . "github.com/onsi/gomega" 29 "github.com/paketoio/libpak" 30 "github.com/sclevine/spec" 31 "github.com/stretchr/testify/mock" 32 ) 33 34 func testBuild(t *testing.T, context spec.G, it spec.S) { 35 var ( 36 Expect = NewWithT(t).Expect 37 38 applicationPath string 39 buildpackPath string 40 buildpackPlanPath string 41 commandPath string 42 environmentWriter *mocks.EnvironmentWriter 43 exitHandler *mocks.ExitHandler 44 layerContributor *mocks.LayerContributor 45 layersPath string 46 platformPath string 47 tomlWriter *mocks.TOMLWriter 48 49 workingDir string 50 ) 51 52 it.Before(func() { 53 var err error 54 55 applicationPath, err = ioutil.TempDir("", "build-application-path") 56 Expect(err).NotTo(HaveOccurred()) 57 applicationPath, err = filepath.EvalSymlinks(applicationPath) 58 Expect(err).NotTo(HaveOccurred()) 59 60 buildpackPath, err = ioutil.TempDir("", "build-buildpack-path") 61 Expect(err).NotTo(HaveOccurred()) 62 63 f, err := ioutil.TempFile("", "build-buildpackplan-path") 64 Expect(err).NotTo(HaveOccurred()) 65 Expect(f.Close()).NotTo(HaveOccurred()) 66 buildpackPlanPath = f.Name() 67 68 commandPath = filepath.Join(buildpackPath, "bin", "build") 69 70 environmentWriter = &mocks.EnvironmentWriter{} 71 environmentWriter.On("Write", mock.Anything, mock.Anything).Return(nil) 72 73 exitHandler = &mocks.ExitHandler{} 74 exitHandler.On("Error", mock.Anything) 75 76 layerContributor = &mocks.LayerContributor{} 77 78 layersPath, err = ioutil.TempDir("", "build-layers-path") 79 Expect(err).NotTo(HaveOccurred()) 80 81 platformPath, err = ioutil.TempDir("", "build-platform-path") 82 Expect(err).NotTo(HaveOccurred()) 83 84 Expect(os.MkdirAll(filepath.Join(platformPath, "bindings", "alpha", "metadata"), 0755)).To(Succeed()) 85 Expect(ioutil.WriteFile( 86 filepath.Join(platformPath, "bindings", "alpha", "metadata", "test-metadata-key"), 87 []byte("test-metadata-value"), 88 0644, 89 )).To(Succeed()) 90 Expect(os.MkdirAll(filepath.Join(platformPath, "bindings", "alpha", "secret"), 0755)).To(Succeed()) 91 Expect(ioutil.WriteFile( 92 filepath.Join(platformPath, "bindings", "alpha", "secret", "test-secret-key"), 93 []byte("test-secret-value"), 94 0644, 95 )).To(Succeed()) 96 Expect(os.Setenv("CNB_BINDINGS", ` 97 [bravo] 98 [bravo.metadata] 99 test-metadata-key = "test-metadata-value" 100 101 [bravo.secret] 102 test-secret-key = "test-secret-value" 103 `)) 104 105 tomlWriter = &mocks.TOMLWriter{} 106 tomlWriter.On("Write", mock.Anything, mock.Anything).Return(nil) 107 108 Expect(os.Setenv("CNB_STACK_ID", "test-stack-id")).To(Succeed()) 109 110 workingDir, err = os.Getwd() 111 Expect(err).NotTo(HaveOccurred()) 112 Expect(os.Chdir(applicationPath)).To(Succeed()) 113 }) 114 115 it.After(func() { 116 Expect(os.Chdir(workingDir)).To(Succeed()) 117 Expect(os.Unsetenv("CNB_BINDINGS")).To(Succeed()) 118 Expect(os.Unsetenv("CNB_STACK_ID")).To(Succeed()) 119 120 Expect(os.RemoveAll(applicationPath)).To(Succeed()) 121 Expect(os.RemoveAll(buildpackPath)).To(Succeed()) 122 Expect(os.RemoveAll(buildpackPlanPath)).To(Succeed()) 123 Expect(os.RemoveAll(layersPath)).To(Succeed()) 124 Expect(os.RemoveAll(platformPath)).To(Succeed()) 125 }) 126 127 it("adds contents of CNB_BINDINGS to platform", func() { 128 var ctx libcnb.BuildContext 129 libpak.Build( 130 func(context libcnb.BuildContext) (libcnb.BuildResult, error) { 131 ctx = context 132 return libcnb.BuildResult{}, nil 133 }, 134 libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}), 135 ) 136 137 Expect(ctx.Platform).To(Equal(libcnb.Platform{ 138 Bindings: libcnb.Bindings{ 139 "alpha": libcnb.Binding{ 140 Metadata: map[string]string{ 141 "test-metadata-key": "test-metadata-value", 142 }, 143 Secret: map[string]string{ 144 "test-secret-key": "test-secret-value", 145 }, 146 }, 147 "bravo": libcnb.Binding{ 148 Metadata: map[string]string{ 149 "test-metadata-key": "test-metadata-value", 150 }, 151 Secret: map[string]string{ 152 "test-secret-key": "test-secret-value", 153 }, 154 }, 155 }, 156 Environment: map[string]string{}, 157 Path: platformPath, 158 })) 159 }) 160 161 it("handles error from BuildFunc", func() { 162 libpak.Build( 163 func(context libcnb.BuildContext) (libcnb.BuildResult, error) { 164 return libcnb.BuildResult{}, fmt.Errorf("test-error") 165 }, 166 libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}), 167 libcnb.WithExitHandler(exitHandler), 168 ) 169 170 Expect(exitHandler.Calls[0].Arguments.Get(0)).To(MatchError("test-error")) 171 }) 172 173 it("removes stale layers", func() { 174 Expect(ioutil.WriteFile(filepath.Join(layersPath, "alpha.toml"), []byte(""), 0644)).To(Succeed()) 175 Expect(ioutil.WriteFile(filepath.Join(layersPath, "bravo.toml"), []byte(""), 0644)).To(Succeed()) 176 Expect(ioutil.WriteFile(filepath.Join(layersPath, "store.toml"), []byte(""), 0644)).To(Succeed()) 177 178 libpak.Build( 179 func(context libcnb.BuildContext) (libcnb.BuildResult, error) { 180 l, err := context.Layers.Layer("alpha") 181 Expect(err).NotTo(HaveOccurred()) 182 183 layerContributor.On("Contribute", mock.Anything).Return(l, nil) 184 layerContributor.On("Name").Return("alpha") 185 186 return libcnb.BuildResult{ 187 Layers: []libcnb.LayerContributor{layerContributor}, 188 }, nil 189 }, 190 libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}), 191 libcnb.WithTOMLWriter(tomlWriter), 192 ) 193 194 Expect(filepath.Join(layersPath, "alpha.toml")).To(BeARegularFile()) 195 Expect(filepath.Join(layersPath, "bravo.toml")).NotTo(BeARegularFile()) 196 Expect(filepath.Join(layersPath, "store.toml")).To(BeARegularFile()) 197 }) 198 }