github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/tags_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestValidateMaximumNumberOfARMTags(t *testing.T) {
    10  	tagsMap := make(map[string]interface{})
    11  	for i := 0; i < 16; i++ {
    12  		tagsMap[fmt.Sprintf("key%d", i)] = fmt.Sprintf("value%d", i)
    13  	}
    14  
    15  	_, es := validateAzureRMTags(tagsMap, "tags")
    16  
    17  	if len(es) != 1 {
    18  		t.Fatal("Expected one validation error for too many tags")
    19  	}
    20  
    21  	if !strings.Contains(es[0].Error(), "a maximum of 15 tags") {
    22  		t.Fatal("Wrong validation error message for too many tags")
    23  	}
    24  }
    25  
    26  func TestValidateARMTagMaxKeyLength(t *testing.T) {
    27  	tooLongKey := strings.Repeat("long", 128) + "a"
    28  	tagsMap := make(map[string]interface{})
    29  	tagsMap[tooLongKey] = "value"
    30  
    31  	_, es := validateAzureRMTags(tagsMap, "tags")
    32  	if len(es) != 1 {
    33  		t.Fatal("Expected one validation error for a key which is > 512 chars")
    34  	}
    35  
    36  	if !strings.Contains(es[0].Error(), "maximum length for a tag key") {
    37  		t.Fatal("Wrong validation error message maximum tag key length")
    38  	}
    39  
    40  	if !strings.Contains(es[0].Error(), tooLongKey) {
    41  		t.Fatal("Expected validated error to contain the key name")
    42  	}
    43  
    44  	if !strings.Contains(es[0].Error(), "513") {
    45  		t.Fatal("Expected the length in the validation error for tag key")
    46  	}
    47  }
    48  
    49  func TestValidateARMTagMaxValueLength(t *testing.T) {
    50  	tagsMap := make(map[string]interface{})
    51  	tagsMap["toolong"] = strings.Repeat("long", 64) + "a"
    52  
    53  	_, es := validateAzureRMTags(tagsMap, "tags")
    54  	if len(es) != 1 {
    55  		t.Fatal("Expected one validation error for a value which is > 256 chars")
    56  	}
    57  
    58  	if !strings.Contains(es[0].Error(), "maximum length for a tag value") {
    59  		t.Fatal("Wrong validation error message for maximum tag value length")
    60  	}
    61  
    62  	if !strings.Contains(es[0].Error(), "toolong") {
    63  		t.Fatal("Expected validated error to contain the key name")
    64  	}
    65  
    66  	if !strings.Contains(es[0].Error(), "257") {
    67  		t.Fatal("Expected the length in the validation error for value")
    68  	}
    69  }
    70  
    71  func TestExpandARMTags(t *testing.T) {
    72  	testData := make(map[string]interface{})
    73  	testData["key1"] = "value1"
    74  	testData["key2"] = 21
    75  	testData["key3"] = "value3"
    76  
    77  	tempExpanded := expandTags(testData)
    78  	expanded := *tempExpanded
    79  
    80  	if len(expanded) != 3 {
    81  		t.Fatalf("Expected 3 results in expanded tag map, got %d", len(expanded))
    82  	}
    83  
    84  	for k, v := range testData {
    85  		var strVal string
    86  		switch v.(type) {
    87  		case string:
    88  			strVal = v.(string)
    89  		case int:
    90  			strVal = fmt.Sprintf("%d", v.(int))
    91  		}
    92  
    93  		if *expanded[k] != strVal {
    94  			t.Fatalf("Expanded value %q incorrect: expected %q, got %q", k, strVal, expanded[k])
    95  		}
    96  	}
    97  }