-
Notifications
You must be signed in to change notification settings - Fork 7
/
resources_test.go
186 lines (163 loc) · 5.29 KB
/
resources_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"fmt"
"io/ioutil"
"testing"
log "github.com/sirupsen/logrus"
)
type testResource struct {
res Resource
expected int
}
func TestGetMilliCPU(t *testing.T) {
tests := []testResource{
testResource{res: Resource{CPU: "130m", Memory: "350"}, expected: 130},
testResource{res: Resource{CPU: "1", Memory: "450"}, expected: 1000},
testResource{res: Resource{CPU: "0.5", Memory: "500"}, expected: 500},
testResource{res: Resource{CPU: "1.64", Memory: "640"}, expected: 1640},
}
log.Infof("%+v", tests)
for i, test := range tests {
log.Infof("test info %d -> %+v", i, test)
if result := test.res.GetMilliCPU(); result != test.expected {
t.Fatalf("Test failed! %d but expected %d", result, test.expected)
}
}
}
func TestGetMiMemory(t *testing.T) {
tests := []testResource{
testResource{res: Resource{CPU: "130m", Memory: "123Mi"}, expected: 123},
testResource{res: Resource{CPU: "1", Memory: "129M"}, expected: 123},
testResource{res: Resource{CPU: "0.5", Memory: "128974848"}, expected: 123},
}
log.Infof("%+v", tests)
for i, test := range tests {
log.Infof("test info %d -> %+v", i, test)
if result := test.res.GetMiMemory(); result != test.expected {
t.Fatalf("Test failed! %d but expected %d", result, test.expected)
}
}
}
func TestGetDeploymentName(t *testing.T) {
type testPodResource struct {
res Pod
expected string
}
tests := []testPodResource{
testPodResource{res: Pod{Metadata: Metadata{Name: "stateful-job-1"}}, expected: "stateful-job"},
testPodResource{res: Pod{Metadata: Metadata{Name: "shippingservice-545f46fb7f-f4c5b"}}, expected: "shippingservice"},
testPodResource{res: Pod{Metadata: Metadata{Name: "shipping-service-545f46fb7f-f4c5b"}}, expected: "shipping-service"},
testPodResource{res: Pod{Metadata: Metadata{Name: "forset"}}, expected: "forset"},
testPodResource{res: Pod{Metadata: Metadata{Name: "other-service"}}, expected: "other-service"},
testPodResource{res: Pod{Metadata: Metadata{Name: "other-service-"}}, expected: "other-service-"},
}
log.Infof("%+v", tests)
for i, test := range tests {
log.Infof("test info %d -> %+v", i, test)
if result := test.res.GetDeploymentName(); result != test.expected {
t.Fatalf("Test failed! %s but expected %s", result, test.expected)
}
}
}
func TestStartupDuration(t *testing.T) {
b, err := ioutil.ReadFile("test-data/one-pod.json")
if err != nil {
fmt.Print(err)
}
str := string(b)
pr := buildPodList(str).Items[0]
expected := 42.
if result := pr.GetStartupDuration().Seconds(); result != expected {
t.Fatalf("Test failed! %f but expected %f", result, expected)
}
}
func TestStartupMissingDurationInfo(t *testing.T) {
b, err := ioutil.ReadFile("test-data/one-pod-missing-duration-info.json")
if err != nil {
fmt.Print(err)
}
str := string(b)
pr := buildPodList(str).Items[0]
expected := 0.
if result := pr.GetStartupDuration().Seconds(); result != expected {
t.Fatalf("Test failed! %f but expected %f", result, expected)
}
}
func TestBuildOnePod(t *testing.T) {
b, err := ioutil.ReadFile("test-data/one-pod.json")
if err != nil {
fmt.Print(err)
}
str := string(b)
pr := buildPodList(str).Items[0]
ex := "shippingservice-545f46fb7f-f4c5b"
if re := pr.Metadata.Name; re != ex {
t.Fatalf("Test failed! %s but expected %s", re, ex)
}
ex = "shippingservice"
if re := pr.GetDeploymentName(); re != ex {
t.Fatalf("Test failed! %s but expected %s", re, ex)
}
ex = "shippingservice-545f46fb7f"
if re := pr.GetReplicaSetName(); re != ex {
t.Fatalf("Test failed! %s but expected %s", re, ex)
}
ex = "gke-central-pool-1-47d730e3-sh01"
if re := pr.Spec.NodeName; re != ex {
t.Fatalf("Test failed! %s but expected %s", re, ex)
}
expected := 200
if result := pr.GetRequestsMilliCPU(); result != expected {
t.Fatalf("Test failed! %d but expected %d", result, expected)
}
expected = 192
if result := pr.GetRequestsMiMemory(); result != expected {
t.Fatalf("Test failed! %d but expected %d", result, expected)
}
expected = 2200
if result := pr.GetLimitsMilliCPU(); result != expected {
t.Fatalf("Test failed! %d but expected %d", result, expected)
}
expected = 256
if result := pr.GetLimitsMiMemory(); result != expected {
t.Fatalf("Test failed! %d but expected %d", result, expected)
}
ex = "shippingservice"
if result := pr.Metadata.Labels["app"]; result != ex {
t.Fatalf("Test failed! %s but expected %s", result, ex)
}
}
func TestBuildManyPods(t *testing.T) {
b, err := ioutil.ReadFile("test-data/many-pods.json")
if err != nil {
fmt.Print(err)
}
str := string(b)
items := buildPodList(str).Items
ex := 23
if l := len(items); l != ex {
t.Fatalf("Test failed! found %d expected %d", l, ex)
}
for _, pod := range items {
if &pod.Metadata.Name == nil || pod.Metadata.Name == "" {
t.Fatalf("Test failed! Name should not be empty")
}
}
}
func TestBuildKubectlCmd(t *testing.T) {
type testCmd struct {
ns string
expected string
}
tests := []testCmd{
testCmd{ns: "test", expected: "kubectl get pods -n test -o json"},
testCmd{ns: "", expected: "kubectl get pods --all-namespaces -o json"},
}
log.Infof("%+v", tests)
for i, test := range tests {
log.Infof("test info %d -> %+v", i, test)
if result := buildKubectlCmd(test.ns); result != test.expected {
t.Fatalf("Test failed! %s but expected %s", result, test.expected)
}
}
}