github.com/metasources/buildx@v0.0.0-20230418141019-7aa1459cedea/test/cli/convert_cmd_test.go (about)

     1  package cli
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestConvertCmd(t *testing.T) {
    10  	assertions := []traitAssertion{
    11  		assertInOutput("PackageName: musl-utils"),
    12  		assertSuccessfulReturnCode,
    13  	}
    14  
    15  	tests := []struct {
    16  		from string
    17  		to   string
    18  	}{
    19  		{from: "buildx-json", to: "spdx-tag-value"},
    20  	}
    21  
    22  	for _, test := range tests {
    23  		t.Run(fmt.Sprintf("from %s to %s", test.from, test.to), func(t *testing.T) {
    24  			sbomArgs := []string{"dir:./test-fixtures/image-pkg-coverage", "-o", test.from}
    25  			cmd, stdout, stderr := runBuildx(t, nil, sbomArgs...)
    26  			if cmd.ProcessState.ExitCode() != 0 {
    27  				t.Log("STDOUT:\n", stdout)
    28  				t.Log("STDERR:\n", stderr)
    29  				t.Log("COMMAND:", strings.Join(cmd.Args, " "))
    30  				t.Fatalf("failure executing buildx creating an sbom")
    31  				return
    32  			}
    33  
    34  			convertArgs := []string{"convert", "-", "-o", test.to}
    35  			cmd = getBuildxCommand(t, convertArgs...)
    36  
    37  			cmd.Stdin = strings.NewReader(stdout)
    38  			stdout, stderr = runCommandObj(t, cmd, nil, false)
    39  
    40  			for _, traitFn := range assertions {
    41  				traitFn(t, stdout, stderr, cmd.ProcessState.ExitCode())
    42  			}
    43  			logOutputOnFailure(t, cmd, stdout, stderr)
    44  		})
    45  	}
    46  }