-
Notifications
You must be signed in to change notification settings - Fork 398
Expand file tree
/
Copy pathstatus_test.go
More file actions
241 lines (226 loc) · 7.59 KB
/
status_test.go
File metadata and controls
241 lines (226 loc) · 7.59 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package status
import (
"bytes"
"context"
"errors"
"net/http"
"testing"
"github.com/docker/docker/api/types/container"
"github.com/h2non/gock"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/supabase/cli/internal/testing/apitest"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/api"
)
func TestStatusCommand(t *testing.T) {
t.Run("shows service status", func(t *testing.T) {
var running []container.Summary
for _, name := range utils.GetDockerIds() {
running = append(running, container.Summary{
Names: []string{name + "_test"},
})
}
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.InitConfig(utils.InitParams{ProjectId: "test"}, fsys))
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/containers/supabase_db_test/json").
Reply(http.StatusOK).
JSON(container.InspectResponse{ContainerJSONBase: &container.ContainerJSONBase{
State: &container.State{
Running: true,
},
}})
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/containers/json").
Reply(http.StatusOK).
JSON(running)
// Run test
assert.NoError(t, Run(context.Background(), CustomName{}, utils.OutputPretty, fsys))
// Check error
assert.Empty(t, apitest.ListUnmatchedRequests())
})
t.Run("throws error on malformed config", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, afero.WriteFile(fsys, utils.ConfigPath, []byte("malformed"), 0644))
// Run test
err := Run(context.Background(), CustomName{}, utils.OutputPretty, fsys)
// Check error
assert.ErrorContains(t, err, "toml: expected = after a key, but the document ends there")
})
t.Run("throws error on missing docker", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.WriteConfig(fsys, false))
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/containers/supabase_db_").
ReplyError(errors.New("network error"))
// Run test
err := Run(context.Background(), CustomName{}, utils.OutputPretty, fsys)
// Check error
assert.ErrorContains(t, err, "network error")
assert.Empty(t, apitest.ListUnmatchedRequests())
})
}
func TestServiceHealth(t *testing.T) {
t.Run("checks all services", func(t *testing.T) {
var running []container.Summary
for _, name := range utils.GetDockerIds() {
running = append(running, container.Summary{
Names: []string{"/" + name},
})
}
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/containers/json").
Reply(http.StatusOK).
JSON(running)
// Run test
stopped, err := checkServiceHealth(context.Background())
// Check error
assert.NoError(t, err)
assert.Empty(t, stopped)
assert.Empty(t, apitest.ListUnmatchedRequests())
})
t.Run("shows stopped container", func(t *testing.T) {
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/containers/json").
Reply(http.StatusOK).
JSON([]container.Summary{})
// Run test
stopped, err := checkServiceHealth(context.Background())
// Check error
assert.NoError(t, err)
assert.ElementsMatch(t, utils.GetDockerIds(), stopped)
assert.Empty(t, apitest.ListUnmatchedRequests())
})
t.Run("throws error on network error", func(t *testing.T) {
errNetwork := errors.New("network error")
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/containers/json").
ReplyError(errNetwork)
// Run test
stopped, err := checkServiceHealth(context.Background())
// Check error
assert.ErrorIs(t, err, errNetwork)
assert.Empty(t, stopped)
assert.Empty(t, apitest.ListUnmatchedRequests())
})
}
func TestPrintStatus(t *testing.T) {
utils.Config.Db.Port = 0
utils.Config.Hostname = "127.0.0.1"
utils.Config.Api.Enabled = false
utils.Config.Auth.Enabled = false
utils.Config.Storage.Enabled = false
utils.Config.Realtime.Enabled = false
utils.Config.Studio.Enabled = false
utils.Config.Analytics.Enabled = false
utils.Config.Inbucket.Enabled = false
t.Run("outputs env var", func(t *testing.T) {
utils.Config.Hostname = "127.0.0.1"
// Run test
var stdout bytes.Buffer
assert.NoError(t, printStatus(CustomName{DbURL: "DB_URL"}, utils.OutputEnv, &stdout))
// Check error
assert.Equal(t, "DB_URL=\"postgresql://postgres:postgres@127.0.0.1:0/postgres\"\n", stdout.String())
})
t.Run("outputs json object", func(t *testing.T) {
// Run test
var stdout bytes.Buffer
assert.NoError(t, printStatus(CustomName{DbURL: "DB_URL"}, utils.OutputJson, &stdout))
// Check error
assert.Equal(t, "{\n \"DB_URL\": \"postgresql://postgres:postgres@127.0.0.1:0/postgres\"\n}\n", stdout.String())
})
t.Run("outputs yaml properties", func(t *testing.T) {
// Run test
var stdout bytes.Buffer
assert.NoError(t, printStatus(CustomName{DbURL: "DB_URL"}, utils.OutputYaml, &stdout))
// Check error
assert.Equal(t, "DB_URL: postgresql://postgres:postgres@127.0.0.1:0/postgres\n", stdout.String())
})
t.Run("outputs toml fields", func(t *testing.T) {
// Run test
var stdout bytes.Buffer
assert.NoError(t, printStatus(CustomName{DbURL: "DB_URL"}, utils.OutputToml, &stdout))
// Check error
assert.Equal(t, "DB_URL = \"postgresql://postgres:postgres@127.0.0.1:0/postgres\"\n", stdout.String())
})
}
func TestRemoteStatusCommand(t *testing.T) {
t.Run("shows remote health status", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
projectRef := apitest.RandomProjectRef()
require.NoError(t, afero.WriteFile(fsys, utils.ProjectRefPath, []byte(projectRef), 0644))
// Setup access token
token := apitest.RandomAccessToken(t)
t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
// Setup mock API
defer gock.OffAll()
gock.New(utils.DefaultApiHost).
Get("/v1/projects/" + projectRef + "/health").
ParamPresent("services").
Reply(http.StatusOK).
JSON([]api.V1ServiceHealthResponse{
{
Name: api.V1ServiceHealthResponseNameAuth,
Healthy: true,
Status: api.ACTIVEHEALTHY,
},
{
Name: api.V1ServiceHealthResponseNameRealtime,
Healthy: true,
Status: api.ACTIVEHEALTHY,
},
{
Name: api.V1ServiceHealthResponseNameRest,
Healthy: true,
Status: api.ACTIVEHEALTHY,
},
{
Name: api.V1ServiceHealthResponseNameStorage,
Healthy: true,
Status: api.ACTIVEHEALTHY,
},
{
Name: api.V1ServiceHealthResponseNameDb,
Healthy: true,
Status: api.ACTIVEHEALTHY,
},
})
// Run test
assert.NoError(t, Run(context.Background(), CustomName{}, utils.OutputPretty, fsys))
// Check error
assert.Empty(t, apitest.ListUnmatchedRequests())
})
t.Run("throws error on missing project ref", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
defer gock.OffAll()
// Reset global state
flags.ProjectRef = ""
// Run test
err := Run(context.Background(), CustomName{}, utils.OutputPretty, fsys)
// Check error
assert.ErrorContains(t, err, "project ref")
})
}