unittests: Fixes unit tests for Windows (part 9)

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

- time.Now() is not as precise on Windows, which means that
  2 consecutive calls may return the same timestamp.
- Different "File not found" error messages on Windows.
- The default Container Runtime URL scheme on Windows is npipe, not unix.
This commit is contained in:
Claudiu Belu 2023-07-19 12:22:18 +00:00
parent 96a5cfe513
commit c2dfcf1e34
2 changed files with 20 additions and 9 deletions

View File

@ -34,17 +34,18 @@ import (
"k8s.io/kubernetes/cmd/kubeadm/app/constants" "k8s.io/kubernetes/cmd/kubeadm/app/constants"
) )
var defaultURLScheme = kubeadmapiv1.DefaultContainerRuntimeURLScheme
var testResetConfig = fmt.Sprintf(`apiVersion: %s var testResetConfig = fmt.Sprintf(`apiVersion: %s
kind: ResetConfiguration kind: ResetConfiguration
force: true force: true
dryRun: true dryRun: true
cleanupTmpDir: true cleanupTmpDir: true
criSocket: unix:///var/run/fake.sock criSocket: %s:///var/run/fake.sock
certificatesDir: /etc/kubernetes/pki2 certificatesDir: /etc/kubernetes/pki2
ignorePreflightErrors: ignorePreflightErrors:
- a - a
- b - b
`, kubeadmapiv1.SchemeGroupVersion.String()) `, kubeadmapiv1.SchemeGroupVersion.String(), defaultURLScheme)
func TestNewResetData(t *testing.T) { func TestNewResetData(t *testing.T) {
// create temp directory // create temp directory
@ -105,7 +106,7 @@ func TestNewResetData(t *testing.T) {
name: "fails if preflight ignores all but has individual check", name: "fails if preflight ignores all but has individual check",
flags: map[string]string{ flags: map[string]string{
options.IgnorePreflightErrors: "all,something-else", options.IgnorePreflightErrors: "all,something-else",
options.NodeCRISocket: "unix:///var/run/crio/crio.sock", options.NodeCRISocket: fmt.Sprintf("%s:///var/run/crio/crio.sock", defaultURLScheme),
}, },
expectError: "don't specify individual checks if 'all' is used", expectError: "don't specify individual checks if 'all' is used",
}, },
@ -113,7 +114,7 @@ func TestNewResetData(t *testing.T) {
name: "pre-flights errors from CLI args", name: "pre-flights errors from CLI args",
flags: map[string]string{ flags: map[string]string{
options.IgnorePreflightErrors: "a,b", options.IgnorePreflightErrors: "a,b",
options.NodeCRISocket: "unix:///var/run/crio/crio.sock", options.NodeCRISocket: fmt.Sprintf("%s:///var/run/crio/crio.sock", defaultURLScheme),
}, },
validate: expectedResetIgnorePreflightErrors(sets.New("a", "b")), validate: expectedResetIgnorePreflightErrors(sets.New("a", "b")),
}, },
@ -125,7 +126,7 @@ func TestNewResetData(t *testing.T) {
}, },
data: &resetData{ data: &resetData{
certificatesDir: "/etc/kubernetes/pki2", // cover the case that default is overridden as well certificatesDir: "/etc/kubernetes/pki2", // cover the case that default is overridden as well
criSocketPath: "unix:///var/run/fake.sock", // cover the case that default is overridden as well criSocketPath: fmt.Sprintf("%s:///var/run/fake.sock", defaultURLScheme), // cover the case that default is overridden as well
ignorePreflightErrors: sets.New("a", "b"), ignorePreflightErrors: sets.New("a", "b"),
forceReset: true, forceReset: true,
dryRun: true, dryRun: true,
@ -134,7 +135,7 @@ func TestNewResetData(t *testing.T) {
TypeMeta: metav1.TypeMeta{Kind: "", APIVersion: ""}, TypeMeta: metav1.TypeMeta{Kind: "", APIVersion: ""},
Force: true, Force: true,
CertificatesDir: "/etc/kubernetes/pki2", CertificatesDir: "/etc/kubernetes/pki2",
CRISocket: "unix:///var/run/fake.sock", CRISocket: fmt.Sprintf("%s:///var/run/fake.sock", defaultURLScheme),
IgnorePreflightErrors: []string{"a", "b"}, IgnorePreflightErrors: []string{"a", "b"},
CleanupTmpDir: true, CleanupTmpDir: true,
DryRun: true, DryRun: true,
@ -176,7 +177,7 @@ func TestNewResetData(t *testing.T) {
name: "--cri-socket flag is not allowed to mix with config", name: "--cri-socket flag is not allowed to mix with config",
flags: map[string]string{ flags: map[string]string{
options.CfgPath: configFilePath, options.CfgPath: configFilePath,
options.NodeCRISocket: "unix:///var/run/bogus.sock", options.NodeCRISocket: fmt.Sprintf("%s:///var/run/bogus.sock", defaultURLScheme),
}, },
expectError: "can not mix '--config' with arguments", expectError: "can not mix '--config' with arguments",
}, },

View File

@ -684,10 +684,20 @@ func Test_InFlightPods(t *testing.T) {
for _, p := range test.initialPods { for _, p := range test.initialPods {
obj = append(obj, p) obj = append(obj, p)
} }
q := NewTestQueueWithObjects(ctx, newDefaultQueueSort(), obj, WithQueueingHintMapPerProfile(test.queueingHintMap)) fakeClock := testingclock.NewFakeClock(time.Now())
q := NewTestQueueWithObjects(ctx, newDefaultQueueSort(), obj, WithQueueingHintMapPerProfile(test.queueingHintMap), WithClock(fakeClock))
// When a Pod is added to the queue, the QueuedPodInfo will have a new timestamp.
// On Windows, time.Now() is not as precise, 2 consecutive calls may return the same timestamp.
// Thus, all the QueuedPodInfos can have the same timestamps, which can be an issue
// when we're expecting them to be popped in a certain order (the Less function
// sorts them by Timestamps if they have the same Pod Priority).
// Using a fake clock for the queue and incrementing it after each added Pod will
// solve this issue on Windows unit test runs.
// For more details on the Windows clock resolution issue, see: https://github.com/golang/go/issues/8687
for _, p := range test.initialPods { for _, p := range test.initialPods {
q.Add(logger, p) q.Add(logger, p)
fakeClock.Step(time.Second)
} }
for _, action := range test.actions { for _, action := range test.actions {