Skip to content

Commit 2572066

Browse files
authored
Merge pull request kubernetes#91424 from prasadkatti/add_validate_etcd_tests
Add tests for ValidateEtcd
2 parents 2a6d25d + c253ccc commit 2572066

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

cmd/kubeadm/app/apis/kubeadm/validation/validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ func ValidateEtcd(e *kubeadm.Etcd, fldPath *field.Path) field.ErrorList {
289289
if (e.External.CertFile == "" && e.External.KeyFile != "") || (e.External.CertFile != "" && e.External.KeyFile == "") {
290290
allErrs = append(allErrs, field.Invalid(externalPath, "", "either both or none of .Etcd.External.CertFile and .Etcd.External.KeyFile must be set"))
291291
}
292-
// If the cert and key are specified, require the VA as well
292+
// If the cert and key are specified, require the CA as well
293293
if e.External.CertFile != "" && e.External.KeyFile != "" && e.External.CAFile == "" {
294294
allErrs = append(allErrs, field.Invalid(externalPath, "", "setting .Etcd.External.CertFile and .Etcd.External.KeyFile requires .Etcd.External.CAFile"))
295295
}

cmd/kubeadm/app/apis/kubeadm/validation/validation_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,3 +961,82 @@ func TestValidateURLs(t *testing.T) {
961961
}
962962
}
963963
}
964+
965+
func TestValidateEtcd(t *testing.T) {
966+
var tests = []struct {
967+
name string
968+
etcd *kubeadm.Etcd
969+
expectedErrors bool
970+
}{
971+
{
972+
name: "either .Etcd.Local or .Etcd.External is required",
973+
etcd: &kubeadm.Etcd{},
974+
expectedErrors: true,
975+
},
976+
{
977+
name: ".Etcd.Local and .Etcd.External are mutually exclusive",
978+
etcd: &kubeadm.Etcd{
979+
Local: &kubeadm.LocalEtcd{
980+
DataDir: "/some/path",
981+
},
982+
External: &kubeadm.ExternalEtcd{
983+
Endpoints: []string{"10.100.0.1:2379", "10.100.0.2:2379"},
984+
},
985+
},
986+
expectedErrors: true,
987+
},
988+
{
989+
name: "either both or none of .Etcd.External.CertFile and .Etcd.External.KeyFile must be set",
990+
etcd: &kubeadm.Etcd{
991+
External: &kubeadm.ExternalEtcd{
992+
Endpoints: []string{"https://external.etcd1:2379", "https://external.etcd2:2379"},
993+
CertFile: "/some/file.crt",
994+
},
995+
},
996+
expectedErrors: true,
997+
},
998+
{
999+
name: "setting .Etcd.External.CertFile and .Etcd.External.KeyFile requires .Etcd.External.CAFile",
1000+
etcd: &kubeadm.Etcd{
1001+
External: &kubeadm.ExternalEtcd{
1002+
Endpoints: []string{"https://external.etcd1:2379", "https://external.etcd2:2379"},
1003+
CertFile: "/some/file.crt",
1004+
KeyFile: "/some/file.key",
1005+
},
1006+
},
1007+
expectedErrors: true,
1008+
},
1009+
{
1010+
name: "valid external etcd",
1011+
etcd: &kubeadm.Etcd{
1012+
External: &kubeadm.ExternalEtcd{
1013+
Endpoints: []string{"https://external.etcd1:2379", "https://external.etcd2:2379"},
1014+
CertFile: "/etcd.crt",
1015+
KeyFile: "/etcd.key",
1016+
CAFile: "/etcd-ca.crt",
1017+
},
1018+
},
1019+
expectedErrors: false,
1020+
},
1021+
{
1022+
name: "valid external etcd (no TLS)",
1023+
etcd: &kubeadm.Etcd{
1024+
External: &kubeadm.ExternalEtcd{
1025+
Endpoints: []string{"http://10.100.0.1:2379", "http://10.100.0.2:2379"},
1026+
},
1027+
},
1028+
expectedErrors: false,
1029+
},
1030+
}
1031+
1032+
for _, tc := range tests {
1033+
actual := ValidateEtcd(tc.etcd, field.NewPath("etcd"))
1034+
actualErrors := len(actual) > 0
1035+
if actualErrors != tc.expectedErrors {
1036+
t.Errorf("Error: \n\texpected: %t\n\t actual: %t",
1037+
tc.expectedErrors,
1038+
actualErrors,
1039+
)
1040+
}
1041+
}
1042+
}

0 commit comments

Comments
 (0)