From ac2d59520d045c026983b2ad2fd78cb9472cd014 Mon Sep 17 00:00:00 2001 From: Arun P Nair Date: Wed, 2 Oct 2024 02:25:57 -0700 Subject: [PATCH] Fix: Handle nil deployment outputs gracefully Signed-off-by: Arun P Nair --- pkg/arm/templates/arm.go | 4 +++ pkg/arm/templates/arm_test.go | 52 +++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 pkg/arm/templates/arm_test.go diff --git a/pkg/arm/templates/arm.go b/pkg/arm/templates/arm.go index 1b227d6..72ce2c0 100644 --- a/pkg/arm/templates/arm.go +++ b/pkg/arm/templates/arm.go @@ -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") diff --git a/pkg/arm/templates/arm_test.go b/pkg/arm/templates/arm_test.go new file mode 100644 index 0000000..ba9b75f --- /dev/null +++ b/pkg/arm/templates/arm_test.go @@ -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) +}