From 846311b46dc237b5ad678837c9c374f5cc94a24d Mon Sep 17 00:00:00 2001 From: jamesread Date: Tue, 18 May 2021 22:28:04 +0100 Subject: [PATCH] More tests, pass config to executor --- cmd/OliveTin/main.go | 3 -- internal/executor/executor.go | 12 ++---- internal/grpcapi/grpcApi.go | 2 +- internal/grpcapi/grpcApi_test.go | 74 ++++++++++++++++++++++++++++++-- 4 files changed, 75 insertions(+), 16 deletions(-) diff --git a/cmd/OliveTin/main.go b/cmd/OliveTin/main.go index 7816be6..6208176 100644 --- a/cmd/OliveTin/main.go +++ b/cmd/OliveTin/main.go @@ -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) diff --git a/internal/executor/executor.go b/internal/executor/executor.go index c7ae113..9394d83 100644 --- a/internal/executor/executor.go +++ b/internal/executor/executor.go @@ -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) diff --git a/internal/grpcapi/grpcApi.go b/internal/grpcapi/grpcApi.go index 282ac91..21582a0 100644 --- a/internal/grpcapi/grpcApi.go +++ b/internal/grpcapi/grpcApi.go @@ -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) { diff --git a/internal/grpcapi/grpcApi_test.go b/internal/grpcapi/grpcApi_test.go index abcc93c..263c6f5 100644 --- a/internal/grpcapi/grpcApi_test.go +++ b/internal/grpcapi/grpcApi_test.go @@ -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() }