util_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. Copyright 2018 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 nodeinfo
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/util/sets"
  20. )
  21. const mb int64 = 1024 * 1024
  22. func TestGetNodeImageStates(t *testing.T) {
  23. tests := []struct {
  24. node *v1.Node
  25. imageExistenceMap map[string]sets.String
  26. expected map[string]*ImageStateSummary
  27. }{
  28. {
  29. node: &v1.Node{
  30. ObjectMeta: metav1.ObjectMeta{Name: "node-0"},
  31. Status: v1.NodeStatus{
  32. Images: []v1.ContainerImage{
  33. {
  34. Names: []string{
  35. "gcr.io/10:v1",
  36. },
  37. SizeBytes: int64(10 * mb),
  38. },
  39. {
  40. Names: []string{
  41. "gcr.io/200:v1",
  42. },
  43. SizeBytes: int64(200 * mb),
  44. },
  45. },
  46. },
  47. },
  48. imageExistenceMap: map[string]sets.String{
  49. "gcr.io/10:v1": sets.NewString("node-0", "node-1"),
  50. "gcr.io/200:v1": sets.NewString("node-0"),
  51. },
  52. expected: map[string]*ImageStateSummary{
  53. "gcr.io/10:v1": {
  54. Size: int64(10 * mb),
  55. NumNodes: 2,
  56. },
  57. "gcr.io/200:v1": {
  58. Size: int64(200 * mb),
  59. NumNodes: 1,
  60. },
  61. },
  62. },
  63. }
  64. for _, test := range tests {
  65. imageStates := getNodeImageStates(test.node, test.imageExistenceMap)
  66. if !reflect.DeepEqual(test.expected, imageStates) {
  67. t.Errorf("expected: %#v, got: %#v", test.expected, imageStates)
  68. }
  69. }
  70. }
  71. func TestCreateImageExistenceMap(t *testing.T) {
  72. tests := []struct {
  73. nodes []*v1.Node
  74. expected map[string]sets.String
  75. }{
  76. {
  77. nodes: []*v1.Node{
  78. {
  79. ObjectMeta: metav1.ObjectMeta{Name: "node-0"},
  80. Status: v1.NodeStatus{
  81. Images: []v1.ContainerImage{
  82. {
  83. Names: []string{
  84. "gcr.io/10:v1",
  85. },
  86. SizeBytes: int64(10 * mb),
  87. },
  88. },
  89. },
  90. },
  91. {
  92. ObjectMeta: metav1.ObjectMeta{Name: "node-1"},
  93. Status: v1.NodeStatus{
  94. Images: []v1.ContainerImage{
  95. {
  96. Names: []string{
  97. "gcr.io/10:v1",
  98. },
  99. SizeBytes: int64(10 * mb),
  100. },
  101. {
  102. Names: []string{
  103. "gcr.io/200:v1",
  104. },
  105. SizeBytes: int64(200 * mb),
  106. },
  107. },
  108. },
  109. },
  110. },
  111. expected: map[string]sets.String{
  112. "gcr.io/10:v1": sets.NewString("node-0", "node-1"),
  113. "gcr.io/200:v1": sets.NewString("node-1"),
  114. },
  115. },
  116. }
  117. for _, test := range tests {
  118. imageMap := createImageExistenceMap(test.nodes)
  119. if !reflect.DeepEqual(test.expected, imageMap) {
  120. t.Errorf("expected: %#v, got: %#v", test.expected, imageMap)
  121. }
  122. }
  123. }