-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathitem_generator_test.go
130 lines (110 loc) · 3.41 KB
/
item_generator_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package sdpcache
import (
"math/rand"
"time"
"github.com/google/uuid"
"github.com/overmindtech/cli/sdp-go"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"
)
var Types = []string{
"person",
"dog",
"kite",
"flag",
"cat",
"leopard",
"fish",
"bird",
"kangaroo",
"ostrich",
"emu",
"hawk",
"mole",
"badger",
"lemur",
}
const MaxAttributes = 30
const MaxTags = 10
const MaxTagKeyLength = 10
const MaxTagValueLength = 10
const MaxAttributeKeyLength = 20
const MaxAttributeValueLength = 50
const MaxLinkedItems = 10
const MaxLinkedItemQueries = 10
// GenerateRandomItem Generates a random item and the tags for this item. The
// tags include the name, type and a tag called "all" with a value of "all"
func GenerateRandomItem() *sdp.Item {
attrs := make(map[string]interface{})
name := randSeq(rand.Intn(MaxAttributeValueLength))
typ := Types[rand.Intn(len(Types))]
scope := randSeq(rand.Intn(MaxAttributeKeyLength))
attrs["name"] = name
for i := 0; i < rand.Intn(MaxAttributes); i++ {
attrs[randSeq(rand.Intn(MaxAttributeKeyLength))] = randSeq(rand.Intn(MaxAttributeValueLength))
}
attributes, _ := sdp.ToAttributes(attrs)
tags := make(map[string]string)
for i := 0; i < rand.Intn(MaxTags); i++ {
tags[randSeq(rand.Intn(MaxTagKeyLength))] = randSeq(rand.Intn(MaxTagValueLength))
}
linkedItems := make([]*sdp.LinkedItem, rand.Intn(MaxLinkedItems))
for i := range linkedItems {
linkedItems[i] = &sdp.LinkedItem{Item: &sdp.Reference{
Type: randSeq(rand.Intn(MaxAttributeKeyLength)),
UniqueAttributeValue: randSeq(rand.Intn(MaxAttributeValueLength)),
Scope: randSeq(rand.Intn(MaxAttributeKeyLength)),
}}
}
linkedItemQueries := make([]*sdp.LinkedItemQuery, rand.Intn(MaxLinkedItemQueries))
for i := range linkedItemQueries {
linkedItemQueries[i] = &sdp.LinkedItemQuery{Query: &sdp.Query{
Type: randSeq(rand.Intn(MaxAttributeKeyLength)),
Method: sdp.QueryMethod(rand.Intn(3)),
Query: randSeq(rand.Intn(MaxAttributeValueLength)),
RecursionBehaviour: &sdp.Query_RecursionBehaviour{
LinkDepth: rand.Uint32(),
FollowOnlyBlastPropagation: rand.Intn(2) == 0,
},
Scope: randSeq(rand.Intn(MaxAttributeKeyLength)),
}}
}
// Generate health (which is an int32 between 0 and 4)
health := sdp.Health(rand.Intn(int(sdp.Health_HEALTH_PENDING) + 1))
queryUuid := uuid.New()
item := sdp.Item{
Type: typ,
UniqueAttribute: "name",
Attributes: attributes,
Scope: scope,
LinkedItemQueries: linkedItemQueries,
LinkedItems: linkedItems,
Metadata: &sdp.Metadata{
SourceName: randSeq(rand.Intn(MaxAttributeKeyLength)),
SourceQuery: &sdp.Query{
Type: typ,
Method: sdp.QueryMethod_GET,
Query: name,
RecursionBehaviour: &sdp.Query_RecursionBehaviour{
LinkDepth: 1,
},
Scope: scope,
UUID: queryUuid[:],
},
Timestamp: timestamppb.New(time.Now()),
SourceDuration: durationpb.New(time.Millisecond * time.Duration(rand.Int63())),
SourceDurationPerItem: durationpb.New(time.Millisecond * time.Duration(rand.Int63())),
},
Tags: tags,
Health: &health,
}
return &item
}
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randSeq(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}