Skip to content

Commit

Permalink
fix(*): update parameter set logic to support file types
Browse files Browse the repository at this point in the history
Signed-off-by: Vaughn Dice <[email protected]>
  • Loading branch information
vdice committed Jul 14, 2020
1 parent 774b08a commit 0ad0b0e
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 5 deletions.
17 changes: 13 additions & 4 deletions pkg/porter/cnab.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,20 @@ func (o *sharedOptions) parseParams() error {

// parseParamSets parses the variable assignments in ParameterSets.
func (o *sharedOptions) parseParamSets(p *Porter) error {
parsed, err := p.loadParameterSets(o.ParameterSets)
if err != nil {
return errors.Wrapf(err, "unable to process provided parameter sets: %v", o.ParameterSets)
if len(o.ParameterSets) > 0 {
// Load the manifest as it may be needed to determine if any
// parameters are of the Porter-exclusive type of 'file'
err := p.LoadManifestFrom(o.File)
if err != nil {
return err
}

parsed, err := p.loadParameterSets(o.ParameterSets)
if err != nil {
return errors.Wrapf(err, "unable to process provided parameter sets: %v", o.ParameterSets)
}
o.parsedParamSets = parsed
}
o.parsedParamSets = parsed
return nil
}

Expand Down
35 changes: 35 additions & 0 deletions pkg/porter/cnab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"get.porter.sh/porter/pkg/build"
"get.porter.sh/porter/pkg/config"
"get.porter.sh/porter/pkg/context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -91,6 +92,10 @@ func TestSharedOptions_defaultDriver(t *testing.T) {

func TestParseParamSets_viaPathOrName(t *testing.T) {
p := NewTestPorter(t)
cxt := p.TestConfig.TestContext

// Add manifest used to parse parameter sets
cxt.AddTestFile("testdata/porter.yaml", config.Name)

p.TestParameters.TestSecrets.AddSecret("foo_secret", "foo_value")
p.TestParameters.TestSecrets.AddSecret("PARAM2_SECRET", "VALUE2")
Expand All @@ -102,6 +107,9 @@ func TestParseParamSets_viaPathOrName(t *testing.T) {
"HELLO_CUSTOM",
"/paramset.json",
},
bundleFileOptions: bundleFileOptions{
File: config.Name,
},
}

err := opts.parseParamSets(p.Porter)
Expand All @@ -114,6 +122,33 @@ func TestParseParamSets_viaPathOrName(t *testing.T) {
assert.Equal(t, wantParams, opts.parsedParamSets, "resolved unexpected parameter values")
}

func TestParseParamSets_FileType(t *testing.T) {
p := NewTestPorter(t)
cxt := p.TestConfig.TestContext

// Add manifest used to parse parameter sets
cxt.AddTestFile("testdata/porter-with-file-param.yaml", config.Name)

p.TestConfig.TestContext.AddTestFile("testdata/paramset-with-file-param.json", "/paramset.json")

opts := sharedOptions{
ParameterSets: []string{
"/paramset.json",
},
bundleFileOptions: bundleFileOptions{
File: config.Name,
},
}

err := opts.parseParamSets(p.Porter)
assert.NoError(t, err)

wantParams := map[string]string{
"my-file-param": "/local/path/to/my-file-param",
}
assert.Equal(t, wantParams, opts.parsedParamSets, "resolved unexpected parameter values")
}

func TestCombineParameters(t *testing.T) {
t.Run("no override present, no parameter set present", func(t *testing.T) {
opts := sharedOptions{}
Expand Down
6 changes: 6 additions & 0 deletions pkg/porter/lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"path/filepath"
"testing"

"get.porter.sh/porter/pkg/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -84,6 +85,10 @@ func TestInstallFromTagIgnoresCurrentBundle(t *testing.T) {

func TestBundleLifecycleOpts_ToActionArgs(t *testing.T) {
p := NewTestPorter(t)
cxt := p.TestConfig.TestContext

// Add manifest which is used to parse parameter sets
cxt.AddTestFile("testdata/porter.yaml", config.Name)

deps := &dependencyExecutioner{}

Expand Down Expand Up @@ -118,6 +123,7 @@ func TestBundleLifecycleOpts_ToActionArgs(t *testing.T) {
sharedOptions: sharedOptions{
bundleFileOptions: bundleFileOptions{
RelocationMapping: "relocation-mapping.json",
File: config.Name,
},
Name: "MyClaim",
Params: []string{
Expand Down
17 changes: 17 additions & 0 deletions pkg/porter/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,23 @@ func (p *Porter) loadParameterSets(params []string) (valuesource.Set, error) {
return nil, err
}

// A parameter may correspond to a Porter-specific parameter type of 'file'
// If so, add value (filepath) directly to map and remove from pset
for _, paramDef := range p.Manifest.Parameters {
if paramDef.Type == "file" {
for i, param := range pset.Parameters {
if param.Name == paramDef.Name {
// Pass through value (filepath) directly to resolvedParameters
resolvedParameters[param.Name] = param.Source.Value
// Eliminate this param from pset to prevent its resolution by
// the cnab-go library, which doesn't support this parameter type
pset.Parameters[i] = pset.Parameters[len(pset.Parameters)-1]
pset.Parameters = pset.Parameters[:len(pset.Parameters)-1]
}
}
}
}

rc, err := p.Parameters.ResolveAll(pset)
if err != nil {
return nil, err
Expand Down
13 changes: 13 additions & 0 deletions pkg/porter/testdata/paramset-with-file-param.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "mypset",
"created": "1983-04-18T01:02:03.000000004Z",
"modified": "1983-04-18T01:02:03.000000004Z",
"parameters": [
{
"name": "my-file-param",
"source": {
"path": "/local/path/to/my-file-param"
}
}
]
}
27 changes: 27 additions & 0 deletions pkg/porter/testdata/porter-with-file-param.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: HELLO_CUSTOM
version: 0.1.0
description: "A bundle with a custom action"
tag: getporter/porter-hello:v0.1.0

parameters:
- name: my-file-param
description: "My file param"
type: file
path: /container/path/to/file

mixins:
- exec

install:
- exec:
description: "cat file"
command: bash
flags:
c: '"cat /container/path/to/file"'

uninstall:
- exec:
description: "cat file"
command: bash
flags:
c: '"cat /container/path/to/file"'
5 changes: 4 additions & 1 deletion tests/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ func TestInstall_fileParam(t *testing.T) {
p.TestConfig.TestContext.AddTestFile(filepath.Join(p.TestDir, "testdata/bundle-with-file-params.yaml"), "porter.yaml")
p.TestConfig.TestContext.AddTestFile(filepath.Join(p.TestDir, "testdata/helpers.sh"), "helpers.sh")
p.TestConfig.TestContext.AddTestFile(filepath.Join(p.TestDir, "testdata/myfile"), "./myfile")
p.TestConfig.TestContext.AddTestFile(filepath.Join(p.TestDir, "testdata/myotherfile"), "./myotherfile")

installOpts := porter.InstallOptions{}
installOpts.Params = []string{"myfile=./myfile"}
installOpts.ParameterSets = []string{filepath.Join(p.TestDir, "testdata/parameter-set-with-file-param.json")}

err := installOpts.Validate([]string{}, p.Porter)
require.NoError(t, err)
Expand All @@ -67,5 +69,6 @@ func TestInstall_fileParam(t *testing.T) {

claim, err := p.Claims.Read(p.Manifest.Name)
require.NoError(t, err, "could not fetch claim")
require.Equal(t, "Hello World!", claim.Outputs["myfile"], "expected output to match the decoded file contents")
require.Equal(t, "Hello World!", claim.Outputs["myfile"], "expected output 'myfile' to match the decoded file contents")
require.Equal(t, "Hello Other World!", claim.Outputs["myotherfile"], "expected output 'myotherfile' to match the decoded file contents")
}
8 changes: 8 additions & 0 deletions tests/testdata/bundle-with-file-params.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ parameters:
- name: myfile
type: file
path: /root/myfile
- name: myotherfile
type: file
path: /root/myotherfile
# This is added to cover bug fix for https://github.com/deislabs/porter/issues/835
# It will be inherently exercised in install_test.go via a default bundle installation
- name: onlyUpgrade
Expand All @@ -22,6 +25,11 @@ outputs:
type: string
applyTo:
- install
- name: myotherfile
type: file
path: /root/myotherfile
applyTo:
- install

install:
- exec:
Expand Down
1 change: 1 addition & 0 deletions tests/testdata/myotherfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello Other World!
13 changes: 13 additions & 0 deletions tests/testdata/parameter-set-with-file-param.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "mybun",
"created": "2020-06-23T13:31:06.22727-06:00",
"modified": "2020-06-23T13:31:44.692834-06:00",
"parameters": [
{
"name": "myotherfile",
"source": {
"path": "./myotherfile"
}
}
]
}

0 comments on commit 0ad0b0e

Please sign in to comment.