|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "syscall" |
4 | 7 | "testing" |
| 8 | + "time" |
5 | 9 |
|
6 | 10 | "github.com/rs/zerolog" |
| 11 | + "github.com/stackrox/stackrox-mcp/internal/config" |
| 12 | + "github.com/stackrox/stackrox-mcp/internal/server" |
| 13 | + "github.com/stackrox/stackrox-mcp/internal/toolsets" |
7 | 14 | "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
8 | 16 | ) |
9 | 17 |
|
10 | 18 | func TestSetupLogging(t *testing.T) { |
11 | 19 | setupLogging() |
12 | 20 | assert.Equal(t, zerolog.InfoLevel, zerolog.GlobalLevel()) |
13 | 21 | } |
| 22 | + |
| 23 | +func TestGetToolsets(t *testing.T) { |
| 24 | + cfg := &config.Config{ |
| 25 | + Central: config.CentralConfig{ |
| 26 | + URL: "central.example.com:8443", |
| 27 | + }, |
| 28 | + Tools: config.ToolsConfig{ |
| 29 | + Vulnerability: config.VulnerabilityConfig{ |
| 30 | + Enabled: true, |
| 31 | + }, |
| 32 | + ConfigManager: config.ConfigManagerConfig{ |
| 33 | + Enabled: true, |
| 34 | + }, |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + toolsets := getToolsets(cfg) |
| 39 | + |
| 40 | + require.NotNil(t, toolsets) |
| 41 | + assert.Len(t, toolsets, 2, "Should have 2 toolsets") |
| 42 | + assert.Equal(t, "config_manager", toolsets[0].GetName()) |
| 43 | + assert.Equal(t, "vulnerability", toolsets[1].GetName()) |
| 44 | +} |
| 45 | + |
| 46 | +func TestGracefulShutdown(t *testing.T) { |
| 47 | + // Set up minimal valid config |
| 48 | + assert.NoError(t, os.Setenv("STACKROX_MCP__TOOLS__VULNERABILITY__ENABLED", "true")) |
| 49 | + defer func() { assert.NoError(t, os.Unsetenv("STACKROX_MCP__TOOLS__VULNERABILITY__ENABLED")) }() |
| 50 | + |
| 51 | + cfg, err := config.LoadConfig("") |
| 52 | + require.NoError(t, err) |
| 53 | + require.NotNil(t, cfg) |
| 54 | + |
| 55 | + // Use a different port to avoid conflicts |
| 56 | + cfg.Server.Port = 9999 |
| 57 | + |
| 58 | + // Create registry and server |
| 59 | + registry := toolsets.NewRegistry(cfg, getToolsets(cfg)) |
| 60 | + srv := server.NewServer(cfg, registry) |
| 61 | + |
| 62 | + // Set up context with cancellation |
| 63 | + ctx, cancel := context.WithCancel(context.Background()) |
| 64 | + |
| 65 | + // Start server in goroutine |
| 66 | + errChan := make(chan error, 1) |
| 67 | + go func() { |
| 68 | + errChan <- srv.Start(ctx) |
| 69 | + }() |
| 70 | + |
| 71 | + // Give server time to start |
| 72 | + time.Sleep(100 * time.Millisecond) |
| 73 | + |
| 74 | + // Simulate shutdown signal by canceling context |
| 75 | + cancel() |
| 76 | + |
| 77 | + // Wait for server to shut down with timeout |
| 78 | + select { |
| 79 | + case err := <-errChan: |
| 80 | + // Server should shut down cleanly (either nil or context.Canceled) |
| 81 | + if err != nil && err != context.Canceled { |
| 82 | + t.Errorf("Server returned unexpected error: %v", err) |
| 83 | + } |
| 84 | + case <-time.After(5 * time.Second): |
| 85 | + t.Fatal("Server did not shut down within timeout period") |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestGracefulShutdown_WithSignal(t *testing.T) { |
| 90 | + // Set up minimal valid config |
| 91 | + assert.NoError(t, os.Setenv("STACKROX_MCP__TOOLS__VULNERABILITY__ENABLED", "true")) |
| 92 | + defer func() { assert.NoError(t, os.Unsetenv("STACKROX_MCP__TOOLS__VULNERABILITY__ENABLED")) }() |
| 93 | + |
| 94 | + cfg, err := config.LoadConfig("") |
| 95 | + require.NoError(t, err) |
| 96 | + require.NotNil(t, cfg) |
| 97 | + |
| 98 | + // Use a different port to avoid conflicts |
| 99 | + cfg.Server.Port = 10000 |
| 100 | + |
| 101 | + // Create registry and server |
| 102 | + registry := toolsets.NewRegistry(cfg, getToolsets(cfg)) |
| 103 | + srv := server.NewServer(cfg, registry) |
| 104 | + |
| 105 | + // Set up context with cancellation and signal handling (like in main) |
| 106 | + ctx, cancel := context.WithCancel(context.Background()) |
| 107 | + defer cancel() |
| 108 | + |
| 109 | + sigChan := make(chan os.Signal, 1) |
| 110 | + // Note: We use a buffered channel and manually send to avoid OS signal complications in tests |
| 111 | + |
| 112 | + go func() { |
| 113 | + <-sigChan |
| 114 | + cancel() |
| 115 | + }() |
| 116 | + |
| 117 | + // Start server in goroutine |
| 118 | + errChan := make(chan error, 1) |
| 119 | + go func() { |
| 120 | + errChan <- srv.Start(ctx) |
| 121 | + }() |
| 122 | + |
| 123 | + // Give server time to start |
| 124 | + time.Sleep(100 * time.Millisecond) |
| 125 | + |
| 126 | + // Simulate signal by sending to channel |
| 127 | + sigChan <- syscall.SIGTERM |
| 128 | + |
| 129 | + // Wait for server to shut down with timeout |
| 130 | + select { |
| 131 | + case err := <-errChan: |
| 132 | + // Server should shut down cleanly |
| 133 | + if err != nil && err != context.Canceled { |
| 134 | + t.Errorf("Server returned unexpected error: %v", err) |
| 135 | + } |
| 136 | + case <-time.After(5 * time.Second): |
| 137 | + t.Fatal("Server did not shut down within timeout period after signal") |
| 138 | + } |
| 139 | +} |
0 commit comments