kubelet_resources_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package kubelet
  14. import (
  15. "testing"
  16. "github.com/stretchr/testify/assert"
  17. cadvisorapi "github.com/google/cadvisor/info/v1"
  18. cadvisorapiv2 "github.com/google/cadvisor/info/v2"
  19. "k8s.io/kubernetes/pkg/api"
  20. "k8s.io/kubernetes/pkg/api/resource"
  21. kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
  22. )
  23. func TestPodResourceLimitsDefaulting(t *testing.T) {
  24. cpuCores := resource.MustParse("10")
  25. memoryCapacity := resource.MustParse("10Gi")
  26. tk := newTestKubelet(t, true)
  27. tk.fakeCadvisor.On("VersionInfo").Return(&cadvisorapi.VersionInfo{}, nil)
  28. tk.fakeCadvisor.On("MachineInfo").Return(&cadvisorapi.MachineInfo{
  29. NumCores: int(cpuCores.Value()),
  30. MemoryCapacity: uint64(memoryCapacity.Value()),
  31. }, nil)
  32. tk.fakeCadvisor.On("ImagesFsInfo").Return(cadvisorapiv2.FsInfo{}, nil)
  33. tk.fakeCadvisor.On("RootFsInfo").Return(cadvisorapiv2.FsInfo{}, nil)
  34. tk.kubelet.reservation = kubetypes.Reservation{
  35. Kubernetes: api.ResourceList{
  36. api.ResourceCPU: resource.MustParse("3"),
  37. api.ResourceMemory: resource.MustParse("4Gi"),
  38. },
  39. System: api.ResourceList{
  40. api.ResourceCPU: resource.MustParse("1"),
  41. api.ResourceMemory: resource.MustParse("2Gi"),
  42. },
  43. }
  44. cases := []struct {
  45. pod *api.Pod
  46. expected *api.Pod
  47. }{
  48. {
  49. pod: getPod("0", "0"),
  50. expected: getPod("6", "4Gi"),
  51. },
  52. {
  53. pod: getPod("1", "0"),
  54. expected: getPod("1", "4Gi"),
  55. },
  56. {
  57. pod: getPod("", ""),
  58. expected: getPod("6", "4Gi"),
  59. },
  60. {
  61. pod: getPod("0", "1Mi"),
  62. expected: getPod("6", "1Mi"),
  63. },
  64. }
  65. as := assert.New(t)
  66. for idx, tc := range cases {
  67. actual, _, err := tk.kubelet.defaultPodLimitsForDownwardApi(tc.pod, nil)
  68. as.Nil(err, "failed to default pod limits: %v", err)
  69. if !api.Semantic.DeepEqual(tc.expected, actual) {
  70. as.Fail("test case [%d] failed. Expected: %+v, Got: %+v", idx, tc.expected, actual)
  71. }
  72. }
  73. }
  74. func getPod(cpuLimit, memoryLimit string) *api.Pod {
  75. resources := api.ResourceRequirements{}
  76. if cpuLimit != "" || memoryLimit != "" {
  77. resources.Limits = make(api.ResourceList)
  78. }
  79. if cpuLimit != "" {
  80. resources.Limits[api.ResourceCPU] = resource.MustParse(cpuLimit)
  81. }
  82. if memoryLimit != "" {
  83. resources.Limits[api.ResourceMemory] = resource.MustParse(memoryLimit)
  84. }
  85. return &api.Pod{
  86. Spec: api.PodSpec{
  87. Containers: []api.Container{
  88. {
  89. Name: "foo",
  90. Resources: resources,
  91. },
  92. },
  93. },
  94. }
  95. }