This repository has been archived by the owner on Jan 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
/
slo_test.go
180 lines (153 loc) · 3.97 KB
/
slo_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
package gapi
import (
"testing"
"github.com/gobs/pretty"
)
func TestSLOs(t *testing.T) {
t.Run("list all SLOs succeeds", func(t *testing.T) {
client := gapiTestTools(t, 200, getSlosJSON)
resp, err := client.ListSlos()
slos := resp.Slos
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(slos))
if len(slos) != 1 {
t.Errorf("wrong number of contact points returned, got %d", len(slos))
}
if slos[0].Name != "list-slos" {
t.Errorf("incorrect name - expected Name-Test, got %s", slos[0].Name)
}
})
t.Run("get individual SLO succeeds", func(t *testing.T) {
client := gapiTestTools(t, 200, getSloJSON)
slo, err := client.GetSlo("qkkrknp12w6tmsdcrfkdf")
t.Log(pretty.PrettyFormat(slo))
if err != nil {
t.Error(err)
}
if slo.UUID != "qkkrknp12w6tmsdcrfkdf" {
t.Errorf("incorrect UID - expected qkkrknp12w6tmsdcrfkdf, got %s", slo.UUID)
}
})
t.Run("get non-existent SLOs fails", func(t *testing.T) {
client := gapiTestTools(t, 404, getSlosJSON)
slo, err := client.GetSlo("qkkrknp12w6tmsdcrfkdf")
if err == nil {
t.Log(pretty.PrettyFormat(slo))
t.Error("expected error but got nil")
}
})
t.Run("create SLO succeeds", func(t *testing.T) {
client := gapiTestTools(t, 201, createSloJSON)
slo := generateSlo()
resp, err := client.CreateSlo(slo)
if err != nil {
t.Error(err)
}
if resp.UUID != "sjnp8wobcbs3eit28n8yb" {
t.Errorf("unexpected UID returned, got %s", resp.UUID)
}
})
t.Run("update SLO succeeds", func(t *testing.T) {
client := gapiTestTools(t, 200, createSloJSON)
slo := generateSlo()
slo.Description = "Updated Description"
err := client.UpdateSlo(slo.UUID, slo)
if err != nil {
t.Error(err)
}
})
t.Run("delete SLO succeeds", func(t *testing.T) {
client := gapiTestTools(t, 204, "")
err := client.DeleteSlo("qkkrknp12w6tmsdcrfkdf")
if err != nil {
t.Log(err)
t.Error(err)
}
})
}
const getSlosJSON = `
{
"slos": [
{
"uuid": "qkkrknp12w6tmsdcrfkdf",
"name": "list-slos",
"description": "list-slos-description",
"query": {
"freeform": {
"query": "sum(rate(apiserver_request_total{code!=\"500\"}[$__rate_interval])) / sum(rate(apiserver_request_total[$__rate_interval]))"
},
"type": "freeform"
},
"objectives": [
{
"value": 0.995,
"window": "28d"
}
],
"drillDownDashboardRef": {
"uid": "5IkqX6P4k"
}
}
]
}`
const getSloJSON = `
{
"uuid": "qkkrknp12w6tmsdcrfkdf",
"name": "Name-Test",
"description": "Description-Test",
"query": {
"freeform": {
"query": "sum(rate(apiserver_request_total{code!=\"500\"}[$__rate_interval])) / sum(rate(apiserver_request_total[$__rate_interval]))"
},
"type": "freeform"
},
"objectives": [
{
"value": 0.995,
"window": "28d"
}
],
"drillDownDashboardRef": {
"uid": "5IkqX6P4k"
}
}`
const createSloJSON = `
{
"uuid": "sjnp8wobcbs3eit28n8yb",
"name": "test-name",
"description": "test-description",
"query": {
"freeform": {
"query": "sum(rate(apiserver_request_total{code!=\"500\"}[$__rate_interval])) / sum(rate(apiserver_request_total[$__rate_interval]))"
},
"type": "freeform"
},
"objectives": [
{
"value": 0.995,
"window": "30d"
}
],
"drillDownDashboardRef": {
"uid": "zz5giRyVk"
}
}
`
func generateSlo() Slo {
objective := []Objective{{Value: 0.995, Window: "30d"}}
query := Query{
Freeform: &FreeformQuery{
Query: "sum(rate(apiserver_request_total{code!=\"500\"}[$__rate_interval])) / sum(rate(apiserver_request_total[$__rate_interval]))",
},
Type: QueryTypeFreeform,
}
slo := Slo{
Name: "test-name",
Description: "test-description",
Objectives: objective,
Query: query,
}
return slo
}