resources_test.go 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Copyright 2023 The Rook Authors. All rights reserved.
  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 multus
  14. import (
  15. "testing"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. func Test_perNodeTypeCount_Increment(t *testing.T) {
  19. tests := []struct {
  20. name string
  21. a *perNodeTypeCount
  22. want *perNodeTypeCount
  23. }{
  24. {"empty", &perNodeTypeCount{}, &perNodeTypeCount{"type": 1}},
  25. {"type already set", &perNodeTypeCount{"type": 2}, &perNodeTypeCount{"type": 3}},
  26. {"other already set", &perNodeTypeCount{"other": 3}, &perNodeTypeCount{"other": 3, "type": 1}},
  27. {"type and other already set", &perNodeTypeCount{"type": 1, "other": 3}, &perNodeTypeCount{"type": 2, "other": 3}},
  28. }
  29. for _, tt := range tests {
  30. t.Run(tt.name, func(t *testing.T) {
  31. tt.a.Increment("type")
  32. assert.Equal(t, *tt.want, *tt.a)
  33. })
  34. }
  35. }
  36. func Test_perNodeTypeCount_Equal(t *testing.T) {
  37. tests := []struct {
  38. name string
  39. a *perNodeTypeCount
  40. b *perNodeTypeCount
  41. want bool
  42. }{
  43. {"empty vs empty", &perNodeTypeCount{}, &perNodeTypeCount{}, true},
  44. {"shared:1 vs empty", &perNodeTypeCount{"shared": 1}, &perNodeTypeCount{}, false},
  45. {"empty vs shared:1", &perNodeTypeCount{}, &perNodeTypeCount{"shared": 1}, false},
  46. {"shared:0 vs shared:0", &perNodeTypeCount{"shared": 0}, &perNodeTypeCount{"shared": 0}, true},
  47. {"shared:1 vs shared:0", &perNodeTypeCount{"shared": 1}, &perNodeTypeCount{"shared": 0}, false},
  48. {"shared:0 vs shared:1", &perNodeTypeCount{"shared": 0}, &perNodeTypeCount{"shared": 1}, false},
  49. {"shared:1 vs shared:1", &perNodeTypeCount{"shared": 1}, &perNodeTypeCount{"shared": 1}, true},
  50. {"shared:1 vs shared:2", &perNodeTypeCount{"shared": 1}, &perNodeTypeCount{"shared": 2}, false},
  51. {"shared:1 vs other:1", &perNodeTypeCount{"shared": 1}, &perNodeTypeCount{"other": 1}, false},
  52. {"shared:1 vs other:0", &perNodeTypeCount{"shared": 1}, &perNodeTypeCount{"other": 0}, false},
  53. {"shared:1,other:2 vs shared:1,other:2", &perNodeTypeCount{"shared": 1, "other": 2}, &perNodeTypeCount{"shared": 1, "other": 2}, true},
  54. {"shared:1,other:2 vs shared:2,other:2", &perNodeTypeCount{"shared": 1, "other": 2}, &perNodeTypeCount{"shared": 2, "other": 2}, false},
  55. {"shared:1,other:2 vs shared:2,other:1", &perNodeTypeCount{"shared": 1, "other": 2}, &perNodeTypeCount{"shared": 2, "other": 1}, false},
  56. {"shared:1,other:2 vs shared:1,other:1", &perNodeTypeCount{"shared": 1, "other": 2}, &perNodeTypeCount{"shared": 1, "other": 1}, false},
  57. }
  58. for _, tt := range tests {
  59. t.Run(tt.name, func(t *testing.T) {
  60. if got := tt.a.Equal(tt.b); got != tt.want {
  61. t.Errorf("perNodeTypeCount.Equal() = %v, want %v", got, tt.want)
  62. }
  63. })
  64. }
  65. }
  66. func Test_perNodeTypeCount_Total(t *testing.T) {
  67. tests := []struct {
  68. name string
  69. a *perNodeTypeCount
  70. want int
  71. }{
  72. {"empty", &perNodeTypeCount{}, 0},
  73. {"sample:1", &perNodeTypeCount{"sample": 1}, 1},
  74. {"sample:1, other:2", &perNodeTypeCount{"sample": 1, "other": 2}, 3},
  75. {"sample:0", &perNodeTypeCount{"sample": 0}, 0},
  76. {"sample:0, other:2", &perNodeTypeCount{"sample": 0, "other": 2}, 2},
  77. }
  78. for _, tt := range tests {
  79. t.Run(tt.name, func(t *testing.T) {
  80. if got := tt.a.Total(); got != tt.want {
  81. t.Errorf("perNodeTypeCount.Total() = %v, want %v", got, tt.want)
  82. }
  83. })
  84. }
  85. }