Skip to content

Commit f40d379

Browse files
committed
wip4
1 parent 59bf9cb commit f40d379

File tree

2 files changed

+128
-189
lines changed

2 files changed

+128
-189
lines changed

tests/api_test.go

Lines changed: 20 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -6,176 +6,63 @@ import (
66
"strings"
77
"testing"
88

9-
log "github.com/sirupsen/logrus"
109
"github.com/stretchr/testify/assert"
1110
)
1211

13-
func TestInfo(t *testing.T) {
14-
// t.Parallel()
15-
//
16-
// type Expected struct {
17-
// body map[string]interface{}
18-
// status int
19-
// err error
20-
// }
21-
//
22-
// cases := []struct {
23-
// description string
24-
// compose *ComposeEnviroment
25-
// // Unlike other expected values, this is a function because the body can change based on
26-
// // the variables. For that reason, 'expected' retrieves 'httpPort' and 'sshPort', which
27-
// // will be used to generate the correct body.
28-
// expected func(httpPort, sshPort string) Expected
29-
// }{
30-
// {
31-
// description: "case 1",
32-
// compose: New().
33-
// WithEnv(map[string]string{
34-
// "SHELLHUB_API_PORT": strconv.Itoa(getFreePort(t)),
35-
// "SHELLHUB_HTTP_PORT": strconv.Itoa(getFreePort(t)),
36-
// "SHELLHUB_SSH_PORT": strconv.Itoa(getFreePort(t)),
37-
// "SHELLHUB_VERSION": "v0.25.2",
38-
// }).
39-
// Build(),
40-
// expected: func(httpPort, sshPort string) Expected {
41-
// return Expected{
42-
// body: map[string]interface{}{
43-
// "endpoints": map[string]interface{}{
44-
// "api": fmt.Sprintf("localhost:%s", httpPort),
45-
// "ssh": fmt.Sprintf("localhost:%s", sshPort),
46-
// },
47-
// "version": "v0.25.2",
48-
// },
49-
// status: 200,
50-
// err: nil,
51-
// }
52-
// },
53-
// },
54-
// {
55-
// description: "case 2",
56-
// compose: New().
57-
// WithEnv(map[string]string{
58-
// "SHELLHUB_API_PORT": strconv.Itoa(getFreePort(t)),
59-
// "SHELLHUB_HTTP_PORT": strconv.Itoa(getFreePort(t)),
60-
// "SHELLHUB_SSH_PORT": strconv.Itoa(getFreePort(t)),
61-
// "SHELLHUB_VERSION": "v0.10.2",
62-
// }).
63-
// Build(),
64-
// expected: func(httpPort, sshPort string) Expected {
65-
// return Expected{
66-
// body: map[string]interface{}{
67-
// "endpoints": map[string]interface{}{
68-
// "api": fmt.Sprintf("localhost:%s", httpPort),
69-
// "ssh": fmt.Sprintf("localhost:%s", sshPort),
70-
// },
71-
// "version": "v0.10.2",
72-
// },
73-
// status: 200,
74-
// err: nil,
75-
// }
76-
// },
77-
// },
78-
// }
79-
//
80-
// for _, tt := range cases {
81-
// // Avoid "loop variable <var> captured by func literal"
82-
// tc := tt
83-
//
84-
// tc.compose.Run(t, tc.description, func(ctx context.Context, _ Services, t *testing.T) {
85-
// t.Parallel()
86-
//
87-
// res, err := resty.
88-
// New().
89-
// R().
90-
// Get(fmt.Sprintf("http://localhost:%s/info", tc.compose.GetEnv("SHELLHUB_HTTP_PORT")))
91-
//
92-
// body := map[string]interface{}{}
93-
// assert.NoError(t, json.Unmarshal(res.Body(), &body))
94-
//
95-
// assert.Equal(
96-
// t,
97-
// tc.expected(tc.compose.GetEnv("SHELLHUB_HTTP_PORT"), tc.compose.GetEnv("SHELLHUB_SSH_PORT")),
98-
// Expected{body, res.StatusCode(), err},
99-
// )
100-
// })
101-
// }
102-
}
103-
10412
func TestSetup(t *testing.T) {
10513
t.Parallel()
10614

107-
instance := New()
15+
ctx := context.TODO()
16+
17+
instance := NewDockerCompose()
10818

10919
type Expected struct {
11020
userMsg string
11121
}
11222

11323
cases := []struct {
11424
description string
115-
run func(context.Context) Expected
25+
test func(t *testing.T, compose *DockerCompose) Expected
11626
expected Expected
11727
}{
11828
{
11929
description: "succeeds",
120-
run: func(ctx context.Context) Expected {
121-
compose := instance.Clone()
122-
teardown := compose.Start()
123-
defer teardown() // nolint: errcheck
30+
test: func(t *testing.T, dockerCompose *DockerCompose) Expected {
31+
cli := dockerCompose.GetServiceCLI()
12432

125-
_, reader, err := compose.Services[ServiceCLI].Exec(ctx, []string{"./cli", "user", "create", "john_doe", "secret", "[email protected]"})
33+
_, reader, err := cli.Exec(ctx, []string{"./cli", "user", "create", "john_doe", "secret", "[email protected]"})
12634

12735
buf := new(strings.Builder)
12836
_, err = io.Copy(buf, reader)
12937
if !assert.NoError(t, err) {
13038
t.Fatal(err)
13139
}
13240

133-
log.Info(buf.String())
134-
13541
return Expected{
13642
userMsg: buf.String(),
13743
}
13844
},
13945
expected: Expected{
140-
userMsg: `
141-
Username: john_doe
142-
143-
`,
46+
userMsg: "\nUsername: john_doe\nEmail: [email protected]\n",
14447
},
14548
},
14649
}
14750

148-
ctx := context.TODO()
149-
150-
for _, tc := range cases {
151-
tt := tc
51+
for _, tt := range cases {
52+
tc := tt
15253

15354
t.Run(tc.description, func(t *testing.T) {
15455
t.Parallel()
155-
actual := tt.run(ctx)
156-
// assert.Contains(t, tt.expected.userMsg, actual.userMsg)
157-
assert.Contains(t, actual.userMsg, tt.expected.userMsg)
56+
dockerCompose := instance.Clone()
57+
58+
teardown, err := dockerCompose.Start()
59+
if !assert.NoError(t, err) {
60+
t.Fatal(err)
61+
}
62+
defer teardown() // nolint: errcheck
63+
64+
actual := tc.test(t, dockerCompose)
65+
assert.Contains(t, actual.userMsg, tc.expected.userMsg)
15866
})
15967
}
16068
}
161-
162-
// tc.compose.Run(t, tc.description, func(ctx context.Context, services Services, t *testing.T) {
163-
// t.Parallel()
164-
//
165-
// _, reader, err := services["cli"].Exec(ctx, []string{"./cli", "user", "create", "john_doe", "secret", "[email protected]"})
166-
//
167-
// buf := new(strings.Builder)
168-
// _, err = io.Copy(buf, reader)
169-
// if !assert.NoError(t, err) {
170-
// t.Fatal(err)
171-
// }
172-
//
173-
// text := `Ntime="2024-02-15T17:05:55Z" level=info msg="Setting log level" log_level=info
174-
// FUser created successfully
175-
// Username: john_doe
176-
177-
// `
178-
//
179-
// assert.Equal(t, buf.String(), text)
180-
// logrus.Info(buf.String())
181-
// })

0 commit comments

Comments
 (0)