-
Notifications
You must be signed in to change notification settings - Fork 23
/
naming_test.go
114 lines (99 loc) · 3.18 KB
/
naming_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"regexp"
"testing"
)
func TestNameWithPrefix_Name(t *testing.T) {
name := NameWithPrefix("full_name", "")
if name != "full_name" {
t.Fatalf("NameWithPrefix should return the name when name is present, name was %s", name)
}
}
func TestNameWithPrefix_NameAndPrefix(t *testing.T) {
name := NameWithPrefix("full_name", "prefix")
if name != "full_name" {
t.Fatalf("NameWithPrefix should return the name when name and prefix are present, name was %s", name)
}
}
func TestNameWithPrefix_Prefix(t *testing.T) {
r := regexp.MustCompile("prefix_[0-9]{26}")
name := NameWithPrefix("", "prefix_")
if !r.Match([]byte(name)) {
t.Fatalf("NameWithPrefix should return a generated name with prefix when prefix is present, name was %s", name)
}
}
func TestNameWithPrefix_NoNameAndNoPrefix(t *testing.T) {
r := regexp.MustCompile("terraform-[0-9]{26}")
name := NameWithPrefix("", "")
if !r.Match([]byte(name)) {
t.Fatalf("NameWithPrefix should return a generated name when prefix is present, name was %s", name)
}
}
func TestNamePrefixFromName_ValidPrefixedName(t *testing.T) {
prefix := NamePrefixFromName("test_role_20211103145836382400000001")
if *prefix != "test_role_" {
t.Fatalf("unexpected name prefix %s", *prefix)
}
}
func TestNamePrefixFromName_InvalidPrefixedName(t *testing.T) {
prefix := NamePrefixFromName("test_role")
if prefix != nil {
t.Fatal("expected prefix to be nil")
}
}
func TestValidRoleName_NameTooShort(t *testing.T) {
name := ""
warn, err := ValidRoleName(name, "")
if len(warn) != 0 || len(err) != 2 {
t.Fatalf("expected 2 validation errors")
}
}
func TestValidRoleName_NameMaxLength(t *testing.T) {
name := "0123456789012345678901234567890123456789012345678901234567890123"
warn, err := ValidRoleName(name, "")
if len(warn) != 0 || len(err) != 0 {
t.Fatalf("expected no validation errors")
}
}
func TestValidRoleName_NameTooLong(t *testing.T) {
name := "01234567890123456789012345678901234567890123456789012345678901234"
warn, err := ValidRoleName(name, "")
if len(warn) != 0 || len(err) != 1 {
t.Fatalf("expected 1 validation error")
}
}
func TestValidRoleName_NameInvalidChar(t *testing.T) {
name := "name!!!!@#$%^&*()-="
warn, err := ValidRoleName(name, "")
if len(warn) != 0 || len(err) != 1 {
t.Fatalf("expected 1 validation error")
}
}
func TestValidRolePrefix_NamePrefixTooShort(t *testing.T) {
prefix := ""
warn, err := ValidRolePrefix(prefix, "")
if len(warn) != 0 || len(err) != 2 {
t.Fatalf("expected 2 validation errors")
}
}
func TestValidRolePrefix_NamePrefixMaxLength(t *testing.T) {
prefix := "01234567890123456789012345678901234567"
warn, err := ValidRolePrefix(prefix, "")
if len(warn) != 0 || len(err) != 0 {
t.Fatalf("expected no validation errors")
}
}
func TestValidRolePrefix_NamePrefixTooLong(t *testing.T) {
prefix := "012345678901234567890123456789012345678"
warn, err := ValidRolePrefix(prefix, "")
if len(warn) != 0 || len(err) != 1 {
t.Fatalf("expected 1 validation error")
}
}
func TestValidRolePrefix_NamePrefixInvalidChar(t *testing.T) {
prefix := "name!!!!@#$%^&*()-="
warn, err := ValidRolePrefix(prefix, "")
if len(warn) != 0 || len(err) != 1 {
t.Fatalf("expected 1 validation error")
}
}