-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsupervisorv1_test.go
96 lines (91 loc) · 2.44 KB
/
supervisorv1_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
package balena
import (
"context"
"fmt"
"io"
"net/http"
"os"
"testing"
"gotest.tools/v3/assert"
)
func TestSupervisorV1Service_Reboot_Cloud(t *testing.T) {
// Given
client, mux, cleanup := newFixture()
defer cleanup()
appID := int64(1514287)
deviceUUID := "00d859f123685e84772676f09465cc55"
mux.HandleFunc(
"/supervisor/v1/reboot",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
b, err := io.ReadAll(r.Body)
assert.NilError(t, err)
assert.Equal(
t,
`{"uuid":"00d859f123685e84772676f09465cc55","method":"POST","data":{"force":true}}`+"\n",
string(b),
)
fmt.Fprint(w, `{"Data":"OK","Error":""}`)
},
)
// When
err := client.SupervisorV1(appID, deviceUUID).Reboot(context.Background(), true)
// Then
assert.NilError(t, err)
}
func TestSupervisorV1Service_Reboot_CloudError(t *testing.T) {
// Given
client, mux, cleanup := newFixture()
defer cleanup()
appID := int64(1514287)
deviceUUID := "00d859f123685e84772676f09465cc55"
mux.HandleFunc(
"/supervisor/v1/reboot",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
b, err := io.ReadAll(r.Body)
assert.NilError(t, err)
assert.Equal(
t,
`{"uuid":"00d859f123685e84772676f09465cc55","method":"POST","data":{"force":true}}`+"\n",
string(b),
)
fmt.Fprint(w, `{"Data":"Not OK","Error":"Something was bad"}`)
},
)
// When
err := client.SupervisorV1(appID, deviceUUID).Reboot(context.Background(), true)
// Then
assert.Assert(t, err != nil)
assert.ErrorContains(t, err, "Something was bad")
}
func TestSupervisorV1Service_Reboot_Local(t *testing.T) {
// Given
assert.NilError(t, os.Setenv("BALENA_SUPERVISOR_API_KEY", "test"))
assert.NilError(t, os.Setenv("BALENA_APP_ID", "1122334"))
assert.NilError(t, os.Setenv("BALENA_DEVICE_UUID", "11223344556677"))
client, mux := supervisorV1Fixture(t)
mux.HandleFunc(
"/v1/reboot",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
b, err := io.ReadAll(r.Body)
assert.NilError(t, err)
expected := "apikey=test"
if r.URL.RawQuery != expected {
http.Error(w, fmt.Sprintf("query = %s ; expected %s", r.URL.RawQuery, expected), http.StatusInternalServerError)
return
}
assert.Equal(
t,
`{"force":true}`+"\n",
string(b),
)
fmt.Fprint(w, `{"Data":"OK","Error":""}`)
},
)
// When
err := client.Reboot(context.Background(), true)
// Then
assert.NilError(t, err)
}