From 8d8c86366bd51b4b8e699470ecf1bb3fcb8b632d Mon Sep 17 00:00:00 2001 From: Amrita Dutta Date: Wed, 7 Nov 2018 22:23:22 +0000 Subject: [PATCH] Config tests + typo fix --- builder/azure/arm/config.go | 8 +- builder/azure/arm/config_test.go | 203 +++++++++++++++++++++++++++++++ 2 files changed, 207 insertions(+), 4 deletions(-) diff --git a/builder/azure/arm/config.go b/builder/azure/arm/config.go index 530439cf4..7eff6d831 100644 --- a/builder/azure/arm/config.go +++ b/builder/azure/arm/config.go @@ -56,8 +56,8 @@ var ( reCaptureNamePrefix = regexp.MustCompile("^[A-Za-z0-9][A-Za-z0-9_\\-\\.]{0,23}$") reManagedDiskName = regexp.MustCompile(validManagedDiskName) reResourceGroupName = regexp.MustCompile(validResourceGroupNameRe) - reSnaspshotName = regexp.MustCompile("^[A-Za-z0-9_]{10,79}$") - reSnaspshotPrefix = regexp.MustCompile("^[A-Za-z0-9_]{10,59}$") + reSnapshotName = regexp.MustCompile("^[A-Za-z0-9_]{10,79}$") + reSnapshotPrefix = regexp.MustCompile("^[A-Za-z0-9_]{10,59}$") ) type PlanInformation struct { @@ -758,14 +758,14 @@ func assertManagedImageName(name, setting string) (bool, error) { } func assertManagedImageOSDiskSnapshotName(name, setting string) (bool, error) { - if !isValidAzureName(reSnaspshotName, name) { + if !isValidAzureName(reSnapshotName, name) { return false, fmt.Errorf("The setting %s must only contain characters from a-z, A-Z, 0-9 and _ and the maximum length is 80 characters", setting) } return true, nil } func assertManagedImageDataDiskSnapshotName(name, setting string) (bool, error) { - if !isValidAzureName(reSnaspshotPrefix, name) { + if !isValidAzureName(reSnapshotPrefix, name) { return false, fmt.Errorf("The setting %s must only contain characters from a-z, A-Z, 0-9 and _ and the maximum length (excluding the prefix) is 60 characters", setting) } return true, nil diff --git a/builder/azure/arm/config_test.go b/builder/azure/arm/config_test.go index 60ef609d3..9dce2a6dd 100644 --- a/builder/azure/arm/config_test.go +++ b/builder/azure/arm/config_test.go @@ -547,6 +547,107 @@ func TestConfigShouldRejectMalformedCaptureContainerName(t *testing.T) { } } +func TestConfigShouldRejectMalformedManagedImageOSDiskSnapshotName(t *testing.T) { + config := map[string]interface{}{ + "image_offer": "ignore", + "image_publisher": "ignore", + "image_sku": "ignore", + "location": "ignore", + "subscription_id": "ignore", + "communicator": "none", + "managed_image_resource_group_name": "ignore", + "managed_image_name": "ignore", + "managed_image_os_disk_snapshot_name": "ignore", + // Does not matter for this test case, just pick one. + "os_type": constants.Target_Linux, + } + + wellFormedManagedImageOSDiskSnapshotName := []string{ + "AbcdefghijklmnopqrstuvwX", + "underscore_underscore", + "0leading_number", + "really_loooooooooooooooooooooooooooooooooooooooooooooooooong", + } + + for _, x := range wellFormedManagedImageOSDiskSnapshotName { + config["managed_image_os_disk_snapshot_name"] = x + _, _, err := newConfig(config, getPackerConfiguration()) + + if err != nil { + t.Errorf("Expected test to pass, but it failed with the well-formed managed_image_os_disk_snapshot_name set to %q.", x) + } + } + + malformedManagedImageOSDiskSnapshotName := []string{ + "min_ten", + "-leading-hyphen", + "trailing-hyphen-", + "trailing-period.", + "punc-!@#$%^&*()_+-=-punc", + "really_looooooooooooooooooooooooooooooooooooooooooooooooooooooong_exceeding_80_char_limit", + } + + for _, x := range malformedManagedImageOSDiskSnapshotName { + config["managed_image_os_disk_snapshot_name"] = x + _, _, err := newConfig(config, getPackerConfiguration()) + + if err == nil { + t.Errorf("Expected test to fail, but it succeeded with the malformed managed_image_os_disk_snapshot_name set to %q.", x) + } + } +} + +func TestConfigShouldRejectMalformedManagedImageDataDiskSnapshotPrefix(t *testing.T) { + config := map[string]interface{}{ + "image_offer": "ignore", + "image_publisher": "ignore", + "image_sku": "ignore", + "location": "ignore", + "subscription_id": "ignore", + "communicator": "none", + "managed_image_resource_group_name": "ignore", + "managed_image_name": "ignore", + "managed_image_data_disk_snapshot_prefix": "ignore", + // Does not matter for this test case, just pick one. + "os_type": constants.Target_Linux, + } + + wellFormedManagedImageDataDiskSnapshotPrefix := []string{ + "min_ten_chars", + "AbcdefghijklmnopqrstuvwX", + "underscore_underscore", + "0leading_number", + "less_than_sixty_characters", + } + + for _, x := range wellFormedManagedImageDataDiskSnapshotPrefix { + config["managed_image_data_disk_snapshot_prefix"] = x + _, _, err := newConfig(config, getPackerConfiguration()) + + if err != nil { + t.Errorf("Expected test to pass, but it failed with the well-formed managed_image_data_disk_snapshot_prefix set to %q.", x) + } + } + + malformedManagedImageDataDiskSnapshotPrefix := []string{ + "more_ten", + "-leading-hyphen", + "trailing-hyphen-", + "trailing-period.", + "punc-!@#$%^&*()_+-=-punc", + "really_looooooooooooooooooooooooooooooooooooooooooooooooooooooong_exceeding_60_char_limit", + } + + for _, x := range malformedManagedImageDataDiskSnapshotPrefix { + config["managed_image_data_disk_snapshot_prefix"] = x + _, _, err := newConfig(config, getPackerConfiguration()) + + if err == nil { + t.Errorf("Expected test to fail, but it succeeded with the malformed managed_image_data_disk_snapshot_prefix set to %q.", x) + } + } +} + func TestConfigShouldAcceptTags(t *testing.T) { config := map[string]interface{}{ "capture_name_prefix": "ignore", @@ -709,6 +810,108 @@ func TestConfigShouldRejectMissingCustomDataFile(t *testing.T) { } } +func TestConfigShouldRejectManagedImageOSDiskSnapshotNameWithoutManagedImageName(t *testing.T) { + config := map[string]interface{}{ + "image_offer": "ignore", + "image_publisher": "ignore", + "image_sku": "ignore", + "location": "ignore", + "subscription_id": "ignore", + "communicator": "none", + "managed_image_resource_group_name": "ignore", + "managed_image_os_disk_snapshot_name": "ignore", + // Does not matter for this test case, just pick one. + "os_type": constants.Target_Linux, + } + + _, _, err := newConfig(config, getPackerConfiguration()) + if err == nil { + t.Fatal("expected config to reject Managed Image build with OS disk snapshot name but without managed image name") + } +} + +func TestConfigShouldRejectManagedImageOSDiskSnapshotNameWithoutManagedImageResourceGroupName(t *testing.T) { + config := map[string]interface{}{ + "image_offer": "ignore", + "image_publisher": "ignore", + "image_sku": "ignore", + "location": "ignore", + "subscription_id": "ignore", + "communicator": "none", + "managed_image_name": "ignore", + "managed_image_os_disk_snapshot_name": "ignore", + // Does not matter for this test case, just pick one. + "os_type": constants.Target_Linux, + } + + _, _, err := newConfig(config, getPackerConfiguration()) + if err == nil { + t.Fatal("expected config to reject Managed Image build with OS disk snapshot name but without managed image resource group name") + } +} + +func TestConfigShouldRejectImageDataDiskSnapshotPrefixWithoutManagedImageName(t *testing.T) { + config := map[string]interface{}{ + "image_offer": "ignore", + "image_publisher": "ignore", + "image_sku": "ignore", + "location": "ignore", + "subscription_id": "ignore", + "communicator": "none", + "managed_image_resource_group_name": "ignore", + "managed_image_data_disk_snapshot_prefix": "ignore", + // Does not matter for this test case, just pick one. + "os_type": constants.Target_Linux, + } + + _, _, err := newConfig(config, getPackerConfiguration()) + if err == nil { + t.Fatal("expected config to reject Managed Image build with data disk snapshot prefix but without managed image name") + } +} + +func TestConfigShouldRejectImageDataDiskSnapshotPrefixWithoutManagedImageResourceGroupName(t *testing.T) { + config := map[string]interface{}{ + "image_offer": "ignore", + "image_publisher": "ignore", + "image_sku": "ignore", + "location": "ignore", + "subscription_id": "ignore", + "communicator": "none", + "managed_image_name": "ignore", + "managed_image_data_disk_snapshot_prefix": "ignore", + // Does not matter for this test case, just pick one. + "os_type": constants.Target_Linux, + } + + _, _, err := newConfig(config, getPackerConfiguration()) + if err == nil { + t.Fatal("expected config to reject Managed Image build with data disk snapshot prefix but without managed image resource group name") + } +} + +func TestConfigShouldAcceptManagedImageOSDiskSnapshotNameAndManagedImageDataDiskSnapshotPrefix(t *testing.T) { + config := map[string]interface{}{ + "image_offer": "ignore", + "image_publisher": "ignore", + "image_sku": "ignore", + "location": "ignore", + "subscription_id": "ignore", + "communicator": "none", + "managed_image_resource_group_name": "ignore", + "managed_image_name": "ignore", + "managed_image_os_disk_snapshot_name": "ignore_ignore", + "managed_image_data_disk_snapshot_prefix": "ignore_ignore", + // Does not matter for this test case, just pick one. + "os_type": constants.Target_Linux, + } + + _, _, err := newConfig(config, getPackerConfiguration()) + if err != nil { + t.Fatal("expected config to accept platform managed image build") + } +} + func TestConfigShouldAcceptPlatformManagedImageBuild(t *testing.T) { config := map[string]interface{}{ "image_offer": "ignore",