github.com/cloudfoundry/libcfbuildpack@v1.91.23/layers/layers_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 layers_test
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	layersBp "github.com/buildpack/libbuildpack/layers"
    26  	loggerBp "github.com/buildpack/libbuildpack/logger"
    27  	"github.com/cloudfoundry/libcfbuildpack/buildpack"
    28  	"github.com/cloudfoundry/libcfbuildpack/layers"
    29  	"github.com/cloudfoundry/libcfbuildpack/logger"
    30  	"github.com/cloudfoundry/libcfbuildpack/test"
    31  	"github.com/fatih/color"
    32  	"github.com/onsi/gomega"
    33  	"github.com/sclevine/spec"
    34  	"github.com/sclevine/spec/report"
    35  )
    36  
    37  func TestLayers(t *testing.T) {
    38  	spec.Run(t, "Layers", func(t *testing.T, _ spec.G, it spec.S) {
    39  
    40  		g := gomega.NewWithT(t)
    41  
    42  		var (
    43  			root string
    44  			info bytes.Buffer
    45  			l    layers.Layers
    46  		)
    47  
    48  		it.Before(func() {
    49  			root = test.ScratchDir(t, "layers")
    50  			logger := logger.Logger{Logger: loggerBp.NewLogger(nil, &info)}
    51  			l = layers.NewLayers(layersBp.Layers{Root: root}, layersBp.Layers{}, buildpack.Buildpack{}, logger)
    52  		})
    53  
    54  		it("logs process types", func() {
    55  			g.Expect(l.WriteApplicationMetadata(layers.Metadata{
    56  				Processes: []layers.Process{
    57  					{Type: "short", Command: "test-command-1"},
    58  					{Type: "a-very-long-type", Command: "test-command-2"},
    59  				},
    60  			})).To(gomega.Succeed())
    61  
    62  			actual := info.String()
    63  			expected := fmt.Sprintf(`  Process types:
    64      %s: test-command-2
    65      %s:            test-command-1
    66  `, color.CyanString("a-very-long-type"), color.CyanString("short"))
    67  			g.Expect(actual).To(gomega.Equal(expected))
    68  		})
    69  
    70  		it("logs number of slices", func() {
    71  			g.Expect(l.WriteApplicationMetadata(layers.Metadata{
    72  				Slices: layers.Slices{
    73  					layers.Slice{},
    74  					layers.Slice{},
    75  				},
    76  			})).To(gomega.Succeed())
    77  
    78  			g.Expect(info.String()).To(gomega.Equal("  2 application slices\n"))
    79  		})
    80  
    81  		it("registers touched layers", func() {
    82  			test.TouchFile(t, l.Root, "test-layer-1.toml")
    83  			test.TouchFile(t, l.Root, "test-layer-2.toml")
    84  
    85  			g.Expect(l.Layer("test-layer-1").Contribute(nil, func(layer layers.Layer) error {
    86  				return nil
    87  			})).To(gomega.Succeed())
    88  
    89  			g.Expect(l.TouchedLayers.Cleanup()).To(gomega.Succeed())
    90  			g.Expect(filepath.Join(l.Root, "test-layer-1.toml")).To(gomega.BeAnExistingFile())
    91  			g.Expect(filepath.Join(l.Root, "test-layer-2.toml")).NotTo(gomega.BeAnExistingFile())
    92  		})
    93  	}, spec.Report(report.Terminal{}))
    94  }