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

     1  //go:build integration
     2  
     3  package integration
     4  
     5  import (
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"get.porter.sh/porter/pkg"
    11  	"get.porter.sh/porter/pkg/porter"
    12  	"get.porter.sh/porter/pkg/secrets"
    13  	"get.porter.sh/porter/pkg/storage"
    14  	"github.com/cnabio/cnab-go/secrets/host"
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func TestInstall_relativePathPorterHome(t *testing.T) {
    20  	t.Parallel()
    21  
    22  	p := porter.NewTestPorter(t)
    23  	defer p.Close()
    24  	ctx := p.SetupIntegrationTest() // This creates a temp porter home directory
    25  
    26  	// Crux for this test: change Porter's home dir to a relative path
    27  	homeDir, err := p.Config.GetHomeDir()
    28  	require.NoError(t, err)
    29  	relDir, err := filepath.Rel(p.Getwd(), homeDir)
    30  	require.NoError(t, err)
    31  	p.SetHomeDir(relDir)
    32  
    33  	// Bring in a porter manifest that has an install action defined
    34  	p.AddTestBundleDir("testdata/bundles/bundle-with-custom-action", true)
    35  
    36  	installOpts := porter.NewInstallOptions()
    37  	err = installOpts.Validate(ctx, []string{}, p.Porter)
    38  	require.NoError(t, err)
    39  
    40  	// Install the bundle, assert no error occurs due to Porter home as relative path
    41  	err = p.InstallBundle(ctx, installOpts)
    42  	require.NoError(t, err)
    43  }
    44  
    45  func TestInstall_fileParam(t *testing.T) {
    46  	t.Parallel()
    47  
    48  	p := porter.NewTestPorter(t)
    49  	defer p.Close()
    50  	ctx := p.SetupIntegrationTest()
    51  
    52  	bundleName := p.AddTestBundleDir("testdata/bundles/bundle-with-file-params", false)
    53  
    54  	installOpts := porter.NewInstallOptions()
    55  	installOpts.Params = []string{"myfile=./myfile"}
    56  	installOpts.ParameterSets = []string{"myparam"}
    57  	testParamSets := storage.NewParameterSet("", "myparam", secrets.SourceMap{
    58  		Name: "myotherfile",
    59  		Source: secrets.Source{
    60  			Strategy: host.SourcePath,
    61  			Hint:     "./myotherfile",
    62  		},
    63  	},
    64  		secrets.SourceMap{
    65  			Name: "yetanotherfile",
    66  			Source: secrets.Source{
    67  				Strategy: host.SourceEnv,
    68  				Hint:     "YET_ANOTHER_FILE_PATH",
    69  			},
    70  		})
    71  
    72  	os.Setenv("YET_ANOTHER_FILE_PATH", "./myfile")
    73  	defer os.Unsetenv("YET_ANOTHER_FILE_PATH")
    74  
    75  	p.TestParameters.InsertParameterSet(ctx, testParamSets)
    76  
    77  	err := installOpts.Validate(ctx, []string{}, p.Porter)
    78  	require.NoError(t, err)
    79  
    80  	err = p.InstallBundle(ctx, installOpts)
    81  	require.NoError(t, err)
    82  
    83  	output := p.TestConfig.TestContext.GetOutput()
    84  	require.Contains(t, output, "Hello World!", "expected action output to contain provided file contents")
    85  
    86  	outputs, err := p.Installations.GetLastOutputs(ctx, "", bundleName)
    87  	require.NoError(t, err, "GetLastOutput failed")
    88  	myfile, ok := outputs.GetByName("myfile")
    89  	require.True(t, ok, "expected myfile output to be persisted")
    90  	assert.Equal(t, "Hello World!", string(myfile.Value), "expected output to match the decoded file contents")
    91  	myotherfile, ok := outputs.GetByName("myotherfile")
    92  	require.True(t, ok, "expected myotherfile output to be persisted")
    93  	assert.Equal(t, "Hello Other World!", string(myotherfile.Value), "expected output 'myotherfile' to match the decoded file contents")
    94  	yetanotherfile, ok := outputs.GetByName("yetanotherfile")
    95  	require.True(t, ok, "expected yetanotherfile output to be persisted")
    96  	assert.Equal(t, "Hello World!", string(yetanotherfile.Value), "expected output 'yetanotherfile' to match the decoded file contents")
    97  }
    98  
    99  func TestInstall_withDockerignore(t *testing.T) {
   100  	t.Parallel()
   101  
   102  	p := porter.NewTestPorter(t)
   103  	defer p.Close()
   104  	ctx := p.SetupIntegrationTest()
   105  
   106  	p.AddTestBundleDir("testdata/bundles/outputs-example", true)
   107  
   108  	// Create .dockerignore file which ignores the helpers script
   109  	err := p.FileSystem.WriteFile(".dockerignore", []byte("helpers.sh"), pkg.FileModeWritable)
   110  	require.NoError(t, err)
   111  
   112  	opts := porter.NewInstallOptions()
   113  	err = opts.Validate(ctx, []string{}, p.Porter)
   114  	require.NoError(t, err)
   115  
   116  	// Verify Porter uses the .dockerignore file (no helpers script added to installer image)
   117  	err = p.InstallBundle(ctx, opts)
   118  	// The following line would be seen from the daemon, but is printed directly to stdout:
   119  	// Error: couldn't run command ./helpers.sh dump-config: fork/exec ./helpers.sh: no such file or directory
   120  	// We should check this once https://github.com/cnabio/cnab-go/issues/78 is closed
   121  	require.EqualError(t, err, "1 error occurred:\n\t* container exit code: 1, message: <nil>\n\n")
   122  }
   123  
   124  func TestInstall_stringParam(t *testing.T) {
   125  
   126  	p := porter.NewTestPorter(t)
   127  	defer p.Close()
   128  	ctx := p.SetupIntegrationTest()
   129  
   130  	p.AddTestBundleDir("testdata/bundles/bundle-with-string-params", false)
   131  
   132  	installOpts := porter.NewInstallOptions()
   133  	installOpts.Params = []string{"name=Demo Time"}
   134  
   135  	err := installOpts.Validate(ctx, []string{}, p.Porter)
   136  	require.NoError(t, err)
   137  
   138  	err = p.InstallBundle(ctx, installOpts)
   139  	require.NoError(t, err)
   140  
   141  	output := p.TestConfig.TestContext.GetOutput()
   142  	require.Contains(t, output, "Hello, Demo Time", "expected action output to contain provided file contents")
   143  }
   144  
   145  func TestInstall_overrideParamIncludedInParamSet(t *testing.T) {
   146  
   147  	p := porter.NewTestPorter(t)
   148  	defer p.Close()
   149  	ctx := p.SetupIntegrationTest()
   150  
   151  	p.AddTestBundleDir("testdata/bundles/bundle-with-string-params", false)
   152  
   153  	installOpts := porter.NewInstallOptions()
   154  	installOpts.Params = []string{"name=Demo Time"}
   155  	installOpts.ParameterSets = []string{"myparam"}
   156  	testParamSets := storage.NewParameterSet("", "myparam", secrets.SourceMap{
   157  		Name: "name",
   158  		Source: secrets.Source{
   159  			Strategy: host.SourceEnv,
   160  			Hint:     "DEMO_TIME",
   161  		},
   162  	})
   163  	p.TestParameters.InsertParameterSet(ctx, testParamSets)
   164  
   165  	err := installOpts.Validate(ctx, []string{}, p.Porter)
   166  	require.NoError(t, err)
   167  
   168  	err = p.InstallBundle(ctx, installOpts)
   169  	require.NoError(t, err)
   170  
   171  	output := p.TestConfig.TestContext.GetOutput()
   172  	require.Contains(t, output, "Hello, Demo Time", "expected action output to contain provided file contents")
   173  }
   174  
   175  func TestInstall_SensitiveValuesAreNotLogged(t *testing.T) {
   176  	p := porter.NewTestPorter(t)
   177  	defer p.Close()
   178  	ctx := p.SetupIntegrationTest()
   179  
   180  	p.AddTestBundleDir("testdata/bundles/failing-bundle-with-sensitive-data", false)
   181  
   182  	installOpts := porter.NewInstallOptions()
   183  	installOpts.Params = []string{"password=topsecret"}
   184  
   185  	err := installOpts.Validate(ctx, []string{}, p.Porter)
   186  	require.NoError(t, err)
   187  
   188  	err = p.InstallBundle(ctx, installOpts)
   189  	require.Error(t, err)
   190  
   191  	output := p.TestConfig.TestContext.GetOutput()
   192  	require.NotContains(t, output, "topsecret", "expected sensitive data to not be logged")
   193  	require.Contains(t, output, "*******")
   194  }