MCPcopy Create free account
hub / github.com/github/gh-aw / TestAnalyzeAccessLogsDirectory

Function TestAnalyzeAccessLogsDirectory

pkg/cli/access_log_test.go:88–137  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

86}
87
88func TestAnalyzeAccessLogsDirectory(t *testing.T) {
89 // Create a temporary directory structure
90 tempDir := testutil.TempDir(t, "test-*")
91
92 t.Run("multiple access logs in subdirectory", func(t *testing.T) {
93 // Test case 1: Multiple access logs in access-logs subdirectory
94 accessLogsDir := filepath.Join(tempDir, "run1", "access.log")
95 err := os.MkdirAll(accessLogsDir, 0755)
96 require.NoError(t, err, "should create access.log directory")
97
98 fetchLogContent := `1701234567.123 180 192.168.1.100 TCP_MISS/200 1234 GET http://example.com/api/data - HIER_DIRECT/93.184.216.34 text/html`
99 fetchLogPath := filepath.Join(accessLogsDir, "access-fetch.log")
100 err = os.WriteFile(fetchLogPath, []byte(fetchLogContent), 0644)
101 require.NoError(t, err, "should create test access-fetch.log")
102
103 analysis, err := analyzeAccessLogs(filepath.Join(tempDir, "run1"), false)
104 require.NoError(t, err, "should analyze access logs")
105 require.NotNil(t, analysis, "should return analysis for valid logs")
106 assert.Equal(t, 1, analysis.TotalRequests, "should count request from log file")
107 })
108
109 t.Run("no access logs - returns nil", func(t *testing.T) {
110 // Test case 2: No access logs
111 run2Dir := filepath.Join(tempDir, "run2")
112 err := os.MkdirAll(run2Dir, 0755)
113 require.NoError(t, err, "should create run2 directory")
114
115 analysis, err := analyzeAccessLogs(run2Dir, false)
116 require.NoError(t, err, "should not error when no logs present")
117 assert.Nil(t, analysis, "should return nil when no logs found")
118 })
119
120 t.Run("access logs in sandbox/firewall/logs/ (new path)", func(t *testing.T) {
121 // Test case 3: Access logs in sandbox/firewall/logs/ directory after artifact download
122 sandboxLogsDir := filepath.Join(tempDir, "run3", "sandbox", "firewall", "logs")
123 err := os.MkdirAll(sandboxLogsDir, 0755)
124 require.NoError(t, err, "should create sandbox/firewall/logs directory")
125
126 fetchLogContent := `1701234567.123 180 192.168.1.100 TCP_MISS/200 1234 GET http://example.com/api/data - HIER_DIRECT/93.184.216.34 text/html
1271701234568.456 250 192.168.1.100 TCP_HIT/200 5678 GET http://api.github.com/repos - HIER_DIRECT/140.82.112.6 application/json`
128 fetchLogPath := filepath.Join(sandboxLogsDir, "access-1.log")
129 err = os.WriteFile(fetchLogPath, []byte(fetchLogContent), 0644)
130 require.NoError(t, err, "should create test access log in sandbox path")
131
132 analysis, err := analyzeAccessLogs(filepath.Join(tempDir, "run3"), false)
133 require.NoError(t, err, "should analyze access logs from sandbox path")
134 require.NotNil(t, analysis, "should return analysis for logs in sandbox path")
135 assert.Equal(t, 2, analysis.TotalRequests, "should count requests from sandbox path")
136 })
137}
138
139func TestExtractDomainFromURL(t *testing.T) {
140 tests := []struct {

Callers

nothing calls this directly

Calls 3

TempDirFunction · 0.92
analyzeAccessLogsFunction · 0.85
RunMethod · 0.45

Tested by

no test coverage detected