|
| 1 | +package deployer |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestSetCentralEndpoint(t *testing.T) { |
| 8 | + tests := []struct { |
| 9 | + name string |
| 10 | + input string |
| 11 | + expectedCentralEndpoint string |
| 12 | + expectedUserProvidedEndpoint string |
| 13 | + }{ |
| 14 | + { |
| 15 | + name: "plain host:port", |
| 16 | + input: "10.0.0.1:443", |
| 17 | + expectedCentralEndpoint: "10.0.0.1:443", |
| 18 | + expectedUserProvidedEndpoint: "10.0.0.1:443", |
| 19 | + }, |
| 20 | + { |
| 21 | + name: "strips https prefix", |
| 22 | + input: "https://10.0.0.1:443", |
| 23 | + expectedCentralEndpoint: "10.0.0.1:443", |
| 24 | + expectedUserProvidedEndpoint: "10.0.0.1:443", |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "hostname with port", |
| 28 | + input: "central.example.com:443", |
| 29 | + expectedCentralEndpoint: "central.example.com:443", |
| 30 | + expectedUserProvidedEndpoint: "central.example.com:443", |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "strips https from hostname", |
| 34 | + input: "https://central.example.com:443", |
| 35 | + expectedCentralEndpoint: "central.example.com:443", |
| 36 | + expectedUserProvidedEndpoint: "central.example.com:443", |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "strips http prefix", |
| 40 | + input: "http://10.0.0.1:443", |
| 41 | + expectedCentralEndpoint: "10.0.0.1:443", |
| 42 | + expectedUserProvidedEndpoint: "10.0.0.1:443", |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "strips http from hostname", |
| 46 | + input: "http://central.example.com:443", |
| 47 | + expectedCentralEndpoint: "central.example.com:443", |
| 48 | + expectedUserProvidedEndpoint: "central.example.com:443", |
| 49 | + }, |
| 50 | + } |
| 51 | + |
| 52 | + for _, tt := range tests { |
| 53 | + t.Run(tt.name, func(t *testing.T) { |
| 54 | + d := &Deployer{} |
| 55 | + d.SetCentralEndpoint(tt.input) |
| 56 | + |
| 57 | + if d.centralEndpoint != tt.expectedCentralEndpoint { |
| 58 | + t.Errorf("centralEndpoint: got %q, want %q", d.centralEndpoint, tt.expectedCentralEndpoint) |
| 59 | + } |
| 60 | + if d.userProvidedCentralEndpoint != tt.expectedUserProvidedEndpoint { |
| 61 | + t.Errorf("userProvidedCentralEndpoint: got %q, want %q", d.userProvidedCentralEndpoint, tt.expectedUserProvidedEndpoint) |
| 62 | + } |
| 63 | + }) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestGetCentralEndpointForSensor(t *testing.T) { |
| 68 | + tests := []struct { |
| 69 | + name string |
| 70 | + userProvided string |
| 71 | + centralNamespace string |
| 72 | + expected string |
| 73 | + }{ |
| 74 | + { |
| 75 | + name: "falls back to internal endpoint", |
| 76 | + userProvided: "", |
| 77 | + centralNamespace: "acs-central", |
| 78 | + expected: "central.acs-central.svc:443", |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "falls back to internal endpoint with custom namespace", |
| 82 | + userProvided: "", |
| 83 | + centralNamespace: "stackrox", |
| 84 | + expected: "central.stackrox.svc:443", |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "uses user-provided endpoint", |
| 88 | + userProvided: "10.0.0.1:443", |
| 89 | + centralNamespace: "acs-central", |
| 90 | + expected: "10.0.0.1:443", |
| 91 | + }, |
| 92 | + } |
| 93 | + |
| 94 | + for _, tt := range tests { |
| 95 | + t.Run(tt.name, func(t *testing.T) { |
| 96 | + d := &Deployer{ |
| 97 | + centralNamespace: tt.centralNamespace, |
| 98 | + userProvidedCentralEndpoint: tt.userProvided, |
| 99 | + } |
| 100 | + |
| 101 | + result := d.getCentralEndpointForSensor() |
| 102 | + if result != tt.expected { |
| 103 | + t.Errorf("got %q, want %q", result, tt.expected) |
| 104 | + } |
| 105 | + }) |
| 106 | + } |
| 107 | +} |
0 commit comments