get.porter.sh/porter@v1.3.0/tests/integration/runner_integration_test.go (about)

     1  //go:build integration
     2  
     3  package integration
     4  
     5  import (
     6  	"bytes"
     7  	"context"
     8  	"io"
     9  	"path/filepath"
    10  	"testing"
    11  
    12  	"get.porter.sh/porter/pkg/pkgmgmt"
    13  	"get.porter.sh/porter/pkg/pkgmgmt/client"
    14  	"get.porter.sh/porter/pkg/portercontext"
    15  	"get.porter.sh/porter/pkg/test"
    16  	"get.porter.sh/porter/tests"
    17  	"github.com/stretchr/testify/assert"
    18  	"github.com/stretchr/testify/require"
    19  )
    20  
    21  func TestRunner_Run(t *testing.T) {
    22  	ctx := context.Background()
    23  	// Provide a way for tests to capture stdout
    24  	output := &bytes.Buffer{}
    25  
    26  	c := portercontext.NewTestContext(t)
    27  	c.UseFilesystem()
    28  	binDir := c.FindBinDir()
    29  
    30  	// I'm not using the TestRunner because I want to use the current filesystem, not an isolated one
    31  	r := client.NewRunner("exec", filepath.Join(binDir, "mixins/exec"), false)
    32  
    33  	// Capture the output
    34  	r.Out = output
    35  	r.Err = output
    36  
    37  	err := r.Validate()
    38  	require.NoError(t, err)
    39  
    40  	cmd := pkgmgmt.CommandOptions{
    41  		Command: "install",
    42  		File:    "testdata/exec_input.yaml",
    43  	}
    44  	err = r.Run(ctx, cmd)
    45  	assert.NoError(t, err)
    46  	assert.Contains(t, string(output.Bytes()), "Hello World")
    47  }
    48  
    49  func TestRunner_RunWithMaskedOutput(t *testing.T) {
    50  	ctx := context.Background()
    51  
    52  	// Provide a way for tests to capture stdout
    53  	output := &bytes.Buffer{}
    54  
    55  	// Copy output to the test log simultaneously, use go test -v to see the output
    56  	aggOutput := io.MultiWriter(output, test.Logger{T: t})
    57  
    58  	// Supply an CensoredWriter with values needing masking
    59  	censoredWriter := portercontext.NewCensoredWriter(aggOutput)
    60  	sensitiveValues := []string{"World"}
    61  	// Add some whitespace values as well, to be sure writer does not replace
    62  	sensitiveValues = append(sensitiveValues, " ", "", "\n", "\r", "\t")
    63  	censoredWriter.SetSensitiveValues(sensitiveValues)
    64  
    65  	c := portercontext.NewTestContext(t)
    66  	c.UseFilesystem()
    67  	binDir := c.FindBinDir()
    68  
    69  	// I'm not using the TestRunner because I want to use the current filesystem, not an isolated one
    70  	r := client.NewRunner("exec", filepath.Join(binDir, "mixins/exec"), false)
    71  
    72  	// Capture the output
    73  	r.Out = censoredWriter
    74  	r.Err = censoredWriter
    75  
    76  	err := r.Validate()
    77  	require.NoError(t, err)
    78  
    79  	cmd := pkgmgmt.CommandOptions{
    80  		Command: "install",
    81  		File:    "testdata/exec_input_with_whitespace.yaml",
    82  	}
    83  
    84  	err = r.Run(ctx, cmd)
    85  	assert.NoError(t, err)
    86  	tests.RequireOutputContains(t, output.String(), "Hello *******")
    87  }