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

     1  package porter
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"get.porter.sh/porter/pkg/cnab"
     8  	"get.porter.sh/porter/pkg/printer"
     9  	"get.porter.sh/porter/pkg/secrets"
    10  	"get.porter.sh/porter/pkg/storage"
    11  	"github.com/cnabio/cnab-go/bundle"
    12  	"github.com/cnabio/cnab-go/bundle/definition"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestPorter_ShowInstallationWithBundle(t *testing.T) {
    17  	t.Parallel()
    18  
    19  	ref := "getporter/wordpress:v0.1.0"
    20  	testcases := []struct {
    21  		name       string
    22  		ref        string
    23  		format     printer.Format
    24  		outputFile string
    25  	}{
    26  		{name: "plain", ref: ref, format: printer.FormatPlaintext, outputFile: "testdata/show/expected-output.txt"},
    27  		{name: "no reference, plain", format: printer.FormatPlaintext, outputFile: "testdata/show/no-reference-expected-output.txt"},
    28  		{name: "json", ref: ref, format: printer.FormatJson, outputFile: "testdata/show/expected-output.json"},
    29  		{name: "yaml", ref: ref, format: printer.FormatYaml, outputFile: "testdata/show/expected-output.yaml"},
    30  	}
    31  
    32  	for _, tc := range testcases {
    33  		t.Run(tc.name, func(t *testing.T) {
    34  			p := NewTestPorter(t)
    35  			defer p.Close()
    36  
    37  			opts := ShowOptions{
    38  				installationOptions: installationOptions{
    39  					Namespace: "dev",
    40  					Name:      "mywordpress",
    41  				},
    42  				PrintOptions: printer.PrintOptions{
    43  					Format: tc.format,
    44  				},
    45  			}
    46  
    47  			// Create test runs
    48  			writeOnly := true
    49  			b := bundle.Bundle{
    50  				Name:    "wordpress",
    51  				Version: "0.1.0",
    52  				Definitions: definition.Definitions{
    53  					"secretString": &definition.Schema{
    54  						Type:      "string",
    55  						WriteOnly: &writeOnly,
    56  					},
    57  					"bar": &definition.Schema{
    58  						Type: "string",
    59  					},
    60  					"logLevel": &definition.Schema{
    61  						Type: "integer",
    62  					},
    63  				},
    64  				Parameters: map[string]bundle.Parameter{
    65  					"logLevel": {
    66  						Definition: "logLevel",
    67  					},
    68  					"token": {
    69  						Definition: "foo",
    70  					},
    71  					"secretString": {
    72  						Definition: "secretString",
    73  					},
    74  				},
    75  				Outputs: map[string]bundle.Output{
    76  					"foo": {
    77  						Definition: "secretString",
    78  						Path:       "/path/to/foo",
    79  					},
    80  					"bar": {
    81  						Definition: "bar",
    82  					},
    83  				},
    84  			}
    85  
    86  			bun := cnab.NewBundle(b)
    87  			i := p.TestInstallations.CreateInstallation(storage.NewInstallation("dev", "mywordpress"), p.TestInstallations.SetMutableInstallationValues, func(i *storage.Installation) {
    88  				if tc.ref != "" {
    89  					i.TrackBundle(cnab.MustParseOCIReference(tc.ref))
    90  				}
    91  				i.Labels = map[string]string{
    92  					"io.cnab/app":        "wordpress",
    93  					"io.cnab/appVersion": "v1.2.3",
    94  				}
    95  				params := []secrets.SourceMap{
    96  					{Name: "logLevel", Source: secrets.Source{Hint: "3"}, ResolvedValue: "3"},
    97  					secrets.SourceMap{Name: "secretString", Source: secrets.Source{Strategy: "secretString", Hint: "foo"}, ResolvedValue: "foo"},
    98  				}
    99  				i.Parameters = i.NewInternalParameterSet(params...)
   100  
   101  				i.ParameterSets = []string{"dev-env"}
   102  
   103  				i.Parameters.Parameters = p.SanitizeParameters(i.Parameters.Parameters, i.ID, bun)
   104  			})
   105  
   106  			run := p.TestInstallations.CreateRun(i.NewRun(cnab.ActionUpgrade, bun), p.TestInstallations.SetMutableRunValues, func(r *storage.Run) {
   107  				r.Bundle = b
   108  				r.BundleReference = tc.ref
   109  				r.BundleDigest = "sha256:88d68ef0bdb9cedc6da3a8e341a33e5d2f8bb19d0cf7ec3f1060d3f9eb73cae9"
   110  
   111  				r.ParameterOverrides = i.NewInternalParameterSet(
   112  					storage.ValueStrategy("logLevel", "3"),
   113  					storage.ValueStrategy("secretString", "foo"),
   114  				)
   115  
   116  				params := []secrets.SourceMap{
   117  					storage.ValueStrategy("logLevel", "3"),
   118  					storage.ValueStrategy("token", "top-secret"),
   119  					storage.ValueStrategy("secretString", "foo"),
   120  				}
   121  
   122  				r.ParameterSets = []string{"dev-env"}
   123  				r.ParameterOverrides.Parameters = p.SanitizeParameters(r.ParameterOverrides.Parameters, r.ID, bun)
   124  				r.Parameters.Parameters = p.SanitizeParameters(params, r.ID, bun)
   125  			})
   126  
   127  			i.Parameters.Parameters = run.ParameterOverrides.Parameters
   128  			err := p.TestInstallations.UpsertInstallation(context.Background(), i)
   129  			require.NoError(t, err)
   130  
   131  			result := p.TestInstallations.CreateResult(run.NewResult(cnab.StatusSucceeded), p.TestInstallations.SetMutableResultValues)
   132  			i.ApplyResult(run, result)
   133  			i.Status.Installed = &now
   134  			ctx := context.Background()
   135  			require.NoError(t, p.TestInstallations.UpdateInstallation(ctx, i))
   136  
   137  			err = p.ShowInstallation(ctx, opts)
   138  			require.NoError(t, err, "ShowInstallation failed")
   139  			p.CompareGoldenFile(tc.outputFile, p.TestConfig.TestContext.GetOutput())
   140  		})
   141  	}
   142  }
   143  
   144  func TestPorter_ShowInstallationWithoutRecordedRun(t *testing.T) {
   145  	t.Parallel()
   146  
   147  	ctx := context.Background()
   148  	p := NewTestPorter(t)
   149  	defer p.Close()
   150  
   151  	opts := ShowOptions{
   152  		installationOptions: installationOptions{
   153  			Namespace: "dev",
   154  			Name:      "mywordpress",
   155  		},
   156  		PrintOptions: printer.PrintOptions{
   157  			Format: printer.FormatPlaintext,
   158  		},
   159  	}
   160  
   161  	// Create test runs
   162  	writeOnly := true
   163  	b := bundle.Bundle{
   164  		Name:    "wordpress",
   165  		Version: "0.1.0",
   166  		Definitions: definition.Definitions{
   167  			"secretString": &definition.Schema{
   168  				Type:      "string",
   169  				WriteOnly: &writeOnly,
   170  			},
   171  			"bar": &definition.Schema{
   172  				Type: "string",
   173  			},
   174  			"logLevel": &definition.Schema{
   175  				Type: "integer",
   176  			},
   177  		},
   178  		Parameters: map[string]bundle.Parameter{
   179  			"logLevel": {
   180  				Definition: "logLevel",
   181  			},
   182  			"token": {
   183  				Definition: "foo",
   184  			},
   185  			"secretString": {
   186  				Definition: "secretString",
   187  			},
   188  		},
   189  		Outputs: map[string]bundle.Output{
   190  			"foo": {
   191  				Definition: "secretString",
   192  				Path:       "/path/to/foo",
   193  			},
   194  			"bar": {
   195  				Definition: "bar",
   196  			},
   197  		},
   198  	}
   199  
   200  	bun := cnab.NewBundle(b)
   201  	p.TestInstallations.CreateInstallation(storage.NewInstallation("dev", "mywordpress"), p.TestInstallations.SetMutableInstallationValues, func(i *storage.Installation) {
   202  		i.TrackBundle(cnab.MustParseOCIReference("getporter/wordpress:v0.1.0"))
   203  		i.Labels = map[string]string{
   204  			"io.cnab/app":        "wordpress",
   205  			"io.cnab/appVersion": "v1.2.3",
   206  		}
   207  		params := []secrets.SourceMap{
   208  			{Name: "logLevel", Source: secrets.Source{Hint: "3"}, ResolvedValue: "3"},
   209  			secrets.SourceMap{Name: "secretString", Source: secrets.Source{Strategy: "secretString", Hint: "foo"}, ResolvedValue: "foo"},
   210  		}
   211  		i.Parameters = i.NewInternalParameterSet(params...)
   212  
   213  		i.ParameterSets = []string{"dev-env"}
   214  
   215  		i.Parameters.Parameters = p.SanitizeParameters(i.Parameters.Parameters, i.ID, bun)
   216  	})
   217  
   218  	// do not create a run, simulate that the installation failed before the bundle could execute (or show was called before the bundle was run)
   219  
   220  	err := p.ShowInstallation(ctx, opts)
   221  	require.NoError(t, err, "ShowInstallation failed")
   222  	p.CompareGoldenFile("testdata/show/bundle-never-run.txt", p.TestConfig.TestContext.GetOutput())
   223  
   224  }