packer-cn/builder/azure/arm/builder_test.go
Braunson b2047bd938
Put the correct AzureTags type in StateBag (#10014)
Azure expects the tags format to use a pointer to the string for the map value.
The configuration from the builder is not a pointer so when storing in the state bag for
reference in later execution we convert it when creating the StateBag.

Fixes #10012 and #10013.

* Use the MapToAzureTags helper and error check in resource group creation

* Added test case for tag values not using a pointer

* test/azure/arm: Add test to verify tags stored in state

* test/azure/arm: Add azure_tags to existing acceptance test for Linux

Test Before Fix
```
--- FAIL: TestBuilderAcc_ManagedDisk_Linux (1.81s)
panic: interface conversion: interface {} is map[string]string, not map[string]*string [recovered]
        panic: interface conversion: interface {} is map[string]string, not map[string]*string
FAIL    github.com/hashicorp/packer/builder/azure/arm   1.822s

```

Test After Fix
```
2020/09/29 17:23:03 ui: ==> test: Resource group has been deleted.
--- PASS: TestBuilderAcc_ManagedDisk_Linux (517.41s)
    PASS
    ok      github.com/hashicorp/packer/builder/azure/arm   517.426s
```

Co-authored-by: Wilken Rivera <dev@wilkenrivera.com>
2020-09-30 09:55:46 -04:00

68 lines
1.7 KiB
Go

package arm
import (
"testing"
"github.com/hashicorp/packer/builder/azure/common/constants"
)
func TestStateBagShouldBePopulatedExpectedValues(t *testing.T) {
var testSubject Builder
_, _, err := testSubject.Prepare(getArmBuilderConfiguration(), getPackerConfiguration())
if err != nil {
t.Fatalf("failed to prepare: %s", err)
}
var expectedStateBagKeys = []string{
constants.AuthorizedKey,
constants.ArmTags,
constants.ArmComputeName,
constants.ArmDeploymentName,
constants.ArmNicName,
constants.ArmResourceGroupName,
constants.ArmStorageAccountName,
constants.ArmVirtualMachineCaptureParameters,
constants.ArmPublicIPAddressName,
constants.ArmAsyncResourceGroupDelete,
}
for _, v := range expectedStateBagKeys {
if _, ok := testSubject.stateBag.GetOk(v); ok == false {
t.Errorf("Expected the builder's state bag to contain '%s', but it did not.", v)
}
}
}
func TestStateBagShouldPoluateExpectedTags(t *testing.T) {
var testSubject Builder
expectedTags := map[string]string{
"env": "test",
"builder": "packer",
}
armConfig := getArmBuilderConfiguration()
armConfig["azure_tags"] = expectedTags
_, _, err := testSubject.Prepare(armConfig, getPackerConfiguration())
if err != nil {
t.Fatalf("failed to prepare: %s", err)
}
tags, ok := testSubject.stateBag.Get(constants.ArmTags).(map[string]*string)
if !ok {
t.Errorf("Expected the builder's state bag to contain tags of type %T, but didn't.", testSubject.config.AzureTags)
}
if len(tags) != len(expectedTags) {
t.Errorf("expect tags from state to be the same length as tags from config")
}
for k, v := range tags {
if expectedTags[k] != *v {
t.Errorf("expect tag value of %s to be %s, but got %s", k, expectedTags[k], *v)
}
}
}