ソースを参照

More tests, pass config to executor

jamesread 5 年 前
コミット
846311b46d

+ 0 - 3
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)
 

+ 4 - 8
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)
 

+ 1 - 1
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) {

+ 70 - 4
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
+
+var lis *bufconn.Listener
+
+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.NotNil(t, api, "Create new server")
+	assert.Nil(t, err, "Empty err after start action")
+	assert.NotNil(t, respSa, "Empty err after start action")
 
-	cfg = config.DefaultConfig()
+    defer conn.Close()
 }