From e90cfb83ed0cfed05f63ecf453fa21e36ae270d0 Mon Sep 17 00:00:00 2001 From: Claudiu Belu Date: Thu, 18 Apr 2024 08:26:57 +0000 Subject: [PATCH] unittests: Fixes unit tests for Windows (part 11) Currently, there are some unit tests that are failing on Windows due to various reasons: - Cannot remove a directory if there's a file open in that directory. - Paths may have / or \ on Windows. --- cmd/import-boss/main.go | 2 ++ cmd/import-boss/main_test.go | 17 ++--------------- .../app/phases/certs/renewal/readwriter_test.go | 17 ++++++++--------- 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/cmd/import-boss/main.go b/cmd/import-boss/main.go index 61366069759..12085e9a1b6 100644 --- a/cmd/import-boss/main.go +++ b/cmd/import-boss/main.go @@ -428,6 +428,8 @@ func hasPathPrefix(path, prefix string) bool { if prefix == "" || path == prefix { return true } + path = filepath.Clean(path) + prefix = filepath.Clean(prefix) if !strings.HasSuffix(path, string(filepath.Separator)) { prefix += string(filepath.Separator) } diff --git a/cmd/import-boss/main_test.go b/cmd/import-boss/main_test.go index c8f2319903c..90a172893ad 100644 --- a/cmd/import-boss/main_test.go +++ b/cmd/import-boss/main_test.go @@ -19,7 +19,6 @@ package main import ( "path/filepath" "reflect" - goruntime "runtime" "strings" "testing" @@ -121,10 +120,6 @@ func TestHasTestFiles(t *testing.T) { } func TestPackageDir(t *testing.T) { - if goruntime.GOOS == "windows" { - // TODO: remove skip once the failing test has been fixed. - t.Skip("Skip failing test on Windows.") - } cases := []struct { input *packages.Package expect string @@ -134,13 +129,13 @@ func TestPackageDir(t *testing.T) { GoFiles: []string{"/src/prj/file.go"}, IgnoredFiles: []string{"/otherdir/file.go"}, }, - expect: "/src/prj", + expect: filepath.Clean("/src/prj"), }, { input: &packages.Package{ PkgPath: "example.com/foo/bar/qux", IgnoredFiles: []string{"/src/prj/file.go"}, }, - expect: "/src/prj", + expect: filepath.Clean("/src/prj"), }, { input: &packages.Package{ PkgPath: "example.com/foo/bar/qux", @@ -157,10 +152,6 @@ func TestPackageDir(t *testing.T) { } func TestHasPathPrefix(t *testing.T) { - if goruntime.GOOS == "windows" { - // TODO: remove skip once the failing test has been fixed. - t.Skip("Skip failing test on Windows.") - } cases := []struct { base string pfx string @@ -228,10 +219,6 @@ func checkAllErrorStrings(t *testing.T, errs []error, expect []string) { } func TestSimpleForward(t *testing.T) { - if goruntime.GOOS == "windows" { - // TODO: remove skip once the failing test has been fixed. - t.Skip("Skip failing test on Windows.") - } pkgs, err := loadPkgs("./testdata/simple-fwd/aaa") if err != nil { t.Fatalf("unexpected failure: %v", err) diff --git a/cmd/kubeadm/app/phases/certs/renewal/readwriter_test.go b/cmd/kubeadm/app/phases/certs/renewal/readwriter_test.go index 7f409054786..bb61853f060 100644 --- a/cmd/kubeadm/app/phases/certs/renewal/readwriter_test.go +++ b/cmd/kubeadm/app/phases/certs/renewal/readwriter_test.go @@ -23,7 +23,6 @@ import ( "net" "os" "path/filepath" - goruntime "runtime" "testing" "k8s.io/client-go/tools/clientcmd" @@ -203,10 +202,6 @@ func writeTestKubeconfig(t *testing.T, dir, name string, caCert *x509.Certificat } func TestFileExists(t *testing.T) { - if goruntime.GOOS == "windows" { - // TODO: remove skip once the failing test has been fixed. - t.Skip("Skip failing test on Windows.") - } tmpdir, err := os.MkdirTemp("", "") if err != nil { t.Fatalf("Couldn't create tmpdir: %v", err) @@ -221,6 +216,10 @@ func TestFileExists(t *testing.T) { if err != nil { t.Fatalf("Couldn't create tmpfile: %v", err) } + if err := tmpfile.Close(); err != nil { + t.Fatalf("Couldn't close tmpfile: %v", err) + } + tests := []struct { name string filename string @@ -308,10 +307,6 @@ func TestPKICertificateReadWriterExists(t *testing.T) { } func TestKubeConfigReadWriterExists(t *testing.T) { - if goruntime.GOOS == "windows" { - // TODO: remove skip once the failing test has been fixed. - t.Skip("Skip failing test on Windows.") - } tmpdir, err := os.MkdirTemp("", "") if err != nil { t.Fatalf("Couldn't create tmpdir: %v", err) @@ -326,6 +321,10 @@ func TestKubeConfigReadWriterExists(t *testing.T) { if err != nil { t.Fatalf("Couldn't create tmpfile: %v", err) } + if err := tmpfile.Close(); err != nil { + t.Fatalf("Couldn't close tmpfile: %v", err) + } + tests := []struct { name string kubeConfigFilePath string