github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/internal/inspectimage/writer/bom_json_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 TestJSONBOM(t *testing.T) { 20 color.Disable(true) 21 defer color.Disable(false) 22 spec.Run(t, "JSON BOM Writer", testJSONBOM, spec.Parallel(), spec.Report(report.Terminal{})) 23 } 24 25 func testJSONBOM(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 { 36 "name": "name-1", 37 "version": "version-1", 38 "metadata": { 39 "LocalData": { 40 "String": "", 41 "Bool": false, 42 "Int": 456, 43 "Nested": { 44 "String": "" 45 } 46 } 47 }, 48 "buildpacks": { 49 "id": "test.bp.one.remote", 50 "version": "1.0.0" 51 } 52 } 53 ] 54 }` 55 expectedRemoteOutput = `{ 56 "remote": [ 57 { 58 "name": "name-1", 59 "version": "version-1", 60 "metadata": { 61 "RemoteData": { 62 "String": "aString", 63 "Bool": true, 64 "Int": 123, 65 "Nested": { 66 "String": "anotherString" 67 } 68 } 69 }, 70 "buildpacks": { 71 "id": "test.bp.one.remote", 72 "version": "1.0.0" 73 } 74 } 75 ] 76 }` 77 ) 78 79 when("Print", func() { 80 it.Before(func() { 81 type someData struct { 82 String string 83 Bool bool 84 Int int 85 Nested struct { 86 String string 87 } 88 } 89 90 remoteInfo = &client.ImageInfo{ 91 BOM: []buildpack.BOMEntry{{ 92 Require: buildpack.Require{ 93 Name: "name-1", 94 Version: "version-1", 95 Metadata: map[string]interface{}{ 96 "RemoteData": someData{ 97 String: "aString", 98 Bool: true, 99 Int: 123, 100 Nested: struct { 101 String string 102 }{ 103 String: "anotherString", 104 }, 105 }, 106 }, 107 }, 108 Buildpack: buildpack.GroupElement{ID: "test.bp.one.remote", Version: "1.0.0", Homepage: "https://some-homepage"}, 109 }}} 110 111 localInfo = &client.ImageInfo{ 112 BOM: []buildpack.BOMEntry{{ 113 Require: buildpack.Require{ 114 Name: "name-1", 115 Version: "version-1", 116 Metadata: map[string]interface{}{ 117 "LocalData": someData{ 118 Bool: false, 119 Int: 456, 120 }, 121 }, 122 }, 123 Buildpack: buildpack.GroupElement{ID: "test.bp.one.remote", Version: "1.0.0", Homepage: "https://some-homepage"}, 124 }}, 125 } 126 127 outBuf = bytes.Buffer{} 128 }) 129 130 when("local and remote image exits", func() { 131 it("prints both local and remote image info in a JSON format", func() { 132 jsonBOMWriter := writer.NewJSONBOM() 133 134 logger := logging.NewLogWithWriters(&outBuf, &outBuf) 135 err := jsonBOMWriter.Print(logger, inspectimage.GeneralInfo{}, localInfo, remoteInfo, nil, nil) 136 assert.Nil(err) 137 138 assert.ContainsJSON(outBuf.String(), expectedLocalOutput) 139 assert.ContainsJSON(outBuf.String(), expectedRemoteOutput) 140 }) 141 }) 142 143 when("only local image exists", func() { 144 it("prints local image info in JSON format", func() { 145 jsonBOMWriter := writer.NewJSONBOM() 146 147 logger := logging.NewLogWithWriters(&outBuf, &outBuf) 148 err := jsonBOMWriter.Print(logger, inspectimage.GeneralInfo{}, localInfo, nil, nil, nil) 149 assert.Nil(err) 150 151 assert.ContainsJSON(outBuf.String(), expectedLocalOutput) 152 153 assert.NotContains(outBuf.String(), "test.stack.id.remote") 154 assert.ContainsJSON(outBuf.String(), expectedLocalOutput) 155 }) 156 }) 157 158 when("only remote image exists", func() { 159 it("prints remote image info in JSON format", func() { 160 jsonBOMWriter := writer.NewJSONBOM() 161 162 logger := logging.NewLogWithWriters(&outBuf, &outBuf) 163 err := jsonBOMWriter.Print(logger, inspectimage.GeneralInfo{}, nil, remoteInfo, nil, nil) 164 assert.Nil(err) 165 166 assert.NotContains(outBuf.String(), "test.stack.id.local") 167 assert.ContainsJSON(outBuf.String(), expectedRemoteOutput) 168 }) 169 }) 170 }) 171 }