github.com/paketoio/libpak@v1.3.1/detect_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 testDetect(t *testing.T, context spec.G, it spec.S) {
    35  	var (
    36  		Expect = NewWithT(t).Expect
    37  
    38  		applicationPath string
    39  		buildpackPath   string
    40  		buildPlanPath   string
    41  		commandPath     string
    42  		exitHandler     *mocks.ExitHandler
    43  		platformPath    string
    44  		tomlWriter      *mocks.TOMLWriter
    45  
    46  		workingDir string
    47  	)
    48  
    49  	it.Before(func() {
    50  		var err error
    51  
    52  		applicationPath, err = ioutil.TempDir("", "detect-application-path")
    53  		Expect(err).NotTo(HaveOccurred())
    54  		applicationPath, err = filepath.EvalSymlinks(applicationPath)
    55  		Expect(err).NotTo(HaveOccurred())
    56  
    57  		buildpackPath, err = ioutil.TempDir("", "detect-buildpack-path")
    58  		Expect(err).NotTo(HaveOccurred())
    59  
    60  		f, err := ioutil.TempFile("", "detect-buildplan-path")
    61  		Expect(err).NotTo(HaveOccurred())
    62  		Expect(f.Close()).NotTo(HaveOccurred())
    63  		buildPlanPath = f.Name()
    64  
    65  		commandPath = filepath.Join(buildpackPath, "bin", "detect")
    66  
    67  		exitHandler = &mocks.ExitHandler{}
    68  		exitHandler.On("Error", mock.Anything)
    69  		exitHandler.On("Fail")
    70  		exitHandler.On("Pass")
    71  
    72  		platformPath, err = ioutil.TempDir("", "detect-platform-path")
    73  		Expect(err).NotTo(HaveOccurred())
    74  
    75  		Expect(os.MkdirAll(filepath.Join(platformPath, "bindings", "alpha", "metadata"), 0755)).To(Succeed())
    76  		Expect(ioutil.WriteFile(
    77  			filepath.Join(platformPath, "bindings", "alpha", "metadata", "test-metadata-key"),
    78  			[]byte("test-metadata-value"),
    79  			0644,
    80  		)).To(Succeed())
    81  		Expect(os.MkdirAll(filepath.Join(platformPath, "bindings", "alpha", "secret"), 0755)).To(Succeed())
    82  		Expect(ioutil.WriteFile(
    83  			filepath.Join(platformPath, "bindings", "alpha", "secret", "test-secret-key"),
    84  			[]byte("test-secret-value"),
    85  			0644,
    86  		)).To(Succeed())
    87  		Expect(os.Setenv("CNB_BINDINGS", `
    88  [bravo]
    89  [bravo.metadata]
    90  test-metadata-key = "test-metadata-value"
    91  
    92  [bravo.secret]
    93  test-secret-key = "test-secret-value"
    94  `))
    95  
    96  		tomlWriter = &mocks.TOMLWriter{}
    97  		tomlWriter.On("Write", mock.Anything, mock.Anything).Return(nil)
    98  
    99  		Expect(os.Setenv("CNB_STACK_ID", "test-stack-id")).To(Succeed())
   100  
   101  		workingDir, err = os.Getwd()
   102  		Expect(err).NotTo(HaveOccurred())
   103  		Expect(os.Chdir(applicationPath)).To(Succeed())
   104  	})
   105  
   106  	it.After(func() {
   107  		Expect(os.Chdir(workingDir)).To(Succeed())
   108  		Expect(os.Unsetenv("CNB_BINDINGS")).To(Succeed())
   109  		Expect(os.Unsetenv("CNB_STACK_ID")).To(Succeed())
   110  
   111  		Expect(os.RemoveAll(applicationPath)).To(Succeed())
   112  		Expect(os.RemoveAll(buildpackPath)).To(Succeed())
   113  		Expect(os.RemoveAll(buildPlanPath)).To(Succeed())
   114  		Expect(os.RemoveAll(platformPath)).To(Succeed())
   115  	})
   116  
   117  	it("adds contents of CNB_BINDINGS to platform", func() {
   118  		var ctx libcnb.DetectContext
   119  		libpak.Detect(
   120  			func(context libcnb.DetectContext) (libcnb.DetectResult, error) {
   121  				ctx = context
   122  				return libcnb.DetectResult{}, nil
   123  			},
   124  			libcnb.WithArguments([]string{commandPath, platformPath, buildPlanPath}),
   125  			libcnb.WithExitHandler(exitHandler),
   126  		)
   127  
   128  		Expect(ctx.Platform).To(Equal(libcnb.Platform{
   129  			Bindings: libcnb.Bindings{
   130  				"alpha": libcnb.Binding{
   131  					Metadata: map[string]string{
   132  						"test-metadata-key": "test-metadata-value",
   133  					},
   134  					Secret: map[string]string{
   135  						"test-secret-key": "test-secret-value",
   136  					},
   137  				},
   138  				"bravo": libcnb.Binding{
   139  					Metadata: map[string]string{
   140  						"test-metadata-key": "test-metadata-value",
   141  					},
   142  					Secret: map[string]string{
   143  						"test-secret-key": "test-secret-value",
   144  					},
   145  				},
   146  			},
   147  			Environment: map[string]string{},
   148  			Path:        platformPath,
   149  		}))
   150  	})
   151  
   152  	it("handles error from DetectFunc", func() {
   153  		libcnb.Detect(
   154  			func(context libcnb.DetectContext) (libcnb.DetectResult, error) {
   155  				return libcnb.DetectResult{}, fmt.Errorf("test-error")
   156  			},
   157  			libcnb.WithArguments([]string{commandPath, platformPath, buildPlanPath}),
   158  			libcnb.WithExitHandler(exitHandler),
   159  		)
   160  
   161  		Expect(exitHandler.Calls[0].Arguments.Get(0)).To(MatchError("test-error"))
   162  	})
   163  }