fake_topology_manager_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. Copyright 2019 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 topologymanager
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/api/core/v1"
  18. "k8s.io/apimachinery/pkg/types"
  19. "k8s.io/kubernetes/pkg/kubelet/lifecycle"
  20. )
  21. func TestNewFakeManager(t *testing.T) {
  22. fm := NewFakeManager()
  23. if _, ok := fm.(Manager); !ok {
  24. t.Errorf("Result is not Manager type")
  25. }
  26. }
  27. func TestFakeGetAffinity(t *testing.T) {
  28. tcases := []struct {
  29. name string
  30. containerName string
  31. podUID string
  32. expected TopologyHint
  33. }{
  34. {
  35. name: "Case1",
  36. containerName: "nginx",
  37. podUID: "0aafa4c4-38e8-11e9-bcb1-a4bf01040474",
  38. expected: TopologyHint{},
  39. },
  40. }
  41. for _, tc := range tcases {
  42. fm := fakeManager{}
  43. actual := fm.GetAffinity(tc.podUID, tc.containerName)
  44. if !reflect.DeepEqual(actual, tc.expected) {
  45. t.Errorf("Expected Affinity in result to be %v, got %v", tc.expected, actual)
  46. }
  47. }
  48. }
  49. func TestFakeAddContainer(t *testing.T) {
  50. testCases := []struct {
  51. name string
  52. containerID string
  53. podUID types.UID
  54. }{
  55. {
  56. name: "Case1",
  57. containerID: "nginx",
  58. podUID: "0aafa4c4-38e8-11e9-bcb1-a4bf01040474",
  59. },
  60. {
  61. name: "Case2",
  62. containerID: "Busy_Box",
  63. podUID: "b3ee37fc-39a5-11e9-bcb1-a4bf01040474",
  64. },
  65. }
  66. fm := fakeManager{}
  67. mngr := manager{}
  68. mngr.podMap = make(map[string]string)
  69. for _, tc := range testCases {
  70. pod := v1.Pod{}
  71. pod.UID = tc.podUID
  72. err := fm.AddContainer(&pod, tc.containerID)
  73. if err != nil {
  74. t.Errorf("Expected error to be nil but got: %v", err)
  75. }
  76. }
  77. }
  78. func TestFakeRemoveContainer(t *testing.T) {
  79. testCases := []struct {
  80. name string
  81. containerID string
  82. podUID string
  83. }{
  84. {
  85. name: "Case1",
  86. containerID: "nginx",
  87. podUID: "0aafa4c4-38e8-11e9-bcb1-a4bf01040474",
  88. },
  89. {
  90. name: "Case2",
  91. containerID: "Busy_Box",
  92. podUID: "b3ee37fc-39a5-11e9-bcb1-a4bf01040474",
  93. },
  94. }
  95. fm := fakeManager{}
  96. mngr := manager{}
  97. mngr.podMap = make(map[string]string)
  98. for _, tc := range testCases {
  99. err := fm.RemoveContainer(tc.containerID)
  100. if err != nil {
  101. t.Errorf("Expected error to be nil but got: %v", err)
  102. }
  103. }
  104. }
  105. func TestFakeAdmit(t *testing.T) {
  106. tcases := []struct {
  107. name string
  108. result lifecycle.PodAdmitResult
  109. qosClass v1.PodQOSClass
  110. expected bool
  111. }{
  112. {
  113. name: "QOSClass set as Guaranteed",
  114. result: lifecycle.PodAdmitResult{},
  115. qosClass: v1.PodQOSGuaranteed,
  116. expected: true,
  117. },
  118. {
  119. name: "QOSClass set as Burstable",
  120. result: lifecycle.PodAdmitResult{},
  121. qosClass: v1.PodQOSBurstable,
  122. expected: true,
  123. },
  124. {
  125. name: "QOSClass set as BestEffort",
  126. result: lifecycle.PodAdmitResult{},
  127. qosClass: v1.PodQOSBestEffort,
  128. expected: true,
  129. },
  130. }
  131. fm := fakeManager{}
  132. for _, tc := range tcases {
  133. mngr := manager{}
  134. mngr.podTopologyHints = make(map[string]map[string]TopologyHint)
  135. podAttr := lifecycle.PodAdmitAttributes{}
  136. pod := v1.Pod{}
  137. pod.Status.QOSClass = tc.qosClass
  138. podAttr.Pod = &pod
  139. actual := fm.Admit(&podAttr)
  140. if reflect.DeepEqual(actual, tc.result) {
  141. t.Errorf("Error occurred, expected Admit in result to be %v got %v", tc.result, actual.Admit)
  142. }
  143. }
  144. }