Skip to content

Commit 54d9728

Browse files
authored
Set terraform healthcheck after writing outputs (#1301)
Ensure that the file used as a healthcheck is written after all the terraform commands have finished. That will make sure that outputs will be available when the container is healthy.
1 parent 23f002b commit 54d9728

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

internal/testrunner/runners/system/servicedeployer/_static/terraform_deployer_run.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ trap cleanup EXIT INT TERM
2020

2121
terraform init
2222
terraform plan
23-
terraform apply -auto-approve && touch /tmp/tf-applied
23+
terraform apply -auto-approve
2424

2525
terraform output -json > /output/tfOutputValues.json
2626

27+
touch /tmp/tf-applied # This file is used as indicator (healthcheck) that the service is UP, and so it must be placed as the last statement in the script
28+
2729
echo "Terraform definitions applied."
2830

2931
set +x

internal/testrunner/runners/system/servicedeployer/terraform.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,10 @@ func addTerraformOutputs(outCtxt ServiceContext) error {
6262
}
6363

6464
// Unmarshall the data into `terraformOutputs`
65-
logger.Debug("Unmarshalling terraform output json")
65+
logger.Debug("Unmarshalling terraform output JSON")
6666
var terraformOutputs map[string]OutputMeta
67-
68-
if !json.Valid(content) {
69-
logger.Debug("Invalid Json content in the terraform output file, skipped creating outputs")
70-
return nil
71-
}
72-
7367
if err = json.Unmarshal(content, &terraformOutputs); err != nil {
74-
return fmt.Errorf("error during json Unmarshal %w", err)
68+
return fmt.Errorf("error during JSON Unmarshal: %w", err)
7569
}
7670

7771
if len(terraformOutputs) == 0 {
@@ -166,7 +160,7 @@ func (tsd TerraformServiceDeployer) SetUp(inCtxt ServiceContext) (DeployedServic
166160

167161
err = addTerraformOutputs(outCtxt)
168162
if err != nil {
169-
return nil, fmt.Errorf("could not handle terraform output %w", err)
163+
return nil, fmt.Errorf("could not handle terraform output: %w", err)
170164
}
171165
service.ctxt = outCtxt
172166
return &service, nil

internal/testrunner/runners/system/servicedeployer/terraform_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func TestAddTerraformOutputs(t *testing.T) {
2020
runId string
2121
content []byte
2222
expectedProps map[string]interface{}
23+
expectedError bool
2324
}{
2425
{
2526
testName: "invalid_json_output",
@@ -31,6 +32,7 @@ func TestAddTerraformOutputs(t *testing.T) {
3132
``,
3233
),
3334
expectedProps: map[string]interface{}{},
35+
expectedError: true,
3436
},
3537
{
3638
testName: "empty_json_output",
@@ -145,6 +147,10 @@ func TestAddTerraformOutputs(t *testing.T) {
145147

146148
// Test that the terraform output values are generated correctly
147149
err := addTerraformOutputs(tc.ctxt)
150+
if tc.expectedError {
151+
require.Error(t, err)
152+
return
153+
}
148154
require.NoError(t, err)
149155
assert.Equal(t, tc.expectedProps, tc.ctxt.CustomProperties)
150156
})

0 commit comments

Comments
 (0)