-
Notifications
You must be signed in to change notification settings - Fork 0
/
mikrus_test.go
218 lines (195 loc) · 4.73 KB
/
mikrus_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package mikrus_test
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/qba73/mikrus"
)
func TestMikrusReturnsInformationAboutServer(t *testing.T) {
t.Parallel()
ts := newTestServer("/info", info, t)
defer ts.Close()
c := mikrus.New("dummyKey", "dummySrv")
c.HTTPClient = ts.Client()
c.URL = ts.URL
got, err := c.Info()
if err != nil {
t.Fatal(err)
}
want := mikrus.Server{
ServerID: "j230",
Expires: "2026-06-08 00:00:00",
ParamRam: "1024",
ParamDisk: "10",
LastLogPanel: "2024-06-05 10:02:55",
MikrusPro: "nie",
}
if !cmp.Equal(want, got) {
t.Error(cmp.Diff(want, got))
}
}
func TestMikrusReturnsListOfServers(t *testing.T) {
t.Parallel()
ts := newTestServer("/serwery", servers, t)
defer ts.Close()
c := mikrus.New("dummyAPIKey", "dummyServerID")
c.HTTPClient = ts.Client()
c.URL = ts.URL
got, err := c.Servers()
if err != nil {
t.Fatal(err)
}
want := mikrus.Servers{
{
ServerID: "a133",
Expires: "2025-06-05 00:00:00",
ParamRam: "1024",
ParamDisk: "10",
},
{
ServerID: "j139",
Expires: "2026-06-08 00:00:00",
ParamRam: "1024",
ParamDisk: "10",
},
}
if !cmp.Equal(want, got) {
t.Error(cmp.Diff(want, got))
}
}
func TestMikrusReturnsServerLogs(t *testing.T) {
t.Parallel()
ts := newTestServer("/logs", logs, t)
defer ts.Close()
c := mikrus.New("dummyAPIKey", "dummyServerID")
c.HTTPClient = ts.Client()
c.URL = ts.URL
got, err := c.Logs()
if err != nil {
t.Fatal(err)
}
want := mikrus.Logs{
{
ID: "3752",
ServerID: "j230",
Task: "kluczssh",
WhenCreated: "2024-06-05 10:05:34",
WhenDone: "2024-06-05 10:06:01",
Output: "Wrzuciłem klucz SSH\n",
},
{
ID: "3751",
ServerID: "j230",
Task: "restart",
WhenCreated: "2024-06-05 09:57:54",
WhenDone: "2024-06-05 09:58:07",
Output: "OK\n",
},
{
ID: "3748",
ServerID: "j230",
Task: "upgrade",
WhenCreated: "2024-06-05 08:59:28",
WhenDone: "2024-06-05 09:00:04",
Output: "=== Aktualne parametry: 768 RAM / 10 DYSK\n2 / 20\nDodaje: +256MB RAM oraz +0GB dysku\nPo zmianie: 1024 MB / 10 GB\n[succes] GOTOWE!\n",
},
}
if !cmp.Equal(want, got) {
t.Error(cmp.Diff(want, got))
}
}
func newTestServer(path string, data []byte, t *testing.T) *httptest.Server {
return httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Fatalf("want POST request, got %q", r.Method)
}
verifyURL(path, r.URL.EscapedPath(), t)
if _, err := io.Copy(w, bytes.NewReader(data)); err != nil {
t.Fatal(err)
}
}))
}
// verifyURL checks if provided URLs are equal.
func verifyURL(wantURL, path string, t *testing.T) {
t.Helper()
wantU, err := url.Parse(wantURL)
if err != nil {
t.Fatalf("error parsing URL %q, %v", wantURL, err)
}
gotU, err := url.Parse(path)
if err != nil {
t.Fatalf("error parsing URL %q, %v", wantURL, err)
}
if !cmp.Equal(wantU.Path, gotU.Path) {
t.Fatalf(cmp.Diff(wantU.Path, gotU.Path))
}
wantQuery, err := url.ParseQuery(wantU.RawQuery)
if err != nil {
t.Fatal(err)
}
gotQuery, err := url.ParseQuery(gotU.RawQuery)
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(wantQuery, gotQuery) {
t.Fatalf("URLs are not equal, \n%s", cmp.Diff(wantQuery, gotQuery))
}
}
var (
info = []byte(`{"server_id": "j230",
"server_name": null,
"expires": "2026-06-08 00:00:00",
"expires_cytrus": null,
"expires_storage": null,
"param_ram": "1024",
"param_disk": "10",
"lastlog_panel": "2024-06-05 10:02:55",
"mikrus_pro": "nie"
}`)
servers = []byte(`[
{
"server_id": "a133",
"server_name": null,
"expires": "2025-06-05 00:00:00",
"param_ram": "1024",
"param_disk": "10"
},
{
"server_id": "j139",
"server_name": null,
"expires": "2026-06-08 00:00:00",
"param_ram": "1024",
"param_disk": "10"
}
]`)
logs = []byte(`[
{
"id": "3752",
"server_id": "j230",
"task": "kluczssh",
"when_created": "2024-06-05 10:05:34",
"when_done": "2024-06-05 10:06:01",
"output": "Wrzuciłem klucz SSH\n"
},
{
"id": "3751",
"server_id": "j230",
"task": "restart",
"when_created": "2024-06-05 09:57:54",
"when_done": "2024-06-05 09:58:07",
"output": "OK\n"
},
{
"id": "3748",
"server_id": "j230",
"task": "upgrade",
"when_created": "2024-06-05 08:59:28",
"when_done": "2024-06-05 09:00:04",
"output": "=== Aktualne parametry: 768 RAM / 10 DYSK\n2 / 20\nDodaje: +256MB RAM oraz +0GB dysku\nPo zmianie: 1024 MB / 10 GB\n[succes] GOTOWE!\n"
}
]`)
)