func TestIsAllowedSquidStatus(t *testing.T) {
t.Parallel()
tests := []struct {
name string
status string
want bool
}{
{"TCP_HIT/200", "TCP_HIT/200", true},
{"TCP_MISS/200", "TCP_MISS/200", true},
{"TCP_REFRESH_MODIFIED/200", "TCP_REFRESH_MODIFIED/200", true},
{"TCP_IMS_HIT/304", "TCP_IMS_HIT/304", true},
{"generic /206 partial content", "TCP_MISS/206", true},
{"TCP_DENIED/403", "TCP_DENIED/403", false},
{"TCP_MISS/301 redirect not treated as allowed", "TCP_MISS/301", false},
{"TCP_DENIED/407", "TCP_DENIED/407", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tt.want, isAllowedSquidStatus(tt.status))
})
}
}
func TestParseSquidAccessLogMissingFile(t *testing.T) {
t.Parallel()
_, err := parseSquidAccessLog(filepath.Join(testutil.TempDir(t, "test-*"), "missing.log"), false)
require.Error(t, err, "should error when access log file does not exist")
}
func TestAccessLogDeduplicatesDomains(t *testing.T) {
t.Parallel()
tempDir := testutil.TempDir(t, "test-*")
content := `1701234567.123 180 192.168.1.100 TCP_MISS/200 1234 GET (example.com/redacted) - HIER_DIRECT/93.184.216.34 text/html
1701234568.456 180 192.168.1.100 TCP_HIT/200 1234 GET (example.com/redacted) - HIER_DIRECT/93.184.216.34 text/html`
path := filepath.Join(tempDir, "access.log")
require.NoError(t, os.WriteFile(path, []byte(content), 0644))
analysis, err := parseSquidAccessLog(path, false)
require.NoError(t, err)
assert.Equal(t, []string{"example.com"}, analysis.AllowedDomains, "duplicate domain should only appear once")
assert.Equal(t, 2, analysis.AllowedRequests, "both requests should still be counted")
}
Current State
pkg/cli/access_log_test.go(347 LOC, 7 top-levelTest*functions, severalt.Runsubtests)pkg/cli/access_log.go(230 LOC, squid access-log parsing/aggregation)!integration; all tests uset.Parallel()and a mix ofassert/requirefrom testify.Strengths
requirevsassertdiscipline: fatal preconditions (parseSquidAccessLogerrors, nil checks) userequire, value checks useassert.TestExtractDomainFromURL,TestParseSquidLogLine, andTestAddMetrics.TestDomainAnalysisJSONWireNamesnicely locks in the legacy JSON wire format (allowed_count/blocked_count) with a round-trip check.t.Parallel()usage throughout, including nestedt.Runsubtests.Prioritized Improvements
1. Missing / high-value tests
Gaps identified by comparing exported/unexported functions in
access_log.goagainst test coverageisAllowedSquidStatushas no dedicated test. It's only exercised indirectly through 2 status codes (TCP_MISS/200,TCP_DENIED/403) insideTestAccessLogParsing. The function has several distinct branches (TCP_HIT/200,TCP_REFRESH_MODIFIED/200,TCP_IMS_HIT/304, generic/200,/206,/304substring matches) that are never independently verified, including negative cases likeTCP_MISS/301orTCP_DENIED/407.addUniqueDomain) is untested. No test verifies that a domain appearing on multiple log lines is only added once toAllowedDomains/BlockedDomains.parseSquidAccessLog(nonexistent path →os.Openfailure) is not covered.processSquidAccessLogLine— comment lines (#...), blank lines, and lines wherestringutil.ExtractDomainFromURLreturns""— are never tested, so a regression that stops skipping them wouldn't be caught.analyzeMultipleAccessLogson an empty directory (noaccess-*.logfiles) has no test asserting the resulting analysis/error shape.Add tests like:
2. Testify assertion upgrades
Replace
assert.Lendomain-count checks with content-level assertionsTestAccessLogParsingandTestMultipleAccessLogAnalysisbuild anexpectedAllowed/expectedDeniedslice but only assertlen(...), discarding the actual expected values. This can hide a bug where the count is right but the domains are wrong (e.g. swapped or duplicated) — a much more common failure mode than a wrong count.Before:
After:
Apply the same change to
expectedDenied/analysis.BlockedDomainsinTestMultipleAccessLogAnalysis.3. Table-driven refactor
Consolidate
TestAccessLogParsingandTestMultipleAccessLogAnalysisscenario dataBoth tests build near-identical squid log fixtures and assert the same three fields (
TotalRequests,AllowedRequests,BlockedRequests) plus domain lists. Once the missing-file, empty-line, and skip-comment cases from item #1 are added, a single table-driven test overparseSquidAccessLoginputs (single file vs. multi-file via a helper) would reduce duplication and make it easy to add new squid status-code fixtures without copy-pasting a full test function.4. Organization / readability
TestAnalyzeAccessLogsDirectoryalready groups its three scenarios well witht.Run— consider applying the same subtest grouping toTestAccessLogParsing/TestMultipleAccessLogAnalysisso shared setup (temp dir, log content) is easier to extend with the new edge cases above."1701234567.123 180 192.168.1.100 TCP_MISS/200 ...") into small named fixture builders/constants to reduce visual noise and make the intent (allowed vs blocked request) clearer at a glance.Acceptance Checklist
TestIsAllowedSquidStatuscovering all status branches (positive and negative cases)parseSquidAccessLogmissing-file error pathanalyzeMultipleAccessLogson an empty directoryassert.Lendomain-count checks withassert.ElementsMatchwhere expected domain values are already computedmake test-unitand confirm allpkg/cliaccess-log tests passWarning
Firewall blocked 1 domain
The following domain was blocked by the firewall during workflow execution:
proxy.golang.orgTo allow these domains, add them to the
network.allowedlist in your workflow frontmatter:See Network Configuration for more information.