Skip to content

Commit a921c4d

Browse files
committed
api wip
1 parent 6de9f4f commit a921c4d

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed

tests/api_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io"
7+
"net/http"
8+
"strconv"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestInfo(t *testing.T) {
15+
type Expected struct {
16+
res map[string]interface{}
17+
err error
18+
}
19+
20+
cases := []struct {
21+
description string
22+
compose *Enviroment
23+
expected func(string, string) Expected
24+
}{
25+
{
26+
description: "case 1",
27+
compose: New().
28+
WithEnv("SHELLHUB_HTTP_PORT", strconv.Itoa(getFreePort(t))).
29+
WithEnv("SHELLHUB_SSH_PORT", strconv.Itoa(getFreePort(t))).
30+
WithEnv("SHELLHUB_VERSION", "v0.14.2").
31+
Build(),
32+
expected: func(httpPort, sshPort string) Expected {
33+
return Expected{
34+
res: map[string]interface{}{
35+
"endpoints": map[string]interface{}{
36+
"api": fmt.Sprintf("localhost:%s", httpPort),
37+
"ssh": fmt.Sprintf("localhost:%s", sshPort),
38+
},
39+
"version": "v0.14.2",
40+
},
41+
err: nil,
42+
}
43+
},
44+
},
45+
{
46+
description: "case 2",
47+
compose: New().
48+
WithEnv("SHELLHUB_HTTP_PORT", strconv.Itoa(getFreePort(t))).
49+
WithEnv("SHELLHUB_SSH_PORT", strconv.Itoa(getFreePort(t))).
50+
WithEnv("SHELLHUB_VERSION", "v0.10.2").
51+
Build(),
52+
expected: func(httpPort, sshPort string) Expected {
53+
return Expected{
54+
res: map[string]interface{}{
55+
"endpoints": map[string]interface{}{
56+
"api": fmt.Sprintf("localhost:%s", httpPort),
57+
"ssh": fmt.Sprintf("localhost:%s", sshPort),
58+
},
59+
"version": "v0.10.2",
60+
},
61+
err: nil,
62+
}
63+
},
64+
},
65+
}
66+
67+
for _, tt := range cases {
68+
// Avoid "loop variable <> captured by func literal"
69+
tc := tt
70+
71+
t.Run(tc.description, func(t *testing.T) {
72+
t.Parallel()
73+
74+
tc.compose.Run(t, func() {
75+
fmt.Printf("desciption %s\n", tc.description)
76+
77+
fmt.Printf("%s request to %s\n", tc.description, fmt.Sprintf("http://localhost:%s/info", tc.compose.GetEnv("SHELLHUB_HTTP_PORT")))
78+
79+
// res, _ := resty.
80+
// New().
81+
// R().
82+
// Get(fmt.Sprintf("http://localhost:%s/info", tc.compose.GetEnv("SHELLHUB_HTTP_PORT")))
83+
84+
res, err := http.Get(fmt.Sprintf("http://localhost:%s/info", tc.compose.GetEnv("SHELLHUB_HTTP_PORT")))
85+
assert.NoError(t, err)
86+
defer res.Body.Close()
87+
88+
foo, err := io.ReadAll(res.Body)
89+
assert.NoError(t, err)
90+
91+
body := map[string]interface{}{}
92+
assert.NoError(t, json.Unmarshal(foo, &body))
93+
94+
fmt.Printf("%s http: %+v\n", tc.description, body)
95+
fmt.Printf("%s env: %s %s %s\n", tc.description, tc.compose.GetEnv("SHELLHUB_HTTP_PORT"), tc.compose.GetEnv("SHELLHUB_SSH_PORT"), tc.compose.GetEnv("SHELLHUB_VERSION"))
96+
fmt.Printf("%s expected: %+v\n", tc.description, tc.expected(tc.compose.GetEnv("SHELLHUB_HTTP_PORT"), tc.compose.GetEnv("SHELLHUB_SSH_PORT")).res)
97+
fmt.Println("********************************************")
98+
//
99+
// assert.Equal(t, tc.expected(tc.compose.GetEnv("SHELLHUB_HTTP_PORT"), tc.compose.GetEnv("SHELLHUB_SSH_PORT")), Expected{body, err})
100+
})
101+
})
102+
}
103+
}

tests/enviroment.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/joho/godotenv"
8+
"github.com/stretchr/testify/assert"
9+
testcontainers "github.com/testcontainers/testcontainers-go/modules/compose"
10+
)
11+
12+
type Enviroment struct {
13+
env map[string]string
14+
}
15+
16+
type EnviromentBuilder struct {
17+
env map[string]string
18+
}
19+
20+
func New() *EnviromentBuilder {
21+
env, _ := godotenv.Read("../.env")
22+
return &EnviromentBuilder{
23+
env: env,
24+
}
25+
}
26+
27+
func (e *EnviromentBuilder) WithEnv(key, val string) *EnviromentBuilder {
28+
e.env[key] = val
29+
30+
return e
31+
}
32+
33+
func (e *EnviromentBuilder) Build() *Enviroment {
34+
return &Enviroment{
35+
env: e.env,
36+
}
37+
}
38+
39+
///////////////////////
40+
///////////////////////
41+
///////////////////////
42+
///////////////////////
43+
///////////////////////
44+
45+
func (e *Enviroment) GetEnv(key string) string {
46+
return e.env[key]
47+
}
48+
49+
func (e *Enviroment) Run(t *testing.T, cb func()) {
50+
compose, err := testcontainers.NewDockerCompose("../docker-compose.yml", "../docker-compose.dev.yml")
51+
assert.NoError(t, err)
52+
53+
t.Cleanup(func() {
54+
err := compose.Down(context.Background(), testcontainers.RemoveOrphans(true), testcontainers.RemoveImagesLocal)
55+
assert.NoError(t, err)
56+
})
57+
58+
ctx, cancel := context.WithCancel(context.Background())
59+
t.Cleanup(cancel)
60+
61+
err = compose.WithEnv(e.env).Up(ctx, testcontainers.Wait(true))
62+
assert.NoError(t, err)
63+
64+
cb()
65+
}

0 commit comments

Comments
 (0)