Skip to content

Commit ac2d595

Browse files
committed
Fix: Handle nil deployment outputs gracefully
Signed-off-by: Arun P Nair <[email protected]>
1 parent a28c285 commit ac2d595

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

pkg/arm/templates/arm.go

+4
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,10 @@ func (d *deployer) pollUntilComplete(
462462
func getOutputs(
463463
deployment *resourcesSDK.DeploymentExtended,
464464
) (map[string]interface{}, error) {
465+
// Check if deployment output is nil as "outputs" section/element is optional in ARM template.
466+
if deployment.Properties.Outputs == nil {
467+
return map[string]interface{}{}, nil
468+
}
465469
outputs, ok := deployment.Properties.Outputs.(map[string]interface{})
466470
if !ok {
467471
return nil, errors.New("error decoding deployment outputs")

pkg/arm/templates/arm_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package templates
2+
3+
import (
4+
"testing"
5+
6+
resourcesSDK "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2017-05-10/resources"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestGetOutputs_ValidOutputs(t *testing.T) {
11+
deployment := &resourcesSDK.DeploymentExtended{
12+
Properties: &resourcesSDK.DeploymentPropertiesExtended{
13+
Outputs: map[string]interface{}{
14+
"output1": map[string]interface{}{
15+
"value": "amaterasu",
16+
},
17+
"output2": map[string]interface{}{
18+
"value": 108,
19+
},
20+
},
21+
},
22+
}
23+
24+
outputs, err := getOutputs(deployment)
25+
assert.NoError(t, err)
26+
assert.Equal(t, "amaterasu", outputs["output1"])
27+
assert.Equal(t, 108, outputs["output2"])
28+
}
29+
30+
func TestGetOutputs_InvalidOutputs(t *testing.T) {
31+
deployment := &resourcesSDK.DeploymentExtended{
32+
Properties: &resourcesSDK.DeploymentPropertiesExtended{
33+
Outputs: "invalid",
34+
},
35+
}
36+
37+
outputs, err := getOutputs(deployment)
38+
assert.Error(t, err)
39+
assert.Nil(t, outputs)
40+
}
41+
42+
func TestGetOutputs_NoOutputs(t *testing.T) {
43+
deployment := &resourcesSDK.DeploymentExtended{
44+
Properties: &resourcesSDK.DeploymentPropertiesExtended{
45+
Outputs: nil,
46+
},
47+
}
48+
49+
outputs, err := getOutputs(deployment)
50+
assert.NoError(t, err)
51+
assert.Empty(t, outputs)
52+
}

0 commit comments

Comments
 (0)