mirror of
https://github.com/ultravioletrs/cocos.git
synced 2026-06-23 04:10:25 +00:00
906d7877b2
* Implement gRPC server with TLS and mTLS support - Added gRPC server implementation in pkg/server/grpc. - Introduced server configuration options for TLS and mTLS. - Implemented health check service for gRPC. - Created tests for server initialization, startup, and shutdown scenarios. - Added mock server for testing purposes. - Implemented graceful shutdown handling for the server. - Included documentation for the server package. Signed-off-by: SammyOina <sammyoina@gmail.com> * Add TLS and ATLS support to gRPC and HTTP clients; refactor security handling Signed-off-by: SammyOina <sammyoina@gmail.com> * Refactor server configuration structure to use Config instead of BaseConfig Signed-off-by: SammyOina <sammyoina@gmail.com> * Fix comments for consistency and clarity in TLS-related code Signed-off-by: SammyOina <sammyoina@gmail.com> * Add comprehensive tests for TLS and ATLS configurations in clients package Signed-off-by: SammyOina <sammyoina@gmail.com> * Refactor file permission constants in client tests to use octal notation Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add tests for HTTP server's TLS configuration and lifecycle management Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add comprehensive tests for TLS certificate handling and configuration Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add comprehensive tests for HTTP client configuration and transport Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Refactor AttestationReportSize constant declaration for clarity Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Refactor client configuration structure and update gRPC client implementations - Consolidated client configuration types into a unified structure with BaseConfig. - Introduced AttestedClientConfig and StandardClientConfig for specific use cases. - Updated gRPC client creation functions to utilize new configuration types. - Refactored tests to align with the new configuration structure. - Removed redundant ClientConfiguration interface and related methods. - Simplified TLS configuration loading logic for both standard and attested clients. Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Refactor client configuration structure and TLS handling - Introduced StandardClientConfig to replace BaseConfig, simplifying client configuration. - Updated AttestedClientConfig to embed StandardClientConfig instead of BaseConfig. - Modified ClientConfiguration interface to use Config() method instead of GetBaseConfig(). - Refactored various client tests to accommodate changes in configuration structure. - Added new TLS handling functions to support basic and attested TLS configurations. - Implemented comprehensive tests for TLS loading and configuration validation. - Removed deprecated methods and unnecessary code related to BaseConfig. Signed-off-by: Sammy Oina <sammyoina@gmail.com> --------- Signed-off-by: SammyOina <sammyoina@gmail.com> Signed-off-by: Sammy Oina <sammyoina@gmail.com>
139 lines
3.2 KiB
Go
139 lines
3.2 KiB
Go
// Copyright (c) Ultraviolet
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"os"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/ultravioletrs/cocos/pkg/server/mocks"
|
|
)
|
|
|
|
func TestStopAllServer(t *testing.T) {
|
|
server1 := new(mocks.Server)
|
|
server2 := new(mocks.Server)
|
|
server1.On("Stop").Return(nil)
|
|
server2.On("Stop").Return(errors.New("failed to stop"))
|
|
tests := []struct {
|
|
name string
|
|
servers []Server
|
|
expectedError bool
|
|
}{
|
|
{
|
|
name: "All servers stop successfully",
|
|
servers: []Server{
|
|
server1,
|
|
server1,
|
|
},
|
|
expectedError: false,
|
|
},
|
|
{
|
|
name: "One server fails to stop",
|
|
servers: []Server{
|
|
server1,
|
|
server2,
|
|
},
|
|
expectedError: true,
|
|
},
|
|
{
|
|
name: "No servers",
|
|
servers: []Server{},
|
|
expectedError: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := stopAllServer(tt.servers...)
|
|
if (err != nil) != tt.expectedError {
|
|
t.Errorf("stopAllServer() error = %v, expectedError %v", err, tt.expectedError)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestStopHandler(t *testing.T) {
|
|
mockServer := new(mocks.Server)
|
|
mockServer.On("Stop").Return(nil)
|
|
tests := []struct {
|
|
name string
|
|
setupFunc func() (context.Context, context.CancelFunc, *slog.Logger, string, []Server)
|
|
triggerSignal bool
|
|
expectedError bool
|
|
expectCanceled bool
|
|
}{
|
|
{
|
|
name: "Graceful shutdown on signal",
|
|
setupFunc: func() (context.Context, context.CancelFunc, *slog.Logger, string, []Server) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
|
|
return ctx, cancel, logger, "test", []Server{mockServer}
|
|
},
|
|
triggerSignal: true,
|
|
expectedError: false,
|
|
expectCanceled: true,
|
|
},
|
|
{
|
|
name: "Context canceled",
|
|
setupFunc: func() (context.Context, context.CancelFunc, *slog.Logger, string, []Server) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
|
|
go func() {
|
|
time.Sleep(100 * time.Millisecond)
|
|
cancel()
|
|
}()
|
|
return ctx, cancel, logger, "test", []Server{mockServer}
|
|
},
|
|
triggerSignal: false,
|
|
expectedError: false,
|
|
expectCanceled: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
ctx, cancel, logger, svcName, servers := tt.setupFunc()
|
|
defer cancel()
|
|
|
|
errChan := make(chan error)
|
|
go func() {
|
|
errChan <- StopHandler(ctx, cancel, logger, svcName, servers...)
|
|
}()
|
|
|
|
if tt.triggerSignal {
|
|
// Simulate SIGINT
|
|
go func() {
|
|
time.Sleep(100 * time.Millisecond)
|
|
err := syscall.Kill(syscall.Getpid(), syscall.SIGINT)
|
|
if err != nil {
|
|
t.Errorf("failed to send signal: %v", err)
|
|
}
|
|
}()
|
|
}
|
|
|
|
select {
|
|
case err := <-errChan:
|
|
if (err != nil) != tt.expectedError {
|
|
t.Errorf("StopHandler() error = %v, expectedError %v", err, tt.expectedError)
|
|
}
|
|
case <-time.After(2 * time.Second):
|
|
t.Error("StopHandler() timed out")
|
|
}
|
|
|
|
if tt.expectCanceled {
|
|
select {
|
|
case <-ctx.Done():
|
|
// Context was canceled as expected
|
|
default:
|
|
t.Error("Context was not canceled")
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|