unittests: Fixes unit tests for Windows

Currently, there are some unit tests that are failing on Windows due to
various reasons:

- Windows file permissions do not work the same way as the Linux ones.
- cp does not exist on Windows, and xcopy should be used instead.
- Get-Item does not work for hidden files / folders like AppData, but
  works if given the -Force flag.
This commit is contained in:
Claudiu Belu 2022-07-29 12:49:04 +03:00
parent b6020d263c
commit 49ef27009d
4 changed files with 54 additions and 11 deletions

View File

@ -22,6 +22,7 @@ import (
"os"
"path"
"regexp"
goruntime "runtime"
"testing"
"github.com/lithammer/dedent"
@ -241,20 +242,26 @@ func TestDownloadCerts(t *testing.T) {
// Check that the written files are either certificates or keys, and that they have
// the expected permissions
if _, err := keyutil.ParsePrivateKeyPEM(diskCertData); err == nil {
if stat, err := os.Stat(certPath); err == nil {
if stat.Mode() != keyFileMode {
t.Errorf("key %q should have mode %#o, has %#o", certName, keyFileMode, stat.Mode())
// File permissions are set differently on Windows, which does not match the expectation below.
if goruntime.GOOS != "windows" {
if stat, err := os.Stat(certPath); err == nil {
if stat.Mode() != keyFileMode {
t.Errorf("key %q should have mode %#o, has %#o", certName, keyFileMode, stat.Mode())
}
} else {
t.Errorf("could not stat key %q: %v", certName, err)
}
} else {
t.Errorf("could not stat key %q: %v", certName, err)
}
} else if _, err := keyutil.ParsePublicKeysPEM(diskCertData); err == nil {
if stat, err := os.Stat(certPath); err == nil {
if stat.Mode() != certFileMode {
t.Errorf("cert %q should have mode %#o, has %#o", certName, certFileMode, stat.Mode())
// File permissions are set differently on Windows, which does not match the expectation below.
if goruntime.GOOS != "windows" {
if stat, err := os.Stat(certPath); err == nil {
if stat.Mode() != certFileMode {
t.Errorf("cert %q should have mode %#o, has %#o", certName, certFileMode, stat.Mode())
}
} else {
t.Errorf("could not stat cert %q: %v", certName, err)
}
} else {
t.Errorf("could not stat cert %q: %v", certName, err)
}
} else {
t.Errorf("secret %q was not identified as a cert or as a key", certName)

View File

@ -1,3 +1,6 @@
//go:build !windows
// +build !windows
/*
Copyright 2017 The Kubernetes Authors.

View File

@ -0,0 +1,32 @@
//go:build windows
// +build windows
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package util
import (
"os/exec"
)
// CopyDir copies the content of a folder
func CopyDir(src string, dst string) error {
// /E Copies directories and subdirectories, including empty ones.
// /H Copies hidden and system files also.
cmd := exec.Command("xcopy", "/E", "/H", src, dst)
return cmd.Run()
}

View File

@ -114,7 +114,8 @@ func evalSymlink(path string) (string, error) {
}
}
// This command will give the target path of a given symlink
cmd := fmt.Sprintf("(Get-Item -LiteralPath %q).Target", upperpath)
// The -Force parameter will allow Get-Item to also evaluate hidden folders, like AppData.
cmd := fmt.Sprintf("(Get-Item -Force -LiteralPath %q).Target", upperpath)
output, err := exec.Command("powershell", "/c", cmd).CombinedOutput()
if err != nil {
return "", err