@@ -2,39 +2,50 @@ package main
22
33import (
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