|
| 1 | +package settings_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/onsi/gomega" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + |
| 10 | + "github.com/kgateway-dev/kgateway/v2/internal/kgateway/extensions2/settings" |
| 11 | +) |
| 12 | + |
| 13 | +func TestSettings(t *testing.T) { |
| 14 | + testCases := []struct { |
| 15 | + // name of the test case |
| 16 | + name string |
| 17 | + |
| 18 | + // env vars that are set at the beginning of test (and removed after test) |
| 19 | + envVars map[string]string |
| 20 | + |
| 21 | + // if set, then these are the expected populated settings |
| 22 | + expectedSettings *settings.Settings |
| 23 | + |
| 24 | + // if set, then an error parsing the settings is expected to occur |
| 25 | + expectedErrorStr string |
| 26 | + }{ |
| 27 | + { |
| 28 | + name: "defaults to empty values", |
| 29 | + envVars: map[string]string{}, |
| 30 | + expectedSettings: &settings.Settings{ |
| 31 | + EnableIstioIntegration: false, |
| 32 | + EnableAutoMtls: false, |
| 33 | + StsClusterName: "", |
| 34 | + StsUri: "", |
| 35 | + }, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "all values set", |
| 39 | + envVars: map[string]string{ |
| 40 | + "KGW_ENABLE_ISTIO_INTEGRATION": "true", |
| 41 | + "KGW_ENABLE_AUTO_MTLS": "true", |
| 42 | + "KGW_STS_CLUSTER_NAME": "my-cluster", |
| 43 | + "KGW_STS_URI": "my.sts.uri", |
| 44 | + }, |
| 45 | + expectedSettings: &settings.Settings{ |
| 46 | + EnableIstioIntegration: true, |
| 47 | + EnableAutoMtls: true, |
| 48 | + StsClusterName: "my-cluster", |
| 49 | + StsUri: "my.sts.uri", |
| 50 | + }, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "errors on invalid bool", |
| 54 | + envVars: map[string]string{ |
| 55 | + "KGW_ENABLE_ISTIO_INTEGRATION": "true123", |
| 56 | + }, |
| 57 | + expectedErrorStr: "invalid syntax", |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "ignores other env vars", |
| 61 | + envVars: map[string]string{ |
| 62 | + "KGW_DOES_NOT_EXIST": "true", |
| 63 | + "ANOTHER_VAR": "abc", |
| 64 | + "KGW_ENABLE_AUTO_MTLS": "true", |
| 65 | + }, |
| 66 | + expectedSettings: &settings.Settings{ |
| 67 | + EnableAutoMtls: true, |
| 68 | + }, |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + for _, tc := range testCases { |
| 73 | + t.Run(tc.name, func(t *testing.T) { |
| 74 | + g := gomega.NewWithT(t) |
| 75 | + |
| 76 | + t.Cleanup(func() { |
| 77 | + for k := range tc.envVars { |
| 78 | + err := os.Unsetenv(k) |
| 79 | + g.Expect(err).NotTo(HaveOccurred()) |
| 80 | + } |
| 81 | + }) |
| 82 | + |
| 83 | + for k, v := range tc.envVars { |
| 84 | + err := os.Setenv(k, v) |
| 85 | + g.Expect(err).NotTo(HaveOccurred()) |
| 86 | + } |
| 87 | + s, err := settings.BuildSettings() |
| 88 | + if tc.expectedErrorStr != "" { |
| 89 | + g.Expect(err).To(HaveOccurred()) |
| 90 | + g.Expect(err.Error()).To(gomega.ContainSubstring(tc.expectedErrorStr)) |
| 91 | + } else { |
| 92 | + g.Expect(err).NotTo(HaveOccurred()) |
| 93 | + g.Expect(s).To(Equal(tc.expectedSettings)) |
| 94 | + } |
| 95 | + }) |
| 96 | + } |
| 97 | +} |
0 commit comments