Skip to content

Commit

Permalink
add more tests and add coverage step
Browse files Browse the repository at this point in the history
  • Loading branch information
avivpxi committed Feb 26, 2024
1 parent 597e6cb commit aa62517
Show file tree
Hide file tree
Showing 11 changed files with 673 additions and 18 deletions.
13 changes: 12 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,16 @@ jobs:
- name: Build
run: go build -v ./...

- name: Test
- name: Test all
run: go test -v ./...

- name: Create coverage for main package
run: |
go test -v -cover ./... -coverprofile coverage.out -coverpkg ./...
go tool cover -func coverage.out -o coverage.out # Replaces coverage.out with the analysis of coverage.out
- name: Go Coverage Badge
uses: tj-actions/coverage-badge-go@v1
with:
green: 80
filename: coverage.out
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 HUMAN Security
Copyright (c) 2024 HUMAN Security.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
7 changes: 4 additions & 3 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"errors"
"fmt"
"github.com/gorilla/mux"
"github.com/perimeterx/envite/ui"
"io"
"net/http"
"strings"
Expand Down Expand Up @@ -354,7 +355,7 @@ type webHandler struct {
// initializing it with a file server that serves the bundled assets.
func newWebHandler() *webHandler {
return &webHandler{
fileServer: http.FileServer(AssetFile()),
fileServer: http.FileServer(ui.AssetFile()),
}
}

Expand All @@ -364,9 +365,9 @@ func (h webHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if len(path) > 0 && path[0] == '/' {
path = path[1:]
}
_, err := Asset(path)
_, err := ui.Asset(path)
if err != nil {
data, _ := Asset(indexFilePath)
data, _ := ui.Asset(indexFilePath)
http.ServeContent(w, r, indexFilePath, time.Time{}, bytes.NewReader(data))
} else {
h.fileServer.ServeHTTP(w, r)
Expand Down
112 changes: 112 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package envite

import (
"bytes"
"context"
"encoding/json"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
)

func TestAPI(t *testing.T) {
component := &mockComponent{}
env, err := NewEnvironment(
"test-env",
NewComponentGraph().AddLayer(map[string]Component{"component": component}),
)
assert.NoError(t, err)
assert.NotNil(t, env)

call := func(handler http.Handler, request, response any) int {
var reqBody io.Reader
if request != nil {
var data []byte
data, err = json.Marshal(request)
assert.NoError(t, err)
reqBody = bytes.NewBuffer(data)
}
req := httptest.NewRequest(http.MethodGet, "/foo", reqBody)
if request != nil {
req.Header.Set(contentType, applicationJSON)
}
res := httptest.NewRecorder()
handler.ServeHTTP(res, req)
if response != nil {
var data []byte
data, err = io.ReadAll(res.Body)
assert.NoError(t, err)
err = json.Unmarshal(data, response)
assert.NoError(t, err)
}
return res.Code
}

getStatusResponse := GetStatusResponse{}
status := call(getStatusHandler{env: env}, nil, &getStatusResponse)
assert.Equal(t, http.StatusOK, status)
assert.Equal(t, "test-env", getStatusResponse.ID)
assert.Len(t, getStatusResponse.Components, 1)
assert.Len(t, getStatusResponse.Components[0], 1)
assert.Equal(t, "component", getStatusResponse.Components[0][0].ID)
assert.Equal(t, "mock", getStatusResponse.Components[0][0].Type)

status = call(postStartHandler{env: env}, postStartRequest{ComponentID: "invalid"}, nil)
assert.Equal(t, http.StatusInternalServerError, status)
status = call(postStartHandler{env: env}, postStartRequest{ComponentID: "component"}, nil)
assert.Equal(t, http.StatusOK, status)
assert.Equal(t, ComponentStatusRunning, component.status)

status = call(postStopHandler{env: env}, postStopRequest{ComponentID: "invalid"}, nil)
assert.Equal(t, http.StatusInternalServerError, status)
status = call(postStopHandler{env: env}, postStopRequest{ComponentID: "component"}, nil)
assert.Equal(t, http.StatusOK, status)
assert.Equal(t, ComponentStatusStopped, component.status)

status = call(postApplyHandler{env: env}, postApplyRequest{EnabledComponentIDs: []string{"component"}}, nil)
assert.Equal(t, http.StatusOK, status)
assert.Equal(t, ComponentStatusRunning, component.status)
status = call(postApplyHandler{env: env}, postApplyRequest{EnabledComponentIDs: []string{}}, nil)
assert.Equal(t, http.StatusOK, status)
assert.Equal(t, ComponentStatusStopped, component.status)

err = env.StartAll(context.Background())
assert.NoError(t, err)
assert.Equal(t, ComponentStatusRunning, component.status)
assert.False(t, component.cleanupCalled)
status = call(postStopAllHandler{env: env}, postStopAllRequest{Cleanup: true}, nil)
assert.Equal(t, http.StatusOK, status)
assert.True(t, component.cleanupCalled)

req := httptest.NewRequest(http.MethodGet, "/foo", nil)
res := httptest.NewRecorder()
ctx, cancel := context.WithCancel(context.Background())
req = req.WithContext(ctx)
startTime := time.Now()
go func() {
// wait one second and close client connection
time.Sleep(time.Second)
cancel()
}()
outputHandler := getOutputHandler{env: env}
outputHandler.ServeHTTP(res, req)
assert.True(t, time.Since(startTime) >= time.Second)

wHandler := newWebHandler()
req = httptest.NewRequest(http.MethodGet, "/", nil)
res = httptest.NewRecorder()
wHandler.ServeHTTP(res, req)
assert.Equal(t, http.StatusOK, res.Code)
req = httptest.NewRequest(http.MethodGet, "/favicon.ico", nil)
res = httptest.NewRecorder()
wHandler.ServeHTTP(res, req)
assert.Equal(t, http.StatusOK, res.Code)

req = httptest.NewRequest(http.MethodGet, "/", nil)
res = httptest.NewRecorder()
optionsHandler(res, req)
assert.Equal(t, http.StatusOK, res.Code)
}
2 changes: 1 addition & 1 deletion build-ui.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ fi

npm --prefix ui run build

go-bindata -o static_files.go -pkg envite -prefix ui/build -fs ui/build/...
go-bindata -o ui/static_files.go -pkg ui -prefix ui/build -fs ui/build/...
Loading

0 comments on commit aa62517

Please sign in to comment.