|
| 1 | +// SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package process |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestIsToolHiveProxyForWorkload_InvalidPID(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + isToolHive, err := IsToolHiveProxyForWorkload(0, "") |
| 16 | + require.NoError(t, err) |
| 17 | + assert.False(t, isToolHive) |
| 18 | + |
| 19 | + isToolHive, err = IsToolHiveProxyForWorkload(-1, "") |
| 20 | + require.NoError(t, err) |
| 21 | + assert.False(t, isToolHive) |
| 22 | +} |
| 23 | + |
| 24 | +func TestIsToolHiveProxyForWorkload_NonToolHiveProcess(t *testing.T) { |
| 25 | + t.Parallel() |
| 26 | + // Use a very high PID that almost certainly doesn't exist. |
| 27 | + // IsToolHiveProxyForWorkload should return false (fail-safe: don't kill unknown processes). |
| 28 | + isToolHive, err := IsToolHiveProxyForWorkload(999999999, "") |
| 29 | + if err != nil { |
| 30 | + // Process may not exist; either way we must not report it as ToolHive |
| 31 | + assert.False(t, isToolHive) |
| 32 | + return |
| 33 | + } |
| 34 | + require.NoError(t, err) |
| 35 | + assert.False(t, isToolHive) |
| 36 | +} |
| 37 | + |
| 38 | +func TestContainsThvBinary(t *testing.T) { |
| 39 | + t.Parallel() |
| 40 | + tests := []struct { |
| 41 | + name string |
| 42 | + input string |
| 43 | + expected bool |
| 44 | + }{ |
| 45 | + {"thv exe", "thv.exe", true}, |
| 46 | + {"path with thv", "/usr/local/bin/thv", true}, |
| 47 | + {"path with thv windows", `C:\tools\thv.exe`, true}, |
| 48 | + {"cmd thv start", "thv start myworkload --foreground", true}, |
| 49 | + {"cmd with thv in middle", "/path/to/thv start x", true}, |
| 50 | + {"toolhive not thv", "toolhive", false}, |
| 51 | + {"toolhive.test", "/tmp/toolhive.test", false}, |
| 52 | + {"unrelated", "node server.js", false}, |
| 53 | + } |
| 54 | + for _, tt := range tests { |
| 55 | + tt := tt |
| 56 | + t.Run(tt.name, func(t *testing.T) { |
| 57 | + t.Parallel() |
| 58 | + got := containsThvBinary(tt.input) |
| 59 | + assert.Equal(t, tt.expected, got, "containsThvBinary(%q)", tt.input) |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestCmdlineContainsWorkload(t *testing.T) { |
| 65 | + t.Parallel() |
| 66 | + tests := []struct { |
| 67 | + name string |
| 68 | + cmdline string |
| 69 | + workload string |
| 70 | + expected bool |
| 71 | + }{ |
| 72 | + {"matches github", "/usr/bin/thv start github --foreground", "github", true}, |
| 73 | + {"matches with debug", "thv start slack --foreground --debug", "slack", true}, |
| 74 | + {"matches at end", "thv start myworkload", "myworkload", true}, |
| 75 | + {"rejects different workload", "/usr/bin/thv start slack --foreground", "github", false}, |
| 76 | + {"rejects partial match", "thv start github --foreground", "git", false}, |
| 77 | + {"rejects suffix match", "thv start github --foreground", "hub", false}, |
| 78 | + {"empty workload", "thv start github --foreground", "", false}, |
| 79 | + } |
| 80 | + for _, tt := range tests { |
| 81 | + tt := tt |
| 82 | + t.Run(tt.name, func(t *testing.T) { |
| 83 | + t.Parallel() |
| 84 | + got := cmdlineContainsWorkload(tt.cmdline, tt.workload) |
| 85 | + assert.Equal(t, tt.expected, got, "cmdlineContainsWorkload(%q, %q)", tt.cmdline, tt.workload) |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
0 commit comments