fix test cases for cpu shares
This commit is contained in:
parent
4c87589300
commit
0b6d27002f
@ -142,9 +142,9 @@ func (m *kubeGenericRuntimeManager) calculateLinuxResources(cpuRequest, cpuLimit
|
|||||||
// If request is not specified, but limit is, we want request to default to limit.
|
// If request is not specified, but limit is, we want request to default to limit.
|
||||||
// API server does this for new containers, but we repeat this logic in Kubelet
|
// API server does this for new containers, but we repeat this logic in Kubelet
|
||||||
// for containers running on existing Kubernetes clusters.
|
// for containers running on existing Kubernetes clusters.
|
||||||
if cpuRequest == nil && !cpuLimit.IsZero() {
|
if cpuRequest == nil {
|
||||||
cpuShares = int64(cm.MilliCPUToShares(cpuLimit.MilliValue()))
|
cpuShares = int64(cm.MilliCPUToShares(cpuLimit.MilliValue()))
|
||||||
} else if cpuRequest != nil {
|
} else {
|
||||||
// if cpuRequest.Amount is nil, then MilliCPUToShares will return the minimal number
|
// if cpuRequest.Amount is nil, then MilliCPUToShares will return the minimal number
|
||||||
// of CPU shares.
|
// of CPU shares.
|
||||||
cpuShares = int64(cm.MilliCPUToShares(cpuRequest.MilliValue()))
|
cpuShares = int64(cm.MilliCPUToShares(cpuRequest.MilliValue()))
|
||||||
|
@ -229,18 +229,23 @@ func TestCalculateLinuxResources(t *testing.T) {
|
|||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
generateResourceQuantity := func(str string) *resource.Quantity {
|
||||||
|
quantity := resource.MustParse(str)
|
||||||
|
return &quantity
|
||||||
|
}
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
cpuReq resource.Quantity
|
cpuReq *resource.Quantity
|
||||||
cpuLim resource.Quantity
|
cpuLim *resource.Quantity
|
||||||
memLim resource.Quantity
|
memLim *resource.Quantity
|
||||||
expected *runtimeapi.LinuxContainerResources
|
expected *runtimeapi.LinuxContainerResources
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Request128MBLimit256MB",
|
name: "Request128MBLimit256MB",
|
||||||
cpuReq: resource.MustParse("1"),
|
cpuReq: generateResourceQuantity("1"),
|
||||||
cpuLim: resource.MustParse("2"),
|
cpuLim: generateResourceQuantity("2"),
|
||||||
memLim: resource.MustParse("128Mi"),
|
memLim: generateResourceQuantity("128Mi"),
|
||||||
expected: &runtimeapi.LinuxContainerResources{
|
expected: &runtimeapi.LinuxContainerResources{
|
||||||
CpuPeriod: 100000,
|
CpuPeriod: 100000,
|
||||||
CpuQuota: 200000,
|
CpuQuota: 200000,
|
||||||
@ -250,9 +255,9 @@ func TestCalculateLinuxResources(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "RequestNoMemory",
|
name: "RequestNoMemory",
|
||||||
cpuReq: resource.MustParse("2"),
|
cpuReq: generateResourceQuantity("2"),
|
||||||
cpuLim: resource.MustParse("8"),
|
cpuLim: generateResourceQuantity("8"),
|
||||||
memLim: resource.MustParse("0"),
|
memLim: generateResourceQuantity("0"),
|
||||||
expected: &runtimeapi.LinuxContainerResources{
|
expected: &runtimeapi.LinuxContainerResources{
|
||||||
CpuPeriod: 100000,
|
CpuPeriod: 100000,
|
||||||
CpuQuota: 800000,
|
CpuQuota: 800000,
|
||||||
@ -262,8 +267,8 @@ func TestCalculateLinuxResources(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "RequestNilCPU",
|
name: "RequestNilCPU",
|
||||||
cpuLim: resource.MustParse("2"),
|
cpuLim: generateResourceQuantity("2"),
|
||||||
memLim: resource.MustParse("0"),
|
memLim: generateResourceQuantity("0"),
|
||||||
expected: &runtimeapi.LinuxContainerResources{
|
expected: &runtimeapi.LinuxContainerResources{
|
||||||
CpuPeriod: 100000,
|
CpuPeriod: 100000,
|
||||||
CpuQuota: 200000,
|
CpuQuota: 200000,
|
||||||
@ -273,9 +278,9 @@ func TestCalculateLinuxResources(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "RequestZeroCPU",
|
name: "RequestZeroCPU",
|
||||||
cpuReq: resource.MustParse("0"),
|
cpuReq: generateResourceQuantity("0"),
|
||||||
cpuLim: resource.MustParse("2"),
|
cpuLim: generateResourceQuantity("2"),
|
||||||
memLim: resource.MustParse("0"),
|
memLim: generateResourceQuantity("0"),
|
||||||
expected: &runtimeapi.LinuxContainerResources{
|
expected: &runtimeapi.LinuxContainerResources{
|
||||||
CpuPeriod: 100000,
|
CpuPeriod: 100000,
|
||||||
CpuQuota: 200000,
|
CpuQuota: 200000,
|
||||||
@ -285,7 +290,7 @@ func TestCalculateLinuxResources(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
linuxContainerResources := m.calculateLinuxResources(&test.cpuReq, &test.cpuLim, &test.memLim)
|
linuxContainerResources := m.calculateLinuxResources(test.cpuReq, test.cpuLim, test.memLim)
|
||||||
assert.Equal(t, test.expected, linuxContainerResources)
|
assert.Equal(t, test.expected, linuxContainerResources)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user