github.com/metasources/buildx@v0.0.0-20230418141019-7aa1459cedea/test/cli/all_formats_convertible_test.go (about) 1 package cli 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "strings" 8 "testing" 9 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestAllFormatsConvertable(t *testing.T) { 14 assertions := []traitAssertion{ 15 assertStdoutLengthGreaterThan(1000), 16 assertSuccessfulReturnCode, 17 } 18 19 tests := []struct { 20 to string 21 from string 22 template string 23 env map[string]string 24 }{ 25 {to: "buildx-json", from: "spdx-json"}, 26 {to: "buildx-json", from: "cyclonedx-json"}, 27 {to: "spdx-json", from: "buildx-json"}, 28 {to: "template", from: "buildx-json", template: "test-fixtures/csv.template"}, 29 {to: "spdx-json", from: "cyclonedx-json"}, 30 {to: "cyclonedx-json", from: "buildx-json"}, 31 {to: "cyclonedx-json", from: "spdx-json"}, 32 } 33 34 for _, test := range tests { 35 t.Run(fmt.Sprintf("from %s to %s", test.from, test.to), func(t *testing.T) { 36 sbomArgs := []string{"dir:./test-fixtures/image-pkg-coverage", "-o", test.from} 37 cmd, stdout, stderr := runBuildx(t, test.env, sbomArgs...) 38 if cmd.ProcessState.ExitCode() != 0 { 39 t.Log("STDOUT:\n", stdout) 40 t.Log("STDERR:\n", stderr) 41 t.Log("COMMAND:", strings.Join(cmd.Args, " ")) 42 t.Fatalf("failure executing buildx creating an sbom") 43 return 44 } 45 46 tempDir := t.TempDir() 47 sbomFile := filepath.Join(tempDir, "sbom.json") 48 require.NoError(t, os.WriteFile(sbomFile, []byte(stdout), 0666)) 49 50 convertArgs := []string{"convert", sbomFile, "-o", test.to} 51 if test.template != "" { 52 convertArgs = append(convertArgs, "--template", test.template) 53 } 54 cmd, stdout, stderr = runBuildx(t, test.env, convertArgs...) 55 for _, traitFn := range assertions { 56 traitFn(t, stdout, stderr, cmd.ProcessState.ExitCode()) 57 } 58 logOutputOnFailure(t, cmd, stdout, stderr) 59 }) 60 } 61 }