github.com/paketo-buildpacks/libpak@v1.70.0/main_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  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"github.com/buildpacks/libcnb"
    25  	"github.com/buildpacks/libcnb/mocks"
    26  	. "github.com/onsi/gomega"
    27  	"github.com/sclevine/spec"
    28  	"github.com/stretchr/testify/mock"
    29  
    30  	"github.com/paketo-buildpacks/libpak"
    31  )
    32  
    33  func testMain(t *testing.T, context spec.G, it spec.S) {
    34  	var (
    35  		Expect = NewWithT(t).Expect
    36  
    37  		applicationPath   string
    38  		builder           *mocks.Builder
    39  		buildpackPath     string
    40  		buildpackPlanPath string
    41  		buildPlanPath     string
    42  		detector          *mocks.Detector
    43  		environmentWriter *mocks.EnvironmentWriter
    44  		exitHandler       *mocks.ExitHandler
    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 = t.TempDir()
    56  		applicationPath, err = filepath.EvalSymlinks(applicationPath)
    57  		Expect(err).NotTo(HaveOccurred())
    58  
    59  		builder = &mocks.Builder{}
    60  
    61  		buildpackPath = t.TempDir()
    62  		Expect(os.Setenv("CNB_BUILDPACK_DIR", buildpackPath)).To(Succeed())
    63  
    64  		Expect(os.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
    65  			[]byte(`
    66  api = "0.6"
    67  
    68  [buildpack]
    69  id = "test-id"
    70  name = "test-name"
    71  version = "1.1.1"
    72  clear-env = true
    73  
    74  [[order]]
    75  [[order.group]]
    76  id = "test-id"
    77  version = "2.2.2"
    78  optional = true
    79  
    80  [[stacks]]
    81  id = "test-id"
    82  mixins = ["test-name"]
    83  
    84  [metadata]
    85  test-key = "test-value"
    86  `),
    87  			0644),
    88  		).To(Succeed())
    89  
    90  		f, err := os.CreateTemp("", "main-buildpackplan-path")
    91  		Expect(err).NotTo(HaveOccurred())
    92  		Expect(f.Close()).NotTo(HaveOccurred())
    93  		buildpackPlanPath = f.Name()
    94  
    95  		Expect(os.WriteFile(buildpackPlanPath,
    96  			[]byte(`
    97  [[entries]]
    98  name = "test-name"
    99  version = "test-version"
   100  
   101  [entries.metadata]
   102  test-key = "test-value"
   103  `),
   104  			0644),
   105  		).To(Succeed())
   106  
   107  		f, err = os.CreateTemp("", "main-buildplan-path")
   108  		Expect(err).NotTo(HaveOccurred())
   109  		Expect(f.Close()).NotTo(HaveOccurred())
   110  		buildPlanPath = f.Name()
   111  
   112  		detector = &mocks.Detector{}
   113  
   114  		environmentWriter = &mocks.EnvironmentWriter{}
   115  		environmentWriter.On("Write", mock.Anything, mock.Anything).Return(nil)
   116  
   117  		exitHandler = &mocks.ExitHandler{}
   118  		exitHandler.On("Error", mock.Anything)
   119  		exitHandler.On("Pass", mock.Anything)
   120  		exitHandler.On("Fail", mock.Anything)
   121  
   122  		layersPath = t.TempDir()
   123  
   124  		Expect(os.WriteFile(filepath.Join(layersPath, "store.toml"),
   125  			[]byte(`
   126  [metadata]
   127  test-key = "test-value"
   128  `),
   129  			0644),
   130  		).To(Succeed())
   131  
   132  		platformPath = t.TempDir()
   133  
   134  		Expect(os.MkdirAll(filepath.Join(platformPath, "bindings", "alpha", "metadata"), 0755)).To(Succeed())
   135  		Expect(os.WriteFile(
   136  			filepath.Join(platformPath, "bindings", "alpha", "metadata", "test-metadata-key"),
   137  			[]byte("test-metadata-value"),
   138  			0644,
   139  		)).To(Succeed())
   140  		Expect(os.MkdirAll(filepath.Join(platformPath, "bindings", "alpha", "secret"), 0755)).To(Succeed())
   141  		Expect(os.WriteFile(
   142  			filepath.Join(platformPath, "bindings", "alpha", "secret", "test-secret-key"),
   143  			[]byte("test-secret-value"),
   144  			0644,
   145  		)).To(Succeed())
   146  
   147  		Expect(os.MkdirAll(filepath.Join(platformPath, "env"), 0755)).To(Succeed())
   148  		Expect(os.WriteFile(filepath.Join(platformPath, "env", "TEST_ENV"), []byte("test-value"), 0644)).
   149  			To(Succeed())
   150  
   151  		tomlWriter = &mocks.TOMLWriter{}
   152  		tomlWriter.On("Write", mock.Anything, mock.Anything).Return(nil)
   153  
   154  		Expect(os.Setenv("CNB_STACK_ID", "test-stack-id")).To(Succeed())
   155  
   156  		workingDir, err = os.Getwd()
   157  		Expect(err).NotTo(HaveOccurred())
   158  		Expect(os.Chdir(applicationPath)).To(Succeed())
   159  	})
   160  
   161  	it.After(func() {
   162  		Expect(os.Chdir(workingDir)).To(Succeed())
   163  		Expect(os.Unsetenv("CNB_BUILDPACK_DIR")).To(Succeed())
   164  		Expect(os.Unsetenv("CNB_STACK_ID")).To(Succeed())
   165  
   166  		Expect(os.RemoveAll(applicationPath)).To(Succeed())
   167  		Expect(os.RemoveAll(buildpackPath)).To(Succeed())
   168  		Expect(os.RemoveAll(buildpackPlanPath)).To(Succeed())
   169  		Expect(os.RemoveAll(layersPath)).To(Succeed())
   170  		Expect(os.RemoveAll(platformPath)).To(Succeed())
   171  	})
   172  
   173  	it("encounters the wrong number of arguments", func() {
   174  		libpak.Main(detector, builder,
   175  			libcnb.WithArguments([]string{}),
   176  			libcnb.WithExitHandler(exitHandler),
   177  		)
   178  
   179  		Expect(exitHandler.Calls[0].Arguments.Get(0)).To(MatchError("expected command name"))
   180  	})
   181  
   182  	it("calls builder for build command", func() {
   183  		builder.On("Build", mock.Anything).Return(libcnb.NewBuildResult(), nil)
   184  		commandPath := filepath.Join("bin", "build")
   185  
   186  		libpak.Main(detector, builder,
   187  			libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
   188  			libcnb.WithExitHandler(exitHandler),
   189  		)
   190  
   191  		Expect(exitHandler.Calls).To(BeEmpty())
   192  	})
   193  
   194  	it("calls detector for detect command", func() {
   195  		detector.On("Detect", mock.Anything).Return(libcnb.DetectResult{Pass: true}, nil)
   196  		commandPath := filepath.Join("bin", "detect")
   197  
   198  		libpak.Main(detector, builder,
   199  			libcnb.WithArguments([]string{commandPath, platformPath, buildPlanPath}),
   200  			libcnb.WithExitHandler(exitHandler),
   201  		)
   202  	})
   203  
   204  	it("calls exitHandler.Pass() on detection pass", func() {
   205  		detector.On("Detect", mock.Anything).Return(libcnb.DetectResult{Pass: true}, nil)
   206  		commandPath := filepath.Join("bin", "detect")
   207  
   208  		libpak.Main(detector, builder,
   209  			libcnb.WithArguments([]string{commandPath, platformPath, buildPlanPath}),
   210  			libcnb.WithExitHandler(exitHandler),
   211  		)
   212  
   213  		Expect(exitHandler.Calls[0].Method).To(BeIdenticalTo("Pass"))
   214  	})
   215  
   216  	it("calls exitHandler.Fail() on detection fail", func() {
   217  		detector.On("Detect", mock.Anything).Return(libcnb.DetectResult{Pass: false}, nil)
   218  		commandPath := filepath.Join("bin", "detect")
   219  
   220  		libpak.Main(detector, builder,
   221  			libcnb.WithArguments([]string{commandPath, platformPath, buildPlanPath}),
   222  			libcnb.WithExitHandler(exitHandler),
   223  		)
   224  
   225  		Expect(exitHandler.Calls[0].Method).To(BeIdenticalTo("Fail"))
   226  	})
   227  
   228  	it("encounters an unknown command", func() {
   229  		commandPath := filepath.Join("bin", "test-command")
   230  
   231  		libpak.Main(detector, builder,
   232  			libcnb.WithArguments([]string{commandPath}),
   233  			libcnb.WithExitHandler(exitHandler),
   234  		)
   235  
   236  		Expect(exitHandler.Calls[0].Arguments.Get(0)).To(MatchError("unsupported command test-command"))
   237  	})
   238  
   239  }