More tests, pass config to executor

This commit is contained in:
jamesread 2021-05-18 22:28:04 +01:00
parent 8584ea5a08
commit 846311b46d
4 changed files with 75 additions and 16 deletions

View File

@ -8,7 +8,6 @@ import (
webuiServer "github.com/jamesread/OliveTin/internal/webuiServer"
config "github.com/jamesread/OliveTin/internal/config"
executor "github.com/jamesread/OliveTin/internal/executor"
"github.com/spf13/viper"
"os"
)
@ -54,8 +53,6 @@ func main() {
log.Debugf("%+v", cfg)
executor.Cfg = cfg
go grpcapi.Start(cfg.ListenAddressGrpcActions, cfg)
go restapi.Start(cfg.ListenAddressRestActions, cfg.ListenAddressGrpcActions, cfg)

View File

@ -11,11 +11,7 @@ import (
"time"
)
var (
Cfg *config.Config
)
func ExecAction(action string) *pb.StartActionResponse {
func ExecAction(cfg *config.Config, action string) *pb.StartActionResponse {
res := &pb.StartActionResponse{}
res.TimedOut = false
@ -23,7 +19,7 @@ func ExecAction(action string) *pb.StartActionResponse {
"actionName": action,
}).Infof("StartAction")
actualAction, err := findAction(action)
actualAction, err := findAction(cfg, action)
if err != nil {
log.Errorf("Error finding action %s, %s", err, action)
@ -70,8 +66,8 @@ func sanitizeAction(action *config.ActionButton) {
}
}
func findAction(actionTitle string) (*config.ActionButton, error) {
for _, action := range Cfg.ActionButtons {
func findAction(cfg *config.Config, actionTitle string) (*config.ActionButton, error) {
for _, action := range cfg.ActionButtons {
if action.Title == actionTitle {
sanitizeAction(&action)

View File

@ -19,7 +19,7 @@ type oliveTinAPI struct {
}
func (api *oliveTinAPI) StartAction(ctx ctx.Context, req *pb.StartActionRequest) (*pb.StartActionResponse, error) {
return executor.ExecAction(req.ActionName), nil
return executor.ExecAction(cfg, req.ActionName), nil
}
func (api *oliveTinAPI) GetButtons(ctx ctx.Context, req *pb.GetButtonsRequest) (*pb.GetButtonsResponse, error) {

View File

@ -1,16 +1,82 @@
package grpcapi
// Thank you: https://stackoverflow.com/questions/42102496/testing-a-grpc-service
import (
"net"
"context"
"github.com/stretchr/testify/assert"
"testing"
"google.golang.org/grpc/test/bufconn"
"google.golang.org/grpc"
log "github.com/sirupsen/logrus"
config "github.com/jamesread/OliveTin/internal/config"
pb "github.com/jamesread/OliveTin/gen/grpc"
)
func TestCreateApi(t *testing.T) {
api := newServer()
const bufSize = 1024 * 1024
assert.NotNil(t, api, "Create new server")
var lis *bufconn.Listener
cfg = config.DefaultConfig()
func init() {
lis = bufconn.Listen(bufSize)
s := grpc.NewServer()
pb.RegisterOliveTinApiServer(s, newServer())
go func() {
if err := s.Serve(lis); err != nil {
log.Fatalf("Server exited with error: %v", err)
}
}()
}
func bufDialer(context.Context, string) (net.Conn, error) {
return lis.Dial()
}
func getNewTestServerAndClient(t *testing.T, injectedConfig *config.Config) (*grpc.ClientConn, pb.OliveTinApiClient) {
cfg = injectedConfig
ctx := context.Background()
conn, err := grpc.DialContext(ctx, "bufnet", grpc.WithContextDialer(bufDialer), grpc.WithInsecure())
if err != nil {
t.Fatalf("Failed to dial bufnet: %v", err)
}
client := pb.NewOliveTinApiClient(conn)
return conn, client
}
func TestGetButtonsAndStart(t *testing.T) {
cfg = config.DefaultConfig();
btn1 := config.ActionButton{}
btn1.Title = "blat"
btn1.Shell = "echo 'test'"
cfg.ActionButtons = append(cfg.ActionButtons, btn1);
conn, client := getNewTestServerAndClient(t, cfg)
respGb, err := client.GetButtons(context.Background(), &pb.GetButtonsRequest{})
if err != nil {
t.Errorf("GetButtons: %v", err)
}
assert.Equal(t, true, true, "sayHello Failed")
assert.Equal(t, 1, len(respGb.Actions), "Got 1 action button back")
log.Printf("Response: %+v", respGb)
respSa, err := client.StartAction(context.Background(), &pb.StartActionRequest{ActionName: "blat"})
assert.Nil(t, err, "Empty err after start action")
assert.NotNil(t, respSa, "Empty err after start action")
defer conn.Close()
}