config_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. _ "embed"
  16. "reflect"
  17. "testing"
  18. "time"
  19. "github.com/stretchr/testify/assert"
  20. )
  21. func TestValidationTestConfig_YAML(t *testing.T) {
  22. emptyValidationTestConfig := &ValidationTestConfig{}
  23. type test struct {
  24. name string
  25. config *ValidationTestConfig
  26. }
  27. tests := []test{
  28. {"empty config", emptyValidationTestConfig},
  29. {"default config", NewDefaultValidationTestConfig()},
  30. {"full config", &ValidationTestConfig{
  31. Namespace: "my-rook",
  32. PublicNetwork: "my-pub",
  33. ClusterNetwork: "my-priv",
  34. ResourceTimeout: 2 * time.Minute,
  35. FlakyThreshold: 30 * time.Second,
  36. NginxImage: "myorg/nginx:latest",
  37. NodeTypes: map[string]NodeConfig{
  38. "osdOnlyNodes": {
  39. OSDsPerNode: 9,
  40. OtherDaemonsPerNode: 0,
  41. Placement: PlacementConfig{
  42. NodeSelector: map[string]string{
  43. "osd-node": "true",
  44. "storage-node": "true",
  45. },
  46. Tolerations: []TolerationType{
  47. {Key: "storage-node", Operator: "Exists", Effect: "NoSchedule"},
  48. {Key: "osd-node", Operator: "Exists", Effect: "NoSchedule"},
  49. },
  50. },
  51. },
  52. "generalStorageNodes": {
  53. OSDsPerNode: 3,
  54. OtherDaemonsPerNode: 16,
  55. Placement: PlacementConfig{
  56. NodeSelector: map[string]string{
  57. "storage-node": "true",
  58. },
  59. Tolerations: []TolerationType{
  60. {Key: "storage-node", Operator: "Exists", Effect: "NoSchedule"},
  61. },
  62. },
  63. },
  64. "workerNodes": {
  65. OSDsPerNode: 0,
  66. OtherDaemonsPerNode: 6,
  67. Placement: PlacementConfig{
  68. NodeSelector: map[string]string{
  69. "worker-node": "true",
  70. },
  71. Tolerations: []TolerationType{
  72. {Key: "special-worker", Operator: "Exists"},
  73. },
  74. },
  75. },
  76. },
  77. }},
  78. }
  79. for _, tt := range tests {
  80. t.Run(tt.name, func(t *testing.T) {
  81. y, err := tt.config.ToYAML()
  82. assert.NoError(t, err)
  83. // basic test to ensure config yamls have comments
  84. assert.Contains(t, y, "# The intended namespace where the validation test will be run.")
  85. c2, err := ValidationTestConfigFromYAML(y)
  86. assert.NoError(t, err)
  87. // config survives round trip to and from yaml
  88. assert.Equal(t, tt.config, c2)
  89. })
  90. }
  91. }
  92. func TestValidationTestConfig_BestNodePlacementForServer(t *testing.T) {
  93. convergedType := NodeConfig{
  94. OSDsPerNode: 3,
  95. OtherDaemonsPerNode: 10,
  96. Placement: PlacementConfig{
  97. NodeSelector: map[string]string{"converged": "type"},
  98. },
  99. }
  100. osdType := NodeConfig{
  101. OSDsPerNode: 3,
  102. OtherDaemonsPerNode: 0,
  103. Placement: PlacementConfig{
  104. NodeSelector: map[string]string{"osd": "type"},
  105. },
  106. }
  107. workerType := NodeConfig{
  108. OSDsPerNode: 0,
  109. OtherDaemonsPerNode: 4,
  110. Placement: PlacementConfig{
  111. NodeSelector: map[string]string{"worker": "type"},
  112. },
  113. }
  114. tests := []struct {
  115. name string
  116. NodeTypes map[string]NodeConfig
  117. want PlacementConfig
  118. wantErr bool
  119. }{
  120. {"empty", map[string]NodeConfig{}, PlacementConfig{}, true},
  121. {"converged", map[string]NodeConfig{"converged": convergedType}, convergedType.Placement, false},
  122. {"worker", map[string]NodeConfig{"worker": workerType}, PlacementConfig{}, true},
  123. {"worker and osd", map[string]NodeConfig{
  124. "worker": workerType,
  125. "osd": osdType,
  126. }, osdType.Placement, false},
  127. {"converged and osd", map[string]NodeConfig{
  128. "converged": convergedType,
  129. "osd": osdType,
  130. }, convergedType.Placement, false},
  131. }
  132. for _, tt := range tests {
  133. t.Run(tt.name, func(t *testing.T) {
  134. c := &ValidationTestConfig{
  135. NodeTypes: tt.NodeTypes,
  136. }
  137. got, err := c.BestNodePlacementForServer()
  138. if (err != nil) != tt.wantErr {
  139. t.Errorf("ValidationTestConfig.BestNodePlacementForServer() error = %v, wantErr %v", err, tt.wantErr)
  140. return
  141. }
  142. if !reflect.DeepEqual(got, tt.want) {
  143. t.Errorf("ValidationTestConfig.BestNodePlacementForServer() = %v, want %v", got, tt.want)
  144. }
  145. })
  146. }
  147. }