Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for ValidateEtcd #91424

Merged
merged 1 commit into from May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/kubeadm/app/apis/kubeadm/validation/validation.go
Expand Up @@ -289,7 +289,7 @@ func ValidateEtcd(e *kubeadm.Etcd, fldPath *field.Path) field.ErrorList {
if (e.External.CertFile == "" && e.External.KeyFile != "") || (e.External.CertFile != "" && e.External.KeyFile == "") {
allErrs = append(allErrs, field.Invalid(externalPath, "", "either both or none of .Etcd.External.CertFile and .Etcd.External.KeyFile must be set"))
}
// If the cert and key are specified, require the VA as well
// If the cert and key are specified, require the CA as well
if e.External.CertFile != "" && e.External.KeyFile != "" && e.External.CAFile == "" {
allErrs = append(allErrs, field.Invalid(externalPath, "", "setting .Etcd.External.CertFile and .Etcd.External.KeyFile requires .Etcd.External.CAFile"))
}
Expand Down
79 changes: 79 additions & 0 deletions cmd/kubeadm/app/apis/kubeadm/validation/validation_test.go
Expand Up @@ -961,3 +961,82 @@ func TestValidateURLs(t *testing.T) {
}
}
}

func TestValidateEtcd(t *testing.T) {
var tests = []struct {
name string
etcd *kubeadm.Etcd
expectedErrors bool
}{
{
name: "either .Etcd.Local or .Etcd.External is required",
etcd: &kubeadm.Etcd{},
expectedErrors: true,
},
{
name: ".Etcd.Local and .Etcd.External are mutually exclusive",
etcd: &kubeadm.Etcd{
Local: &kubeadm.LocalEtcd{
DataDir: "/some/path",
},
External: &kubeadm.ExternalEtcd{
Endpoints: []string{"10.100.0.1:2379", "10.100.0.2:2379"},
},
},
expectedErrors: true,
},
{
name: "either both or none of .Etcd.External.CertFile and .Etcd.External.KeyFile must be set",
etcd: &kubeadm.Etcd{
External: &kubeadm.ExternalEtcd{
Endpoints: []string{"https://external.etcd1:2379", "https://external.etcd2:2379"},
CertFile: "/some/file.crt",
},
},
expectedErrors: true,
},
{
name: "setting .Etcd.External.CertFile and .Etcd.External.KeyFile requires .Etcd.External.CAFile",
etcd: &kubeadm.Etcd{
External: &kubeadm.ExternalEtcd{
Endpoints: []string{"https://external.etcd1:2379", "https://external.etcd2:2379"},
CertFile: "/some/file.crt",
KeyFile: "/some/file.key",
},
},
expectedErrors: true,
},
{
name: "valid external etcd",
etcd: &kubeadm.Etcd{
External: &kubeadm.ExternalEtcd{
Endpoints: []string{"https://external.etcd1:2379", "https://external.etcd2:2379"},
CertFile: "/etcd.crt",
KeyFile: "/etcd.key",
CAFile: "/etcd-ca.crt",
},
},
expectedErrors: false,
},
{
name: "valid external etcd (no TLS)",
etcd: &kubeadm.Etcd{
External: &kubeadm.ExternalEtcd{
Endpoints: []string{"http://10.100.0.1:2379", "http://10.100.0.2:2379"},
},
},
expectedErrors: false,
},
}

for _, tc := range tests {
actual := ValidateEtcd(tc.etcd, field.NewPath("etcd"))
actualErrors := len(actual) > 0
if actualErrors != tc.expectedErrors {
t.Errorf("Error: \n\texpected: %t\n\t actual: %t",
tc.expectedErrors,
actualErrors,
)
}
}
}