Skip to content

Commit 3c1cd53

Browse files
kylos101ona-agent
andcommitted
test(agent-smith): update filesystem tests to reflect new workspace directory approach
- Remove references to /proc filesystem in tests - Update file paths to use WorkingArea/{InstanceID} pattern - Add comprehensive tests for workspace directory discovery - Add tests for signature-driven file matching with wildcards - Test both direct file paths and wildcard patterns - Verify proper handling of hidden directories and daemon directories - All tests reflect the new signature-driven scanning approach Co-authored-by: Ona <[email protected]>
1 parent 0bdbf34 commit 3c1cd53

File tree

1 file changed

+212
-5
lines changed

1 file changed

+212
-5
lines changed

components/ee/agent-smith/pkg/detector/filesystem_test.go

Lines changed: 212 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package detector
66

77
import (
8+
"os"
9+
"path/filepath"
810
"testing"
911
"time"
1012

@@ -109,25 +111,25 @@ func TestFilesystemDetector_DisabledConfig(t *testing.T) {
109111
}
110112
}
111113

112-
func TestFilesystemFile_Fields(t *testing.T) {
114+
func TestFile_Fields(t *testing.T) {
113115
workspace := &common.Workspace{
114116
OwnerID: "user123",
115117
WorkspaceID: "ws456",
116118
InstanceID: "inst789",
117119
GitURL: "https://github.com/example/repo",
118-
PID: 12345,
119120
}
120121

121122
file := File{
122-
Path: "/proc/12345/root/workspace/config.json",
123+
Path: "/var/gitpod/workspaces/inst789/config.json",
123124
Workspace: workspace,
124125
Size: 1024,
125126
ModTime: time.Now(),
126127
}
127128

128129
// Verify all fields are properly set
129-
if file.Path != "/proc/12345/root/workspace/config.json" {
130-
t.Errorf("Path = %q, expected %q", file.Path, "/proc/12345/root/workspace/config.json")
130+
expectedPath := "/var/gitpod/workspaces/inst789/config.json"
131+
if file.Path != expectedPath {
132+
t.Errorf("Path = %q, expected %q", file.Path, expectedPath)
131133
}
132134
if file.Workspace != workspace {
133135
t.Error("Workspace not properly set")
@@ -139,3 +141,208 @@ func TestFilesystemFile_Fields(t *testing.T) {
139141
t.Error("ModTime should not be zero")
140142
}
141143
}
144+
145+
func TestWorkspaceDirectory_Fields(t *testing.T) {
146+
wsDir := WorkspaceDirectory{
147+
InstanceID: "inst789",
148+
Path: "/var/gitpod/workspaces/inst789",
149+
}
150+
151+
if wsDir.InstanceID != "inst789" {
152+
t.Errorf("InstanceID = %q, expected %q", wsDir.InstanceID, "inst789")
153+
}
154+
155+
expectedPath := "/var/gitpod/workspaces/inst789"
156+
if wsDir.Path != expectedPath {
157+
t.Errorf("Path = %q, expected %q", wsDir.Path, expectedPath)
158+
}
159+
}
160+
161+
func TestFilesystemSignature_Fields(t *testing.T) {
162+
sig := FilesystemSignature{
163+
Name: "config_files",
164+
Filenames: []string{"*.conf", "config.json", ".env"},
165+
}
166+
167+
if sig.Name != "config_files" {
168+
t.Errorf("Name = %q, expected %q", sig.Name, "config_files")
169+
}
170+
171+
expectedFilenames := []string{"*.conf", "config.json", ".env"}
172+
if len(sig.Filenames) != len(expectedFilenames) {
173+
t.Errorf("Filenames length = %d, expected %d", len(sig.Filenames), len(expectedFilenames))
174+
}
175+
176+
for i, filename := range sig.Filenames {
177+
if filename != expectedFilenames[i] {
178+
t.Errorf("Filenames[%d] = %q, expected %q", i, filename, expectedFilenames[i])
179+
}
180+
}
181+
}
182+
183+
func TestDiscoverWorkspaceDirectories(t *testing.T) {
184+
// Create a temporary working area
185+
tempDir, err := os.MkdirTemp("", "agent-smith-test-*")
186+
if err != nil {
187+
t.Fatalf("failed to create temp dir: %v", err)
188+
}
189+
defer os.RemoveAll(tempDir)
190+
191+
// Create mock workspace directories
192+
workspaceIDs := []string{"ws-abc123", "ws-def456", "ws-ghi789"}
193+
for _, wsID := range workspaceIDs {
194+
wsDir := filepath.Join(tempDir, wsID)
195+
if err := os.Mkdir(wsDir, 0755); err != nil {
196+
t.Fatalf("failed to create workspace dir %s: %v", wsDir, err)
197+
}
198+
}
199+
200+
// Create some files that should be ignored
201+
if err := os.Mkdir(filepath.Join(tempDir, ".hidden"), 0755); err != nil {
202+
t.Fatalf("failed to create hidden dir: %v", err)
203+
}
204+
if err := os.Mkdir(filepath.Join(tempDir, "ws-service-daemon"), 0755); err != nil {
205+
t.Fatalf("failed to create daemon dir: %v", err)
206+
}
207+
208+
// Create detector with temp working area
209+
config := FilesystemScanningConfig{
210+
Enabled: true,
211+
WorkingArea: tempDir,
212+
}
213+
detector, err := NewFilesystemDetector(config)
214+
if err != nil {
215+
t.Fatalf("failed to create detector: %v", err)
216+
}
217+
218+
// Test workspace directory discovery
219+
workspaceDirs, err := detector.discoverWorkspaceDirectories()
220+
if err != nil {
221+
t.Fatalf("failed to discover workspace directories: %v", err)
222+
}
223+
224+
// Should find exactly 3 workspace directories (ignoring hidden and daemon dirs)
225+
if len(workspaceDirs) != 3 {
226+
t.Errorf("found %d workspace directories, expected 3", len(workspaceDirs))
227+
}
228+
229+
// Verify the discovered directories
230+
foundIDs := make(map[string]bool)
231+
for _, wsDir := range workspaceDirs {
232+
foundIDs[wsDir.InstanceID] = true
233+
234+
// Verify path is correct
235+
expectedPath := filepath.Join(tempDir, wsDir.InstanceID)
236+
if wsDir.Path != expectedPath {
237+
t.Errorf("workspace %s path = %q, expected %q", wsDir.InstanceID, wsDir.Path, expectedPath)
238+
}
239+
}
240+
241+
// Verify all expected workspace IDs were found
242+
for _, expectedID := range workspaceIDs {
243+
if !foundIDs[expectedID] {
244+
t.Errorf("workspace ID %q not found in discovered directories", expectedID)
245+
}
246+
}
247+
}
248+
249+
func TestFindMatchingFiles(t *testing.T) {
250+
// Create a temporary workspace directory
251+
tempDir, err := os.MkdirTemp("", "agent-smith-workspace-*")
252+
if err != nil {
253+
t.Fatalf("failed to create temp dir: %v", err)
254+
}
255+
defer os.RemoveAll(tempDir)
256+
257+
// Create test files
258+
testFiles := map[string]string{
259+
"config.json": `{"key": "value"}`,
260+
"settings.conf": `setting=value`,
261+
"script.sh": `#!/bin/bash\necho "hello"`,
262+
"wallet.dat": `wallet data`,
263+
"normal.txt": `just text`,
264+
"subdir/nested.conf": `nested config`,
265+
}
266+
267+
for filePath, content := range testFiles {
268+
fullPath := filepath.Join(tempDir, filePath)
269+
270+
// Create directory if needed
271+
if dir := filepath.Dir(fullPath); dir != tempDir {
272+
if err := os.MkdirAll(dir, 0755); err != nil {
273+
t.Fatalf("failed to create dir %s: %v", dir, err)
274+
}
275+
}
276+
277+
if err := os.WriteFile(fullPath, []byte(content), 0644); err != nil {
278+
t.Fatalf("failed to create file %s: %v", fullPath, err)
279+
}
280+
}
281+
282+
// Create detector
283+
config := FilesystemScanningConfig{
284+
Enabled: true,
285+
WorkingArea: "/tmp", // Not used in this test
286+
}
287+
detector, err := NewFilesystemDetector(config)
288+
if err != nil {
289+
t.Fatalf("failed to create detector: %v", err)
290+
}
291+
292+
tests := []struct {
293+
name string
294+
pattern string
295+
expected []string
296+
}{
297+
{
298+
name: "direct file match",
299+
pattern: "config.json",
300+
expected: []string{filepath.Join(tempDir, "config.json")},
301+
},
302+
{
303+
name: "wildcard pattern",
304+
pattern: "*.conf",
305+
expected: []string{filepath.Join(tempDir, "settings.conf")},
306+
},
307+
{
308+
name: "shell script pattern",
309+
pattern: "*.sh",
310+
expected: []string{filepath.Join(tempDir, "script.sh")},
311+
},
312+
{
313+
name: "no matches",
314+
pattern: "*.nonexistent",
315+
expected: []string{},
316+
},
317+
{
318+
name: "nonexistent direct file",
319+
pattern: "missing.txt",
320+
expected: []string{},
321+
},
322+
}
323+
324+
for _, tt := range tests {
325+
t.Run(tt.name, func(t *testing.T) {
326+
matches := detector.findMatchingFiles(tempDir, tt.pattern)
327+
328+
if len(matches) != len(tt.expected) {
329+
t.Errorf("found %d matches, expected %d", len(matches), len(tt.expected))
330+
t.Errorf("matches: %v", matches)
331+
t.Errorf("expected: %v", tt.expected)
332+
return
333+
}
334+
335+
// Convert to map for easier comparison
336+
matchMap := make(map[string]bool)
337+
for _, match := range matches {
338+
matchMap[match] = true
339+
}
340+
341+
for _, expected := range tt.expected {
342+
if !matchMap[expected] {
343+
t.Errorf("expected match %q not found", expected)
344+
}
345+
}
346+
})
347+
}
348+
}

0 commit comments

Comments
 (0)