Skip to content

Commit

Permalink
feat(run.go): create ApplyBundleOutputs function; add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vdice committed Jul 3, 2019
1 parent f9c4e52 commit 759d16d
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 43 deletions.
92 changes: 49 additions & 43 deletions pkg/porter/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,49 +141,10 @@ func (p *Porter) Run(opts RunOptions) error {
return err
}

// TODO: refactor for unit testing
// Apply/update bundle outputs
for _, bundleOutput := range p.Manifest.Outputs {
doApply := true

// If ApplyTo array non-empty, default doApply to false
// and only set to true if at least one entry matches current Action
if len(bundleOutput.ApplyTo) > 0 {
doApply = false
for _, applyTo := range bundleOutput.ApplyTo {
if opts.Action == applyTo {
doApply = true
}
}
}

if doApply {
// Ensure outputs directory exists
exists, err := p.FileSystem.DirExists(config.BundleOutputsDir)
if err != nil {
return err
}
if !exists {
if err := p.FileSystem.MkdirAll(config.BundleOutputsDir, 0755); err != nil {
return errors.Wrap(err, "could not make CNAB outputs directory")
}
}

// Write outputs to expected CNAB locations
for _, output := range outputs {
// Currently, outputs are all transfered in one file, delimited by newlines
// We therefore have to check if a given output (line) corresponds to this bundle output
// TODO: refactor once outputs are transferred in the form of files
outputKey := strings.Split(output, "=")[0]
if outputKey == bundleOutput.Name {
outpath := filepath.Join(config.BundleOutputsDir, bundleOutput.Name)
err := p.FileSystem.WriteFile(outpath, []byte(output), 0755)
if err != nil {
return errors.Wrapf(err, "could not write output file %s", outpath)
}
}
}
}
// Apply any Bundle Outputs declared in this step
err = p.ApplyBundleOutputs(opts, outputs)
if err != nil {
return err
}
}
}
Expand Down Expand Up @@ -273,3 +234,48 @@ func (p *Porter) readOutputs() ([]string, error) {

return outputs, nil
}

// ApplyBundleOutputs writes the provided outputs to the proper location
// in the execution environment
func (p *Porter) ApplyBundleOutputs(opts RunOptions, outputs []string) error {
for _, output := range outputs {
// Iterate through bundle outputs declared in the manifest
for _, bundleOutput := range p.Manifest.Outputs {
// Currently, outputs are all transfered in one file, delimited by newlines
// We therefore have to check if a given output (line) corresponds to this bundle output
// TODO: refactor once outputs are transferred in the form of files
outputKey := strings.Split(output, "=")[0]

// If a given step output matches a bundle output, proceed
if outputKey == bundleOutput.Name {
doApply := true

// If ApplyTo array non-empty, default doApply to false
// and only set to true if at least one entry matches current Action
if len(bundleOutput.ApplyTo) > 0 {
doApply = false
for _, applyTo := range bundleOutput.ApplyTo {
if opts.Action == applyTo {
doApply = true
}
}
}

if doApply {
// Ensure outputs directory exists
if err := p.FileSystem.MkdirAll(config.BundleOutputsDir, 0755); err != nil {
return errors.Wrap(err, "unable to ensure CNAB outputs directory exists")
}

// Write output
outpath := filepath.Join(config.BundleOutputsDir, bundleOutput.Name)
err := p.FileSystem.WriteFile(outpath, []byte(output), 0755)
if err != nil {
return errors.Wrapf(err, "unable to write output file %s", outpath)
}
}
}
}
}
return nil
}
107 changes: 107 additions & 0 deletions pkg/porter/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,110 @@ func TestActionInput_MarshalYAML(t *testing.T) {
wantYaml := "install:\n- exec:\n command: echo hi\n"
assert.Equal(t, wantYaml, string(b))
}

func TestApplyBundleOutputs_None(t *testing.T) {
p := NewTestPorter(t)
p.Manifest = &config.Manifest{
Name: "mybun",
}
opts := NewRunOptions(p.Config)

outputs := []string{"foo=bar", "123=abc"}

err := p.ApplyBundleOutputs(opts, outputs)
assert.NoError(t, err)
}

func TestApplyBundleOutputs_Some_Match(t *testing.T) {
p := NewTestPorter(t)
p.Manifest = &config.Manifest{
Name: "mybun",
Outputs: []config.OutputDefinition{
{
Name: "foo",
},
{
Name: "123",
},
},
}
opts := NewRunOptions(p.Config)

outputs := []string{"foo=bar", "123=abc"}

err := p.ApplyBundleOutputs(opts, outputs)
assert.NoError(t, err)

bytes, err := p.FileSystem.ReadFile(filepath.Join(config.BundleOutputsDir, "foo"))
assert.NoError(t, err)
assert.Equal(t, outputs[0], string(bytes))

bytes, err = p.FileSystem.ReadFile(filepath.Join(config.BundleOutputsDir, "123"))
assert.NoError(t, err)
assert.Equal(t, outputs[1], string(bytes))
}

func TestApplyBundleOutputs_Some_NoMatch(t *testing.T) {
p := NewTestPorter(t)
p.Manifest = &config.Manifest{
Name: "mybun",
Outputs: []config.OutputDefinition{
{
Name: "bar",
},
{
Name: "456",
},
},
}
opts := NewRunOptions(p.Config)

outputs := []string{"foo=bar", "123=abc"}

err := p.ApplyBundleOutputs(opts, outputs)
assert.NoError(t, err)

// No outputs declared in the manifest match those in outputs,
// so no output file is expected to be written
for _, output := range []string{"foo", "bar", "123", "456"} {
_, err := p.FileSystem.ReadFile(filepath.Join(config.BundleOutputsDir, output))
assert.Error(t, err)
}
}

func TestApplyBundleOutputs_ApplyTo_True(t *testing.T) {
p := NewTestPorter(t)
p.Manifest = &config.Manifest{
Name: "mybun",
Outputs: []config.OutputDefinition{
{
Name: "foo",
ApplyTo: []string{
"upgrade",
},
},
{
Name: "123",
ApplyTo: []string{
"install",
},
},
},
}
opts := NewRunOptions(p.Config)
opts.Action = "install"

outputs := []string{"foo=bar", "123=abc"}

err := p.ApplyBundleOutputs(opts, outputs)
assert.NoError(t, err)

// foo output should not exist (applyTo doesn't match)
_, err = p.FileSystem.ReadFile(filepath.Join(config.BundleOutputsDir, "foo"))
assert.Error(t, err)

// 123 output should exist (applyTo matches)
bytes, err := p.FileSystem.ReadFile(filepath.Join(config.BundleOutputsDir, "123"))
assert.NoError(t, err)
assert.Equal(t, []byte("123=abc"), bytes)
}
1 change: 1 addition & 0 deletions scripts/test/test-wordpress.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ if cat ${install_log} | grep -q "${sensitive_value}"; then
fi

# Verify outputs
# TODO: use porter cli to access and verify
cat ${PORTER_HOME}/outputs/wordpress/wordpress-password | grep "${sensitive_value}"
cat ${PORTER_HOME}/outputs/wordpress/mysql-password | grep "insecure-db-password"

Expand Down

0 comments on commit 759d16d

Please sign in to comment.