Skip to content

Commit 6a9e346

Browse files
committed
wip: integration tests
1 parent 807b0e8 commit 6a9e346

File tree

5 files changed

+121
-81
lines changed

5 files changed

+121
-81
lines changed

docker-compose.mongo.test.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
services:
2+
mongo:
3+
image: mongo:4.4.29
4+
restart: unless-stopped
5+
healthcheck:
6+
test: 'test $$(echo "rs.initiate({ _id: ''rs'', members: [ { _id: 0, host: ''mongo:27017'' } ] }).ok || rs.status().ok" | mongo --quiet) -eq 1'
7+
interval: 30s
8+
start_period: 10s
9+
command: ["--replSet", "rs", "--bind_ip_all"]
10+
networks:
11+
- shellhub
12+
api:
13+
depends_on:
14+
mongo:
15+
condition: service_healthy

docker-compose.postgres.test.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
services:
2+
postgres:
3+
image: postgres:18.0
4+
command: postgres -c io_method=io_uring
5+
security_opt:
6+
# Disable seccomp to allow io_uring syscalls (io_uring_setup, io_uring_enter, io_uring_register)
7+
# which are blocked by Docker's default seccomp profile for security reasons.
8+
- seccomp=unconfined
9+
healthcheck:
10+
start_period: 90s
11+
interval: 5s
12+
timeout: 5s
13+
retries: 5
14+
test: ["CMD-SHELL", "pg_isready -U ${SHELLHUB_POSTGRES_USERNAME} -d ${SHELLHUB_POSTGRES_DATABASE}"]
15+
environment:
16+
- POSTGRES_USER=${SHELLHUB_POSTGRES_USERNAME}
17+
- POSTGRES_PASSWORD=${SHELLHUB_POSTGRES_PASSWORD}
18+
- POSTGRES_DB=${SHELLHUB_POSTGRES_DATABASE}
19+
networks:
20+
- shellhub
21+
api:
22+
depends_on:
23+
postgres:
24+
condition: service_healthy

docker-compose.test.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,3 @@ services:
3434
start_period: 10s
3535
retries: 20
3636
ports: []
37-
mongo:
38-
healthcheck:
39-
interval: 5s
40-
start_period: 10s
41-
retries: 20
42-
ports: []

tests/environment/configurator.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ func New(t *testing.T) *DockerComposeConfigurator {
3434
envs["SHELLHUB_SSH_PORT"] = GetFreePort(t)
3535
envs["SHELLHUB_NETWORK"] = "shellhub_network_" + uuid.Generate()
3636
envs["SHELLHUB_LOG_LEVEL"] = "trace"
37-
envs["SHELLHUB_DATABASE"] = "mongo"
38-
envs["DATABASE"] = "mongo"
3937

4038
return &DockerComposeConfigurator{
4139
envs: envs,
@@ -98,8 +96,16 @@ func (dcc *DockerComposeConfigurator) Up(ctx context.Context) *DockerCompose {
9896
down: nil,
9997
}
10098

99+
dockerFiles := []string{"../docker-compose.yml", "../docker-compose.test.yml"}
100+
switch dc.envs["SHELLHUB_DATABASE"] {
101+
case "postgres":
102+
dockerFiles = append(dockerFiles, "../docker-compose.postgres.test.yml")
103+
default:
104+
dockerFiles = append(dockerFiles, "../docker-compose.mongo.test.yml")
105+
}
106+
101107
tcDc, err := compose.NewDockerComposeWith(
102-
compose.WithStackFiles("../docker-compose.yml", "../docker-compose.mongo.yml", "../docker-compose.test.yml"),
108+
compose.WithStackFiles(dockerFiles...),
103109
compose.WithLogger(log.New(io.Discard, "", log.LstdFlags)),
104110
)
105111
if !assert.NoError(dcc.t, err) {

tests/ssh_test.go

Lines changed: 73 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,89 +1322,90 @@ func TestSSH(t *testing.T) {
13221322
},
13231323
}
13241324

1325-
ctx := context.Background()
1325+
databases := []string{"mongo", "postgres"}
1326+
for _, db := range databases {
1327+
ctx := context.Background()
13261328

1327-
compose := environment.New(t).Up(ctx)
1328-
t.Cleanup(func() {
1329-
compose.Down()
1330-
})
1329+
compose := environment.New(t).WithEnv("SHELLHUB_DATABASE", db).Up(ctx)
1330+
compose.NewUser(t, ShellHubUsername, ShellHubEmail, ShellHubPassword)
1331+
compose.NewNamespace(t, ShellHubUsername, ShellHubNamespaceName, ShellHubNamespace)
13311332

1332-
compose.NewUser(t, ShellHubUsername, ShellHubEmail, ShellHubPassword)
1333-
compose.NewNamespace(t, ShellHubUsername, ShellHubNamespaceName, ShellHubNamespace)
1334-
1335-
auth := models.UserAuthResponse{}
1336-
1337-
require.EventuallyWithT(t, func(tt *assert.CollectT) {
1338-
resp, err := compose.R(ctx).
1339-
SetBody(map[string]string{
1340-
"username": ShellHubUsername,
1341-
"password": ShellHubPassword,
1342-
}).
1343-
SetResult(&auth).
1344-
Post("/api/login")
1345-
assert.Equal(tt, 200, resp.StatusCode())
1346-
assert.NoError(tt, err)
1347-
}, 30*time.Second, 1*time.Second)
1348-
1349-
compose.JWT(auth.Token)
1350-
1351-
for _, tc := range tests {
1352-
test := tc
1353-
t.Run(test.name, func(tt *testing.T) {
1354-
agent, err := NewAgentContainer(
1355-
ctx,
1356-
compose.Env("SHELLHUB_HTTP_PORT"),
1357-
test.options...,
1358-
)
1359-
require.NoError(tt, err)
1360-
1361-
agent.Stop(ctx, nil)
1362-
1363-
err = agent.Start(ctx)
1364-
require.NoError(tt, err)
1365-
1366-
tt.Cleanup(func() {
1367-
agent.Stop(context.Background(), nil)
1368-
})
1369-
1370-
t.Cleanup(func() {
1371-
agent.Terminate(context.Background())
1372-
})
1333+
auth := models.UserAuthResponse{}
13731334

1374-
devices := []models.Device{}
1335+
require.EventuallyWithT(t, func(tt *assert.CollectT) {
1336+
resp, err := compose.R(ctx).
1337+
SetBody(map[string]string{
1338+
"username": ShellHubUsername,
1339+
"password": ShellHubPassword,
1340+
}).
1341+
SetResult(&auth).
1342+
Post("/api/login")
1343+
assert.Equal(tt, 200, resp.StatusCode())
1344+
assert.NoError(tt, err)
1345+
}, 30*time.Second, 1*time.Second)
1346+
1347+
compose.JWT(auth.Token)
1348+
1349+
for _, tc := range tests {
1350+
test := tc
1351+
t.Run(test.name, func(tt *testing.T) {
1352+
agent, err := NewAgentContainer(
1353+
ctx,
1354+
compose.Env("SHELLHUB_HTTP_PORT"),
1355+
test.options...,
1356+
)
1357+
require.NoError(tt, err)
1358+
1359+
agent.Stop(ctx, nil)
1360+
1361+
err = agent.Start(ctx)
1362+
require.NoError(tt, err)
1363+
1364+
tt.Cleanup(func() {
1365+
agent.Stop(context.Background(), nil)
1366+
})
13751367

1376-
require.EventuallyWithT(tt, func(tt *assert.CollectT) {
1377-
resp, err := compose.R(ctx).SetResult(&devices).
1378-
Get("/api/devices?status=pending")
1379-
assert.Equal(tt, 200, resp.StatusCode())
1380-
assert.NoError(tt, err)
1368+
t.Cleanup(func() {
1369+
agent.Terminate(context.Background())
1370+
})
13811371

1382-
assert.Len(tt, devices, 1)
1383-
}, 30*time.Second, 1*time.Second)
1372+
devices := []models.Device{}
13841373

1385-
resp, err := compose.R(ctx).
1386-
Patch(fmt.Sprintf("/api/devices/%s/accept", devices[0].UID))
1387-
require.Equal(tt, 200, resp.StatusCode())
1388-
require.NoError(tt, err)
1374+
require.EventuallyWithT(tt, func(tt *assert.CollectT) {
1375+
resp, err := compose.R(ctx).SetResult(&devices).
1376+
Get("/api/devices?status=pending")
1377+
assert.Equal(tt, 200, resp.StatusCode())
1378+
assert.NoError(tt, err)
13891379

1390-
device := models.Device{}
1380+
assert.Len(tt, devices, 1)
1381+
}, 30*time.Second, 1*time.Second)
13911382

1392-
require.EventuallyWithT(tt, func(tt *assert.CollectT) {
13931383
resp, err := compose.R(ctx).
1394-
SetResult(&device).
1395-
Get(fmt.Sprintf("/api/devices/%s", devices[0].UID))
1396-
assert.Equal(tt, 200, resp.StatusCode())
1397-
assert.NoError(tt, err)
1384+
Patch(fmt.Sprintf("/api/devices/%s/accept", devices[0].UID))
1385+
require.Equal(tt, 200, resp.StatusCode())
1386+
require.NoError(tt, err)
1387+
1388+
device := models.Device{}
1389+
1390+
require.EventuallyWithT(tt, func(tt *assert.CollectT) {
1391+
resp, err := compose.R(ctx).
1392+
SetResult(&device).
1393+
Get(fmt.Sprintf("/api/devices/%s", devices[0].UID))
1394+
assert.Equal(tt, 200, resp.StatusCode())
1395+
assert.NoError(tt, err)
13981396

1399-
assert.True(tt, device.Online)
1400-
}, 30*time.Second, 1*time.Second)
1397+
assert.True(tt, device.Online)
1398+
}, 30*time.Second, 1*time.Second)
14011399

1402-
// --
1400+
// --
14031401

1404-
test.run(tt, &Environment{
1405-
services: compose,
1406-
agent: agent,
1407-
}, &device)
1408-
})
1402+
test.run(tt, &Environment{
1403+
services: compose,
1404+
agent: agent,
1405+
}, &device)
1406+
})
1407+
}
1408+
1409+
compose.Down()
14091410
}
14101411
}

0 commit comments

Comments
 (0)