-
Notifications
You must be signed in to change notification settings - Fork 33
/
service_metadata_test.go
155 lines (135 loc) · 4.75 KB
/
service_metadata_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
package mackerel
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
)
func TestGetServiceMetaData(t *testing.T) {
var (
serviceName = "testService"
namespace = "testing"
lastModified = time.Date(2018, 3, 6, 3, 0, 0, 0, time.UTC)
)
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
u := fmt.Sprintf("/api/v0/services/%s/metadata/%s", serviceName, namespace)
if req.URL.Path != u {
t.Errorf("request URL should be %v but %v:", u, req.URL.Path)
}
if req.Method != "GET" {
t.Error("request method should be GET but: ", req.Method)
}
respJSON := `{"type":12345,"region":"jp","env":"staging","instance_type":"c4.xlarge"}`
res.Header()["Content-Type"] = []string{"application/json"}
res.Header()["Last-Modified"] = []string{lastModified.Format(http.TimeFormat)}
fmt.Fprint(res, respJSON)
}))
defer ts.Close()
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
metadataResp, err := client.GetServiceMetaData(serviceName, namespace)
if err != nil {
t.Error("err should be nil but: ", err)
}
metadata := metadataResp.ServiceMetaData
if metadata.(map[string]interface{})["type"].(float64) != 12345 {
t.Errorf("got: %v, want: %v", metadata.(map[string]interface{})["type"], 12345)
}
if metadata.(map[string]interface{})["region"] != "jp" {
t.Errorf("got: %v, want: %v", metadata.(map[string]interface{})["region"], "jp")
}
if metadata.(map[string]interface{})["env"] != "staging" {
t.Errorf("got: %v, want: %v", metadata.(map[string]interface{})["env"], "staging")
}
if metadata.(map[string]interface{})["instance_type"] != "c4.xlarge" {
t.Errorf("got: %v, want: %v", metadata.(map[string]interface{})["instance_type"], "c4.xlarge")
}
if !metadataResp.LastModified.Equal(lastModified) {
t.Errorf("got: %v, want: %v", metadataResp.LastModified, lastModified)
}
}
func TestGetServiceMetaDataNameSpaces(t *testing.T) {
var (
serviceName = "testService"
)
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
u := fmt.Sprintf("/api/v0/services/%s/metadata", serviceName)
if req.URL.Path != u {
t.Errorf("request URL should be %v but %v:", u, req.URL.Path)
}
if req.Method != "GET" {
t.Error("request method should be GET but: ", req.Method)
}
respJSON := `{"metadata":[{"namespace":"testing1"}, {"namespace":"testing2"}]}`
res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, respJSON)
}))
defer ts.Close()
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
namespaces, err := client.GetServiceMetaDataNameSpaces(serviceName)
if err != nil {
t.Error("err should be nil but: ", err)
}
if !reflect.DeepEqual(namespaces, []string{"testing1", "testing2"}) {
t.Errorf("got: %v, want: %v", namespaces, []string{"testing1", "testing2"})
}
}
func TestPutServiceMetaData(t *testing.T) {
var (
serviceName = "testService"
namespace = "testing"
)
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
u := fmt.Sprintf("/api/v0/services/%s/metadata/%s", serviceName, namespace)
if req.URL.Path != u {
t.Errorf("request URL should be %v but %v:", u, req.URL.Path)
}
if req.Method != "PUT" {
t.Error("request method should be PUT but: ", req.Method)
}
body, _ := io.ReadAll(req.Body)
reqJSON := `{"env":"staging","instance_type":"c4.xlarge","region":"jp","type":12345}` + "\n"
if string(body) != reqJSON {
t.Errorf("request body should be %v but %v", reqJSON, string(body))
}
res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, `{"success":true}`)
}))
defer ts.Close()
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
metadata := map[string]interface{}{
"type": 12345,
"region": "jp",
"env": "staging",
"instance_type": "c4.xlarge",
}
err := client.PutServiceMetaData(serviceName, namespace, &metadata)
if err != nil {
t.Error("err should be nil but: ", err)
}
}
func TestDeleteServiceMetaData(t *testing.T) {
var (
serviceName = "testService"
namespace = "testing"
)
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
u := fmt.Sprintf("/api/v0/services/%s/metadata/%s", serviceName, namespace)
if req.URL.Path != u {
t.Errorf("request URL should be %v but %v:", u, req.URL.Path)
}
if req.Method != "DELETE" {
t.Error("request method should be DELETE but: ", req.Method)
}
res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, `{"success":true}`)
}))
defer ts.Close()
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
err := client.DeleteServiceMetaData(serviceName, namespace)
if err != nil {
t.Error("err should be nil but: ", err)
}
}