| 1 | /* |
| 2 | Copyright 2022 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package options |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "reflect" |
| 22 | "testing" |
| 23 | "time" |
| 24 | |
| 25 | "github.com/stretchr/testify/assert" |
| 26 | ) |
| 27 | |
| 28 | /* |
| 29 | * TestDefaultServerOptions is intended to ensure we do not make a backward incompatible |
| 30 | * change to the default flag values for the ANP agent. |
| 31 | */ |
| 32 | func TestDefaultServerOptions(t *testing.T) { |
| 33 | defaultAgentOptions := NewGrpcProxyAgentOptions() |
| 34 | assertDefaultValue(t, "AgentCert", defaultAgentOptions.AgentCert, "") |
| 35 | assertDefaultValue(t, "AgentKey", defaultAgentOptions.AgentKey, "") |
| 36 | assertDefaultValue(t, "CaCert", defaultAgentOptions.CaCert, "") |
| 37 | assertDefaultValue(t, "ProxyServerHost", defaultAgentOptions.ProxyServerHost, "127.0.0.1") |
| 38 | assertDefaultValue(t, "ProxyServerPort", defaultAgentOptions.ProxyServerPort, 8091) |
| 39 | assertDefaultValue(t, "HealthServerHost", defaultAgentOptions.HealthServerHost, "") |
| 40 | assertDefaultValue(t, "HealthServerPort", defaultAgentOptions.HealthServerPort, 8093) |
| 41 | assertDefaultValue(t, "AdminBindAddress", defaultAgentOptions.AdminBindAddress, "127.0.0.1") |
| 42 | assertDefaultValue(t, "AdminServerPort", defaultAgentOptions.AdminServerPort, 8094) |
| 43 | assertDefaultValue(t, "EnableProfiling", defaultAgentOptions.EnableProfiling, false) |
| 44 | assertDefaultValue(t, "EnableContentionProfiling", defaultAgentOptions.EnableContentionProfiling, false) |
| 45 | assertDefaultValue(t, "AgentIdentifiers", defaultAgentOptions.AgentIdentifiers, "") |
| 46 | assertDefaultValue(t, "SyncInterval", defaultAgentOptions.SyncInterval, 1*time.Second) |
| 47 | assertDefaultValue(t, "ProbeInterval", defaultAgentOptions.ProbeInterval, 1*time.Second) |
| 48 | assertDefaultValue(t, "SyncIntervalCap", defaultAgentOptions.SyncIntervalCap, 10*time.Second) |
| 49 | assertDefaultValue(t, "KeepaliveTime", defaultAgentOptions.KeepaliveTime, 1*time.Hour) |
| 50 | assertDefaultValue(t, "ServiceAccountTokenPath", defaultAgentOptions.ServiceAccountTokenPath, "") |
| 51 | assertDefaultValue(t, "WarnOnChannelLimit", defaultAgentOptions.WarnOnChannelLimit, false) |
| 52 | assertDefaultValue(t, "SyncForever", defaultAgentOptions.SyncForever, false) |
| 53 | assertDefaultValue(t, "XfrChannelSize", defaultAgentOptions.XfrChannelSize, 150) |
| 54 | assertDefaultValue(t, "APIContentType", defaultAgentOptions.APIContentType, "application/vnd.kubernetes.protobuf") |
| 55 | } |
| 56 | |
| 57 | func assertDefaultValue(t *testing.T, fieldName string, actual, expected interface{}) { |
| 58 | t.Helper() |
| 59 | assert.IsType(t, expected, actual, "For field %s, got the wrong type.", fieldName) |
| 60 | assert.Equal(t, expected, actual, "For field %s, got the wrong value.", fieldName) |
| 61 | } |
| 62 | |
| 63 | func TestValidate(t *testing.T) { |
| 64 | for desc, tc := range map[string]struct { |
| 65 | fieldMap map[string]interface{} |
| 66 | expected error |
| 67 | }{ |
| 68 | "default": { |
| 69 | fieldMap: map[string]interface{}{}, |
| 70 | expected: nil, |
| 71 | }, |
| 72 | "ZeroProxyServerPort": { |
| 73 | fieldMap: map[string]interface{}{"ProxyServerPort": 0}, |
| 74 | expected: fmt.Errorf("proxy server port 0 must be greater than 0"), |
| 75 | }, |
| 76 | "NegativeProxyServerPort": { |
| 77 | fieldMap: map[string]interface{}{"ProxyServerPort": -1}, |
| 78 | expected: fmt.Errorf("proxy server port -1 must be greater than 0"), |
| 79 | }, |
| 80 | "ReservedProxyServerPort": { |
| 81 | fieldMap: map[string]interface{}{"ProxyServerPort": 1023}, |
| 82 | expected: nil, //TODO: fmt.Errorf("please do not try to use reserved port 1023 for the proxy server port"), |
| 83 | }, |
| 84 | "StartValidProxyServerPort": { |
| 85 | fieldMap: map[string]interface{}{"ProxyServerPort": 1024}, |
| 86 | expected: nil, |
| 87 | }, |
| 88 | "EndValidProxyServerPort": { |
| 89 | fieldMap: map[string]interface{}{"ProxyServerPort": 49151}, |
| 90 | expected: nil, |
| 91 | }, |
| 92 | "StartEphemeralProxyServerPort": { |
| 93 | fieldMap: map[string]interface{}{"ProxyServerPort": 49152}, |
| 94 | expected: nil, //TODO: fmt.Errorf("please do not try to use ephemeral port 49152 for the proxy server port"), |
| 95 | }, |
| 96 | "ZeroHealthServerPort": { |
| 97 | fieldMap: map[string]interface{}{"HealthServerPort": 0}, |
| 98 | expected: fmt.Errorf("health server port 0 must be greater than 0"), |
| 99 | }, |
| 100 | "NegativeHealthServerPort": { |
| 101 | fieldMap: map[string]interface{}{"HealthServerPort": -1}, |
| 102 | expected: fmt.Errorf("health server port -1 must be greater than 0"), |
| 103 | }, |
| 104 | "ReservedHealthServerPort": { |
| 105 | fieldMap: map[string]interface{}{"HealthServerPort": 1023}, |
| 106 | expected: nil, //TODO: fmt.Errorf("please do not try to use reserved port 1023 for the health server port"), |
| 107 | }, |
| 108 | "StartValidHealthServerPort": { |
| 109 | fieldMap: map[string]interface{}{"HealthServerPort": 1024}, |
| 110 | expected: nil, |
| 111 | }, |
| 112 | "EndValidHealthServerPort": { |
| 113 | fieldMap: map[string]interface{}{"HealthServerPort": 49151}, |
| 114 | expected: nil, |
| 115 | }, |
| 116 | "StartEphemeralHealthServerPort": { |
| 117 | fieldMap: map[string]interface{}{"HealthServerPort": 49152}, |
| 118 | expected: nil, //TODO: fmt.Errorf("please do not try to use ephemeral port 49152 for the health server port"), |
| 119 | }, |
| 120 | "ZeroAdminServerPort": { |
| 121 | fieldMap: map[string]interface{}{"AdminServerPort": 0}, |
| 122 | expected: fmt.Errorf("admin server port 0 must be greater than 0"), |
| 123 | }, |
| 124 | "NegativeAdminServerPort": { |
| 125 | fieldMap: map[string]interface{}{"AdminServerPort": -1}, |
| 126 | expected: fmt.Errorf("admin server port -1 must be greater than 0"), |
| 127 | }, |
| 128 | "ReservedAdminServerPort": { |
| 129 | fieldMap: map[string]interface{}{"AdminServerPort": 1023}, |
| 130 | expected: nil, //TODO: fmt.Errorf("please do not try to use reserved port 1023 for the health port"), |
| 131 | }, |
| 132 | "StartValidAdminServerPort": { |
| 133 | fieldMap: map[string]interface{}{"AdminServerPort": 1024}, |
| 134 | expected: nil, |
| 135 | }, |
| 136 | "EndValidAdminServerPort": { |
| 137 | fieldMap: map[string]interface{}{"AdminServerPort": 49151}, |
| 138 | expected: nil, |
| 139 | }, |
| 140 | "StartEphemeralAdminServerPort": { |
| 141 | fieldMap: map[string]interface{}{"AdminServerPort": 49152}, |
| 142 | expected: nil, //TODO: fmt.Errorf("please do not try to use ephemeral port 49152 for the health port"), |
| 143 | }, |
| 144 | "ContentionProfilingRequiresProfiling": { |
| 145 | fieldMap: map[string]interface{}{ |
| 146 | "EnableContentionProfiling": true, |
| 147 | "EnableProfiling": false, |
| 148 | }, |
| 149 | expected: fmt.Errorf("if --enable-contention-profiling is set, --enable-profiling must also be set"), |
| 150 | }, |
| 151 | "ZeroXfrChannelSize": { |
| 152 | fieldMap: map[string]interface{}{"XfrChannelSize": 0}, |
| 153 | expected: fmt.Errorf("channel size 0 must be greater than 0"), |
| 154 | }, |
| 155 | "NegativeXfrChannelSize": { |
| 156 | fieldMap: map[string]interface{}{"XfrChannelSize": -10}, |
| 157 | expected: fmt.Errorf("channel size -10 must be greater than 0"), |
| 158 | }, |
| 159 | "ServerCountSource": { |
| 160 | fieldMap: map[string]interface{}{"ServerCountSource": "foobar"}, |
| 161 | expected: fmt.Errorf("--server-count-source must be one of '', 'default', 'max', got foobar"), |
| 162 | }, |
| 163 | } { |
| 164 | t.Run(desc, func(t *testing.T) { |
| 165 | testAgentOptions := NewGrpcProxyAgentOptions() |
| 166 | for field, value := range tc.fieldMap { |
| 167 | rv := reflect.ValueOf(testAgentOptions) |
| 168 | rv = rv.Elem() |
| 169 | fv := rv.FieldByName(field) |
| 170 | switch reflect.TypeOf(value).Kind() { |
| 171 | case reflect.String: |
| 172 | svalue := value.(string) |
| 173 | fv.SetString(svalue) |
| 174 | case reflect.Int: |
| 175 | ivalue := value.(int) |
| 176 | fv.SetInt(int64(ivalue)) |
| 177 | case reflect.Bool: |
| 178 | bvalue := value.(bool) |
| 179 | fv.SetBool(bvalue) |
| 180 | } |
| 181 | } |
| 182 | actual := testAgentOptions.Validate() |
| 183 | assert.IsType(t, tc.expected, actual, "Validation for case %s, got the wrong type.", desc) |
| 184 | assert.Equal(t, tc.expected, actual, "Validation for case %s, got the wrong value.", desc) |
| 185 | }) |
| 186 | } |
| 187 | } |