| 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 server. |
| 31 | */ |
| 32 | func TestDefaultServerOptions(t *testing.T) { |
| 33 | defaultServerOptions := NewProxyRunOptions() |
| 34 | assertDefaultValue(t, "ServerCert", defaultServerOptions.ServerCert, "") |
| 35 | assertDefaultValue(t, "ServerKey", defaultServerOptions.ServerKey, "") |
| 36 | assertDefaultValue(t, "ServerCaCert", defaultServerOptions.ServerCaCert, "") |
| 37 | assertDefaultValue(t, "ClusterCert", defaultServerOptions.ClusterCert, "") |
| 38 | assertDefaultValue(t, "ClusterKey", defaultServerOptions.ClusterKey, "") |
| 39 | assertDefaultValue(t, "ClusterCaCert", defaultServerOptions.ClusterCaCert, "") |
| 40 | assertDefaultValue(t, "Mode", defaultServerOptions.Mode, "grpc") |
| 41 | assertDefaultValue(t, "UdsName", defaultServerOptions.UdsName, "") |
| 42 | assertDefaultValue(t, "DeleteUDSFile", defaultServerOptions.DeleteUDSFile, true) |
| 43 | assertDefaultValue(t, "ServerPort", defaultServerOptions.ServerPort, 8090) |
| 44 | assertDefaultValue(t, "ServerBindAddress", defaultServerOptions.ServerBindAddress, "") |
| 45 | assertDefaultValue(t, "AgentPort", defaultServerOptions.AgentPort, 8091) |
| 46 | assertDefaultValue(t, "AgentBindAddress", defaultServerOptions.AgentBindAddress, "") |
| 47 | assertDefaultValue(t, "HealthPort", defaultServerOptions.HealthPort, 8092) |
| 48 | assertDefaultValue(t, "HealthBindAddress", defaultServerOptions.HealthBindAddress, "") |
| 49 | assertDefaultValue(t, "AdminPort", defaultServerOptions.AdminPort, 8095) |
| 50 | assertDefaultValue(t, "AdminBindAddress", defaultServerOptions.AdminBindAddress, "127.0.0.1") |
| 51 | assertDefaultValue(t, "KeepaliveTime", defaultServerOptions.KeepaliveTime, 1*time.Hour) |
| 52 | assertDefaultValue(t, "FrontendKeepaliveTime", defaultServerOptions.FrontendKeepaliveTime, 1*time.Hour) |
| 53 | assertDefaultValue(t, "EnableProfiling", defaultServerOptions.EnableProfiling, false) |
| 54 | assertDefaultValue(t, "EnableContentionProfiling", defaultServerOptions.EnableContentionProfiling, false) |
| 55 | assertDefaultValue(t, "ServerCount", defaultServerOptions.ServerCount, 1) |
| 56 | assertDefaultValue(t, "AgentNamespace", defaultServerOptions.AgentNamespace, "") |
| 57 | assertDefaultValue(t, "AgentServiceAccount", defaultServerOptions.AgentServiceAccount, "") |
| 58 | assertDefaultValue(t, "KubeconfigPath", defaultServerOptions.KubeconfigPath, "") |
| 59 | assertDefaultValue(t, "KubeconfigQPS", defaultServerOptions.KubeconfigQPS, float32(0)) |
| 60 | assertDefaultValue(t, "KubeconfigBurst", defaultServerOptions.KubeconfigBurst, 0) |
| 61 | assertDefaultValue(t, "AuthenticationAudience", defaultServerOptions.AuthenticationAudience, "") |
| 62 | assertDefaultValue(t, "ProxyStrategies", defaultServerOptions.ProxyStrategies, "default") |
| 63 | assertDefaultValue(t, "CipherSuites", defaultServerOptions.CipherSuites, make([]string, 0)) |
| 64 | assertDefaultValue(t, "XfrChannelSize", defaultServerOptions.XfrChannelSize, 10) |
| 65 | assertDefaultValue(t, "APIContentType", defaultServerOptions.APIContentType, "application/vnd.kubernetes.protobuf") |
| 66 | |
| 67 | } |
| 68 | |
| 69 | func assertDefaultValue(t *testing.T, fieldName string, actual, expected interface{}) { |
| 70 | t.Helper() |
| 71 | assert.IsType(t, expected, actual, "For field %s, got the wrong type.", fieldName) |
| 72 | assert.Equal(t, expected, actual, "For field %s, got the wrong value.", fieldName) |
| 73 | } |
| 74 | |
| 75 | func TestValidate(t *testing.T) { |
| 76 | for desc, tc := range map[string]struct { |
| 77 | field string |
| 78 | value interface{} |
| 79 | expected error |
| 80 | }{ |
| 81 | "default": { |
| 82 | field: "", |
| 83 | value: nil, |
| 84 | expected: nil, |
| 85 | }, |
| 86 | "ReservedServerPort": { |
| 87 | field: "ServerPort", |
| 88 | value: 1023, |
| 89 | expected: fmt.Errorf("please do not try to use reserved port 1023 for the server port"), |
| 90 | }, |
| 91 | "StartValidServerPort": { |
| 92 | field: "ServerPort", |
| 93 | value: 1024, |
| 94 | expected: nil, |
| 95 | }, |
| 96 | "EndValidServerPort": { |
| 97 | field: "ServerPort", |
| 98 | value: 49151, |
| 99 | expected: nil, |
| 100 | }, |
| 101 | "StartEphemeralServerPort": { |
| 102 | field: "ServerPort", |
| 103 | value: 49152, |
| 104 | expected: fmt.Errorf("please do not try to use ephemeral port 49152 for the server port"), |
| 105 | }, |
| 106 | "ReservedAdminPort": { |
| 107 | field: "AdminPort", |
| 108 | value: 1023, |
| 109 | expected: fmt.Errorf("please do not try to use reserved port 1023 for the admin port"), |
| 110 | }, |
| 111 | "StartValidAdminPort": { |
| 112 | field: "AdminPort", |
| 113 | value: 1024, |
| 114 | expected: nil, |
| 115 | }, |
| 116 | "EndValidAdminPort": { |
| 117 | field: "AdminPort", |
| 118 | value: 49151, |
| 119 | expected: nil, |
| 120 | }, |
| 121 | "StartEphemeralAdminPort": { |
| 122 | field: "AdminPort", |
| 123 | value: 49152, |
| 124 | expected: fmt.Errorf("please do not try to use ephemeral port 49152 for the admin port"), |
| 125 | }, |
| 126 | "ReservedHealthPort": { |
| 127 | field: "HealthPort", |
| 128 | value: 1023, |
| 129 | expected: fmt.Errorf("please do not try to use reserved port 1023 for the health port"), |
| 130 | }, |
| 131 | "StartValidHealthPort": { |
| 132 | field: "HealthPort", |
| 133 | value: 1024, |
| 134 | expected: nil, |
| 135 | }, |
| 136 | "EndValidHealthPort": { |
| 137 | field: "HealthPort", |
| 138 | value: 49151, |
| 139 | expected: nil, |
| 140 | }, |
| 141 | "StartEphemeralHealthPort": { |
| 142 | field: "HealthPort", |
| 143 | value: 49152, |
| 144 | expected: fmt.Errorf("please do not try to use ephemeral port 49152 for the health port"), |
| 145 | }, |
| 146 | "CommaSparatedCipherSuites": { |
| 147 | field: "CipherSuites", |
| 148 | value: "TLS_AES_256_GCM_SHA384,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", |
| 149 | expected: nil, |
| 150 | }, |
| 151 | "Empty proxy strategies": { |
| 152 | field: "ProxyStrategies", |
| 153 | value: "", |
| 154 | expected: fmt.Errorf("ProxyStrategies cannot be empty"), |
| 155 | }, |
| 156 | "Invalid proxy strategies": { |
| 157 | field: "ProxyStrategies", |
| 158 | value: "invalid", |
| 159 | expected: fmt.Errorf("invalid proxy strategies: unknown proxy strategy: invalid"), |
| 160 | }, |
| 161 | "ZeroXfrChannelSize": { |
| 162 | field: "XfrChannelSize", |
| 163 | value: 0, |
| 164 | expected: fmt.Errorf("channel size 0 must be greater than 0"), |
| 165 | }, |
| 166 | "NegativeXfrChannelSize": { |
| 167 | field: "XfrChannelSize", |
| 168 | value: -10, |
| 169 | expected: fmt.Errorf("channel size -10 must be greater than 0"), |
| 170 | }, |
| 171 | } { |
| 172 | t.Run(desc, func(t *testing.T) { |
| 173 | testServerOptions := NewProxyRunOptions() |
| 174 | if tc.field == "CipherSuites" { |
| 175 | testServerOptions.Flags().Set("cipher-suites", tc.value.(string)) |
| 176 | } else if tc.field != "" { |
| 177 | rv := reflect.ValueOf(testServerOptions) |
| 178 | rv = rv.Elem() |
| 179 | fv := rv.FieldByName(tc.field) |
| 180 | switch reflect.TypeOf(tc.value).Kind() { |
| 181 | case reflect.String: |
| 182 | svalue := tc.value.(string) |
| 183 | fv.SetString(svalue) |
| 184 | case reflect.Int: |
| 185 | ivalue := tc.value.(int) |
| 186 | fv.SetInt(int64(ivalue)) |
| 187 | } |
| 188 | } |
| 189 | actual := testServerOptions.Validate() |
| 190 | assert.IsType(t, tc.expected, actual, "Validation for field %s, got the wrong type.", tc.field) |
| 191 | assert.Equal(t, tc.expected, actual, "Validation for field %s, got the wrong value.", tc.field) |
| 192 | }) |
| 193 | } |
| 194 | } |