github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/internal/inspectimage/writer/bom_yaml_test.go (about)

     1  package writer_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/buildpacks/lifecycle/buildpack"
     8  	"github.com/heroku/color"
     9  	"github.com/sclevine/spec"
    10  	"github.com/sclevine/spec/report"
    11  
    12  	"github.com/buildpacks/pack/internal/inspectimage"
    13  	"github.com/buildpacks/pack/internal/inspectimage/writer"
    14  	"github.com/buildpacks/pack/pkg/client"
    15  	"github.com/buildpacks/pack/pkg/logging"
    16  	h "github.com/buildpacks/pack/testhelpers"
    17  )
    18  
    19  func TestYAMLBOM(t *testing.T) {
    20  	color.Disable(true)
    21  	defer color.Disable(false)
    22  	spec.Run(t, "YAML BOM Writer", testYAMLBOM, spec.Parallel(), spec.Report(report.Terminal{}))
    23  }
    24  
    25  func testYAMLBOM(t *testing.T, when spec.G, it spec.S) {
    26  	var (
    27  		assert = h.NewAssertionManager(t)
    28  		outBuf bytes.Buffer
    29  
    30  		remoteInfo *client.ImageInfo
    31  		localInfo  *client.ImageInfo
    32  
    33  		expectedLocalOutput = `---
    34  local:
    35  - name: name-1
    36    version: version-1
    37    metadata:
    38      LocalData:
    39        string: ''
    40        bool: false
    41        int: 456
    42        nested:
    43          string: ''
    44    buildpacks:
    45      id: test.bp.one.remote
    46      version: 1.0.0
    47  `
    48  		expectedRemoteOutput = `---
    49  remote:
    50  - name: name-1
    51    version: version-1
    52    metadata:
    53      RemoteData:
    54        string: aString
    55        bool: true
    56        int: 123
    57        nested:
    58          string: anotherString
    59    buildpacks:
    60      id: test.bp.one.remote
    61      version: 1.0.0
    62  `
    63  	)
    64  
    65  	when("Print", func() {
    66  		it.Before(func() {
    67  			type someData struct {
    68  				String string
    69  				Bool   bool
    70  				Int    int
    71  				Nested struct {
    72  					String string
    73  				}
    74  			}
    75  
    76  			remoteInfo = &client.ImageInfo{
    77  				BOM: []buildpack.BOMEntry{{
    78  					Require: buildpack.Require{
    79  						Name:    "name-1",
    80  						Version: "version-1",
    81  						Metadata: map[string]interface{}{
    82  							"RemoteData": someData{
    83  								String: "aString",
    84  								Bool:   true,
    85  								Int:    123,
    86  								Nested: struct {
    87  									String string
    88  								}{
    89  									String: "anotherString",
    90  								},
    91  							},
    92  						},
    93  					},
    94  					Buildpack: buildpack.GroupElement{ID: "test.bp.one.remote", Version: "1.0.0", Homepage: "https://some-homepage"},
    95  				}}}
    96  
    97  			localInfo = &client.ImageInfo{
    98  				BOM: []buildpack.BOMEntry{{
    99  					Require: buildpack.Require{
   100  						Name:    "name-1",
   101  						Version: "version-1",
   102  						Metadata: map[string]interface{}{
   103  							"LocalData": someData{
   104  								Bool: false,
   105  								Int:  456,
   106  							},
   107  						},
   108  					},
   109  					Buildpack: buildpack.GroupElement{ID: "test.bp.one.remote", Version: "1.0.0", Homepage: "https://some-homepage"},
   110  				}},
   111  			}
   112  
   113  			outBuf = bytes.Buffer{}
   114  		})
   115  
   116  		when("local and remote image exits", func() {
   117  			it("prints both local and remote image info in a YAML format", func() {
   118  				yamlBOMWriter := writer.NewYAMLBOM()
   119  
   120  				logger := logging.NewLogWithWriters(&outBuf, &outBuf)
   121  				err := yamlBOMWriter.Print(logger, inspectimage.GeneralInfo{}, localInfo, remoteInfo, nil, nil)
   122  				assert.Nil(err)
   123  
   124  				assert.ContainsYAML(outBuf.String(), expectedLocalOutput)
   125  				assert.ContainsYAML(outBuf.String(), expectedRemoteOutput)
   126  			})
   127  		})
   128  
   129  		when("only local image exists", func() {
   130  			it("prints local image info in YAML format", func() {
   131  				yamlBOMWriter := writer.NewYAMLBOM()
   132  
   133  				logger := logging.NewLogWithWriters(&outBuf, &outBuf)
   134  				err := yamlBOMWriter.Print(logger, inspectimage.GeneralInfo{}, localInfo, nil, nil, nil)
   135  				assert.Nil(err)
   136  
   137  				assert.ContainsYAML(outBuf.String(), expectedLocalOutput)
   138  
   139  				assert.NotContains(outBuf.String(), "test.stack.id.remote")
   140  				assert.ContainsYAML(outBuf.String(), expectedLocalOutput)
   141  			})
   142  		})
   143  
   144  		when("only remote image exists", func() {
   145  			it("prints remote image info in YAML format", func() {
   146  				yamlBOMWriter := writer.NewYAMLBOM()
   147  
   148  				logger := logging.NewLogWithWriters(&outBuf, &outBuf)
   149  				err := yamlBOMWriter.Print(logger, inspectimage.GeneralInfo{}, nil, remoteInfo, nil, nil)
   150  				assert.Nil(err)
   151  
   152  				assert.NotContains(outBuf.String(), "test.stack.id.local")
   153  				assert.ContainsYAML(outBuf.String(), expectedRemoteOutput)
   154  			})
   155  		})
   156  	})
   157  }