get.porter.sh/porter@v1.3.0/pkg/porter/outputs_test.go (about)

     1  package porter
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"os"
     7  	"testing"
     8  
     9  	"get.porter.sh/porter/pkg/cnab"
    10  	"get.porter.sh/porter/pkg/printer"
    11  	"get.porter.sh/porter/pkg/storage"
    12  	"github.com/cnabio/cnab-go/bundle"
    13  	"github.com/cnabio/cnab-go/bundle/definition"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestPorter_printOutputsTable(t *testing.T) {
    18  	t.Parallel()
    19  
    20  	b, err := os.ReadFile("testdata/show/object-parameter-value.json")
    21  	require.NoError(t, err)
    22  	var objVal map[string]interface{}
    23  	err = json.Unmarshal(b, &objVal)
    24  	require.NoError(t, err)
    25  
    26  	p := NewTestPorter(t)
    27  	defer p.Close()
    28  
    29  	want := `---------------------------------------------------------------------------------
    30    Name     Type    Value                                                         
    31  ---------------------------------------------------------------------------------
    32    bar      string  ******                                                        
    33    foo      string  /path/to/foo                                                  
    34    object   object  {"a":{"b":1,"c":2},"d":"yay"}                                 
    35    longfoo  string  DFo6Wc2jDhmA7Yt4PbHyh8RO4vVG7leOzK412gf2TXNPJhuCUs1rB29nk...  
    36  `
    37  
    38  	outputs := DisplayValues{
    39  		{Name: "bar", Type: "string", Value: "bar-value", Sensitive: true},
    40  		{Name: "foo", Type: "string", Value: "/path/to/foo"},
    41  		{Name: "object", Type: "object", Value: objVal},
    42  		{Name: "longfoo", Type: "string", Value: "DFo6Wc2jDhmA7Yt4PbHyh8RO4vVG7leOzK412gf2TXNPJhuCUs1rB29nkJJd4ICimZGpyWpMGalSvDxf"},
    43  	}
    44  	err = p.printDisplayValuesTable(outputs)
    45  	require.NoError(t, err)
    46  
    47  	got := p.TestConfig.TestContext.GetOutput()
    48  	require.Equal(t, want, got)
    49  }
    50  
    51  func TestPorter_PrintBundleOutputs(t *testing.T) {
    52  	t.Parallel()
    53  
    54  	testcases := []struct {
    55  		name           string
    56  		format         printer.Format
    57  		expectedOutput string
    58  	}{
    59  		{name: "text", format: printer.FormatPlaintext, expectedOutput: "testdata/outputs/show-expected-output.txt"},
    60  		{name: "json", format: printer.FormatJson, expectedOutput: "testdata/outputs/show-expected-output.json"},
    61  		{name: "yaml", format: printer.FormatYaml, expectedOutput: "testdata/outputs/show-expected-output.yaml"},
    62  	}
    63  
    64  	for _, tc := range testcases {
    65  		t.Run(tc.name, func(t *testing.T) {
    66  			p := NewTestPorter(t)
    67  			defer p.Close()
    68  
    69  			// Create test claim
    70  			writeOnly := true
    71  			b := bundle.Bundle{
    72  				Definitions: definition.Definitions{
    73  					"foo": &definition.Schema{
    74  						Type:      "string",
    75  						WriteOnly: &writeOnly,
    76  					},
    77  					"bar": &definition.Schema{
    78  						Type: "string",
    79  					},
    80  					"longfoo": &definition.Schema{
    81  						Type: "string",
    82  					},
    83  					"porter-state": &definition.Schema{
    84  						Type:    "string",
    85  						Comment: "porter-internal", // This output should be hidden because it's internal
    86  					},
    87  				},
    88  				Outputs: map[string]bundle.Output{
    89  					"foo": {
    90  						Definition: "foo",
    91  						Path:       "/path/to/foo",
    92  					},
    93  					"bar": {
    94  						Definition: "bar",
    95  					},
    96  					"longfoo": {
    97  						Definition: "longfoo",
    98  					},
    99  					"porter-state": {
   100  						Definition: "porter-state",
   101  						Path:       "/cnab/app/outputs/porter-state.tgz",
   102  					},
   103  				},
   104  			}
   105  
   106  			extB := cnab.NewBundle(b)
   107  			i := p.TestInstallations.CreateInstallation(storage.NewInstallation("", "test"), func(i *storage.Installation) {
   108  				i.Parameters.Parameters = p.SanitizeParameters(i.Parameters.Parameters, i.ID, extB)
   109  			})
   110  			c := p.TestInstallations.CreateRun(i.NewRun(cnab.ActionInstall, extB), func(r *storage.Run) {
   111  				r.Bundle = b
   112  				r.ParameterOverrides.Parameters = p.SanitizeParameters(r.ParameterOverrides.Parameters, r.ID, extB)
   113  			})
   114  			r := p.TestInstallations.CreateResult(c.NewResult(cnab.StatusSucceeded))
   115  			p.CreateOutput(r.NewOutput("foo", []byte("foo-output")), extB)
   116  			p.CreateOutput(r.NewOutput("bar", []byte("bar-output")), extB)
   117  			p.CreateOutput(r.NewOutput("longfoo", []byte("DFo6Wc2jDhmA7Yt4PbHyh8RO4vVG7leOzK412gf2TXNPJhuCUs1rB29nkJJd4ICimZGpyWpMGalSvDxf")), extB)
   118  			p.CreateOutput(r.NewOutput("porter-state", []byte("porter-state.tgz contents")), extB)
   119  
   120  			opts := OutputListOptions{
   121  				installationOptions: installationOptions{
   122  					Name: "test",
   123  				},
   124  				PrintOptions: printer.PrintOptions{
   125  					Format: tc.format,
   126  				},
   127  			}
   128  			err := p.PrintBundleOutputs(context.Background(), opts)
   129  			require.NoError(t, err, "could not print bundle outputs")
   130  
   131  			p.CompareGoldenFile(tc.expectedOutput, p.TestConfig.TestContext.GetOutput())
   132  		})
   133  	}
   134  }