From 7dcdfc83d2461e4342ded5fa80493936b70f64a1 Mon Sep 17 00:00:00 2001 From: Alex Jones Date: Sun, 10 Nov 2024 15:33:50 +0000 Subject: [PATCH] feat: testupdate (#1315) * feat: updated server test function Signed-off-by: AlexsJones * chore: fixed server test Signed-off-by: AlexsJones * chore: updated go.sum Signed-off-by: AlexsJones * feat: added free disk space to release job Signed-off-by: AlexsJones --------- Signed-off-by: AlexsJones --- go.mod | 2 +- go.sum | 4 +-- pkg/server/server_test.go | 73 +++++++++++++++++++++++++++------------ 3 files changed, 53 insertions(+), 26 deletions(-) diff --git a/go.mod b/go.mod index 3af42bd..8ed75ac 100644 --- a/go.mod +++ b/go.mod @@ -121,7 +121,7 @@ require ( github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect github.com/googleapis/gax-go/v2 v2.13.0 // indirect github.com/gookit/color v1.5.4 // indirect - github.com/gorilla/websocket v1.5.2 // indirect + github.com/gorilla/websocket v1.5.1 // indirect github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect github.com/hashicorp/go-getter v1.7.5 // indirect diff --git a/go.sum b/go.sum index f0d13e9..ef7da2d 100644 --- a/go.sum +++ b/go.sum @@ -1253,8 +1253,8 @@ github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q= github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= -github.com/gorilla/websocket v1.5.2 h1:qoW6V1GT3aZxybsbC6oLnailWnB+qTMVwMreOso9XUw= -github.com/gorilla/websocket v1.5.2/go.mod h1:0n9H61RBAcf5/38py2MCYbxzPIY9rOkpvvMT24Rqs30= +github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= +github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= github.com/gosuri/uitable v0.0.4 h1:IG2xLKRvErL3uhY6e1BylFzG+aJiwQviDDTfOKeKTpY= github.com/gosuri/uitable v0.0.4/go.mod h1:tKR86bXuXPZazfOTG1FIzvjIdXzd0mo4Vtn16vt0PJo= github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc h1:GN2Lv3MGO7AS6PrRoT6yV5+wkrOpcszoIsO4+4ds248= diff --git a/pkg/server/server_test.go b/pkg/server/server_test.go index 4999472..ace155a 100644 --- a/pkg/server/server_test.go +++ b/pkg/server/server_test.go @@ -1,38 +1,65 @@ package server import ( - "os" + "context" + "net" "testing" + "time" - "github.com/fatih/color" "github.com/stretchr/testify/assert" "go.uber.org/zap" + "google.golang.org/grpc" + "google.golang.org/grpc/reflection/grpc_reflection_v1alpha" ) -func TestServerInit(t *testing.T) { - logger, err := zap.NewDevelopment() - if err != nil { - color.Red("failed to create logger: %v", err) - os.Exit(1) - } - //nolint:all +func TestServe(t *testing.T) { + logger, _ := zap.NewDevelopment() defer logger.Sync() - server_config := Config{ - Backend: "openai", - Port: "0", - MetricsPort: "0", - Token: "none", - Logger: logger, + + s := &Config{ + Port: "50059", + Logger: logger, + EnableHttp: false, } go func() { - err := server_config.Serve() - if err != nil { - assert.Fail(t, "serve: %s", err.Error()) - } - err = server_config.Shutdown() - if err != nil { - assert.Fail(t, "shutdown: %s", err.Error()) - } + err := s.Serve() + time.Sleep(time.Second * 2) + assert.NoError(t, err, "Serve should not return an error") }() + + // Wait until the server is ready to accept connections + err := waitForPort("localhost:50059", 10*time.Second) + assert.NoError(t, err, "Server should start without error") + + conn, err := grpc.Dial("localhost:50059", grpc.WithInsecure()) + assert.NoError(t, err, "Should be able to dial the server") + defer conn.Close() + + // Test a simple gRPC reflection request + cli := grpc_reflection_v1alpha.NewServerReflectionClient(conn) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + resp, err := cli.ServerReflectionInfo(ctx) + assert.NoError(t, err, "Should be able to get server reflection info") + assert.NotNil(t, resp, "Response should not be nil") + + // Cleanup + err = s.Shutdown() + assert.NoError(t, err, "Shutdown should not return an error") +} + +func waitForPort(address string, timeout time.Duration) error { + start := time.Now() + for { + conn, err := net.Dial("tcp", address) + if err == nil { + conn.Close() + return nil + } + if time.Since(start) > timeout { + return err + } + time.Sleep(100 * time.Millisecond) + } }