updates Go dependencies, refactors and expands unit tests for config, logging, OpenTelemetry, HTTP, and gRPC components

This commit is contained in:
2025-09-02 13:06:02 -04:00
parent 8d6297a0cb
commit c7e42a7544
9 changed files with 578 additions and 193 deletions

111
pkg/srv/grpc/grpc_test.go Normal file
View File

@@ -0,0 +1,111 @@
package grpc
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/grpc/opts"
)
// Mock gRPC service
type mockService struct{}
type MockServiceServer interface {
TestMethod(context.Context, *mockRequest) (*mockResponse, error)
}
type mockRequest struct{}
type mockResponse struct{}
func (s *mockService) TestMethod(ctx context.Context, req *mockRequest) (*mockResponse, error) {
return &mockResponse{}, nil
}
var _MockService_serviceDesc = grpc.ServiceDesc{
ServiceName: "test.MockService",
HandlerType: (*MockServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "TestMethod",
Handler: func(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(mockRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(MockServiceServer).TestMethod(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/test.MockService/TestMethod",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(MockServiceServer).TestMethod(ctx, req.(*mockRequest))
}
return interceptor(ctx, in, info, handler)
},
},
},
Streams: []grpc.StreamDesc{},
Metadata: "mock.proto",
}
func TestInitGRPCServer(t *testing.T) {
cfg := &config.AppConfig{
Name: "test-app",
GRPC: &config.GRPCConfig{
Enabled: true,
Listen: "127.0.0.1:0", // Use random available port
},
}
ctx := cfg.AddToCtx(context.Background())
ctx, _ = otel.Init(ctx)
grpcOpts := &opts.GRPCOpts{
GRPCConfig: cfg.GRPC,
AppGRPC: &opts.AppGRPC{
Services: []*opts.GRPCService{
{
Name: "mock",
Type: &_MockService_serviceDesc,
Service: &mockService{},
},
},
},
}
shutdown, done, err := InitGRPCServer(ctx, grpcOpts)
assert.NoError(t, err)
assert.NotNil(t, shutdown)
assert.NotNil(t, done)
// Shutdown the server
err = shutdown(context.Background())
assert.NoError(t, err)
}
func TestInitGRPCServer_NoServices(t *testing.T) {
cfg := &config.AppConfig{
Name: "test-app",
GRPC: &config.GRPCConfig{
Enabled: true,
Listen: "127.0.0.1:0",
},
}
ctx := cfg.AddToCtx(context.Background())
ctx, _ = otel.Init(ctx)
grpcOpts := &opts.GRPCOpts{
GRPCConfig: cfg.GRPC,
AppGRPC: &opts.AppGRPC{},
}
_, _, err := InitGRPCServer(ctx, grpcOpts)
assert.Error(t, err)
}

88
pkg/srv/http/http_test.go Normal file
View File

@@ -0,0 +1,88 @@
package http
import (
"context"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/http/opts"
)
func TestInitHTTPServer(t *testing.T) {
cfg := &config.AppConfig{
Name: "test-app",
HTTP: &config.HTTPConfig{
Enabled: true,
Listen: "127.0.0.1:0", // Use random available port
},
OTEL: &config.OTELConfig{
Enabled: true,
},
}
ctx := cfg.AddToCtx(context.Background())
ctx, _ = otel.Init(ctx)
httpOpts := &opts.AppHTTP{
Ctx: ctx,
Funcs: []opts.HTTPFunc{
{
Path: "/test",
HandlerFunc: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
},
},
},
}
shutdown, done, err := InitHTTPServer(httpOpts)
assert.NoError(t, err)
assert.NotNil(t, shutdown)
assert.NotNil(t, done)
// Shutdown the server
err = shutdown(context.Background())
assert.NoError(t, err)
}
func TestHealthCheck(t *testing.T) {
req := httptest.NewRequest("GET", "/health", nil)
w := httptest.NewRecorder()
handleHealthCheckFunc(context.Background())(w, req)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "ok", string(body))
}
func TestLoggingMiddleware(t *testing.T) {
cfg := &config.AppConfig{
Name: "test-app",
HTTP: &config.HTTPConfig{
LogRequests: true,
},
}
ctx := cfg.AddToCtx(context.Background())
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
middleware := loggingMiddleware(ctx, handler)
req := httptest.NewRequest("GET", "/test", nil)
w := httptest.NewRecorder()
middleware.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
}