Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(terraform.go): remove trailing newline from output value #50

Merged
merged 4 commits into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package terraform

import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -50,7 +51,11 @@ func (m *Mixin) getOutput(outputName string) ([]byte, error) {
cmd := m.NewCommand("terraform", "output", outputName)
cmd.Stderr = m.Err

// Terraform appears to auto-append a newline character when printing outputs
// Trim this character before returning the output
out, err := cmd.Output()
out = bytes.TrimRight(out, "\n")

if err != nil {
prettyCmd := fmt.Sprintf("%s %s", cmd.Path, strings.Join(cmd.Args, " "))
return nil, errors.Wrap(err, fmt.Sprintf("couldn't run command %s", prettyCmd))
Expand Down
27 changes: 19 additions & 8 deletions scripts/test/test-cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ mkdir -p ${TEST_DIR}
pushd ${TEST_DIR}
trap popd EXIT

function verify-output() {
# Verify the output matches the expected value
if [[ "$(${PORTER_HOME}/porter installation output show $1)" != "$2" ]]; then
echo "Output '$1' value: '${output}' does not match expected"
return 1
fi

# Verify the output has no extra newline (mixin should trim newline added by terraform cli)
if [[ "$(${PORTER_HOME}/porter installation output show $1 | wc -l)" > 1 ]]; then
echo "Output '$1' has an extra newline character"
return 1
fi
}


# Copy terraform assets
cp -r ${REPO_DIR}/build/testdata/bundles/terraform/terraform .

Expand All @@ -20,18 +35,14 @@ ${PORTER_HOME}/porter build

${PORTER_HOME}/porter install --debug --param file_contents='foo!'

echo "Verifying installation output(s) via 'porter installation outputs list' after install"
list_outputs=$(${PORTER_HOME}/porter installation outputs list)
echo "${list_outputs}"
echo "${list_outputs}" | grep -q "file_contents"
echo "${list_outputs}" | grep -q "foo!"
echo "Verifying installation output after install"
verify-output "file_contents" 'foo!'

${PORTER_HOME}/porter invoke --action=plan --debug

${PORTER_HOME}/porter upgrade --debug --param file_contents='bar!'

echo "Verifying installation output(s) via 'porter installation output show' after upgrade"
${PORTER_HOME}/porter installation output show file_contents | grep -q "bar!"
echo "Verifying installation output after upgrade"
verify-output "file_contents" 'bar!'

${PORTER_HOME}/porter uninstall --debug