Skip to content

Commit 59bf9cb

Browse files
committed
wip3
1 parent cbc066c commit 59bf9cb

File tree

3 files changed

+219
-207
lines changed

3 files changed

+219
-207
lines changed

tests/api_test.go

Lines changed: 125 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -2,133 +2,159 @@ package main
22

33
import (
44
"context"
5-
"encoding/json"
6-
"fmt"
75
"io"
8-
"strconv"
96
"strings"
107
"testing"
118

12-
"github.com/go-resty/resty/v2"
13-
"github.com/sirupsen/logrus"
9+
log "github.com/sirupsen/logrus"
1410
"github.com/stretchr/testify/assert"
1511
)
1612

1713
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+
104+
func TestSetup(t *testing.T) {
18105
t.Parallel()
19106

107+
instance := New()
108+
20109
type Expected struct {
21-
body map[string]interface{}
22-
status int
23-
err error
110+
userMsg string
24111
}
25112

26113
cases := []struct {
27114
description string
28-
compose *ComposeEnviroment
29-
// Unlike other expected values, this is a function because the body can change based on
30-
// the variables. For that reason, 'expected' retrieves 'httpPort' and 'sshPort', which
31-
// will be used to generate the correct body.
32-
expected func(httpPort, sshPort string) Expected
115+
run func(context.Context) Expected
116+
expected Expected
33117
}{
34118
{
35-
description: "case 1",
36-
compose: New().
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-
}).
43-
Build(),
44-
expected: func(httpPort, sshPort string) Expected {
45-
return Expected{
46-
body: map[string]interface{}{
47-
"endpoints": map[string]interface{}{
48-
"api": fmt.Sprintf("localhost:%s", httpPort),
49-
"ssh": fmt.Sprintf("localhost:%s", sshPort),
50-
},
51-
"version": "v0.25.2",
52-
},
53-
status: 200,
54-
err: nil,
55-
}
56-
},
57-
},
58-
{
59-
description: "case 2",
60-
compose: New().
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-
}).
67-
Build(),
68-
expected: func(httpPort, sshPort string) Expected {
69-
return Expected{
70-
body: map[string]interface{}{
71-
"endpoints": map[string]interface{}{
72-
"api": fmt.Sprintf("localhost:%s", httpPort),
73-
"ssh": fmt.Sprintf("localhost:%s", sshPort),
74-
},
75-
"version": "v0.10.2",
76-
},
77-
status: 200,
78-
err: nil,
79-
}
80-
},
81-
},
82-
}
83-
84-
for _, tt := range cases {
85-
// Avoid "loop variable <var> captured by func literal"
86-
tc := tt
87-
88-
tc.compose.Run(t, tc.description, func(ctx context.Context, _ Services, t *testing.T) {
89-
t.Parallel()
90-
91-
res, err := resty.
92-
New().
93-
R().
94-
Get(fmt.Sprintf("http://localhost:%s/info", tc.compose.GetEnv("SHELLHUB_HTTP_PORT")))
95-
96-
body := map[string]interface{}{}
97-
assert.NoError(t, json.Unmarshal(res.Body(), &body))
119+
description: "succeeds",
120+
run: func(ctx context.Context) Expected {
121+
compose := instance.Clone()
122+
teardown := compose.Start()
123+
defer teardown() // nolint: errcheck
98124

99-
assert.Equal(
100-
t,
101-
tc.expected(tc.compose.GetEnv("SHELLHUB_HTTP_PORT"), tc.compose.GetEnv("SHELLHUB_SSH_PORT")),
102-
Expected{body, res.StatusCode(), err},
103-
)
104-
})
105-
}
106-
}
125+
_, reader, err := compose.Services[ServiceCLI].Exec(ctx, []string{"./cli", "user", "create", "john_doe", "secret", "[email protected]"})
107126

108-
func TestSetup(t *testing.T) {
109-
t.Parallel()
110-
111-
instance := New().WithServices([]ServiceKey{ServiceCLI})
127+
buf := new(strings.Builder)
128+
_, err = io.Copy(buf, reader)
129+
if !assert.NoError(t, err) {
130+
t.Fatal(err)
131+
}
112132

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()
133+
log.Info(buf.String())
123134

124-
return nil
135+
return Expected{
136+
userMsg: buf.String(),
137+
}
138+
},
139+
expected: Expected{
140+
userMsg: `
141+
Username: john_doe
142+
143+
`,
125144
},
126145
},
127146
}
128147

148+
ctx := context.TODO()
149+
129150
for _, tc := range cases {
151+
tt := tc
152+
130153
t.Run(tc.description, func(t *testing.T) {
131154
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)
132158
})
133159
}
134160
}

0 commit comments

Comments
 (0)