Skip to content

Commit

Permalink
Updating the parameter generation to add some opinions (#169)
Browse files Browse the repository at this point in the history
Parameters without a default are automatically required by porter
  • Loading branch information
jeremyrickard authored and carolynvs-msft committed Feb 19, 2019
1 parent 0ad8554 commit 2f31e0e
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 6 deletions.
25 changes: 25 additions & 0 deletions pkg/config/manifest_test.go
Expand Up @@ -205,6 +205,31 @@ func TestResolveArray(t *testing.T) {
assert.Equal(t, "Ralpha", args[0])
}

func TestResolveInMainDict(t *testing.T) {
c := NewTestConfig(t)
c.SetupPorterHome()

c.TestContext.AddTestFile("testdata/param-test-in-block.yaml", Name)

require.NoError(t, c.LoadManifest())

installStep := c.Manifest.Install[0]

os.Setenv("COMMAND", "echo hello world")
err := c.Manifest.ResolveStep(installStep)
assert.NoError(t, err)

assert.NotNil(t, installStep.Data)
t.Logf("install data %v", installStep.Data)
exec := installStep.Data["exec"].(map[interface{}]interface{})
assert.NotNil(t, exec)
command := exec["command"].(interface{})
assert.NotNil(t, command)
cmdVal, ok := command.(string)
assert.True(t, ok)
assert.Equal(t, "echo hello world", cmdVal)
}

func TestResolveSliceWithAMap(t *testing.T) {
c := NewTestConfig(t)
c.SetupPorterHome()
Expand Down
26 changes: 26 additions & 0 deletions pkg/config/testdata/param-test-in-block.yaml
@@ -0,0 +1,26 @@
mixins:
- exec

name: HELLO
version: 0.1.0
description: "An example Porter configuration"
invocationImage: jeremyrickard/porter-hello:latest

parameters:
- name: command
type: string
default: "echo Hello World"

install:
- description: "Install Hello World"
exec:
command:
source: bundle.parameters.command

uninstall:
- description: "Uninstall Hello World"
exec:
command: bash
arguments:
- -c
- echo Goodbye World
7 changes: 6 additions & 1 deletion pkg/porter/build.go
Expand Up @@ -298,12 +298,17 @@ func (p *Porter) generateBundleParameters() map[string]ParameterDefinition {
DataType: param.DataType,
DefaultValue: param.DefaultValue,
AllowedValues: param.AllowedValues,
Required: param.Required,
MinValue: param.MinValue,
MaxValue: param.MaxValue,
MinLength: param.MinLength,
MaxLength: param.MaxLength,
}

// If the default is empty, set required to true.
if param.DefaultValue == nil {
p.Required = true
}

if param.Metadata.Description != "" {
p.Metadata = ParameterMetadata{Description: param.Metadata.Description}
}
Expand Down
27 changes: 27 additions & 0 deletions pkg/porter/build_test.go
Expand Up @@ -125,3 +125,30 @@ func TestPorter_buildBundle(t *testing.T) {
require.Equal(t, bundle.Version, "0.1.0")
require.Equal(t, bundle.Description, "An example Porter configuration")
}

func TestPorter_paramRequired(t *testing.T) {
p := NewTestPorter(t)
p.TestConfig.SetupPorterHome()
p.TestConfig.TestContext.AddTestFile("./testdata/paramafest.yaml", config.Name)

err := p.LoadManifest()
require.NoError(t, err)

err = p.buildBundle("foo", "digest")
require.NoError(t, err)

bundleBytes, err := p.FileSystem.ReadFile("bundle.json")
require.NoError(t, err)

var bundle Bundle
err = json.Unmarshal(bundleBytes, &bundle)
require.NoError(t, err)

p1, ok := bundle.Parameters["command"]
require.True(t, ok)
require.False(t, p1.Required)

p2, ok := bundle.Parameters["command2"]
require.True(t, ok)
require.True(t, p2.Required)
}
10 changes: 5 additions & 5 deletions pkg/porter/packrd/packed-packr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions pkg/porter/testdata/paramafest.yaml
@@ -0,0 +1,31 @@
mixins:
- exec

name: HELLO
version: 0.1.0
description: "An example Porter configuration"
invocationImage: jeremyrickard/porter-hello:latest

parameters:
- name: command
type: string
default: "echo Hello World"
- name: command2
type: string

install:
- description: "Install Hello World"
exec:
command: bash
arguments:
- -c
- source: bundle.parameters.command


uninstall:
- description: "Uninstall Hello World"
exec:
command: bash
arguments:
- -c
- echo Goodbye World

0 comments on commit 2f31e0e

Please sign in to comment.