-
Notifications
You must be signed in to change notification settings - Fork 0
/
item_tests.go
98 lines (79 loc) · 2.83 KB
/
item_tests.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
// Reusable testing libraries for testing adapters
package discovery
import (
"regexp"
"testing"
"github.com/overmindtech/sdp-go"
)
var RFC1123 = regexp.MustCompile(`^[a-z0-9]([a-z0-9-]*[a-z0-9])?$`)
// TestValidateItem Checks an item to ensure it is a valid SDP item. This includes
// checking that all required attributes are populated
func TestValidateItem(t *testing.T, i *sdp.Item) {
// Ensure that the item has the required fields set i.e.
//
// * Type
// * UniqueAttribute
// * Attributes
if i.GetType() == "" {
t.Errorf("Item %v has an empty Type", i.GloballyUniqueName())
}
// Validate that the pattern is RFC1123
if !RFC1123.MatchString(i.GetType()) {
pattern := `
Type names should match RFC1123 (lower case). This means the name must:
* contain at most 63 characters
* contain only lowercase alphanumeric characters or '-'
* start with an alphanumeric character
* end with an alphanumeric character
`
t.Errorf("Item type %v is invalid. %v", i.GetType(), pattern)
}
if i.GetUniqueAttribute() == "" {
t.Errorf("Item %v has an empty UniqueAttribute", i.GloballyUniqueName())
}
attrMap := i.GetAttributes().GetAttrStruct().AsMap()
if len(attrMap) == 0 {
t.Errorf("Attributes for item %v are empty", i.GloballyUniqueName())
}
// Check the attributes themselves for validity
for k := range attrMap {
if k == "" {
t.Errorf("Item %v has an attribute with an empty name", i.GloballyUniqueName())
}
}
// Make sure that the UniqueAttributeValue is populated
if i.UniqueAttributeValue() == "" {
t.Errorf("UniqueAttribute %v for item %v is empty", i.GetUniqueAttribute(), i.GloballyUniqueName())
}
for index, linkedItem := range i.GetLinkedItems() {
item := linkedItem.GetItem()
if item.GetType() == "" {
t.Errorf("LinkedItem %v of item %v has empty type", index, i.GloballyUniqueName())
}
if item.GetUniqueAttributeValue() == "" {
t.Errorf("LinkedItem %v of item %v has empty UniqueAttributeValue", index, i.GloballyUniqueName())
}
// We don't need to check for an empty scope here since if it's empty
// it will just inherit the scope of the parent
}
for index, linkedItemQuery := range i.GetLinkedItemQueries() {
query := linkedItemQuery.GetQuery()
if query.GetType() == "" {
t.Errorf("LinkedItemQueries %v of item %v has empty type", index, i.GloballyUniqueName())
}
if query.GetMethod() != sdp.QueryMethod_LIST {
if query.GetQuery() == "" {
t.Errorf("LinkedItemQueries %v of item %v has empty query. This is not allowed unless the method is LIST", index, i.GloballyUniqueName())
}
}
if query.GetScope() == "" {
t.Errorf("LinkedItemQueries %v of item %v has empty scope", index, i.GloballyUniqueName())
}
}
}
// TestValidateItems Runs TestValidateItem on many items
func TestValidateItems(t *testing.T, is []*sdp.Item) {
for _, i := range is {
TestValidateItem(t, i)
}
}