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

     1  //go:build integration
     2  
     3  package integration
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"testing"
     9  
    10  	"get.porter.sh/porter/pkg/cnab"
    11  	"get.porter.sh/porter/pkg/porter"
    12  	"get.porter.sh/porter/pkg/printer"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestExecOutputs(t *testing.T) {
    18  	t.Parallel()
    19  
    20  	p := porter.NewTestPorter(t)
    21  	defer p.Close()
    22  	ctx := p.SetupIntegrationTest()
    23  
    24  	// Install a bundle with exec outputs
    25  	bundleName := installExecOutputsBundle(ctx, p)
    26  	defer CleanupCurrentBundle(ctx, p)
    27  
    28  	// Verify that its file output was captured
    29  	usersOutput, err := p.ReadBundleOutput(ctx, "users.json", bundleName, "")
    30  	require.NoError(t, err, "could not read users output")
    31  	assert.Equal(t, fmt.Sprintln(`{"users": ["sally"]}`), usersOutput, "expected the users output to be populated correctly")
    32  
    33  	// Verify that its bundle level file output was captured
    34  	opts := porter.OutputListOptions{}
    35  	opts.Name = bundleName
    36  	opts.Format = printer.FormatPlaintext
    37  	displayOutputs, err := p.ListBundleOutputs(ctx, &opts)
    38  	require.NoError(t, err, "ListBundleOutputs failed")
    39  	var kubeconfigOutput *porter.DisplayValue
    40  	for _, do := range displayOutputs {
    41  		if do.Name == "kubeconfig" {
    42  			kubeconfigOutput = &do
    43  			break
    44  		}
    45  	}
    46  	require.NotNil(t, kubeconfigOutput, "could not find kubeconfig output")
    47  	assert.Equal(t, "file", kubeconfigOutput.Type)
    48  	assert.Contains(t, kubeconfigOutput.Value, "apiVersion")
    49  
    50  	invokeExecOutputsBundle(ctx, p, "add-user")
    51  	invokeExecOutputsBundle(ctx, p, "get-users")
    52  
    53  	// Verify logs were captured as an output
    54  	logs, err := p.ReadBundleOutput(ctx, cnab.OutputInvocationImageLogs, bundleName, "")
    55  	require.NoError(t, err, "ListBundleOutputs failed")
    56  	assert.Contains(t, logs, "executing get-users action from exec-outputs", "expected the logs to contain bundle output from the last action")
    57  
    58  	// Verify that its jsonPath output was captured
    59  	userOutput, err := p.ReadBundleOutput(ctx, "user-names", bundleName, "")
    60  	require.NoError(t, err, "could not read user-names output")
    61  	assert.Equal(t, `["sally","wei"]`, userOutput, "expected the user-names output to be populated correctly")
    62  
    63  	invokeExecOutputsBundle(ctx, p, "test")
    64  
    65  	// Verify that its regex output was captured
    66  	testOutputs, err := p.ReadBundleOutput(ctx, "failed-tests", bundleName, "")
    67  	require.NoError(t, err, "could not read failed-tests output")
    68  	assert.Equal(t, "TestInstall\nTestUpgrade", testOutputs, "expected the failed-tests output to be populated correctly")
    69  }
    70  
    71  func CleanupCurrentBundle(ctx context.Context, p *porter.TestPorter) {
    72  	// Uninstall the bundle
    73  	uninstallOpts := porter.NewUninstallOptions()
    74  	err := uninstallOpts.Validate(ctx, []string{}, p.Porter)
    75  	assert.NoError(p.T(), err, "validation of uninstall opts failed for current bundle")
    76  
    77  	err = p.UninstallBundle(ctx, uninstallOpts)
    78  	assert.NoError(p.T(), err, "uninstall failed for current bundle")
    79  }
    80  
    81  func installExecOutputsBundle(ctx context.Context, p *porter.TestPorter) string {
    82  	err := p.Create()
    83  	require.NoError(p.T(), err)
    84  
    85  	bundleName := p.AddTestBundleDir("testdata/bundles/exec-outputs", true)
    86  
    87  	installOpts := porter.NewInstallOptions()
    88  	err = installOpts.Validate(ctx, []string{}, p.Porter)
    89  	require.NoError(p.T(), err)
    90  	err = p.InstallBundle(ctx, installOpts)
    91  	require.NoError(p.T(), err)
    92  
    93  	return bundleName
    94  }
    95  
    96  func invokeExecOutputsBundle(ctx context.Context, p *porter.TestPorter, action string) {
    97  	statusOpts := porter.NewInvokeOptions()
    98  	statusOpts.Action = action
    99  	err := statusOpts.Validate(ctx, []string{}, p.Porter)
   100  	require.NoError(p.T(), err)
   101  	err = p.InvokeBundle(ctx, statusOpts)
   102  	require.NoError(p.T(), err, "invoke %s should have succeeded", action)
   103  }
   104  
   105  func TestStepLevelAndBundleLevelOutputs(t *testing.T) {
   106  	t.Parallel()
   107  
   108  	p := porter.NewTestPorter(t)
   109  	defer p.Close()
   110  	ctx := p.SetupIntegrationTest()
   111  
   112  	p.AddTestBundleDir("testdata/bundles/outputs-example", true)
   113  
   114  	// Install the bundle
   115  	// A step-level output will be used during this action
   116  	installOpts := porter.NewInstallOptions()
   117  	err := installOpts.Validate(ctx, []string{}, p.Porter)
   118  	require.NoError(t, err)
   119  	err = p.InstallBundle(ctx, installOpts)
   120  	require.NoError(t, err, "install should have succeeded")
   121  
   122  	// Upgrade the bundle
   123  	// A bundle-level output will be produced during this action
   124  	upgradeOpts := porter.NewUpgradeOptions()
   125  	err = upgradeOpts.Validate(ctx, []string{}, p.Porter)
   126  	require.NoError(t, err)
   127  	err = p.UpgradeBundle(ctx, upgradeOpts)
   128  	require.NoError(t, err, "upgrade should have succeeded")
   129  
   130  	// Uninstall the bundle
   131  	// A bundle-level output will be used during this action
   132  	uninstallOpts := porter.NewUninstallOptions()
   133  	err = uninstallOpts.Validate(ctx, []string{}, p.Porter)
   134  	require.NoError(t, err)
   135  	err = p.UninstallBundle(ctx, uninstallOpts)
   136  	require.NoError(t, err, "uninstall should have succeeded")
   137  }