Skip to content

Commit

Permalink
Fix: Handle nil deployment outputs gracefully
Browse files Browse the repository at this point in the history
Signed-off-by: Arun P Nair <[email protected]>
  • Loading branch information
arunpnair committed Oct 2, 2024
1 parent a28c285 commit ac2d595
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/arm/templates/arm.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,10 @@ func (d *deployer) pollUntilComplete(
func getOutputs(
deployment *resourcesSDK.DeploymentExtended,
) (map[string]interface{}, error) {
// Check if deployment output is nil as "outputs" section/element is optional in ARM template.
if deployment.Properties.Outputs == nil {
return map[string]interface{}{}, nil
}
outputs, ok := deployment.Properties.Outputs.(map[string]interface{})
if !ok {
return nil, errors.New("error decoding deployment outputs")
Expand Down
52 changes: 52 additions & 0 deletions pkg/arm/templates/arm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package templates

import (
"testing"

resourcesSDK "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2017-05-10/resources"
"github.com/stretchr/testify/assert"
)

func TestGetOutputs_ValidOutputs(t *testing.T) {
deployment := &resourcesSDK.DeploymentExtended{
Properties: &resourcesSDK.DeploymentPropertiesExtended{
Outputs: map[string]interface{}{
"output1": map[string]interface{}{
"value": "amaterasu",
},
"output2": map[string]interface{}{
"value": 108,
},
},
},
}

outputs, err := getOutputs(deployment)
assert.NoError(t, err)
assert.Equal(t, "amaterasu", outputs["output1"])
assert.Equal(t, 108, outputs["output2"])
}

func TestGetOutputs_InvalidOutputs(t *testing.T) {
deployment := &resourcesSDK.DeploymentExtended{
Properties: &resourcesSDK.DeploymentPropertiesExtended{
Outputs: "invalid",
},
}

outputs, err := getOutputs(deployment)
assert.Error(t, err)
assert.Nil(t, outputs)
}

func TestGetOutputs_NoOutputs(t *testing.T) {
deployment := &resourcesSDK.DeploymentExtended{
Properties: &resourcesSDK.DeploymentPropertiesExtended{
Outputs: nil,
},
}

outputs, err := getOutputs(deployment)
assert.NoError(t, err)
assert.Empty(t, outputs)
}

0 comments on commit ac2d595

Please sign in to comment.