-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #130 from simongdavies/command_outputs
Add support for outputs to command driver
- Loading branch information
Showing
5 changed files
with
264 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
// +build !windows | ||
|
||
package command | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/deislabs/cnab-go/bundle" | ||
"github.com/deislabs/cnab-go/bundle/definition" | ||
"github.com/deislabs/cnab-go/driver" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCommandDriverOutputs(t *testing.T) { | ||
content := `#!/bin/sh | ||
mkdir -p "${CNAB_OUTPUT_DIR}/cnab/app/outputs" | ||
echo "TEST_OUTPUT_1" >> "${CNAB_OUTPUT_DIR}/cnab/app/outputs/output1" | ||
echo "TEST_OUTPUT_2" >> "${CNAB_OUTPUT_DIR}/cnab/app/outputs/output2" | ||
` | ||
name := "test-outputs-exist.sh" | ||
testfunc := func(t *testing.T, cmddriver *Driver) { | ||
if !cmddriver.CheckDriverExists() { | ||
t.Fatalf("Expected driver %s to exist Driver Name %s ", name, cmddriver.Name) | ||
} | ||
op := driver.Operation{ | ||
Action: "install", | ||
Installation: "test", | ||
Parameters: map[string]interface{}{}, | ||
Image: bundle.InvocationImage{ | ||
BaseImage: bundle.BaseImage{ | ||
Image: "cnab/helloworld:latest", | ||
ImageType: "docker", | ||
}, | ||
}, | ||
Revision: "01DDY0MT808KX0GGZ6SMXN4TW", | ||
Environment: map[string]string{}, | ||
Files: map[string]string{ | ||
"/cnab/app/image-map.json": "{}", | ||
}, | ||
Outputs: []string{"/cnab/app/outputs/output1", "/cnab/app/outputs/output2"}, | ||
Out: os.Stdout, | ||
Bundle: &bundle.Bundle{ | ||
Definitions: definition.Definitions{ | ||
"output1": &definition.Schema{}, | ||
"output2": &definition.Schema{}, | ||
}, | ||
Outputs: map[string]bundle.Output{ | ||
"output1": { | ||
Definition: "output1", | ||
Path: "/cnab/app/outputs/output1", | ||
}, | ||
"output2": { | ||
Definition: "output2", | ||
Path: "/cnab/app/outputs/output2", | ||
}, | ||
}, | ||
}, | ||
} | ||
opResult, err := cmddriver.Run(&op) | ||
if err != nil { | ||
t.Fatalf("Driver Run failed %v", err) | ||
} | ||
assert.Equal(t, 2, len(opResult.Outputs), "Expecting two output files") | ||
assert.Equal(t, map[string]string{ | ||
"/cnab/app/outputs/output1": "TEST_OUTPUT_1\n", | ||
"/cnab/app/outputs/output2": "TEST_OUTPUT_2\n", | ||
}, opResult.Outputs) | ||
} | ||
CreateAndRunTestCommandDriver(t, name, content, testfunc) | ||
// Test for an output missing and no defaults | ||
content = `#!/bin/sh | ||
mkdir -p "${CNAB_OUTPUT_DIR}/cnab/app/outputs" | ||
echo "TEST_OUTPUT_1" >> "${CNAB_OUTPUT_DIR}/cnab/app/outputs/output1" | ||
` | ||
name = "test-outputs-missing.sh" | ||
testfunc = func(t *testing.T, cmddriver *Driver) { | ||
if !cmddriver.CheckDriverExists() { | ||
t.Fatalf("Expected driver %s to exist Driver Name %s ", name, cmddriver.Name) | ||
} | ||
op := driver.Operation{ | ||
Action: "install", | ||
Installation: "test", | ||
Parameters: map[string]interface{}{}, | ||
Image: bundle.InvocationImage{ | ||
BaseImage: bundle.BaseImage{ | ||
Image: "cnab/helloworld:latest", | ||
ImageType: "docker", | ||
}, | ||
}, | ||
Revision: "01DDY0MT808KX0GGZ6SMXN4TW", | ||
Environment: map[string]string{}, | ||
Files: map[string]string{ | ||
"/cnab/app/image-map.json": "{}", | ||
}, | ||
Outputs: []string{"/cnab/app/outputs/output1", "/cnab/app/outputs/output2"}, | ||
Out: os.Stdout, | ||
Bundle: &bundle.Bundle{ | ||
Definitions: definition.Definitions{ | ||
"output1": &definition.Schema{}, | ||
"output2": &definition.Schema{}, | ||
}, | ||
Outputs: map[string]bundle.Output{ | ||
"output1": { | ||
Definition: "output1", | ||
Path: "/cnab/app/outputs/output1", | ||
}, | ||
"output2": { | ||
Definition: "output2", | ||
Path: "/cnab/app/outputs/output2", | ||
}, | ||
}, | ||
}, | ||
} | ||
_, err := cmddriver.Run(&op) | ||
assert.Errorf(t, err, "Command driver (test-outputs-missing.sh) failed for item: /cnab/app/outputs/output2 no output value found and no default value set") | ||
} | ||
CreateAndRunTestCommandDriver(t, name, content, testfunc) | ||
// Test for an output missing with default value present | ||
content = `#!/bin/sh | ||
mkdir -p "${CNAB_OUTPUT_DIR}/cnab/app/outputs" | ||
echo "TEST_OUTPUT_1" >> "${CNAB_OUTPUT_DIR}/cnab/app/outputs/output1" | ||
` | ||
name = "test-outputs-missing.sh" | ||
testfunc = func(t *testing.T, cmddriver *Driver) { | ||
if !cmddriver.CheckDriverExists() { | ||
t.Fatalf("Expected driver %s to exist Driver Name %s ", name, cmddriver.Name) | ||
} | ||
op := driver.Operation{ | ||
Action: "install", | ||
Installation: "test", | ||
Parameters: map[string]interface{}{}, | ||
Image: bundle.InvocationImage{ | ||
BaseImage: bundle.BaseImage{ | ||
Image: "cnab/helloworld:latest", | ||
ImageType: "docker", | ||
}, | ||
}, | ||
Revision: "01DDY0MT808KX0GGZ6SMXN4TW", | ||
Environment: map[string]string{}, | ||
Files: map[string]string{ | ||
"/cnab/app/image-map.json": "{}", | ||
}, | ||
Outputs: []string{"/cnab/app/outputs/output1", "/cnab/app/outputs/output2"}, | ||
Out: os.Stdout, | ||
Bundle: &bundle.Bundle{ | ||
Definitions: definition.Definitions{ | ||
"output1": &definition.Schema{}, | ||
"output2": &definition.Schema{ | ||
Default: "DEFAULT OUTPUT 2", | ||
}, | ||
}, | ||
Outputs: map[string]bundle.Output{ | ||
"output1": { | ||
Definition: "output1", | ||
Path: "/cnab/app/outputs/output1", | ||
}, | ||
"output2": { | ||
Definition: "output2", | ||
Path: "/cnab/app/outputs/output2", | ||
}, | ||
}, | ||
}, | ||
} | ||
opResult, err := cmddriver.Run(&op) | ||
if err != nil { | ||
t.Fatalf("Driver Run failed %v", err) | ||
} | ||
assert.Equal(t, 2, len(opResult.Outputs), "Expecting two output files") | ||
assert.Equal(t, map[string]string{ | ||
"/cnab/app/outputs/output1": "TEST_OUTPUT_1\n", | ||
"/cnab/app/outputs/output2": "DEFAULT OUTPUT 2", | ||
}, opResult.Outputs) | ||
} | ||
CreateAndRunTestCommandDriver(t, name, content, testfunc) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters