Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Initial setup for integration tests #80

Merged
merged 19 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adds headless login workaround
  • Loading branch information
cmoog committed Jul 29, 2020
commit 9d358a60b23a5906cf2b7faa8ba6c6506cb615e7
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
ci/bin
cmd/coder/coder
ci/integration/bin
ci/integration/env.sh
39 changes: 36 additions & 3 deletions ci/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,12 @@ func TestHostRunner(t *testing.T) {
}

func TestCoderCLI(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
defer cancel()

c, err := tcli.NewContainerRunner(ctx, &tcli.ContainerConfig{
Image: "ubuntu:latest",
Name: "test-container",
Image: "codercom/enterprise-dev",
Name: "coder-cli-tests",
BindMounts: map[string]string{
binpath: "/bin/coder",
},
Expand All @@ -138,4 +139,36 @@ func TestCoderCLI(t *testing.T) {
tcli.StderrMatches("Usage: coder"),
tcli.StdoutEmpty(),
)

creds := login(ctx, t)
c.Run(ctx, fmt.Sprintf("mkdir -p ~/.config/coder && echo -ne %s > ~/.config/coder/session", creds.token)).Assert(t,
tcli.Success(),
)
c.Run(ctx, fmt.Sprintf("echo -ne %s > ~/.config/coder/url", creds.url)).Assert(t,
tcli.Success(),
)

c.Run(ctx, "coder envs").Assert(t,
tcli.Success(),
)

c.Run(ctx, "coder urls").Assert(t,
tcli.Error(),
)

c.Run(ctx, "coder sync").Assert(t,
tcli.Error(),
)

c.Run(ctx, "coder sh").Assert(t,
tcli.Error(),
)

c.Run(ctx, "coder logout").Assert(t,
tcli.Success(),
)

c.Run(ctx, "coder envs").Assert(t,
tcli.Error(),
)
}
75 changes: 75 additions & 0 deletions ci/integration/login_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package integration

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"testing"

"cdr.dev/slog/sloggers/slogtest/assert"
)

type credentials struct {
url, token string
}

func login(ctx context.Context, t *testing.T) credentials {
var (
email = requireEnv(t, "CODER_EMAIL")
password = requireEnv(t, "CODER_PASSWORD")
rawURL = requireEnv(t, "CODER_URL")
)
sessionToken := getSessionToken(ctx, t, email, password, rawURL)

return credentials{
url: rawURL,
token: sessionToken,
}
}

func requireEnv(t *testing.T, key string) string {
value := os.Getenv(key)
assert.True(t, fmt.Sprintf("%q is nonempty", key), value != "")
return value
}

type loginBuiltInAuthReq struct {
Email string `json:"email"`
Password string `json:"password"`
}

type loginBuiltInAuthResp struct {
SessionToken string `json:"session_token"`
}

func getSessionToken(ctx context.Context, t *testing.T, email, password, rawURL string) string {
reqbody := loginBuiltInAuthReq{
Email: email,
Password: password,
}
body, err := json.Marshal(reqbody)
assert.Success(t, "marshal login req body", err)

u, err := url.Parse(rawURL)
assert.Success(t, "parse raw url", err)
u.Path = "/auth/basic/login"

req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), bytes.NewReader(body))
assert.Success(t, "new request", err)

resp, err := http.DefaultClient.Do(req)
assert.Success(t, "do request", err)
assert.Equal(t, "request status 201", http.StatusCreated, resp.StatusCode)

var tokenResp loginBuiltInAuthResp
err = json.NewDecoder(resp.Body).Decode(&tokenResp)
assert.Success(t, "decode response", err)

defer resp.Body.Close()

return tokenResp.SessionToken
}
4 changes: 4 additions & 0 deletions ci/tcli/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Package tcli provides a framework for CLI integration testing.
// Execute commands on the raw host of inside docker container.
// Define custom Assertion types to extend test functionality.
package tcli
18 changes: 15 additions & 3 deletions ci/tcli/tcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ func (a Assertable) Assert(t *testing.T, option ...Assertion) {
name = named.Name()
}
t.Run(name, func(t *testing.T) {
t.Parallel()
err := o.Valid(&cmdResult)
assert.Success(t, name, err)
})
Expand Down Expand Up @@ -248,12 +247,25 @@ func Success() Assertion {
return ExitCodeIs(0)
}

// Error asserts that the command exited with a nonzero exit code
func Error() Assertion {
return simpleFuncAssert{
valid: func(r *CommandResult) error {
if r.ExitCode == 0 {
return xerrors.Errorf("expected nonzero exit code, got %v", r.ExitCode)
}
return nil
},
name: fmt.Sprintf("error"),
}
}

// ExitCodeIs asserts that the command exited with the given code
func ExitCodeIs(code int) Assertion {
return simpleFuncAssert{
valid: func(r *CommandResult) error {
if r.ExitCode != code {
return xerrors.Errorf("exit code of %v expected, got %v", code, r.ExitCode)
return xerrors.Errorf("exit code of %v expected, got %v, (%s)", code, r.ExitCode, string(r.Stderr))
}
return nil
},
Expand All @@ -279,7 +291,7 @@ func GetResult(result **CommandResult) Assertion {
*result = r
return nil
},
name: "get-stdout",
name: "get-result",
}
}

Expand Down