unittests: Fixes unit tests for Windows (part 6)

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

- On Windows, consecutive time.Now() calls may return the same timestamp, which would cause
  the TestFreeSpaceRemoveByLeastRecentlyUsed test to flake.
- tests in kuberuntime_container_windows_test.go fail on Nodes that have fewer than 3 CPUs,
  expecting the CPU max set to be more than 100% of available CPUs, which is not possible.
- calls in summary_windows_test.go are missing context.
- filterTerminatedContainerInfoAndAssembleByPodCgroupKey will filter and group container
  information by the Pod cgroup key, if it exists. However, we don't have cgroups on Windows,
  thus we can't make the same assertions.
This commit is contained in:
Claudiu Belu 2022-12-15 16:03:37 +00:00
parent f9a3fd2810
commit ec753fcb55
5 changed files with 33 additions and 61 deletions

View File

@ -19,12 +19,7 @@ package validation
import ( import (
_ "time/tzdata" _ "time/tzdata"
"archive/zip"
"fmt" "fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings" "strings"
"testing" "testing"
@ -44,10 +39,10 @@ var (
timeZoneEmpty = "" timeZoneEmpty = ""
timeZoneLocal = "LOCAL" timeZoneLocal = "LOCAL"
timeZoneUTC = "UTC" timeZoneUTC = "UTC"
timeZoneCorrect = "Continent/Zone" timeZoneCorrect = "Europe/Rome"
timeZoneBadPrefix = " Continent/Zone" timeZoneBadPrefix = " Europe/Rome"
timeZoneBadSuffix = "Continent/Zone " timeZoneBadSuffix = "Europe/Rome "
timeZoneBadName = "Continent/InvalidZone" timeZoneBadName = "Europe/InvalidRome"
timeZoneEmptySpace = " " timeZoneEmptySpace = " "
) )
@ -1444,12 +1439,6 @@ func TestValidateCronJob(t *testing.T) {
validPodTemplateSpec := getValidPodTemplateSpecForGenerated(getValidGeneratedSelector()) validPodTemplateSpec := getValidPodTemplateSpecForGenerated(getValidGeneratedSelector())
validPodTemplateSpec.Labels = map[string]string{} validPodTemplateSpec.Labels = map[string]string{}
zoneDir := t.TempDir()
if err := setupFakeTimeZoneDatabase(zoneDir); err != nil {
t.Fatalf("Unexpected error setting up fake timezone database: %v", err)
}
t.Setenv("ZONEINFO", zoneDir)
successCases := map[string]batch.CronJob{ successCases := map[string]batch.CronJob{
"basic scheduled job": { "basic scheduled job": {
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
@ -1942,42 +1931,6 @@ func TestValidateCronJob(t *testing.T) {
} }
} }
// Sets up fake timezone database in a zoneDir directory with a single valid
// time zone called "Continent/Zone" by copying UTC metadata from golang's
// built-in databse. Returns an error in case of problems.
func setupFakeTimeZoneDatabase(zoneDir string) error {
reader, err := zip.OpenReader(runtime.GOROOT() + "/lib/time/zoneinfo.zip")
if err != nil {
return err
}
defer reader.Close()
if err := os.Mkdir(filepath.Join(zoneDir, "Continent"), os.ModePerm); err != nil {
return err
}
zoneFile, err := os.OpenFile(filepath.Join(zoneDir, "Continent", "Zone"), os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0666)
if err != nil {
return err
}
defer zoneFile.Close()
for _, file := range reader.File {
if file.Name != "UTC" {
continue
}
rc, err := file.Open()
if err != nil {
return err
}
if _, err := io.Copy(zoneFile, rc); err != nil {
return err
}
rc.Close()
break
}
return nil
}
func TestValidateCronJobSpec(t *testing.T) { func TestValidateCronJobSpec(t *testing.T) {
validPodTemplateSpec := getValidPodTemplateSpecForGenerated(getValidGeneratedSelector()) validPodTemplateSpec := getValidPodTemplateSpecForGenerated(getValidGeneratedSelector())
validPodTemplateSpec.Labels = map[string]string{} validPodTemplateSpec.Labels = map[string]string{}

View File

@ -19,6 +19,7 @@ package images
import ( import (
"context" "context"
"fmt" "fmt"
goruntime "runtime"
"testing" "testing"
"time" "time"
@ -464,6 +465,13 @@ func TestFreeSpaceRemoveByLeastRecentlyUsed(t *testing.T) {
}, },
}}, }},
} }
// manager.detectImages uses time.Now() to update the image's lastUsed field.
// On Windows, consecutive time.Now() calls can return the same timestamp, which would mean
// that the second image is NOT newer than the first one.
// time.Sleep will result in the timestamp to be updated as well.
if goruntime.GOOS == "windows" {
time.Sleep(time.Millisecond)
}
_, err = manager.detectImages(ctx, time.Now()) _, err = manager.detectImages(ctx, time.Now())
require.NoError(t, err) require.NoError(t, err)
fakeRuntime.AllPodList = []*containertest.FakePod{ fakeRuntime.AllPodList = []*containertest.FakePod{

View File

@ -86,6 +86,11 @@ func TestApplyPlatformSpecificContainerConfig(t *testing.T) {
limit := int64(3000) limit := int64(3000)
expectedCpuMax := 10 * limit / int64(winstats.ProcessorCount()) expectedCpuMax := 10 * limit / int64(winstats.ProcessorCount())
// Above, we're setting the limit to 3 CPUs. But we can't expect more than 100% of the CPUs
// we have. (e.g.: if we only have 2 CPUs, we can't have 150% CPU max).
if expectedCpuMax > 10000 {
expectedCpuMax = 10000
}
expectedWindowsConfig := &runtimeapi.WindowsContainerConfig{ expectedWindowsConfig := &runtimeapi.WindowsContainerConfig{
Resources: &runtimeapi.WindowsContainerResources{ Resources: &runtimeapi.WindowsContainerResources{
CpuMaximum: expectedCpuMax, CpuMaximum: expectedCpuMax,

View File

@ -20,6 +20,7 @@ limitations under the License.
package stats package stats
import ( import (
"context"
"testing" "testing"
"time" "time"
@ -36,6 +37,7 @@ import (
func TestSummaryProvider(t *testing.T) { func TestSummaryProvider(t *testing.T) {
var ( var (
ctx = context.Background()
podStats = []statsapi.PodStats{*getPodStats()} podStats = []statsapi.PodStats{*getPodStats()}
imageFsStats = getFsStats() imageFsStats = getFsStats()
rootFsStats = getFsStats() rootFsStats = getFsStats()
@ -60,9 +62,9 @@ func TestSummaryProvider(t *testing.T) {
mockStatsProvider.EXPECT().GetNode().Return(node, nil).AnyTimes() mockStatsProvider.EXPECT().GetNode().Return(node, nil).AnyTimes()
mockStatsProvider.EXPECT().GetNodeConfig().Return(nodeConfig).AnyTimes() mockStatsProvider.EXPECT().GetNodeConfig().Return(nodeConfig).AnyTimes()
mockStatsProvider.EXPECT().GetPodCgroupRoot().Return(cgroupRoot).AnyTimes() mockStatsProvider.EXPECT().GetPodCgroupRoot().Return(cgroupRoot).AnyTimes()
mockStatsProvider.EXPECT().ListPodStats().Return(podStats, nil).AnyTimes() mockStatsProvider.EXPECT().ListPodStats(ctx).Return(podStats, nil).AnyTimes()
mockStatsProvider.EXPECT().ListPodStatsAndUpdateCPUNanoCoreUsage().Return(podStats, nil).AnyTimes() mockStatsProvider.EXPECT().ListPodStatsAndUpdateCPUNanoCoreUsage(ctx).Return(podStats, nil).AnyTimes()
mockStatsProvider.EXPECT().ImageFsStats().Return(imageFsStats, nil).AnyTimes() mockStatsProvider.EXPECT().ImageFsStats(ctx).Return(imageFsStats, nil).AnyTimes()
mockStatsProvider.EXPECT().RootFsStats().Return(rootFsStats, nil).AnyTimes() mockStatsProvider.EXPECT().RootFsStats().Return(rootFsStats, nil).AnyTimes()
mockStatsProvider.EXPECT().RlimitStats().Return(nil, nil).AnyTimes() mockStatsProvider.EXPECT().RlimitStats().Return(nil, nil).AnyTimes()
mockStatsProvider.EXPECT().GetCgroupStats("/", true).Return(cgroupStatsMap["/"].cs, cgroupStatsMap["/"].ns, nil).AnyTimes() mockStatsProvider.EXPECT().GetCgroupStats("/", true).Return(cgroupStatsMap["/"].cs, cgroupStatsMap["/"].ns, nil).AnyTimes()
@ -70,7 +72,7 @@ func TestSummaryProvider(t *testing.T) {
kubeletCreationTime := metav1.Now() kubeletCreationTime := metav1.Now()
systemBootTime := metav1.Now() systemBootTime := metav1.Now()
provider := summaryProviderImpl{kubeletCreationTime: kubeletCreationTime, systemBootTime: systemBootTime, provider: mockStatsProvider} provider := summaryProviderImpl{kubeletCreationTime: kubeletCreationTime, systemBootTime: systemBootTime, provider: mockStatsProvider}
summary, err := provider.Get(true) summary, err := provider.Get(ctx, true)
assert.NoError(err) assert.NoError(err)
assert.Equal(summary.Node.NodeName, "test-node") assert.Equal(summary.Node.NodeName, "test-node")

View File

@ -40,11 +40,6 @@ import (
) )
func TestFilterTerminatedContainerInfoAndAssembleByPodCgroupKey(t *testing.T) { func TestFilterTerminatedContainerInfoAndAssembleByPodCgroupKey(t *testing.T) {
// Skip tests that fail on Windows, as discussed during the SIG Testing meeting from January 10, 2023
if runtime.GOOS == "windows" {
t.Skip("Skipping test that fails on Windows")
}
const ( const (
seedPastPod0Infra = 1000 seedPastPod0Infra = 1000
seedPastPod0Container0 = 2000 seedPastPod0Container0 = 2000
@ -98,7 +93,16 @@ func TestFilterTerminatedContainerInfoAndAssembleByPodCgroupKey(t *testing.T) {
t.Errorf("%q is expected to be in the output\n", c) t.Errorf("%q is expected to be in the output\n", c)
} }
} }
for _, c := range []string{"pod0-i-terminated-1", "pod0-c0-terminated-1", "pod0-i-terminated-2", "pod0-c0-terminated-2", "pod0-i", "pod0-c0", "c1"} {
expectedInfoKeys := []string{"pod0-i-terminated-1", "pod0-c0-terminated-1", "pod0-i-terminated-2", "pod0-c0-terminated-2", "pod0-i", "pod0-c0"}
// NOTE: on Windows, IsSystemdStyleName will return false, which means that the Container Info will
// not be assembled by cgroup key.
if runtime.GOOS != "windows" {
expectedInfoKeys = append(expectedInfoKeys, "c1")
} else {
expectedInfoKeys = append(expectedInfoKeys, "pod1-c1.slice")
}
for _, c := range expectedInfoKeys {
if _, found := allInfos[c]; !found { if _, found := allInfos[c]; !found {
t.Errorf("%q is expected to be in the output\n", c) t.Errorf("%q is expected to be in the output\n", c)
} }