pod_container_manager_linux_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // +build linux
  2. /*
  3. Copyright 2016 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package cm
  15. import (
  16. "strings"
  17. "testing"
  18. "k8s.io/api/core/v1"
  19. "k8s.io/apimachinery/pkg/types"
  20. )
  21. func TestIsCgroupPod(t *testing.T) {
  22. qosContainersInfo := QOSContainersInfo{
  23. Guaranteed: RootCgroupName,
  24. Burstable: NewCgroupName(RootCgroupName, strings.ToLower(string(v1.PodQOSBurstable))),
  25. BestEffort: NewCgroupName(RootCgroupName, strings.ToLower(string(v1.PodQOSBestEffort))),
  26. }
  27. podUID := types.UID("123")
  28. testCases := []struct {
  29. input CgroupName
  30. expectedResult bool
  31. expectedUID types.UID
  32. }{
  33. {
  34. input: RootCgroupName,
  35. expectedResult: false,
  36. expectedUID: types.UID(""),
  37. },
  38. {
  39. input: NewCgroupName(qosContainersInfo.Guaranteed),
  40. expectedResult: false,
  41. expectedUID: types.UID(""),
  42. },
  43. {
  44. input: NewCgroupName(qosContainersInfo.Guaranteed, GetPodCgroupNameSuffix(podUID)),
  45. expectedResult: true,
  46. expectedUID: podUID,
  47. },
  48. {
  49. input: NewCgroupName(qosContainersInfo.Guaranteed, GetPodCgroupNameSuffix(podUID), "container.scope"),
  50. expectedResult: false,
  51. expectedUID: types.UID(""),
  52. },
  53. {
  54. input: NewCgroupName(qosContainersInfo.Burstable),
  55. expectedResult: false,
  56. expectedUID: types.UID(""),
  57. },
  58. {
  59. input: NewCgroupName(qosContainersInfo.Burstable, GetPodCgroupNameSuffix(podUID)),
  60. expectedResult: true,
  61. expectedUID: podUID,
  62. },
  63. {
  64. input: NewCgroupName(qosContainersInfo.Burstable, GetPodCgroupNameSuffix(podUID), "container.scope"),
  65. expectedResult: false,
  66. expectedUID: types.UID(""),
  67. },
  68. {
  69. input: NewCgroupName(qosContainersInfo.BestEffort),
  70. expectedResult: false,
  71. expectedUID: types.UID(""),
  72. },
  73. {
  74. input: NewCgroupName(qosContainersInfo.BestEffort, GetPodCgroupNameSuffix(podUID)),
  75. expectedResult: true,
  76. expectedUID: podUID,
  77. },
  78. {
  79. input: NewCgroupName(qosContainersInfo.BestEffort, GetPodCgroupNameSuffix(podUID), "container.scope"),
  80. expectedResult: false,
  81. expectedUID: types.UID(""),
  82. },
  83. {
  84. input: NewCgroupName(RootCgroupName, "system"),
  85. expectedResult: false,
  86. expectedUID: types.UID(""),
  87. },
  88. {
  89. input: NewCgroupName(RootCgroupName, "system", "kubelet"),
  90. expectedResult: false,
  91. expectedUID: types.UID(""),
  92. },
  93. }
  94. for _, cgroupDriver := range []string{"cgroupfs", "systemd"} {
  95. pcm := &podContainerManagerImpl{
  96. cgroupManager: NewCgroupManager(nil, cgroupDriver),
  97. enforceCPULimits: true,
  98. qosContainersInfo: qosContainersInfo,
  99. }
  100. for _, testCase := range testCases {
  101. // give the right cgroup structure based on driver
  102. cgroupfs := testCase.input.ToCgroupfs()
  103. if cgroupDriver == "systemd" {
  104. cgroupfs = testCase.input.ToSystemd()
  105. }
  106. // check if this is a pod or not with the literal cgroupfs input
  107. result, resultUID := pcm.IsPodCgroup(cgroupfs)
  108. if result != testCase.expectedResult {
  109. t.Errorf("Unexpected result for driver: %v, input: %v, expected: %v, actual: %v", cgroupDriver, testCase.input, testCase.expectedResult, result)
  110. }
  111. if resultUID != testCase.expectedUID {
  112. t.Errorf("Unexpected result for driver: %v, input: %v, expected: %v, actual: %v", cgroupDriver, testCase.input, testCase.expectedUID, resultUID)
  113. }
  114. }
  115. }
  116. }