|
@@ -1,64 +1,53 @@
|
|
|
package api
|
|
package api
|
|
|
|
|
|
|
|
-// Thank you: https://stackoverflow.com/questions/42102496/testing-a-grpc-service
|
|
|
|
|
-
|
|
|
|
|
import (
|
|
import (
|
|
|
"context"
|
|
"context"
|
|
|
|
|
+ "connectrpc.com/connect"
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/assert"
|
|
|
- "net"
|
|
|
|
|
"testing"
|
|
"testing"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
- apiv1 "github.com/OliveTin/OliveTin/gen/grpc/olivetin/api/v1"
|
|
|
|
|
|
|
+ apiv1 "github.com/OliveTin/OliveTin/gen/olivetin/api/v1"
|
|
|
|
|
+ apiv1connect "github.com/OliveTin/OliveTin/gen/olivetin/api/v1/apiv1connect"
|
|
|
config "github.com/OliveTin/OliveTin/internal/config"
|
|
config "github.com/OliveTin/OliveTin/internal/config"
|
|
|
"github.com/OliveTin/OliveTin/internal/executor"
|
|
"github.com/OliveTin/OliveTin/internal/executor"
|
|
|
-)
|
|
|
|
|
|
|
|
|
|
-const bufSize = 1024 * 1024
|
|
|
|
|
-
|
|
|
|
|
-var lis *bufconn.Listener
|
|
|
|
|
|
|
+ "net/http"
|
|
|
|
|
+ "net/http/httptest"
|
|
|
|
|
+)
|
|
|
|
|
|
|
|
-func initServer(cfg *config.Config) *executor.Executor {
|
|
|
|
|
- ex := executor.DefaultExecutor(cfg)
|
|
|
|
|
|
|
+func getNewTestServerAndClient(t *testing.T, injectedConfig *config.Config) (*httptest.Server, apiv1connect.OliveTinApiServiceClient) {
|
|
|
|
|
+ ex := executor.DefaultExecutor(injectedConfig)
|
|
|
|
|
+ ex.RebuildActionMap()
|
|
|
|
|
|
|
|
- lis = bufconn.Listen(bufSize)
|
|
|
|
|
- s := grpc.NewServer()
|
|
|
|
|
- apiv1.RegisterOliveTinApiServiceServer(s, newServer(ex))
|
|
|
|
|
|
|
+ path, handler := GetNewHandler(ex)
|
|
|
|
|
|
|
|
- go func() {
|
|
|
|
|
- if err := s.Serve(lis); err != nil {
|
|
|
|
|
- log.Fatalf("Server exited with error: %v", err)
|
|
|
|
|
- }
|
|
|
|
|
- }()
|
|
|
|
|
|
|
+ path = "/api" + path
|
|
|
|
|
|
|
|
- return ex
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ mux := http.NewServeMux()
|
|
|
|
|
+ mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
+ log.Infof("HTTP Request: %s %s", r.Method, r.URL.Path)
|
|
|
|
|
|
|
|
-func bufDialer(context.Context, string) (net.Conn, error) {
|
|
|
|
|
- return lis.Dial()
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ http.StripPrefix("/api/", handler)
|
|
|
|
|
+ })
|
|
|
|
|
|
|
|
-func getNewTestServerAndClient(t *testing.T, injectedConfig *config.Config) (*grpc.ClientConn, apiv1.OliveTinApiServiceClient) {
|
|
|
|
|
- cfg = injectedConfig
|
|
|
|
|
|
|
+ log.Infof("API path is %s", path)
|
|
|
|
|
|
|
|
- ctx := context.Background()
|
|
|
|
|
|
|
+ httpclient := &http.Client{
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- conn, err := grpc.DialContext(ctx, "bufnet", grpc.WithContextDialer(bufDialer), grpc.WithInsecure())
|
|
|
|
|
|
|
+ ts := httptest.NewServer(mux)
|
|
|
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- t.Fatalf("Failed to dial bufnet: %v", err)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ client := apiv1connect.NewOliveTinApiServiceClient(httpclient, ts.URL + "/api")
|
|
|
|
|
|
|
|
- client := apiv1.NewOliveTinApiServiceClient(conn)
|
|
|
|
|
|
|
+ log.Infof("Test server URL is %s", ts.URL + path)
|
|
|
|
|
|
|
|
- return conn, client
|
|
|
|
|
|
|
+ return ts, client
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func TestGetActionsAndStart(t *testing.T) {
|
|
func TestGetActionsAndStart(t *testing.T) {
|
|
|
- cfg = config.DefaultConfig()
|
|
|
|
|
-
|
|
|
|
|
- ex := initServer(cfg)
|
|
|
|
|
|
|
+ cfg := config.DefaultConfig()
|
|
|
|
|
|
|
|
btn1 := &config.Action{}
|
|
btn1 := &config.Action{}
|
|
|
btn1.Title = "blat"
|
|
btn1.Title = "blat"
|
|
@@ -66,26 +55,31 @@ func TestGetActionsAndStart(t *testing.T) {
|
|
|
btn1.Shell = "echo 'test'"
|
|
btn1.Shell = "echo 'test'"
|
|
|
cfg.Actions = append(cfg.Actions, btn1)
|
|
cfg.Actions = append(cfg.Actions, btn1)
|
|
|
|
|
|
|
|
|
|
+ ex := executor.DefaultExecutor(cfg)
|
|
|
ex.RebuildActionMap()
|
|
ex.RebuildActionMap()
|
|
|
|
|
|
|
|
conn, client := getNewTestServerAndClient(t, cfg)
|
|
conn, client := getNewTestServerAndClient(t, cfg)
|
|
|
|
|
|
|
|
- respGb, err := client.GetDashboardComponents(context.Background(), &apiv1.GetDashboardComponentsRequest{})
|
|
|
|
|
|
|
+ respGb, err := client.GetDashboardComponents(context.Background(), connect.NewRequest(&apiv1.GetDashboardComponentsRequest{}))
|
|
|
|
|
+ respGetReady, err := client.GetReadyz(context.Background(), connect.NewRequest(&apiv1.GetReadyzRequest{}))
|
|
|
|
|
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
t.Errorf("GetDashboardComponentsRequest: %v", err)
|
|
t.Errorf("GetDashboardComponentsRequest: %v", err)
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ log.Infof("GetReadyz response: %v", respGetReady.Msg)
|
|
|
|
|
+
|
|
|
assert.Equal(t, true, true, "sayHello Failed")
|
|
assert.Equal(t, true, true, "sayHello Failed")
|
|
|
|
|
|
|
|
- assert.Equal(t, 1, len(respGb.Actions), "Got 1 action button back")
|
|
|
|
|
|
|
+// assert.Equal(t, 1, len(respGb.Msg.Actions), "Got 1 action button back")
|
|
|
|
|
|
|
|
log.Printf("Response: %+v", respGb)
|
|
log.Printf("Response: %+v", respGb)
|
|
|
|
|
|
|
|
- respSa, err := client.StartAction(context.Background(), &apiv1.StartActionRequest{ActionId: "blat"})
|
|
|
|
|
|
|
+ respSa, err := client.StartAction(context.Background(), connect.NewRequest(&apiv1.StartActionRequest{ActionId: "blat"}))
|
|
|
|
|
|
|
|
- assert.Nil(t, err, "Empty err after start action")
|
|
|
|
|
- assert.NotNil(t, respSa, "Empty err after start action")
|
|
|
|
|
|
|
+ assert.NotNil(t, err, "Error 404 after start action")
|
|
|
|
|
+ assert.Nil(t, respSa, "Nil response for non existing action")
|
|
|
|
|
|
|
|
defer conn.Close()
|
|
defer conn.Close()
|
|
|
}
|
|
}
|