|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestBuildDefaultNamespaces(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + name string |
| 11 | + watchNamespace string |
| 12 | + wantNil bool |
| 13 | + wantKeys []string |
| 14 | + }{ |
| 15 | + { |
| 16 | + name: "empty string returns nil (cluster-scoped cache)", |
| 17 | + watchNamespace: "", |
| 18 | + wantNil: true, |
| 19 | + }, |
| 20 | + { |
| 21 | + name: "single namespace", |
| 22 | + watchNamespace: "my-namespace", |
| 23 | + wantKeys: []string{"my-namespace"}, |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "multiple namespaces", |
| 27 | + watchNamespace: "ns1,ns2,ns3", |
| 28 | + wantKeys: []string{"ns1", "ns2", "ns3"}, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "namespaces with surrounding spaces are trimmed", |
| 32 | + watchNamespace: "ns1, ns2 , ns3", |
| 33 | + wantKeys: []string{"ns1", "ns2", "ns3"}, |
| 34 | + }, |
| 35 | + } |
| 36 | + |
| 37 | + for _, tt := range tests { |
| 38 | + t.Run(tt.name, func(t *testing.T) { |
| 39 | + got := buildDefaultNamespaces(tt.watchNamespace) |
| 40 | + |
| 41 | + if tt.wantNil { |
| 42 | + if got != nil { |
| 43 | + t.Errorf("expected nil, got %v", got) |
| 44 | + } |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + if got == nil { |
| 49 | + t.Fatal("expected non-nil map, got nil") |
| 50 | + } |
| 51 | + |
| 52 | + if len(got) != len(tt.wantKeys) { |
| 53 | + t.Errorf("expected %d namespaces, got %d: %v", len(tt.wantKeys), len(got), got) |
| 54 | + } |
| 55 | + |
| 56 | + for _, key := range tt.wantKeys { |
| 57 | + if _, ok := got[key]; !ok { |
| 58 | + t.Errorf("expected key %q in map, got %v", key, got) |
| 59 | + } |
| 60 | + } |
| 61 | + }) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestGetWatchNamespace(t *testing.T) { |
| 66 | + tests := []struct { |
| 67 | + name string |
| 68 | + envVal string |
| 69 | + envSet bool |
| 70 | + wantNS string |
| 71 | + wantErr bool |
| 72 | + }{ |
| 73 | + { |
| 74 | + name: "env var not set returns error", |
| 75 | + envSet: false, |
| 76 | + wantErr: true, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "empty env var returns empty string (cluster-scoped)", |
| 80 | + envSet: true, |
| 81 | + envVal: "", |
| 82 | + wantNS: "", |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "single namespace", |
| 86 | + envSet: true, |
| 87 | + envVal: "my-namespace", |
| 88 | + wantNS: "my-namespace", |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "multiple namespaces passed through as-is", |
| 92 | + envSet: true, |
| 93 | + envVal: "ns1,ns2", |
| 94 | + wantNS: "ns1,ns2", |
| 95 | + }, |
| 96 | + } |
| 97 | + |
| 98 | + for _, tt := range tests { |
| 99 | + t.Run(tt.name, func(t *testing.T) { |
| 100 | + os.Unsetenv("WATCH_NAMESPACE") |
| 101 | + if tt.envSet { |
| 102 | + os.Setenv("WATCH_NAMESPACE", tt.envVal) |
| 103 | + defer os.Unsetenv("WATCH_NAMESPACE") |
| 104 | + } |
| 105 | + |
| 106 | + got, err := getWatchNamespace() |
| 107 | + |
| 108 | + if tt.wantErr { |
| 109 | + if err == nil { |
| 110 | + t.Error("expected error, got nil") |
| 111 | + } |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + if err != nil { |
| 116 | + t.Errorf("unexpected error: %v", err) |
| 117 | + } |
| 118 | + if got != tt.wantNS { |
| 119 | + t.Errorf("expected %q, got %q", tt.wantNS, got) |
| 120 | + } |
| 121 | + }) |
| 122 | + } |
| 123 | +} |
0 commit comments