Skip to content

Commit

Permalink
wip4
Browse files Browse the repository at this point in the history
  • Loading branch information
heiytor committed Feb 16, 2024
1 parent 59bf9cb commit f40d379
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 189 deletions.
153 changes: 20 additions & 133 deletions tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,176 +6,63 @@ import (
"strings"
"testing"

log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

func TestInfo(t *testing.T) {
// t.Parallel()
//
// type Expected struct {
// body map[string]interface{}
// status int
// err error
// }
//
// cases := []struct {
// description string
// compose *ComposeEnviroment
// // Unlike other expected values, this is a function because the body can change based on
// // the variables. For that reason, 'expected' retrieves 'httpPort' and 'sshPort', which
// // will be used to generate the correct body.
// expected func(httpPort, sshPort string) Expected
// }{
// {
// description: "case 1",
// compose: New().
// WithEnv(map[string]string{
// "SHELLHUB_API_PORT": strconv.Itoa(getFreePort(t)),
// "SHELLHUB_HTTP_PORT": strconv.Itoa(getFreePort(t)),
// "SHELLHUB_SSH_PORT": strconv.Itoa(getFreePort(t)),
// "SHELLHUB_VERSION": "v0.25.2",
// }).
// Build(),
// expected: func(httpPort, sshPort string) Expected {
// return Expected{
// body: map[string]interface{}{
// "endpoints": map[string]interface{}{
// "api": fmt.Sprintf("localhost:%s", httpPort),
// "ssh": fmt.Sprintf("localhost:%s", sshPort),
// },
// "version": "v0.25.2",
// },
// status: 200,
// err: nil,
// }
// },
// },
// {
// description: "case 2",
// compose: New().
// WithEnv(map[string]string{
// "SHELLHUB_API_PORT": strconv.Itoa(getFreePort(t)),
// "SHELLHUB_HTTP_PORT": strconv.Itoa(getFreePort(t)),
// "SHELLHUB_SSH_PORT": strconv.Itoa(getFreePort(t)),
// "SHELLHUB_VERSION": "v0.10.2",
// }).
// Build(),
// expected: func(httpPort, sshPort string) Expected {
// return Expected{
// body: map[string]interface{}{
// "endpoints": map[string]interface{}{
// "api": fmt.Sprintf("localhost:%s", httpPort),
// "ssh": fmt.Sprintf("localhost:%s", sshPort),
// },
// "version": "v0.10.2",
// },
// status: 200,
// err: nil,
// }
// },
// },
// }
//
// for _, tt := range cases {
// // Avoid "loop variable <var> captured by func literal"
// tc := tt
//
// tc.compose.Run(t, tc.description, func(ctx context.Context, _ Services, t *testing.T) {
// t.Parallel()
//
// res, err := resty.
// New().
// R().
// Get(fmt.Sprintf("http://localhost:%s/info", tc.compose.GetEnv("SHELLHUB_HTTP_PORT")))
//
// body := map[string]interface{}{}
// assert.NoError(t, json.Unmarshal(res.Body(), &body))
//
// assert.Equal(
// t,
// tc.expected(tc.compose.GetEnv("SHELLHUB_HTTP_PORT"), tc.compose.GetEnv("SHELLHUB_SSH_PORT")),
// Expected{body, res.StatusCode(), err},
// )
// })
// }
}

func TestSetup(t *testing.T) {
t.Parallel()

instance := New()
ctx := context.TODO()

instance := NewDockerCompose()

type Expected struct {
userMsg string
}

cases := []struct {
description string
run func(context.Context) Expected
test func(t *testing.T, compose *DockerCompose) Expected
expected Expected
}{
{
description: "succeeds",
run: func(ctx context.Context) Expected {
compose := instance.Clone()
teardown := compose.Start()
defer teardown() // nolint: errcheck
test: func(t *testing.T, dockerCompose *DockerCompose) Expected {
cli := dockerCompose.GetServiceCLI()

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

buf := new(strings.Builder)
_, err = io.Copy(buf, reader)
if !assert.NoError(t, err) {
t.Fatal(err)
}

log.Info(buf.String())

return Expected{
userMsg: buf.String(),
}
},
expected: Expected{
userMsg: `
Username: john_doe
Email: [email protected]
`,
userMsg: "\nUsername: john_doe\nEmail: [email protected]\n",
},
},
}

ctx := context.TODO()

for _, tc := range cases {
tt := tc
for _, tt := range cases {
tc := tt

t.Run(tc.description, func(t *testing.T) {
t.Parallel()
actual := tt.run(ctx)
// assert.Contains(t, tt.expected.userMsg, actual.userMsg)
assert.Contains(t, actual.userMsg, tt.expected.userMsg)
dockerCompose := instance.Clone()

teardown, err := dockerCompose.Start()
if !assert.NoError(t, err) {
t.Fatal(err)
}
defer teardown() // nolint: errcheck

actual := tc.test(t, dockerCompose)
assert.Contains(t, actual.userMsg, tc.expected.userMsg)
})
}
}

// tc.compose.Run(t, tc.description, func(ctx context.Context, services Services, t *testing.T) {
// t.Parallel()
//
// _, reader, err := services["cli"].Exec(ctx, []string{"./cli", "user", "create", "john_doe", "secret", "[email protected]"})
//
// buf := new(strings.Builder)
// _, err = io.Copy(buf, reader)
// if !assert.NoError(t, err) {
// t.Fatal(err)
// }
//
// text := `Ntime="2024-02-15T17:05:55Z" level=info msg="Setting log level" log_level=info
// FUser created successfully
// Username: john_doe
// Email: [email protected]
// `
//
// assert.Equal(t, buf.String(), text)
// logrus.Info(buf.String())
// })
Loading

0 comments on commit f40d379

Please sign in to comment.