|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "net" |
| 7 | + "net/http" |
| 8 | + "strconv" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/stackrox/stackrox-mcp/internal/config" |
| 13 | + "github.com/stackrox/stackrox-mcp/internal/server" |
| 14 | + "github.com/stackrox/stackrox-mcp/internal/testutil" |
| 15 | + "github.com/stackrox/stackrox-mcp/internal/toolsets" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | +) |
| 19 | + |
| 20 | +func getDefaultConfig() *config.Config { |
| 21 | + return &config.Config{ |
| 22 | + Global: config.GlobalConfig{ |
| 23 | + ReadOnlyTools: false, |
| 24 | + }, |
| 25 | + Central: config.CentralConfig{ |
| 26 | + URL: "central.example.com:8443", |
| 27 | + }, |
| 28 | + Server: config.ServerConfig{ |
| 29 | + Address: "localhost", |
| 30 | + Port: 8080, |
| 31 | + }, |
| 32 | + Tools: config.ToolsConfig{ |
| 33 | + Vulnerability: config.ToolsetVulnerabilityConfig{ |
| 34 | + Enabled: true, |
| 35 | + }, |
| 36 | + ConfigManager: config.ToolConfigManagerConfig{ |
| 37 | + Enabled: false, |
| 38 | + }, |
| 39 | + }, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestGetToolsets(t *testing.T) { |
| 44 | + cfg := getDefaultConfig() |
| 45 | + cfg.Tools.ConfigManager.Enabled = true |
| 46 | + |
| 47 | + allToolsets := getToolsets(cfg) |
| 48 | + |
| 49 | + require.NotNil(t, allToolsets) |
| 50 | + assert.Len(t, allToolsets, 2, "Should have 2 allToolsets") |
| 51 | + assert.Equal(t, "config_manager", allToolsets[0].GetName()) |
| 52 | + assert.Equal(t, "vulnerability", allToolsets[1].GetName()) |
| 53 | +} |
| 54 | + |
| 55 | +func TestGracefulShutdown(t *testing.T) { |
| 56 | + // Set up minimal valid config. |
| 57 | + t.Setenv("STACKROX_MCP__TOOLS__VULNERABILITY__ENABLED", "true") |
| 58 | + |
| 59 | + cfg, err := config.LoadConfig("") |
| 60 | + require.NoError(t, err) |
| 61 | + require.NotNil(t, cfg) |
| 62 | + cfg.Server.Port = testutil.GetPortForTest(t) |
| 63 | + |
| 64 | + registry := toolsets.NewRegistry(cfg, getToolsets(cfg)) |
| 65 | + srv := server.NewServer(cfg, registry) |
| 66 | + ctx, cancel := context.WithCancel(context.Background()) |
| 67 | + |
| 68 | + errChan := make(chan error, 1) |
| 69 | + |
| 70 | + go func() { |
| 71 | + errChan <- srv.Start(ctx) |
| 72 | + }() |
| 73 | + |
| 74 | + serverURL := "http://" + net.JoinHostPort(cfg.Server.Address, strconv.Itoa(cfg.Server.Port)) |
| 75 | + err = testutil.WaitForServerReady(serverURL, 3*time.Second) |
| 76 | + require.NoError(t, err, "Server should start within timeout") |
| 77 | + |
| 78 | + // Establish actual HTTP connection to verify server is responding. |
| 79 | + //nolint:gosec,noctx |
| 80 | + resp, err := http.Get(serverURL) |
| 81 | + if err == nil { |
| 82 | + _ = resp.Body.Close() |
| 83 | + } |
| 84 | + |
| 85 | + require.NoError(t, err, "Should be able to establish HTTP connection to server") |
| 86 | + |
| 87 | + // Simulate shutdown signal by canceling context. |
| 88 | + cancel() |
| 89 | + |
| 90 | + // Wait for server to shut down. |
| 91 | + select { |
| 92 | + case err := <-errChan: |
| 93 | + // Server should shut down cleanly (either nil or context.Canceled). |
| 94 | + if err != nil && errors.Is(err, context.Canceled) { |
| 95 | + t.Errorf("Server returned unexpected error: %v", err) |
| 96 | + } |
| 97 | + case <-time.After(5 * time.Second): |
| 98 | + t.Fatal("Server did not shut down within timeout period") |
| 99 | + } |
| 100 | +} |
0 commit comments