mirror of https://github.com/k3s-io/k3s
kubeadm: add better test coverage to reset.go
added tests in: - TestNewReset() - TestNewCmdReset() - TestConfigDirCleaner()pull/8/head
parent
891b471064
commit
29f8ed23ec
|
@ -86,6 +86,7 @@ go_test(
|
||||||
embed = [":go_default_library"],
|
embed = [":go_default_library"],
|
||||||
deps = [
|
deps = [
|
||||||
"//cmd/kubeadm/app/apis/kubeadm/v1alpha1:go_default_library",
|
"//cmd/kubeadm/app/apis/kubeadm/v1alpha1:go_default_library",
|
||||||
|
"//cmd/kubeadm/app/apis/kubeadm/validation:go_default_library",
|
||||||
"//cmd/kubeadm/app/constants:go_default_library",
|
"//cmd/kubeadm/app/constants:go_default_library",
|
||||||
"//cmd/kubeadm/app/preflight:go_default_library",
|
"//cmd/kubeadm/app/preflight:go_default_library",
|
||||||
"//vendor/k8s.io/api/core/v1:go_default_library",
|
"//vendor/k8s.io/api/core/v1:go_default_library",
|
||||||
|
|
|
@ -18,12 +18,15 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
kubeadmapiext "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1alpha1"
|
||||||
|
"k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/validation"
|
||||||
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
||||||
"k8s.io/kubernetes/cmd/kubeadm/app/preflight"
|
"k8s.io/kubernetes/cmd/kubeadm/app/preflight"
|
||||||
"k8s.io/utils/exec"
|
"k8s.io/utils/exec"
|
||||||
|
@ -52,8 +55,38 @@ func assertDirEmpty(t *testing.T, path string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNewReset(t *testing.T) {
|
||||||
|
certsDir := kubeadmapiext.DefaultCertificatesDir
|
||||||
|
criSocketPath := "/var/run/dockershim.sock"
|
||||||
|
skipPreFlight := false
|
||||||
|
|
||||||
|
ignorePreflightErrors := []string{"all"}
|
||||||
|
ignorePreflightErrorsSet, _ := validation.ValidateIgnorePreflightErrors(ignorePreflightErrors, skipPreFlight)
|
||||||
|
NewReset(ignorePreflightErrorsSet, certsDir, criSocketPath)
|
||||||
|
|
||||||
|
ignorePreflightErrors = []string{}
|
||||||
|
ignorePreflightErrorsSet, _ = validation.ValidateIgnorePreflightErrors(ignorePreflightErrors, skipPreFlight)
|
||||||
|
NewReset(ignorePreflightErrorsSet, certsDir, criSocketPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewCmdReset(t *testing.T) {
|
||||||
|
var out io.Writer
|
||||||
|
cmd := NewCmdReset(out)
|
||||||
|
|
||||||
|
tmpDir, err := ioutil.TempDir("", "kubeadm-reset-test")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unable to create temporary directory: %v", err)
|
||||||
|
}
|
||||||
|
args := []string{"--ignore-preflight-errors=all", "--cert-dir=" + tmpDir}
|
||||||
|
cmd.SetArgs(args)
|
||||||
|
if err := cmd.Execute(); err != nil {
|
||||||
|
t.Errorf("Cannot execute reset command: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestConfigDirCleaner(t *testing.T) {
|
func TestConfigDirCleaner(t *testing.T) {
|
||||||
tests := map[string]struct {
|
tests := map[string]struct {
|
||||||
|
resetDir string
|
||||||
setupDirs []string
|
setupDirs []string
|
||||||
setupFiles []string
|
setupFiles []string
|
||||||
verifyExists []string
|
verifyExists []string
|
||||||
|
@ -139,6 +172,12 @@ func TestConfigDirCleaner(t *testing.T) {
|
||||||
"manifests",
|
"manifests",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
"not a directory": {
|
||||||
|
resetDir: "test-path",
|
||||||
|
setupFiles: []string{
|
||||||
|
"test-path",
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for name, test := range tests {
|
for name, test := range tests {
|
||||||
|
@ -167,7 +206,10 @@ func TestConfigDirCleaner(t *testing.T) {
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
resetConfigDir(tmpDir, filepath.Join(tmpDir, "pki"))
|
if test.resetDir == "" {
|
||||||
|
test.resetDir = "pki"
|
||||||
|
}
|
||||||
|
resetConfigDir(tmpDir, filepath.Join(tmpDir, test.resetDir))
|
||||||
|
|
||||||
// Verify the files we cleanup implicitly in every test:
|
// Verify the files we cleanup implicitly in every test:
|
||||||
assertExists(t, tmpDir)
|
assertExists(t, tmpDir)
|
||||||
|
|
Loading…
Reference in New Issue