Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more tests and add coverage step #17

Merged
merged 8 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 9 additions & 9 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.20
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.20.0

- name: Build
run: go build -v ./...
- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
- name: Test
run: go test -v ./...
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[![Run Tests](https://img.shields.io/github/actions/workflow/status/perimeterx/envite/go.yml?branch=main&logo=github&label=Run%20Tests)](https://github.com/PerimeterX/envite/actions/workflows/go.yml?query=branch%3Amain)
[![Dependency Review](https://img.shields.io/github/actions/workflow/status/perimeterx/envite/dependency-review.yml?logo=github&label=Dependency%20Review)](https://github.com/PerimeterX/envite/actions/workflows/dependency-review.yml?query=branch%3Amain)
[![Go Report Card](https://goreportcard.com/badge/github.com/perimeterx/envite)](https://goreportcard.com/report/github.com/perimeterx/envite)
![Manual Code Coverage](https://img.shields.io/badge/coverage-83.3%25-green)
[![Go Reference](https://pkg.go.dev/badge/github.com/perimeterx/envite.svg)](https://pkg.go.dev/github.com/perimeterx/envite)
[![Licence](https://img.shields.io/github/license/perimeterx/envite)](LICENSE)
[![Latest Release](https://img.shields.io/github/v/release/perimeterx/envite)](https://github.com/PerimeterX/envite/releases)
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
Loading