Skip to content

Commit cbc066c

Browse files
committed
wip2
1 parent 2542d62 commit cbc066c

File tree

2 files changed

+146
-32
lines changed

2 files changed

+146
-32
lines changed

tests/api_test.go

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package main
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
7+
"io"
68
"strconv"
9+
"strings"
710
"testing"
811

912
"github.com/go-resty/resty/v2"
13+
"github.com/sirupsen/logrus"
1014
"github.com/stretchr/testify/assert"
1115
)
1216

@@ -21,7 +25,7 @@ func TestInfo(t *testing.T) {
2125

2226
cases := []struct {
2327
description string
24-
compose *Enviroment
28+
compose *ComposeEnviroment
2529
// Unlike other expected values, this is a function because the body can change based on
2630
// the variables. For that reason, 'expected' retrieves 'httpPort' and 'sshPort', which
2731
// will be used to generate the correct body.
@@ -30,10 +34,12 @@ func TestInfo(t *testing.T) {
3034
{
3135
description: "case 1",
3236
compose: New().
33-
WithEnv("SHELLHUB_API_PORT", strconv.Itoa(getFreePort(t))).
34-
WithEnv("SHELLHUB_HTTP_PORT", strconv.Itoa(getFreePort(t))).
35-
WithEnv("SHELLHUB_SSH_PORT", strconv.Itoa(getFreePort(t))).
36-
WithEnv("SHELLHUB_VERSION", "v0.25.2").
37+
WithEnv(map[string]string{
38+
"SHELLHUB_API_PORT": strconv.Itoa(getFreePort(t)),
39+
"SHELLHUB_HTTP_PORT": strconv.Itoa(getFreePort(t)),
40+
"SHELLHUB_SSH_PORT": strconv.Itoa(getFreePort(t)),
41+
"SHELLHUB_VERSION": "v0.25.2",
42+
}).
3743
Build(),
3844
expected: func(httpPort, sshPort string) Expected {
3945
return Expected{
@@ -52,10 +58,12 @@ func TestInfo(t *testing.T) {
5258
{
5359
description: "case 2",
5460
compose: New().
55-
WithEnv("SHELLHUB_API_PORT", strconv.Itoa(getFreePort(t))).
56-
WithEnv("SHELLHUB_HTTP_PORT", strconv.Itoa(getFreePort(t))).
57-
WithEnv("SHELLHUB_SSH_PORT", strconv.Itoa(getFreePort(t))).
58-
WithEnv("SHELLHUB_VERSION", "v0.10.2").
61+
WithEnv(map[string]string{
62+
"SHELLHUB_API_PORT": strconv.Itoa(getFreePort(t)),
63+
"SHELLHUB_HTTP_PORT": strconv.Itoa(getFreePort(t)),
64+
"SHELLHUB_SSH_PORT": strconv.Itoa(getFreePort(t)),
65+
"SHELLHUB_VERSION": "v0.10.2",
66+
}).
5967
Build(),
6068
expected: func(httpPort, sshPort string) Expected {
6169
return Expected{
@@ -77,7 +85,7 @@ func TestInfo(t *testing.T) {
7785
// Avoid "loop variable <var> captured by func literal"
7886
tc := tt
7987

80-
tc.compose.Run(t, tc.description, func(t *testing.T) {
88+
tc.compose.Run(t, tc.description, func(ctx context.Context, _ Services, t *testing.T) {
8189
t.Parallel()
8290

8391
res, err := resty.
@@ -96,3 +104,52 @@ func TestInfo(t *testing.T) {
96104
})
97105
}
98106
}
107+
108+
func TestSetup(t *testing.T) {
109+
t.Parallel()
110+
111+
instance := New().WithServices([]ServiceKey{ServiceCLI})
112+
113+
cases := []struct {
114+
description string
115+
// compose *ComposeEnviroment
116+
setup func() error
117+
}{
118+
{
119+
description: "succeeds",
120+
// compose: instance.Build(),
121+
setup: func() error {
122+
compose := instance.Build()
123+
124+
return nil
125+
},
126+
},
127+
}
128+
129+
for _, tc := range cases {
130+
t.Run(tc.description, func(t *testing.T) {
131+
t.Parallel()
132+
})
133+
}
134+
}
135+
136+
// tc.compose.Run(t, tc.description, func(ctx context.Context, services Services, t *testing.T) {
137+
// t.Parallel()
138+
//
139+
// _, reader, err := services["cli"].Exec(ctx, []string{"./cli", "user", "create", "john_doe", "secret", "[email protected]"})
140+
//
141+
// buf := new(strings.Builder)
142+
// _, err = io.Copy(buf, reader)
143+
// if !assert.NoError(t, err) {
144+
// t.Fatal(err)
145+
// }
146+
//
147+
// text := `Ntime="2024-02-15T17:05:55Z" level=info msg="Setting log level" log_level=info
148+
// FUser created successfully
149+
// Username: john_doe
150+
151+
// `
152+
//
153+
// assert.Equal(t, buf.String(), text)
154+
// logrus.Info(buf.String())
155+
// })

tests/enviroment.go

Lines changed: 79 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,50 @@ package main
22

33
import (
44
"context"
5+
"strconv"
56
"testing"
6-
"time"
77

88
"github.com/joho/godotenv"
99
"github.com/stretchr/testify/assert"
10-
testcontainers "github.com/testcontainers/testcontainers-go/modules/compose"
10+
"github.com/testcontainers/testcontainers-go"
11+
dockercompose "github.com/testcontainers/testcontainers-go/modules/compose"
1112
"github.com/testcontainers/testcontainers-go/wait"
1213
)
1314

14-
type Enviroment struct {
15-
env map[string]string
15+
type ComposeEnviroment struct {
16+
env map[string]string
17+
services []ServiceKey
1618
}
1719

18-
type EnviromentBuilder struct {
19-
env map[string]string
20+
type ComposeEnviromentBuilder struct {
21+
env map[string]string
22+
services []ServiceKey
2023
}
2124

22-
func New() *EnviromentBuilder {
25+
func New() *ComposeEnviromentBuilder {
2326
env, _ := godotenv.Read("../.env")
24-
return &EnviromentBuilder{
25-
env: env,
27+
return &ComposeEnviromentBuilder{
28+
env: env,
29+
services: []ServiceKey{},
2630
}
2731
}
2832

29-
func (e *EnviromentBuilder) WithEnv(key, val string) *EnviromentBuilder {
30-
e.env[key] = val
33+
func (e *ComposeEnviromentBuilder) WithEnv(env map[string]string) *ComposeEnviromentBuilder {
34+
e.env = env
3135

3236
return e
3337
}
3438

35-
func (e *EnviromentBuilder) Build() *Enviroment {
36-
return &Enviroment{
37-
env: e.env,
39+
func (e *ComposeEnviromentBuilder) WithServices(services []ServiceKey) *ComposeEnviromentBuilder {
40+
e.services = append(e.services, services...)
41+
42+
return e
43+
}
44+
45+
func (e *ComposeEnviromentBuilder) Build() *ComposeEnviroment {
46+
return &ComposeEnviroment{
47+
env: e.env,
48+
services: e.services,
3849
}
3950
}
4051

@@ -44,19 +55,34 @@ func (e *EnviromentBuilder) Build() *Enviroment {
4455
///////////////////////
4556
///////////////////////
4657

47-
func (e *Enviroment) GetEnv(key string) string {
58+
func (e *ComposeEnviroment) GetEnv(key string) string {
4859
return e.env[key]
4960
}
5061

62+
type ServiceKey string
63+
64+
const (
65+
ServiceAPI ServiceKey = "api"
66+
ServiceCLI ServiceKey = "cli"
67+
ServiceGateway ServiceKey = "gateway"
68+
ServiceAgent ServiceKey = "agent"
69+
)
70+
71+
type Services map[ServiceKey]*testcontainers.DockerContainer
72+
5173
// Run starts and executes a callback cb within the specified compose environment.
5274
// It's a wrapper around t.Run().
53-
func (e *Enviroment) Run(t *testing.T, description string, cb func(t *testing.T)) {
75+
func (e *ComposeEnviroment) Run(t *testing.T, description string, cb func(context.Context, Services, *testing.T)) {
76+
e.env["SHELLHUB_API_PORT"] = strconv.Itoa(getFreePort(t))
77+
e.env["SHELLHUB_HTTP_PORT"] = strconv.Itoa(getFreePort(t))
78+
e.env["SHELLHUB_SSH_PORT"] = strconv.Itoa(getFreePort(t))
79+
5480
t.Run(description, func(t *testing.T) {
55-
compose, err := testcontainers.NewDockerCompose("../docker-compose.yml", "../docker-compose.dev.yml")
81+
compose, err := dockercompose.NewDockerCompose("../docker-compose.yml", "../docker-compose.dev.yml")
5682
assert.NoError(t, err)
5783

5884
t.Cleanup(func() {
59-
err := compose.Down(context.Background(), testcontainers.RemoveOrphans(true), testcontainers.RemoveImagesLocal)
85+
err := compose.Down(context.Background(), dockercompose.RemoveOrphans(true), dockercompose.RemoveImagesLocal)
6086
assert.NoError(t, err)
6187
})
6288

@@ -67,11 +93,42 @@ func (e *Enviroment) Run(t *testing.T, description string, cb func(t *testing.T)
6793
t,
6894
compose.
6995
WithEnv(e.env).
70-
// TODO: how can we wait for "api"?
71-
WaitForService("gateway", wait.ForHTTP("/healthcheck").WithStartupTimeout(10*time.Second)).
72-
Up(ctx, testcontainers.Wait(true)),
96+
WaitForService("gateway", wait.ForHealthCheck()).
97+
Up(ctx, dockercompose.Wait(true)),
7398
)
7499

75-
cb(t)
100+
services := make(Services)
101+
for _, service := range e.services {
102+
switch service {
103+
case "api":
104+
api, err := compose.ServiceContainer(ctx, "api")
105+
if !assert.NoError(t, err) {
106+
t.Fatal(err)
107+
}
108+
109+
services[ServiceAPI] = api
110+
111+
case "cli":
112+
cli, err := compose.ServiceContainer(ctx, "cli")
113+
if !assert.NoError(t, err) {
114+
t.Fatal(err)
115+
}
116+
117+
services[ServiceCLI] = cli
118+
119+
case "gateway":
120+
gateway, err := compose.ServiceContainer(ctx, "gateway")
121+
if !assert.NoError(t, err) {
122+
t.Fatal(err)
123+
}
124+
125+
services[ServiceGateway] = gateway
126+
127+
default:
128+
continue
129+
}
130+
}
131+
132+
cb(ctx, services, t)
76133
})
77134
}

0 commit comments

Comments
 (0)