Golang unit tests.

This commit is contained in:
jamesread 2021-05-18 00:27:48 +01:00
parent a32c041fa5
commit 66c7bddb2e
21 changed files with 311 additions and 232 deletions

4
.gitignore vendored
View File

@ -1,5 +1,5 @@
web/package-lock.json
web/node_modules
webui/package-lock.json
webui/node_modules
**/*.swp
**/*.swo
gen/

View File

@ -1,6 +1,19 @@
compile:
compile: daemon-compile
daemon-compile:
go build -o OliveTin github.com/jamesread/OliveTin/cmd/OliveTin
daemon-codestyle:
go fmt ./...
go vet ./...
golint ./...
gocyclo -over 4 cmd internal
daemon-unittests:
mkdir -p reports
go test ./... -coverprofile reports/unittests.out
go tool cover -html=reports/unittests.out -o reports/unittests.html
grpc:
protoc -I.:/usr/share/gocode/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/ --go_out=plugins=grpc:gen/grpc/ OliveTin.proto
protoc -I.:/usr/share/gocode/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/ --grpc-gateway_out=gen/grpc --grpc-gateway_opt paths=source_relative OliveTin.proto
@ -20,4 +33,8 @@ devrun: compile
devcontainer: compile podman-image podman-container
webui-codestyle:
cd webui && eslint main.js js/*
cd webui && stylelint style.css
.PHONY: grpc

View File

@ -3,25 +3,25 @@ package main
import (
log "github.com/sirupsen/logrus"
restApi "github.com/jamesread/OliveTin/pkg/restApi"
grpcApi "github.com/jamesread/OliveTin/pkg/grpcApi"
webuiServer "github.com/jamesread/OliveTin/pkg/webuiServer"
grpcapi "github.com/jamesread/OliveTin/internal/grpcapi"
restapi "github.com/jamesread/OliveTin/internal/restapi"
webuiServer "github.com/jamesread/OliveTin/internal/webuiServer"
config "github.com/jamesread/OliveTin/pkg/config"
executor "github.com/jamesread/OliveTin/pkg/executor"
config "github.com/jamesread/OliveTin/internal/config"
executor "github.com/jamesread/OliveTin/internal/executor"
"github.com/spf13/viper"
"os"
)
var (
cfg *config.Config;
cfg *config.Config
)
func init() {
log.Info("OliveTin initializing");
log.Info("OliveTin initializing")
log.SetLevel(log.DebugLevel) // Default to debug, to catch cfg issues
viper.AutomaticEnv();
viper.AutomaticEnv()
viper.SetConfigName("config.yaml")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
@ -29,35 +29,35 @@ func init() {
viper.AddConfigPath("/etc/OliveTin/")
if err := viper.ReadInConfig(); err != nil {
log.Errorf("Config file error at startup. %s", err);
os.Exit(1);
};
log.Errorf("Config file error at startup. %s", err)
os.Exit(1)
}
cfg = config.DefaultConfig();
cfg = config.DefaultConfig()
if err := viper.UnmarshalExact(cfg); err != nil {
log.Errorf("Config unmarshal error %+v", err)
os.Exit(1);
os.Exit(1)
}
log.SetLevel(cfg.GetLogLevel())
viper.WatchConfig();
viper.WatchConfig()
}
func main() {
log.WithFields(log.Fields{
"listenAddressGrpcActions": cfg.ListenAddressGrpcActions,
"listenAddressRestActions": cfg.ListenAddressRestActions,
"listenAddressWebUi": cfg.ListenAddressWebUi,
}).Info("OliveTin started");
"listenAddressWebUI": cfg.ListenAddressWebUI,
}).Info("OliveTin started")
log.Debugf("%+v", cfg)
executor.Cfg = cfg;
executor.Cfg = cfg
go grpcApi.Start(cfg.ListenAddressGrpcActions, cfg)
go restApi.Start(cfg.ListenAddressRestActions, cfg.ListenAddressGrpcActions, cfg)
go grpcapi.Start(cfg.ListenAddressGrpcActions, cfg)
go restapi.Start(cfg.ListenAddressRestActions, cfg.ListenAddressGrpcActions, cfg)
webuiServer.Start(cfg.ListenAddressWebUi, cfg.ListenAddressRestActions)
webuiServer.Start(cfg.ListenAddressWebUI, cfg.ListenAddressRestActions)
}

4
go.mod
View File

@ -10,10 +10,8 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.7.0 // indirect
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect
golang.org/x/text v0.3.5 // indirect
golang.org/x/tools v0.1.0 // indirect
golang.org/x/tools v0.1.1 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/genproto v0.0.0-20210224155714-063164c882e6
google.golang.org/grpc v1.37.0

53
internal/config/config.go Normal file
View File

@ -0,0 +1,53 @@
package config
import (
log "github.com/sirupsen/logrus"
"strings"
)
type ActionButton struct {
Title string
Icon string
Shell string
CSS map[string]string `mapstructure:"omitempty"`
Timeout int
}
type Entity struct {
Title string
Icon string
ActionButtons []ActionButton `mapstructure:"actions"`
CSS map[string]string
}
type Config struct {
ListenAddressWebUI string
ListenAddressRestActions string
ListenAddressGrpcActions string
LogLevel string
ActionButtons []ActionButton `mapstructure:"actions"`
Entities []Entity `mapstructure:"omitempty"`
}
func DefaultConfig() *Config {
config := Config{}
config.ListenAddressWebUI = "0.0.0.0:1337"
config.ListenAddressRestActions = "0.0.0.0:1338"
config.ListenAddressGrpcActions = "0.0.0.0:1339"
config.LogLevel = "INFO"
return &config
}
func (cfg *Config) GetLogLevel() log.Level {
switch strings.ToUpper(cfg.LogLevel) {
case "INFO":
return log.InfoLevel
case "WARN":
return log.WarnLevel
case "DEBUG":
return log.DebugLevel
default:
return log.InfoLevel
}
}

View File

@ -0,0 +1,29 @@
package config
import (
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"testing"
)
func TestGetLog(t *testing.T) {
c := Config{}
c.LogLevel = ""
assert.Equal(t, c.GetLogLevel(), log.InfoLevel, "Info log level should be default")
c.LogLevel = "INFO"
assert.Equal(t, c.GetLogLevel(), log.InfoLevel, "set info log level")
c.LogLevel = "WARN"
assert.Equal(t, c.GetLogLevel(), log.WarnLevel, "set warn log level")
c.LogLevel = "DEBUG"
assert.Equal(t, c.GetLogLevel(), log.DebugLevel, "set debug log level")
}
func TestCreateDefaultConfig(t *testing.T) {
c := DefaultConfig()
assert.NotNil(t, c, "Create a default config")
}

23
internal/cors/cors.go Normal file
View File

@ -0,0 +1,23 @@
package cors
import (
log "github.com/sirupsen/logrus"
"net/http"
)
// AllowCors takes a HTTP handler and adds Access-Control-Allow-Origin headers to
// responses.
//
// Note: HTTP OPTIONS requests (which need to be preflighted" for CORS) are not
// handled because this app does not use HTTP PUT/PATCH/etc.
func AllowCors(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if origin := r.Header.Get("Origin"); origin != "" {
log.Debugf("Adding CORS header origin: %v", origin)
w.Header().Set("Access-Control-Allow-Origin", origin)
}
h.ServeHTTP(w, r)
})
}

View File

@ -0,0 +1,22 @@
package cors
import (
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
func TestCors(t *testing.T) {
req, _ := http.NewRequest("GET", "/health-check", nil)
req.Header.Add("Origin", "1.2.3.4")
blat := AllowCors(http.FileServer(http.Dir(".")))
rr := httptest.NewRecorder()
blat.ServeHTTP(rr, req)
assert.Equal(t, http.StatusNotFound, rr.Code, "HTTP 404 on CORS")
assert.Equal(t, "1.2.3.4", rr.Header().Get("Access-Control-Allow-Origin"), "CORS Header set")
}

View File

@ -1,23 +1,23 @@
package executor
import (
config "github.com/jamesread/OliveTin/pkg/config"
log "github.com/sirupsen/logrus"
pb "github.com/jamesread/OliveTin/gen/grpc"
config "github.com/jamesread/OliveTin/internal/config"
log "github.com/sirupsen/logrus"
"context"
"errors"
"os/exec"
"context"
"time"
)
var (
Cfg *config.Config;
Cfg *config.Config
)
func ExecAction(action string) (*pb.StartActionResponse) {
func ExecAction(action string) *pb.StartActionResponse {
res := &pb.StartActionResponse{}
res.TimedOut = false;
res.TimedOut = false
log.WithFields(log.Fields{
"actionName": action,
@ -81,5 +81,3 @@ func findAction(actionTitle string) (*config.ActionButton, error) {
return nil, errors.New("Action not found")
}

15
internal/grpcapi/emoji.go Normal file
View File

@ -0,0 +1,15 @@
package grpcapi
var emojis = map[string]string{
"poop": "💩",
"smile": "😀",
"ping": "📡",
}
func lookupHTMLIcon(keyToLookup string) string {
if emoji, found := emojis[keyToLookup]; found {
return emoji
}
return keyToLookup
}

View File

@ -0,0 +1,12 @@
package grpcapi
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestGetEmojiByShortName(t *testing.T) {
assert.Equal(t, "😀", lookupHTMLIcon("smile"), "Find an eomji by short name")
assert.Equal(t, "notfound", lookupHTMLIcon("notfound"), "Find an eomji by undefined short name")
}

View File

@ -0,0 +1,59 @@
package grpcapi
import (
ctx "context"
pb "github.com/jamesread/OliveTin/gen/grpc"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"net"
config "github.com/jamesread/OliveTin/internal/config"
executor "github.com/jamesread/OliveTin/internal/executor"
)
var (
cfg *config.Config
)
type oliveTinAPI struct {
}
func (api *oliveTinAPI) StartAction(ctx ctx.Context, req *pb.StartActionRequest) (*pb.StartActionResponse, error) {
return executor.ExecAction(req.ActionName), nil
}
func (api *oliveTinAPI) GetButtons(ctx ctx.Context, req *pb.GetButtonsRequest) (*pb.GetButtonsResponse, error) {
res := &pb.GetButtonsResponse{}
for _, action := range cfg.ActionButtons {
btn := pb.ActionButton{
Title: action.Title,
Icon: lookupHTMLIcon(action.Icon),
}
res.Actions = append(res.Actions, &btn)
}
log.Infof("getButtons: %v", res)
return res, nil
}
func Start(listenAddress string, globalConfig *config.Config) {
cfg = globalConfig
lis, err := net.Listen("tcp", listenAddress)
if err != nil {
log.Fatalf("Failed to listen - %v", err)
}
grpcServer := grpc.NewServer()
pb.RegisterOliveTinApiServer(grpcServer, newServer())
grpcServer.Serve(lis)
}
func newServer() *oliveTinAPI {
server := oliveTinAPI{}
return &server
}

View File

@ -0,0 +1,16 @@
package grpcapi
import (
"github.com/stretchr/testify/assert"
"testing"
config "github.com/jamesread/OliveTin/internal/config"
)
func TestCreateApi(t *testing.T) {
api := newServer()
assert.NotNil(t, api, "Create new server")
cfg = config.DefaultConfig()
}

View File

@ -1,25 +1,24 @@
package restApi;
package restapi
import (
"google.golang.org/grpc"
log "github.com/sirupsen/logrus"
"context"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"net/http"
gw "github.com/jamesread/OliveTin/gen/grpc"
cors "github.com/jamesread/OliveTin/pkg/cors"
cors "github.com/jamesread/OliveTin/internal/cors"
config "github.com/jamesread/OliveTin/pkg/config"
config "github.com/jamesread/OliveTin/internal/config"
)
var (
cfg *config.Config;
cfg *config.Config
)
func Start(listenAddressRest string, listenAddressGrpc string, globalConfig *config.Config) (error) {
func Start(listenAddressRest string, listenAddressGrpc string, globalConfig *config.Config) error {
cfg = globalConfig
ctx := context.Background()

View File

@ -1,14 +1,14 @@
package staticWebserverForUi
package webuiServer
import (
"net/http"
"encoding/json"
cors "github.com/jamesread/OliveTin/internal/cors"
log "github.com/sirupsen/logrus"
cors "github.com/jamesread/OliveTin/pkg/cors"
"net/http"
"os"
)
type WebUiSettings struct {
type WebUISettings struct {
Rest string
}
@ -35,14 +35,14 @@ func Start(listenAddress string, listenAddressRest string) {
http.Handle("/", cors.AllowCors(http.FileServer(http.Dir(findWebuiDir()))))
http.HandleFunc("/webUiSettings.json", func(w http.ResponseWriter, r *http.Request) {
ret := WebUiSettings {
ret := WebUISettings{
Rest: "http://" + listenAddressRest + "/",
}
jsonRet, _ := json.Marshal(ret)
w.Write([]byte(jsonRet));
w.Write([]byte(jsonRet))
})
log.Fatal(http.ListenAndServe(listenAddress, nil));
log.Fatal(http.ListenAndServe(listenAddress, nil))
}

View File

@ -0,0 +1,12 @@
package webuiServer
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestGetWebuiDir(t *testing.T) {
dir := findWebuiDir()
assert.Equal(t, "./webui", dir, "Finding the webui dir")
}

View File

@ -1,50 +0,0 @@
package config
import (
log "github.com/sirupsen/logrus"
"strings"
)
type ActionButton struct {
Title string
Icon string
Shell string
Css map[string]string `mapstructure:omitempty`
Timeout int
}
type Entity struct {
Title string
Icon string
ActionButtons []ActionButton `mapstructure:"actions"`
Css map[string]string
}
type Config struct {
ListenAddressWebUi string
ListenAddressRestActions string
ListenAddressGrpcActions string
LogLevel string
ActionButtons []ActionButton `mapstructure:"actions"`
Entities []Entity `mapstructure:omitempty`
}
func DefaultConfig() *Config {
config := Config{};
config.ListenAddressWebUi = "0.0.0.0:1337"
config.ListenAddressRestActions = "0.0.0.0:1338"
config.ListenAddressGrpcActions = "0.0.0.0:1339"
config.LogLevel = "INFO"
return &config
}
func (cfg *Config) GetLogLevel() (log.Level) {
switch (strings.ToUpper(cfg.LogLevel)) {
case "INFO": return log.InfoLevel;
case "WARN": return log.WarnLevel;
case "DEBUG": return log.DebugLevel;
default: return log.DebugLevel;
}
}

View File

@ -1,15 +0,0 @@
package config
import (
"testing"
log "github.com/sirupsen/logrus"
)
func TestGetLog(t *testing.T) {
c := Config{}
c.LogLevel = ""
if c.GetLogLevel() != log.InfoLevel {
t.Errorf("Info expected")
}
}

View File

@ -1,33 +0,0 @@
package cors
import (
"net/http"
"strings"
log "github.com/sirupsen/logrus"
)
func AllowCors(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if origin := r.Header.Get("Origin"); origin != "" {
log.Infof("Setting CORS header: %v", origin)
w.Header().Set("Access-Control-Allow-Origin", origin)
if r.Method == "OPTIONS" && r.Header.Get("Access-Control-Request-Method") != "" {
preflightHandler(w, r)
return
}
}
h.ServeHTTP(w, r)
})
}
func preflightHandler(w http.ResponseWriter, r *http.Request) {
log.Infof("preflight request for %s", r.URL.Path)
headers := []string{"Content-Type", "Accept"}
w.Header().Set("Access-Control-Allow-Headers", strings.Join(headers, ","))
methods := []string{"GET", "HEAD", "POST", "PUT", "DELETE"}
w.Header().Set("Access-Control-Allow-Methods", strings.Join(methods, ","))
}

View File

@ -1,61 +0,0 @@
package grpcApi;
import (
"google.golang.org/grpc"
pb "github.com/jamesread/OliveTin/gen/grpc"
"net"
log "github.com/sirupsen/logrus"
ctx "context"
config "github.com/jamesread/OliveTin/pkg/config"
executor "github.com/jamesread/OliveTin/pkg/executor"
)
var (
cfg *config.Config;
)
type OliveTinApi struct {
}
func (api *OliveTinApi) StartAction(ctx ctx.Context, req *pb.StartActionRequest) (*pb.StartActionResponse, error) {
return executor.ExecAction(req.ActionName), nil
}
func (api *OliveTinApi) GetButtons(ctx ctx.Context, req *pb.GetButtonsRequest) (*pb.GetButtonsResponse, error) {
res := &pb.GetButtonsResponse{};
for _, action := range cfg.ActionButtons {
btn := pb.ActionButton {
Title: action.Title,
Icon: lookupHtmlIcon(action.Icon),
}
res.Actions = append(res.Actions, &btn);
}
log.Infof("getButtons: %v", res)
return res, nil
}
func Start(listenAddress string, globalConfig *config.Config) {
cfg = globalConfig
lis, err := net.Listen("tcp", listenAddress);
if err != nil {
log.Fatalf("Failed to listen - %v", err);
}
grpcServer := grpc.NewServer();
pb.RegisterOliveTinApiServer(grpcServer, newServer());
grpcServer.Serve(lis)
}
func newServer() (*OliveTinApi) {
server := OliveTinApi {};
return &server;
}

View File

@ -1,15 +0,0 @@
package grpcApi
var emojis = map[string]string {
"poop": "💩",
"smile": "😀",
"ping": "📡",
}
func lookupHtmlIcon(keyToLookup string) (string) {
if emoji, found := emojis[keyToLookup]; found {
return emoji;
}
return keyToLookup;
}