Repository navigation
Expand file tree
/
Copy pathservice_test.go
More file actions
138 lines (129 loc) · 5.38 KB
/
Copy pathservice_test.go
File metadata and controls
138 lines (129 loc) · 5.38 KB
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
package ilert
import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
// The topology filter is a single condition expression, conditions are combined with "and"
// inside it, so it goes on the query as one value.
func TestGetServiceTopology(t *testing.T) {
var path string
var query url.Values
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path = r.URL.Path
query = r.URL.Query()
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"nodes":[{"id":1,"name":"checkout","status":"OPERATIONAL"},{"id":2,"restricted":true}],"edges":[{"id":9,"sourceServiceId":1,"targetServiceId":2,"type":"HARD"}]}`))
}))
defer srv.Close()
expression := "environment == 'production' and severity != 'low'"
result, err := newTestClient(t, srv.URL).GetServiceTopology(&GetServiceTopologyInput{Labels: String(expression)})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if path != "/api/services/topology" {
t.Errorf("path = %q, want /api/services/topology", path)
}
if got := query.Get("labels"); got != expression {
t.Errorf("labels = %q, want %q", got, expression)
}
if len(query["labels"]) != 1 {
t.Errorf("labels params = %v, want a single expression", query["labels"])
}
if len(result.ServiceGraph.Nodes) != 2 || result.ServiceGraph.Nodes[0].Name != "checkout" {
t.Errorf("nodes = %+v, want the checkout node first", result.ServiceGraph.Nodes)
}
if !result.ServiceGraph.Nodes[1].Restricted {
t.Errorf("node = %+v, want it marked restricted", result.ServiceGraph.Nodes[1])
}
if len(result.ServiceGraph.Edges) != 1 || result.ServiceGraph.Edges[0].TargetServiceID != 2 {
t.Errorf("edges = %+v, want one edge pointing at service 2", result.ServiceGraph.Edges)
}
}
func TestGetServiceTopologyWithoutFilter(t *testing.T) {
var query url.Values
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
query = r.URL.Query()
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"nodes":[],"edges":[]}`))
}))
defer srv.Close()
if _, err := newTestClient(t, srv.URL).GetServiceTopology(nil); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if _, ok := query["labels"]; ok {
t.Errorf("labels param is present, want it absent")
}
}
// The status query param is what keeps a status change that happened in the meantime from
// being published by accident, so it must always reach the API.
func TestPublishServiceStatus(t *testing.T) {
var method, path string
var query url.Values
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
method = r.Method
path = r.URL.Path
query = r.URL.Query()
w.Header().Set("Content-Type", "application/json")
// the endpoint answers with the plain service, without the optional publicStatus
_, _ = w.Write([]byte(`{"id":3,"name":"checkout","status":"MAJOR_OUTAGE"}`))
}))
defer srv.Close()
result, err := newTestClient(t, srv.URL).PublishServiceStatus(&PublishServiceStatusInput{
ServiceID: Int64(3),
Status: String(ServiceStatus.MajorOutage),
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if method != http.MethodPut {
t.Errorf("method = %s, want PUT", method)
}
if path != "/api/services/3/publish-status" {
t.Errorf("path = %q, want /api/services/3/publish-status", path)
}
if got := query.Get("status"); got != "MAJOR_OUTAGE" {
t.Errorf("status = %q, want MAJOR_OUTAGE", got)
}
if result.Service.ID != 3 || result.Service.Status != ServiceStatus.MajorOutage {
t.Errorf("service = %+v, want service 3 with status MAJOR_OUTAGE", result.Service)
}
}
func TestPublishServiceStatusRequiredInputs(t *testing.T) {
c := NewClient()
if _, err := c.PublishServiceStatus(&PublishServiceStatusInput{Status: String(ServiceStatus.Degraded)}); err == nil {
t.Error("PublishServiceStatus without a service id = nil error, want an error")
}
if _, err := c.PublishServiceStatus(&PublishServiceStatusInput{ServiceID: Int64(3)}); err == nil {
t.Error("PublishServiceStatus without a status = nil error, want an error")
}
}
// Labels come back as a plain object from the service endpoints and as an entry list when the
// service is nested in a status update or an automation rule; both have to decode to the same map.
func TestServiceLabelsDecodeInBothShapes(t *testing.T) {
for name, body := range map[string]string{
"object": `{"id":1,"labels":{"team":"payments"}}`,
"entry list": `{"id":1,"labels":{"entry":[{"key":"team","value":"payments"}]}}`,
"entry object": `{"id":1,"labels":{"entry":{"key":"team","value":"payments"}}}`,
} {
service := Service{}
if err := json.Unmarshal([]byte(body), &service); err != nil {
t.Errorf("%s: unexpected error: %v", name, err)
continue
}
if service.ID != 1 || service.Labels == nil || (*service.Labels)["team"] != "payments" || len(*service.Labels) != 1 {
t.Errorf("%s: service = %+v, want id 1 with team=payments", name, service)
}
}
for name, body := range map[string]string{"absent": `{"id":1}`, "null": `{"id":1,"labels":null}`} {
service := Service{}
if err := json.Unmarshal([]byte(body), &service); err != nil || service.Labels != nil {
t.Errorf("%s: labels = %v, err %v, want nil and no error", name, service.Labels, err)
}
}
if err := json.Unmarshal([]byte(`{"id":1,"labels":["team"]}`), &Service{}); err == nil {
t.Error("labels as a list decoded without an error, want one")
}
}