From a85fc1038cc25cb5a977b015bcd0da1554d74fb4 Mon Sep 17 00:00:00 2001 From: Prasad Katti Date: Mon, 25 May 2020 19:32:25 -0700 Subject: [PATCH] Add tests for ValidateURLs (kubeadm validation) --- .../kubeadm/validation/validation_test.go | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/cmd/kubeadm/app/apis/kubeadm/validation/validation_test.go b/cmd/kubeadm/app/apis/kubeadm/validation/validation_test.go index e7a23638ef6..e129d839818 100644 --- a/cmd/kubeadm/app/apis/kubeadm/validation/validation_test.go +++ b/cmd/kubeadm/app/apis/kubeadm/validation/validation_test.go @@ -914,3 +914,50 @@ func TestValidateSocketPath(t *testing.T) { } } } + +func TestValidateURLs(t *testing.T) { + var tests = []struct { + name string + urls []string + requireHTTPS bool + expectedErrors bool + }{ + { + name: "valid urls (https not required)", + urls: []string{"http://example.com", "https://example.org"}, + requireHTTPS: false, + expectedErrors: false, + }, + { + name: "valid urls (https required)", + urls: []string{"https://example.com", "https://example.org"}, + requireHTTPS: true, + expectedErrors: false, + }, + { + name: "invalid url (https required)", + urls: []string{"http://example.com", "https://example.org"}, + requireHTTPS: true, + expectedErrors: true, + }, + { + name: "URL parse error", + urls: []string{"::://example.com"}, + requireHTTPS: false, + expectedErrors: true, + }, + { + name: "URL without scheme", + urls: []string{"example.com"}, + requireHTTPS: false, + expectedErrors: true, + }, + } + for _, tc := range tests { + actual := ValidateURLs(tc.urls, tc.requireHTTPS, nil) + actualErrors := len(actual) > 0 + if actualErrors != tc.expectedErrors { + t.Errorf("error:\n\texpected: %t\n\t actual: %t", tc.expectedErrors, actualErrors) + } + } +}