sigs.k8s.io/external-dns@v0.14.1/pkg/apis/externaldns/validation/validation_test.go (about) 1 /* 2 Copyright 2017 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 validation 18 19 import ( 20 "testing" 21 22 "sigs.k8s.io/external-dns/pkg/apis/externaldns" 23 24 "github.com/stretchr/testify/assert" 25 "github.com/stretchr/testify/require" 26 ) 27 28 func TestValidateFlags(t *testing.T) { 29 cfg := newValidConfig(t) 30 assert.NoError(t, ValidateConfig(cfg)) 31 32 cfg = newValidConfig(t) 33 cfg.LogFormat = "test" 34 assert.Error(t, ValidateConfig(cfg)) 35 36 cfg = newValidConfig(t) 37 cfg.LogFormat = "" 38 assert.Error(t, ValidateConfig(cfg)) 39 40 for _, format := range []string{"text", "json"} { 41 cfg = newValidConfig(t) 42 cfg.LogFormat = format 43 assert.NoError(t, ValidateConfig(cfg)) 44 } 45 46 cfg = newValidConfig(t) 47 cfg.Sources = []string{} 48 assert.Error(t, ValidateConfig(cfg)) 49 50 cfg = newValidConfig(t) 51 cfg.Provider = "" 52 assert.Error(t, ValidateConfig(cfg)) 53 } 54 55 func newValidConfig(t *testing.T) *externaldns.Config { 56 cfg := externaldns.NewConfig() 57 58 cfg.LogFormat = "json" 59 cfg.Sources = []string{"test-source"} 60 cfg.Provider = "test-provider" 61 62 require.NoError(t, ValidateConfig(cfg)) 63 64 return cfg 65 } 66 67 func addRequiredFieldsForDyn(cfg *externaldns.Config) { 68 cfg.LogFormat = "json" 69 cfg.Sources = []string{"ingress"} 70 cfg.Provider = "dyn" 71 } 72 73 func TestValidateBadDynConfig(t *testing.T) { 74 badConfigs := []*externaldns.Config{ 75 {}, 76 { 77 // only username 78 DynUsername: "test", 79 }, 80 { 81 // only customer name 82 DynCustomerName: "test", 83 }, 84 { 85 // negative timeout 86 DynUsername: "test", 87 DynCustomerName: "test", 88 DynMinTTLSeconds: -1, 89 }, 90 } 91 92 for _, cfg := range badConfigs { 93 addRequiredFieldsForDyn(cfg) 94 err := ValidateConfig(cfg) 95 assert.NotNil(t, err, "Configuration %+v should NOT have passed validation", cfg) 96 } 97 } 98 99 func TestValidateGoodDynConfig(t *testing.T) { 100 goodConfigs := []*externaldns.Config{ 101 { 102 DynUsername: "test", 103 DynCustomerName: "test", 104 DynMinTTLSeconds: 600, 105 }, 106 { 107 DynUsername: "test", 108 DynCustomerName: "test", 109 DynMinTTLSeconds: 0, 110 }, 111 } 112 113 for _, cfg := range goodConfigs { 114 addRequiredFieldsForDyn(cfg) 115 err := ValidateConfig(cfg) 116 assert.Nil(t, err, "Configuration should be valid, got this error instead", err) 117 } 118 } 119 120 func TestValidateBadIgnoreHostnameAnnotationsConfig(t *testing.T) { 121 cfg := externaldns.NewConfig() 122 cfg.IgnoreHostnameAnnotation = true 123 cfg.FQDNTemplate = "" 124 125 assert.Error(t, ValidateConfig(cfg)) 126 } 127 128 func TestValidateBadRfc2136Config(t *testing.T) { 129 cfg := externaldns.NewConfig() 130 131 cfg.LogFormat = "json" 132 cfg.Sources = []string{"test-source"} 133 cfg.Provider = "rfc2136" 134 cfg.RFC2136MinTTL = -1 135 cfg.RFC2136BatchChangeSize = 50 136 137 err := ValidateConfig(cfg) 138 139 assert.NotNil(t, err) 140 } 141 142 func TestValidateBadRfc2136Batch(t *testing.T) { 143 cfg := externaldns.NewConfig() 144 145 cfg.LogFormat = "json" 146 cfg.Sources = []string{"test-source"} 147 cfg.Provider = "rfc2136" 148 cfg.RFC2136MinTTL = 3600 149 cfg.RFC2136BatchChangeSize = 0 150 151 err := ValidateConfig(cfg) 152 153 assert.NotNil(t, err) 154 } 155 156 func TestValidateGoodRfc2136Config(t *testing.T) { 157 cfg := externaldns.NewConfig() 158 159 cfg.LogFormat = "json" 160 cfg.Sources = []string{"test-source"} 161 cfg.Provider = "rfc2136" 162 cfg.RFC2136MinTTL = 3600 163 cfg.RFC2136BatchChangeSize = 50 164 165 err := ValidateConfig(cfg) 166 167 assert.Nil(t, err) 168 } 169 170 func TestValidateBadRfc2136GssTsigConfig(t *testing.T) { 171 invalidRfc2136GssTsigConfigs := []*externaldns.Config{ 172 { 173 LogFormat: "json", 174 Sources: []string{"test-source"}, 175 Provider: "rfc2136", 176 RFC2136GSSTSIG: true, 177 RFC2136KerberosRealm: "test-realm", 178 RFC2136KerberosUsername: "test-user", 179 RFC2136KerberosPassword: "", 180 RFC2136MinTTL: 3600, 181 RFC2136BatchChangeSize: 50, 182 }, 183 { 184 LogFormat: "json", 185 Sources: []string{"test-source"}, 186 Provider: "rfc2136", 187 RFC2136GSSTSIG: true, 188 RFC2136KerberosRealm: "test-realm", 189 RFC2136KerberosUsername: "", 190 RFC2136KerberosPassword: "test-pass", 191 RFC2136MinTTL: 3600, 192 RFC2136BatchChangeSize: 50, 193 }, 194 { 195 LogFormat: "json", 196 Sources: []string{"test-source"}, 197 Provider: "rfc2136", 198 RFC2136GSSTSIG: true, 199 RFC2136Insecure: true, 200 RFC2136KerberosRealm: "test-realm", 201 RFC2136KerberosUsername: "test-user", 202 RFC2136KerberosPassword: "test-pass", 203 RFC2136MinTTL: 3600, 204 RFC2136BatchChangeSize: 50, 205 }, 206 { 207 LogFormat: "json", 208 Sources: []string{"test-source"}, 209 Provider: "rfc2136", 210 RFC2136GSSTSIG: true, 211 RFC2136KerberosRealm: "", 212 RFC2136KerberosUsername: "test-user", 213 RFC2136KerberosPassword: "", 214 RFC2136MinTTL: 3600, 215 RFC2136BatchChangeSize: 50, 216 }, 217 { 218 LogFormat: "json", 219 Sources: []string{"test-source"}, 220 Provider: "rfc2136", 221 RFC2136GSSTSIG: true, 222 RFC2136KerberosRealm: "", 223 RFC2136KerberosUsername: "", 224 RFC2136KerberosPassword: "test-pass", 225 RFC2136MinTTL: 3600, 226 RFC2136BatchChangeSize: 50, 227 }, 228 { 229 LogFormat: "json", 230 Sources: []string{"test-source"}, 231 Provider: "rfc2136", 232 RFC2136GSSTSIG: true, 233 RFC2136Insecure: true, 234 RFC2136KerberosRealm: "", 235 RFC2136KerberosUsername: "test-user", 236 RFC2136KerberosPassword: "test-pass", 237 RFC2136MinTTL: 3600, 238 RFC2136BatchChangeSize: 50, 239 }, 240 { 241 LogFormat: "json", 242 Sources: []string{"test-source"}, 243 Provider: "rfc2136", 244 RFC2136GSSTSIG: true, 245 RFC2136KerberosRealm: "", 246 RFC2136KerberosUsername: "test-user", 247 RFC2136KerberosPassword: "test-pass", 248 RFC2136MinTTL: 3600, 249 RFC2136BatchChangeSize: 50, 250 }, 251 } 252 253 for _, cfg := range invalidRfc2136GssTsigConfigs { 254 err := ValidateConfig(cfg) 255 256 assert.NotNil(t, err) 257 } 258 } 259 260 func TestValidateGoodRfc2136GssTsigConfig(t *testing.T) { 261 validRfc2136GssTsigConfigs := []*externaldns.Config{ 262 { 263 LogFormat: "json", 264 Sources: []string{"test-source"}, 265 Provider: "rfc2136", 266 RFC2136GSSTSIG: true, 267 RFC2136Insecure: false, 268 RFC2136KerberosRealm: "test-realm", 269 RFC2136KerberosUsername: "test-user", 270 RFC2136KerberosPassword: "test-pass", 271 RFC2136MinTTL: 3600, 272 RFC2136BatchChangeSize: 50, 273 }, 274 } 275 276 for _, cfg := range validRfc2136GssTsigConfigs { 277 err := ValidateConfig(cfg) 278 279 assert.Nil(t, err) 280 } 281 }