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

     1  //go:build integration
     2  
     3  package integration
     4  
     5  import (
     6  	"testing"
     7  
     8  	"get.porter.sh/porter/pkg/porter"
     9  	"get.porter.sh/porter/tests"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestInvokeCustomAction(t *testing.T) {
    15  	// this is sentinel output that is only output by the porter runtime when in debug mode
    16  	const runtimeDebugOutputCheck = "=== Step Template ==="
    17  
    18  	t.Parallel()
    19  
    20  	p := porter.NewTestPorter(t)
    21  	defer p.Close()
    22  	ctx := p.SetupIntegrationTest()
    23  
    24  	// Install a bundle with a custom action defined
    25  	err := p.Create()
    26  	require.NoError(t, err)
    27  
    28  	bundleName := p.AddTestBundleDir("testdata/bundles/bundle-with-custom-action", true)
    29  
    30  	installOpts := porter.NewInstallOptions()
    31  	// explicitly do not set --debug for install
    32  	err = installOpts.Validate(ctx, []string{}, p.Porter)
    33  	require.NoError(t, err)
    34  	err = p.InstallBundle(ctx, installOpts)
    35  	require.NoError(t, err)
    36  
    37  	// Make sure that when --debug is not passed, we do not output porter runtimes debug lines
    38  	gotErr := p.TestConfig.TestContext.GetError()
    39  	require.NotContains(t, gotErr, runtimeDebugOutputCheck, "expected no debug output from the porter runtime since --debug was not passed")
    40  
    41  	// Invoke the custom action
    42  	invokeOpts := porter.NewInvokeOptions()
    43  	invokeOpts.DebugMode = true
    44  	invokeOpts.Action = "zombies"
    45  	err = invokeOpts.Validate(ctx, []string{}, p.Porter)
    46  	require.NoError(t, err)
    47  	err = p.InvokeBundle(ctx, invokeOpts)
    48  	require.NoError(t, err, "invoke should have succeeded")
    49  
    50  	gotOutput := p.TestConfig.TestContext.GetOutput()
    51  	tests.RequireOutputContains(t, gotOutput, "oh noes my brains", "invoke should have printed a cry for halp")
    52  
    53  	// Check that debug output from the porter runtime was printed by the bundle and porter collected it
    54  	// This checks that the PORTER_DEBUG parameter is being properly passed to a bundle when run with porter invoke --debug
    55  	gotStderr := p.TestConfig.TestContext.GetOutput()
    56  	tests.RequireOutputContains(t, gotStderr, runtimeDebugOutputCheck, "expected debug output from the porter runtime to be output by the bundle")
    57  
    58  	// Verify that the custom action was recorded properly
    59  	i, err := p.Installations.GetInstallation(ctx, "", bundleName)
    60  	require.NoError(t, err, "could not fetch installation")
    61  	c, err := p.Installations.GetLastRun(ctx, i.Namespace, i.Name)
    62  	require.NoError(t, err, "GetLastClaim failed")
    63  	assert.Equal(t, "zombies", c.Action, "the custom action wasn't recorded in the installation")
    64  }