Skip to content

Commit

Permalink
Add basic unit tests and ci
Browse files Browse the repository at this point in the history
  • Loading branch information
cmoog committed May 22, 2020
1 parent b61fa9c commit f81b7d0
Show file tree
Hide file tree
Showing 13 changed files with 406 additions and 9 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: ci
on: [push, pull_request]

jobs:
fmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/cache@v1
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: fmt
uses: ./ci/image
with:
args: ./ci/fmt.sh
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/cache@v1
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: lint
uses: ./ci/image
with:
args: ./ci/lint.sh
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/cache@v1
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: test
uses: ./ci/image
with:
args: go test ./...
16 changes: 16 additions & 0 deletions ci/fmt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
echo "Formatting..."

go mod tidy
gofmt -w -s .
goimports -w "-local=$$(go list -m)" .

if [ "$CI" != "" ]; then
if [[ $(git ls-files --other --modified --exclude-standard) != "" ]]; then
echo "Files need generation or are formatted incorrectly:"
git -c color.ui=always status | grep --color=no '\e\[31m'
echo "Please run the following locally:"
echo " ./ci/fmt.sh"
exit 1
fi
fi
8 changes: 8 additions & 0 deletions ci/image/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM golang:1

ENV GOFLAGS="-mod=readonly"
ENV CI=true

RUN go get golang.org/x/tools/cmd/goimports
RUN go get golang.org/x/lint/golint
RUN go get github.com/mattn/goveralls
6 changes: 6 additions & 0 deletions ci/lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

echo "Linting..."

go vet ./...
golint -set_exit_status ./...
1 change: 1 addition & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func RemoteExecer(conn *websocket.Conn) Execer {
return remoteExec{conn: conn}
}

// Command represents an external command to be run
type Command struct {
Command string
Args []string
Expand Down
43 changes: 43 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package wsep

import (
"bytes"
"io"
"io/ioutil"
"net"
"testing"

"cdr.dev/slog/sloggers/slogtest/assert"
"cdr.dev/wsep/internal/proto"
"github.com/google/go-cmp/cmp"
)

func TestRemoteStdin(t *testing.T) {
inputs := []string{
"pwd",
"echo 123\n456",
"\necho 123456\n",
}

for _, tcase := range inputs {
server, client := net.Pipe()
var stdin io.WriteCloser = remoteStdin{
conn: client,
}
go func() {
defer client.Close()
_, err := stdin.Write([]byte(tcase))
assert.Success(t, "write to stdin", err)
}()

bytecmp := cmp.Comparer(bytes.Equal)

msg, err := ioutil.ReadAll(server)
assert.Success(t, "read from server", err)

header, body := proto.SplitMessage(msg)

assert.Equal(t, "stdin body", body, []byte(tcase), bytecmp)
assert.Equal(t, "stdin header", header, []byte(`{"type":"stdin"}`), bytecmp)
}
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ module cdr.dev/wsep
go 1.14

require (
cdr.dev/slog v1.3.0
github.com/creack/pty v1.1.11
github.com/google/go-cmp v0.4.0
github.com/spf13/pflag v1.0.5
go.coder.com/cli v0.4.0
go.coder.com/flog v0.0.0-20190906214207-47dd47ea0512
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
nhooyr.io/websocket v1.8.6
)
234 changes: 232 additions & 2 deletions go.sum

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions internal/proto/clientmsg.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package proto

// Client message header type
const (
TypeStart = "start"
TypeResize = "resize_header"
TypeStdin = "stdin"
TypeCloseStdin = "close_stdin"
)

// ClientResizeHeader specifies a terminal window resize request
type ClientResizeHeader struct {
Type string `json:"type"`
Rows uint16 `json:"rows"`
Cols uint16 `json:"cols"`
}

// ClientStartHeader specifies a request to start command
type ClientStartHeader struct {
Type string `json:"type"`
Command Command `json:"command"`
Expand Down
8 changes: 2 additions & 6 deletions internal/proto/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ func SplitMessage(b []byte) (header []byte, body []byte) {
}

type headerWriter struct {
w io.WriteCloser
w io.Writer
header []byte
}

// WithHeader adds the given header to all writes
func WithHeader(w io.WriteCloser, header []byte) io.WriteCloser {
func WithHeader(w io.Writer, header []byte) io.Writer {
return headerWriter{
header: header,
w: w,
Expand All @@ -48,7 +48,3 @@ func (h headerWriter) Write(b []byte) (int, error) {
}
return len(b), nil // TODO: potential buggy
}

func (h headerWriter) Close() error {
return h.w.Close()
}
43 changes: 43 additions & 0 deletions internal/proto/protocol_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package proto

import (
"bytes"
"io/ioutil"
"testing"

"cdr.dev/slog/sloggers/slogtest/assert"
"github.com/google/go-cmp/cmp"
)

func TestWithHeader(t *testing.T) {
tests := []struct {
inputheader, inputbody, header, body []byte
}{
{
inputheader: []byte("header"),
inputbody: []byte("body"),
header: []byte("header"),
body: []byte("body"),
},
{
inputheader: []byte("header"),
inputbody: []byte("b\nody\n"),
header: []byte("header"),
body: []byte("b\nody\n"),
},
}
bytecmp := cmp.Comparer(bytes.Equal)

for _, tcase := range tests {
b := bytes.NewBuffer(nil)
withheader := WithHeader(b, []byte(tcase.inputheader))
withheader.Write([]byte(tcase.inputbody))

msg, err := ioutil.ReadAll(b)
assert.Success(t, "read buffer", err)

header, body := SplitMessage(msg)
assert.Equal(t, "header is expected value", header, tcase.header, bytecmp)
assert.Equal(t, "body is expected value", body, tcase.body, bytecmp)
}
}
3 changes: 3 additions & 0 deletions internal/proto/servermsg.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package proto

// Server message header type
const (
TypePid = "pid"
TypeStdout = "stdout"
TypeStderr = "stderr"
TypeExitCode = "exit_code"
)

// ServerPidHeader specifies the message send immediately after the request command starts
type ServerPidHeader struct {
Type string `json:"type"`
Pid int `json:"pid"`
}

// ServerExitCodeHeader specifies the final message from the server after the command exits
type ServerExitCodeHeader struct {
Type string `json:"type"`
ExitCode int `json:"exit_code"`
Expand Down
1 change: 1 addition & 0 deletions localexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (l *localProcess) Pid() int {
return l.cmd.Process.Pid
}

// Start executes the given command locally
func (l LocalExecer) Start(ctx context.Context, c Command) (Process, error) {
var (
process localProcess
Expand Down

0 comments on commit f81b7d0

Please sign in to comment.