Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 85a3715

Browse files
author
Jean-Christophe Sirot
committed
Fix handling parameters for generic CNAB bundle
Signed-off-by: Jean-Christophe Sirot <[email protected]>
1 parent a14ddbd commit 85a3715

File tree

5 files changed

+128
-16
lines changed

5 files changed

+128
-16
lines changed

e2e/cnab_test.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ package e2e
33
import (
44
"fmt"
55
"path"
6-
"runtime"
76
"testing"
87

98
"gotest.tools/assert"
109
is "gotest.tools/assert/cmp"
11-
"gotest.tools/fs"
1210
"gotest.tools/icmd"
1311
)
1412

@@ -45,18 +43,8 @@ func TestCallCustomStatusAction(t *testing.T) {
4543
t.Run(testCase.name, func(t *testing.T) {
4644
cmd, cleanup := dockerCli.createTestCmd()
4745
defer cleanup()
48-
49-
tmpDir := fs.NewDir(t, t.Name())
50-
defer tmpDir.Remove()
5146
testDir := path.Join("testdata", testCase.cnab)
5247

53-
// We need to explicitly set the SYSTEMROOT on windows
54-
// otherwise we get the error:
55-
// "panic: failed to read random bytes: CryptAcquireContext: Provider DLL failed to initialize correctly."
56-
// See: https://github.com/golang/go/issues/25210
57-
if runtime.GOOS == "windows" {
58-
cmd.Env = append(cmd.Env, `SYSTEMROOT=C:\WINDOWS`)
59-
}
6048
// Build CNAB invocation image
6149
cmd.Command = dockerCli.Command("build", "--file", path.Join(testDir, "cnab", "build", "Dockerfile"), "--tag", fmt.Sprintf("e2e/%s:v0.1.0", testCase.cnab), testDir)
6250
icmd.RunCmd(cmd).Assert(t, icmd.Success)
@@ -79,3 +67,32 @@ func TestCallCustomStatusAction(t *testing.T) {
7967
})
8068
}
8169
}
70+
71+
func TestCnabParameters(t *testing.T) {
72+
cmd, cleanup := dockerCli.createTestCmd()
73+
defer cleanup()
74+
testDir := path.Join("testdata", "cnab-parameters")
75+
76+
// Build CNAB invocation image
77+
cmd.Command = dockerCli.Command("build", "--file", path.Join(testDir, "cnab", "build", "Dockerfile"), "--tag", "e2e/cnab-parameters:v0.1.0", testDir)
78+
icmd.RunCmd(cmd).Assert(t, icmd.Success)
79+
80+
// docker app uninstall
81+
defer func() {
82+
cmd.Command = dockerCli.Command("app", "uninstall", "cnab-parameters")
83+
icmd.RunCmd(cmd).Assert(t, icmd.Success)
84+
}()
85+
86+
// docker app install
87+
cmd.Command = dockerCli.Command("app", "install", path.Join(testDir, "bundle.json"), "--name", "cnab-parameters",
88+
"--set", "boolParam=true",
89+
"--set", "stringParam=value",
90+
"--set", "intParam=42",
91+
"--set", "floatParam=3.14")
92+
result := icmd.RunCmd(cmd).Assert(t, icmd.Success)
93+
expectedOutput := `boolParam=true
94+
stringParam=value
95+
intParam=42
96+
floatParam=3.14`
97+
assert.Assert(t, is.Contains(result.Combined(), expectedOutput))
98+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"schemaVersion": "v1.0.0-WD",
3+
"name": "cnab-parameters",
4+
"version": "0.1.0",
5+
"invocationImages": [
6+
{
7+
"imageType": "docker",
8+
"image": "e2e/cnab-parameters:v0.1.0"
9+
}
10+
],
11+
"definitions": {
12+
"boolParam_type": {
13+
"type": "boolean"
14+
},
15+
"stringParam_type": {
16+
"type": "string"
17+
},
18+
"intParam_type": {
19+
"type": "integer"
20+
},
21+
"floatParam_type": {
22+
"type": "number"
23+
}
24+
},
25+
"parameters": {
26+
"fields": {
27+
"boolParam": {
28+
"definition": "boolParam_type",
29+
"destination": {
30+
"env": "BOOL_PARAM"
31+
}
32+
},
33+
"stringParam": {
34+
"definition": "stringParam_type",
35+
"destination": {
36+
"env": "STRING_PARAM"
37+
}
38+
},
39+
"intParam": {
40+
"definition": "intParam_type",
41+
"destination": {
42+
"env": "INT_PARAM"
43+
}
44+
},
45+
"floatParam": {
46+
"definition": "floatParam_type",
47+
"destination": {
48+
"env": "FLOAT_PARAM"
49+
}
50+
}
51+
}
52+
}
53+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
3+
action=$CNAB_ACTION
4+
name=$CNAB_INSTALLATION_NAME
5+
6+
case $action in
7+
install)
8+
echo "Install action"
9+
echo "boolParam=$BOOL_PARAM"
10+
echo "stringParam=$STRING_PARAM"
11+
echo "intParam=$INT_PARAM"
12+
echo "floatParam=$FLOAT_PARAM"
13+
;;
14+
uninstall)
15+
echo "uninstall action"
16+
;;
17+
upgrade)
18+
echo "Upgrade action"
19+
;;
20+
*)
21+
echo "No action for $action"
22+
;;
23+
esac
24+
echo "Action $action complete for $name"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ARG ALPINE_VERSION=3.9.2
2+
3+
FROM alpine:${ALPINE_VERSION}
4+
5+
COPY cnab/app/run /cnab/app/run
6+
7+
CMD /cnab/app/run

internal/commands/parameters.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"strings"
88

99
"github.com/deislabs/cnab-go/bundle"
10-
"github.com/deislabs/cnab-go/bundle/definition"
1110
"github.com/docker/app/internal"
1211
"github.com/docker/app/internal/store"
1312
"github.com/docker/app/types/parameters"
@@ -101,24 +100,36 @@ func mergeBundleParameters(installation *store.Installation, ops ...mergeBundleO
101100
return err
102101
}
103102
}
104-
if err := matchAndMergeParametersDefinition(installation.Parameters, cfg.params, bndl.Definitions, cfg.strictMode, cfg.stderr); err != nil {
103+
if err := matchAndMergeParametersDefinition(installation.Parameters, cfg.params, cfg.bundle, cfg.strictMode, cfg.stderr); err != nil {
105104
return err
106105
}
107106
var err error
108107
installation.Parameters, err = bundle.ValuesOrDefaults(installation.Parameters, bndl)
109108
return err
110109
}
111110

112-
func matchAndMergeParametersDefinition(currentValues map[string]interface{}, parameterValues map[string]string, parameterDefinitions definition.Definitions, strictMode bool, stderr io.Writer) error {
111+
func getParameterFromBundle(name string, bndl *bundle.Bundle) (bundle.ParameterDefinition, bool) {
112+
if bndl.Parameters == nil {
113+
return bundle.ParameterDefinition{}, false
114+
}
115+
param, found := bndl.Parameters.Fields[name]
116+
return param, found
117+
}
118+
119+
func matchAndMergeParametersDefinition(currentValues map[string]interface{}, parameterValues map[string]string, bundle *bundle.Bundle, strictMode bool, stderr io.Writer) error {
113120
for k, v := range parameterValues {
114-
definition, ok := parameterDefinitions[k]
121+
param, ok := getParameterFromBundle(k, bundle)
115122
if !ok {
116123
if strictMode {
117124
return fmt.Errorf("parameter %q is not defined in the bundle", k)
118125
}
119126
fmt.Fprintf(stderr, "Warning: parameter %q is not defined in the bundle\n", k)
120127
continue
121128
}
129+
definition, ok := bundle.Definitions[param.Definition]
130+
if !ok {
131+
return fmt.Errorf("invalid bundle: definition not found for parameter %q", k)
132+
}
122133
value, err := definition.ConvertValue(v)
123134
if err != nil {
124135
return errors.Wrapf(err, "invalid value for parameter %q", k)

0 commit comments

Comments
 (0)