Merge pull request #58540 from neolit123/test-reset
Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. kubeadm: add better test coverage to reset.go **What this PR does / why we need it**: a PR for adding some more tests in `kubeadm/cmd` for `reset.go`. increasing coverage from ~11% to ~92% the remaining 8% are kind of hard to test or not worth the complexity. original idea to add more tests in kubeadm was suggested to me by @luxas at slack. in time, i might add better coverage to all cmd `.go` files. **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: please, link issue # if you know of such. **Special notes for your reviewer**: none **Release note**: ```release-note NONE ```
This commit is contained in:
		@@ -87,6 +87,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 {
 | 
				
			||||||
@@ -166,7 +205,10 @@ func TestConfigDirCleaner(t *testing.T) {
 | 
				
			|||||||
			f.Close()
 | 
								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)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user