cgroup_manager_linux_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. "path"
  17. "reflect"
  18. "testing"
  19. )
  20. // TestNewCgroupName tests confirms that #68416 is fixed
  21. func TestNewCgroupName(t *testing.T) {
  22. a := ParseCgroupfsToCgroupName("/a/")
  23. ab := NewCgroupName(a, "b")
  24. expectedAB := CgroupName([]string{"a", "", "b"})
  25. if !reflect.DeepEqual(ab, expectedAB) {
  26. t.Errorf("Expected %d%+v; got %d%+v", len(expectedAB), expectedAB, len(ab), ab)
  27. }
  28. abc := NewCgroupName(ab, "c")
  29. expectedABC := CgroupName([]string{"a", "", "b", "c"})
  30. if !reflect.DeepEqual(abc, expectedABC) {
  31. t.Errorf("Expected %d%+v; got %d%+v", len(expectedABC), expectedABC, len(abc), abc)
  32. }
  33. _ = NewCgroupName(ab, "d")
  34. if !reflect.DeepEqual(abc, expectedABC) {
  35. t.Errorf("Expected %d%+v; got %d%+v", len(expectedABC), expectedABC, len(abc), abc)
  36. }
  37. }
  38. func TestCgroupNameToSystemdBasename(t *testing.T) {
  39. testCases := []struct {
  40. input CgroupName
  41. expected string
  42. }{
  43. {
  44. input: RootCgroupName,
  45. expected: "/",
  46. },
  47. {
  48. input: NewCgroupName(RootCgroupName, "system"),
  49. expected: "system.slice",
  50. },
  51. {
  52. input: NewCgroupName(RootCgroupName, "system", "Burstable"),
  53. expected: "system-Burstable.slice",
  54. },
  55. {
  56. input: NewCgroupName(RootCgroupName, "Burstable", "pod-123"),
  57. expected: "Burstable-pod_123.slice",
  58. },
  59. {
  60. input: NewCgroupName(RootCgroupName, "test", "a", "b"),
  61. expected: "test-a-b.slice",
  62. },
  63. {
  64. input: NewCgroupName(RootCgroupName, "test", "a", "b", "Burstable"),
  65. expected: "test-a-b-Burstable.slice",
  66. },
  67. {
  68. input: NewCgroupName(RootCgroupName, "Burstable"),
  69. expected: "Burstable.slice",
  70. },
  71. {
  72. input: NewCgroupName(RootCgroupName, "BestEffort", "pod-6c1a4e95-6bb6-11e6-bc26-28d2444e470d"),
  73. expected: "BestEffort-pod_6c1a4e95_6bb6_11e6_bc26_28d2444e470d.slice",
  74. },
  75. }
  76. for _, testCase := range testCases {
  77. if actual := path.Base(testCase.input.ToSystemd()); actual != testCase.expected {
  78. t.Errorf("Unexpected result, input: %v, expected: %v, actual: %v", testCase.input, testCase.expected, actual)
  79. }
  80. }
  81. }
  82. func TestCgroupNameToSystemd(t *testing.T) {
  83. testCases := []struct {
  84. input CgroupName
  85. expected string
  86. }{
  87. {
  88. input: RootCgroupName,
  89. expected: "/",
  90. },
  91. {
  92. input: NewCgroupName(RootCgroupName, "Burstable"),
  93. expected: "/Burstable.slice",
  94. },
  95. {
  96. input: NewCgroupName(RootCgroupName, "Burstable", "pod-123"),
  97. expected: "/Burstable.slice/Burstable-pod_123.slice",
  98. },
  99. {
  100. input: NewCgroupName(RootCgroupName, "BestEffort", "pod-6c1a4e95-6bb6-11e6-bc26-28d2444e470d"),
  101. expected: "/BestEffort.slice/BestEffort-pod_6c1a4e95_6bb6_11e6_bc26_28d2444e470d.slice",
  102. },
  103. {
  104. input: NewCgroupName(RootCgroupName, "kubepods"),
  105. expected: "/kubepods.slice",
  106. },
  107. }
  108. for _, testCase := range testCases {
  109. if actual := testCase.input.ToSystemd(); actual != testCase.expected {
  110. t.Errorf("Unexpected result, input: %v, expected: %v, actual: %v", testCase.input, testCase.expected, actual)
  111. }
  112. }
  113. }
  114. func TestCgroupNameToCgroupfs(t *testing.T) {
  115. testCases := []struct {
  116. input CgroupName
  117. expected string
  118. }{
  119. {
  120. input: RootCgroupName,
  121. expected: "/",
  122. },
  123. {
  124. input: NewCgroupName(RootCgroupName, "Burstable"),
  125. expected: "/Burstable",
  126. },
  127. }
  128. for _, testCase := range testCases {
  129. if actual := testCase.input.ToCgroupfs(); actual != testCase.expected {
  130. t.Errorf("Unexpected result, input: %v, expected: %v, actual: %v", testCase.input, testCase.expected, actual)
  131. }
  132. }
  133. }