get.porter.sh/porter@v1.3.0/pkg/porter/install_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/portercontext"
     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/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestInstallOptions_validateInstallationName(t *testing.T) {
    18  	testcases := []struct {
    19  		name      string
    20  		args      []string
    21  		wantClaim string
    22  		wantError string
    23  	}{
    24  		{"none", nil, "", ""},
    25  		{"name set", []string{"wordpress"}, "wordpress", ""},
    26  		{"too many args", []string{"wordpress", "extra"}, "", "only one positional argument may be specified, the installation name, but multiple were received: [wordpress extra]"},
    27  	}
    28  
    29  	for _, tc := range testcases {
    30  		t.Run(tc.name, func(t *testing.T) {
    31  			opts := NewInstallOptions()
    32  			err := opts.validateInstallationName(tc.args)
    33  
    34  			if tc.wantError == "" {
    35  				require.NoError(t, err)
    36  				assert.Equal(t, tc.wantClaim, opts.Name)
    37  			} else {
    38  				require.EqualError(t, err, tc.wantError)
    39  			}
    40  		})
    41  	}
    42  }
    43  
    44  func TestInstallOptions_validateDriver(t *testing.T) {
    45  	testcases := []struct {
    46  		name       string
    47  		driver     string
    48  		wantDriver string
    49  		wantError  string
    50  	}{
    51  		{"debug", "debug", DebugDriver, ""},
    52  		{"docker", "docker", DockerDriver, ""},
    53  		{"invalid driver provided", "dbeug", "", "unsupported driver or driver not found in PATH: dbeug"},
    54  	}
    55  
    56  	cxt := portercontext.NewTestContext(t)
    57  	for _, tc := range testcases {
    58  		t.Run(tc.name, func(t *testing.T) {
    59  			opts := NewInstallOptions()
    60  			opts.Driver = tc.driver
    61  			err := opts.validateDriver(cxt.Context)
    62  
    63  			if tc.wantError == "" {
    64  				require.NoError(t, err)
    65  				assert.Equal(t, tc.wantDriver, opts.Driver)
    66  			} else {
    67  				require.EqualError(t, err, tc.wantError)
    68  			}
    69  		})
    70  	}
    71  }
    72  
    73  func TestPorter_ApplyParametersToInstallation(t *testing.T) {
    74  	setup := func() (context.Context, *TestPorter, *storage.Installation) {
    75  		ctx := context.Background()
    76  		p := NewTestPorter(t)
    77  
    78  		require.NoError(t, p.TestParameters.InsertParameterSet(ctx, storage.ParameterSet{
    79  			ParameterSetSpec: storage.ParameterSetSpec{
    80  				Name: "oldps1",
    81  				Parameters: []secrets.SourceMap{
    82  					{Name: "logLevel", Source: secrets.Source{Strategy: "value", Hint: "2"}},
    83  				},
    84  			},
    85  		}))
    86  
    87  		require.NoError(t, p.TestParameters.InsertParameterSet(ctx, storage.ParameterSet{
    88  			ParameterSetSpec: storage.ParameterSetSpec{
    89  				Name: "newps1",
    90  				Parameters: []secrets.SourceMap{
    91  					{Name: "logLevel", Source: secrets.Source{Strategy: "value", Hint: "11"}},
    92  				},
    93  			},
    94  		}))
    95  		inst := storage.NewInstallation("myns", "mybuns")
    96  		inst.Bundle = storage.OCIReferenceParts{
    97  			Repository: "example.com/mybuns",
    98  			Version:    "1.0.0",
    99  		}
   100  		inst.ParameterSets = []string{"oldps1"}
   101  		inst.CredentialSets = []string{"oldcs1", "oldcs2"}
   102  		return ctx, p, &inst
   103  	}
   104  
   105  	t.Run("replace previous sets", func(t *testing.T) {
   106  		ctx, p, inst := setup()
   107  
   108  		// We should replace the previously used sets since we specified different ones
   109  		opts := NewInstallOptions()
   110  		opts.Reference = kahnlatest.String()
   111  		opts.bundleRef = &cnab.BundleReference{
   112  			Reference: kahnlatest,
   113  			Definition: cnab.NewBundle(bundle.Bundle{
   114  				Credentials: map[string]bundle.Credential{
   115  					"userid": {},
   116  				},
   117  				Parameters: map[string]bundle.Parameter{
   118  					"logLevel": {Definition: "logLevel"},
   119  				},
   120  				Definitions: map[string]*definition.Schema{
   121  					"logLevel": {Type: "string"},
   122  				},
   123  			}),
   124  		}
   125  
   126  		opts.ParameterSets = []string{"newps1"}
   127  		opts.CredentialIdentifiers = []string{"newcs1"}
   128  
   129  		require.NoError(t, opts.Validate(ctx, nil, p.Porter))
   130  		err := p.applyActionOptionsToInstallation(ctx, opts, inst)
   131  		require.NoError(t, err, "applyActionOptionsToInstallation failed")
   132  
   133  		require.Equal(t, opts.ParameterSets, inst.ParameterSets, "expected the installation to replace the credential sets with those specified")
   134  		require.Equal(t, opts.CredentialIdentifiers, inst.CredentialSets, "expected the installation to replace the credential sets with those specified")
   135  	})
   136  
   137  	t.Run("reuse previous sets", func(t *testing.T) {
   138  		ctx, p, inst := setup()
   139  
   140  		// We should reuse the previously used sets since we specified different ones
   141  		opts := NewInstallOptions()
   142  		opts.Reference = kahnlatest.String()
   143  		opts.bundleRef = &cnab.BundleReference{
   144  			Reference: kahnlatest,
   145  			Definition: cnab.NewBundle(bundle.Bundle{
   146  				Credentials: map[string]bundle.Credential{
   147  					"userid": {},
   148  				},
   149  				Parameters: map[string]bundle.Parameter{
   150  					"logLevel": {Definition: "logLevel"},
   151  				},
   152  				Definitions: map[string]*definition.Schema{
   153  					"logLevel": {Type: "string"},
   154  				},
   155  			}),
   156  		}
   157  		opts.ParameterSets = []string{}
   158  		opts.CredentialIdentifiers = []string{}
   159  
   160  		require.NoError(t, opts.Validate(ctx, nil, p.Porter))
   161  		err := p.applyActionOptionsToInstallation(ctx, opts, inst)
   162  		require.NoError(t, err, "applyActionOptionsToInstallation failed")
   163  
   164  		require.Equal(t, []string{"oldps1"}, inst.ParameterSets, "expected the installation to reuse the previous credential sets")
   165  		require.Equal(t, []string{"oldcs1", "oldcs2"}, inst.CredentialSets, "expected the installation to reuse the previous credential sets")
   166  	})
   167  }