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

     1  package porter
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"get.porter.sh/porter/pkg/cnab"
     9  	"get.porter.sh/porter/pkg/printer"
    10  	"get.porter.sh/porter/pkg/storage"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  var now = time.Date(2020, time.April, 18, 1, 2, 3, 4, time.UTC)
    16  
    17  func TestPorter_ListInstallationRuns(t *testing.T) {
    18  	p := NewTestPorter(t)
    19  	defer p.Close()
    20  
    21  	installationName1 := "shared-mysql"
    22  	run1 := storage.NewRun("", installationName1)
    23  	run1.NewResult("running")
    24  	run1.NewResult("succeeded")
    25  
    26  	p.TestInstallations.CreateInstallation(storage.NewInstallation("", installationName1), p.TestInstallations.SetMutableInstallationValues)
    27  	p.TestInstallations.CreateRun(run1)
    28  
    29  	installationName2 := "shared-k8s"
    30  
    31  	run2 := storage.NewRun("dev", installationName2)
    32  	run2.NewResult("running")
    33  
    34  	run3 := storage.NewRun("dev", installationName2)
    35  	run3.NewResult("running")
    36  
    37  	p.TestInstallations.CreateInstallation(storage.NewInstallation("dev", installationName2), p.TestInstallations.SetMutableInstallationValues)
    38  	p.TestInstallations.CreateRun(run2)
    39  	p.TestInstallations.CreateRun(run3)
    40  
    41  	t.Run("global namespace", func(t *testing.T) {
    42  		opts := RunListOptions{installationOptions: installationOptions{
    43  			Namespace: "",
    44  			Name:      installationName1,
    45  		}}
    46  		results, err := p.ListInstallationRuns(context.Background(), opts)
    47  		require.NoError(t, err)
    48  		assert.Len(t, results, 1)
    49  	})
    50  
    51  	t.Run("specified namespace", func(t *testing.T) {
    52  		opts := RunListOptions{installationOptions: installationOptions{
    53  			Namespace: "dev",
    54  			Name:      installationName2,
    55  		}}
    56  		results, err := p.ListInstallationRuns(context.Background(), opts)
    57  		require.NoError(t, err)
    58  		assert.Len(t, results, 2)
    59  	})
    60  }
    61  
    62  func TestPorter_PrintInstallationRunsOutput(t *testing.T) {
    63  	outputTestcases := []struct {
    64  		name       string
    65  		format     printer.Format
    66  		outputFile string
    67  	}{
    68  		{name: "yaml", format: printer.FormatYaml, outputFile: "testdata/runs/expected-output.yaml"},
    69  		{name: "json", format: printer.FormatJson, outputFile: "testdata/runs/expected-output.json"},
    70  		{name: "plaintext", format: printer.FormatPlaintext, outputFile: "testdata/runs/expected-output.txt"},
    71  	}
    72  
    73  	for _, tc := range outputTestcases {
    74  		tc := tc
    75  		t.Run(tc.name, func(t *testing.T) {
    76  			p := NewTestPorter(t)
    77  			defer p.Close()
    78  			ctx := context.Background()
    79  
    80  			installation := p.TestInstallations.CreateInstallation(storage.NewInstallation("staging", "shared-k8s"), p.TestInstallations.SetMutableInstallationValues)
    81  
    82  			bun := cnab.ExtendedBundle{}
    83  			installRun := p.TestInstallations.CreateRun(installation.NewRun(cnab.ActionInstall, bun), p.TestInstallations.SetMutableRunValues)
    84  			uninstallRun := p.TestInstallations.CreateRun(installation.NewRun(cnab.ActionUninstall, bun), p.TestInstallations.SetMutableRunValues)
    85  			result := p.TestInstallations.CreateResult(installRun.NewResult(cnab.StatusSucceeded), p.TestInstallations.SetMutableResultValues)
    86  			result2 := p.TestInstallations.CreateResult(uninstallRun.NewResult(cnab.StatusSucceeded), p.TestInstallations.SetMutableResultValues)
    87  
    88  			installation.ApplyResult(installRun, result)
    89  			installation.ApplyResult(uninstallRun, result2)
    90  			installation.Status.Installed = &now
    91  
    92  			require.NoError(t, p.TestInstallations.UpdateInstallation(ctx, installation))
    93  
    94  			opts := RunListOptions{installationOptions: installationOptions{
    95  				Namespace: "staging",
    96  				Name:      "shared-k8s",
    97  			}, PrintOptions: printer.PrintOptions{Format: tc.format},
    98  			}
    99  
   100  			err := p.PrintInstallationRuns(context.Background(), opts)
   101  			require.NoError(t, err)
   102  
   103  			p.CompareGoldenFile(tc.outputFile, p.TestConfig.TestContext.GetOutput())
   104  		})
   105  
   106  	}
   107  }