-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkerset_test.go
110 lines (101 loc) · 2.21 KB
/
workerset_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
package main
import (
"testing"
v1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestNewWorkerSetList(t *testing.T) {
workerSet := NewWorkerSetList(mockDeploymentList())
if len(workerSet) != 1 {
t.Error("should list deployments with annotations only")
}
}
func mockDeploymentList() *v1.DeploymentList {
list := &v1.DeploymentList{}
list.Items = []v1.Deployment{
*mockDeployMissingAnnotations(), *mockDeployWithAnnotations(),
}
return list
}
func TestNewWorkerSet(t *testing.T) {
workerSet := NewWorkerSet(mockDeployWithAnnotations())
if workerSet.MinReplicas != 1 {
t.Errorf("MinReplicas should be parsed")
}
if workerSet.MaxReplicas != 15 {
t.Errorf("MaxReplicas should be parsed")
}
if workerSet.MessagesPerPod != 5 {
t.Errorf("MessagesPerPod should be parsed")
}
if workerSet.Queue != "some-queue" {
t.Errorf("Queue name should be parsed")
}
if workerSet.Deploy == nil {
t.Errorf("Deploy should be set")
}
}
func mockDeployWithAnnotations() *v1.Deployment {
return &v1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"rabbitmkube/min-replicas": "1",
"rabbitmkube/max-replicas": "15",
"rabbitmkube/messages-per-pod": "5",
"rabbitmkube/queue-name": "some-queue",
},
},
}
}
func mockDeployMissingAnnotations() *v1.Deployment {
return &v1.Deployment{}
}
func TestValidate(t *testing.T) {
var tests = []struct {
worker WorkerSet
wantError bool
}{
{
worker: WorkerSet{
MaxReplicas: 0,
MinReplicas: 1,
MessagesPerPod: 1,
Queue: "queue",
},
wantError: true,
},
{
worker: WorkerSet{
MaxReplicas: 0,
MinReplicas: -1,
MessagesPerPod: 1,
Queue: "queue",
},
wantError: true,
},
{
worker: WorkerSet{
MaxReplicas: 0,
MinReplicas: 0,
MessagesPerPod: 0,
Queue: "queue",
},
wantError: true,
},
{
worker: WorkerSet{
MaxReplicas: 0,
MinReplicas: 0,
MessagesPerPod: 1,
Queue: " ",
},
wantError: true,
},
}
for _, tt := range tests {
err := tt.worker.Validate()
if err == nil && tt.wantError {
t.Errorf("%v should be invalid", tt.worker)
}
}
}