kubernetes/pkg/kubelet/kuberuntime/kuberuntime_container_windows_test.go
Mark Rossetti 498d065cc5
Promoting WindowsHostProcessContainers to stable
Signed-off-by: Mark Rossetti <marosset@microsoft.com>
2022-11-01 14:06:25 -07:00

104 lines
3.0 KiB
Go

//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 kuberuntime
import (
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilfeature "k8s.io/apiserver/pkg/util/feature"
featuregatetesting "k8s.io/component-base/featuregate/testing"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
"k8s.io/kubernetes/pkg/features"
)
func TestApplyPlatformSpecificContainerConfig(t *testing.T) {
_, _, fakeRuntimeSvc, err := createTestRuntimeManager()
require.NoError(t, err)
containerConfig := &runtimeapi.ContainerConfig{}
resources := v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceMemory: resource.MustParse("128Mi"),
v1.ResourceCPU: resource.MustParse("1"),
},
Limits: v1.ResourceList{
v1.ResourceMemory: resource.MustParse("256Mi"),
v1.ResourceCPU: resource.MustParse("3"),
},
}
gmsaCredSpecName := "gmsa spec name"
gmsaCredSpec := "credential spec"
username := "ContainerAdministrator"
asHostProcess := true
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
UID: "12345678",
Name: "bar",
Namespace: "new",
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "foo",
Image: "busybox",
ImagePullPolicy: v1.PullIfNotPresent,
Command: []string{"testCommand"},
WorkingDir: "testWorkingDir",
Resources: resources,
SecurityContext: &v1.SecurityContext{
WindowsOptions: &v1.WindowsSecurityContextOptions{
GMSACredentialSpecName: &gmsaCredSpecName,
GMSACredentialSpec: &gmsaCredSpec,
RunAsUserName: &username,
HostProcess: &asHostProcess,
},
},
},
},
},
}
err = fakeRuntimeSvc.applyPlatformSpecificContainerConfig(containerConfig, &pod.Spec.Containers[0], pod, new(int64), "foo", nil)
require.NoError(t, err)
expectedCpuMax := ((10000 * 3000) / int64(runtime.NumCPU()) / 1000)
expectedWindowsConfig := &runtimeapi.WindowsContainerConfig{
Resources: &runtimeapi.WindowsContainerResources{
CpuMaximum: expectedCpuMax,
MemoryLimitInBytes: 256 * 1024 * 1024,
},
SecurityContext: &runtimeapi.WindowsContainerSecurityContext{
CredentialSpec: gmsaCredSpec,
RunAsUsername: "ContainerAdministrator",
HostProcess: true,
},
}
assert.Equal(t, expectedWindowsConfig, containerConfig.Windows)
}