|
1 | 1 | package policy |
2 | 2 |
|
3 | | -import "testing" |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestPolicySourceFields(t *testing.T) { |
| 10 | + ps := PolicySource{ |
| 11 | + Label: "mypolicy", |
| 12 | + URL: "https://example.com/policy.yml", |
| 13 | + FilePath: "/tmp/policy.yml", |
| 14 | + CacheAge: 120, |
| 15 | + Stale: true, |
| 16 | + } |
| 17 | + if ps.Label != "mypolicy" { |
| 18 | + t.Errorf("Label = %q, want mypolicy", ps.Label) |
| 19 | + } |
| 20 | + if !ps.Stale { |
| 21 | + t.Error("Stale should be true") |
| 22 | + } |
| 23 | + if ps.CacheAge != 120 { |
| 24 | + t.Errorf("CacheAge = %d, want 120", ps.CacheAge) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func TestPolicyStatusFields(t *testing.T) { |
| 29 | + s := &PolicyStatus{ |
| 30 | + Discovered: true, |
| 31 | + ProjectRoot: "/my/project", |
| 32 | + RuleCount: map[string]int{"allow": 3, "deny": 1}, |
| 33 | + } |
| 34 | + if !s.Discovered { |
| 35 | + t.Error("Discovered should be true") |
| 36 | + } |
| 37 | + if s.RuleCount["allow"] != 3 { |
| 38 | + t.Errorf("RuleCount allow = %d, want 3", s.RuleCount["allow"]) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestDiscoverPolicyFile_NotFound(t *testing.T) { |
| 43 | + dir := t.TempDir() |
| 44 | + path, err := discoverPolicyFile(dir) |
| 45 | + if err != nil { |
| 46 | + t.Fatalf("unexpected error: %v", err) |
| 47 | + } |
| 48 | + if path != "" { |
| 49 | + t.Errorf("expected empty path, got %q", path) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestDiscoverPolicyFile_Found(t *testing.T) { |
| 54 | + dir := t.TempDir() |
| 55 | + policyFile := filepath.Join(dir, "apm-policy.yml") |
| 56 | + if err := os.WriteFile(policyFile, []byte("allow:\n"), 0o644); err != nil { |
| 57 | + t.Fatal(err) |
| 58 | + } |
| 59 | + path, err := discoverPolicyFile(dir) |
| 60 | + if err != nil { |
| 61 | + t.Fatalf("unexpected error: %v", err) |
| 62 | + } |
| 63 | + if path != policyFile { |
| 64 | + t.Errorf("discoverPolicyFile = %q, want %q", path, policyFile) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestCountRules(t *testing.T) { |
| 69 | + dir := t.TempDir() |
| 70 | + f := filepath.Join(dir, "policy.yml") |
| 71 | + content := "allow:\ndeny:\n# comment\nrules:\n - foo\n" |
| 72 | + if err := os.WriteFile(f, []byte(content), 0o644); err != nil { |
| 73 | + t.Fatal(err) |
| 74 | + } |
| 75 | + counts, err := countRules(f) |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("countRules: %v", err) |
| 78 | + } |
| 79 | + if counts["allow"] != 1 { |
| 80 | + t.Errorf("allow count = %d, want 1", counts["allow"]) |
| 81 | + } |
| 82 | + if counts["deny"] != 1 { |
| 83 | + t.Errorf("deny count = %d, want 1", counts["deny"]) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestStatusOptionsFields(t *testing.T) { |
| 88 | + opts := StatusOptions{ |
| 89 | + ProjectRoot: "/proj", |
| 90 | + Format: "json", |
| 91 | + Verbose: true, |
| 92 | + NoFetch: false, |
| 93 | + } |
| 94 | + if opts.Format != "json" { |
| 95 | + t.Errorf("Format = %q, want json", opts.Format) |
| 96 | + } |
| 97 | + if !opts.Verbose { |
| 98 | + t.Error("Verbose should be true") |
| 99 | + } |
| 100 | +} |
4 | 101 |
|
5 | 102 | func TestStripSourcePrefix(t *testing.T) { |
6 | 103 | tests := []struct{ in, want string }{ |
|
0 commit comments