eviction_test.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package eviction
  15. import (
  16. "encoding/json"
  17. "testing"
  18. "github.com/stretchr/testify/assert"
  19. )
  20. func TestNewConfig(t *testing.T) {
  21. type tCase struct {
  22. name string
  23. input string
  24. assertFunc func(*testing.T, Config) error
  25. }
  26. cases := []tCase{
  27. {
  28. name: "Should has default config with empty input",
  29. input: ``,
  30. assertFunc: func(t *testing.T, config Config) error {
  31. memBytes, _ := config.GetHard().GetMemoryAvailable().Value.Quantity.AsInt64()
  32. assert.Equal(t, int64(1024*1024*100), memBytes)
  33. assert.Equal(t, float32(0.1), config.GetHard().GetNodeFsAvailable().Value.Percentage)
  34. assert.Equal(t, float32(0.05), config.GetHard().GetNodeFsInodesFree().Value.Percentage)
  35. assert.Equal(t, float32(0.15), config.GetHard().GetImageFsAvailable().Value.Percentage)
  36. return nil
  37. },
  38. },
  39. {
  40. name: "With all config",
  41. input: `
  42. evictionHard:
  43. imagefs.available: 25%
  44. memory.available: 1024Mi
  45. nodefs.available: 15%
  46. nodefs.inodesFree: 10%`,
  47. assertFunc: func(t *testing.T, config Config) error {
  48. memBytes, _ := config.GetHard().GetMemoryAvailable().Value.Quantity.AsInt64()
  49. assert.Equal(t, int64(1024*1024*1024), memBytes)
  50. assert.Equal(t, float32(0.25), config.GetHard().GetImageFsAvailable().Value.Percentage)
  51. assert.Equal(t, float32(0.15), config.GetHard().GetNodeFsAvailable().Value.Percentage)
  52. assert.Equal(t, float32(0.1), config.GetHard().GetNodeFsInodesFree().Value.Percentage)
  53. return nil
  54. },
  55. },
  56. {
  57. name: "Mixed config with default",
  58. input: `
  59. evictionHard:
  60. imagefs.available: 5%
  61. nodefs.inodesFree: 10%`,
  62. assertFunc: func(t *testing.T, config Config) error {
  63. memBytes, _ := config.GetHard().GetMemoryAvailable().Value.Quantity.AsInt64()
  64. assert.Equal(t, int64(1024*1024*100), memBytes)
  65. assert.Equal(t, float32(0.05), config.GetHard().GetImageFsAvailable().Value.Percentage)
  66. assert.Equal(t, float32(0.1), config.GetHard().GetNodeFsAvailable().Value.Percentage)
  67. assert.Equal(t, float32(0.1), config.GetHard().GetNodeFsInodesFree().Value.Percentage)
  68. return nil
  69. },
  70. },
  71. }
  72. for _, tc := range cases {
  73. t.Run(tc.name, func(st *testing.T) {
  74. if config, err := NewConfig([]byte(tc.input)); err != nil {
  75. st.Errorf("[%s] NewConfig error: %v", tc.name, err)
  76. } else {
  77. str, _ := json.MarshalIndent(config, "", " ")
  78. st.Logf("%s", str)
  79. if err := tc.assertFunc(st, config); err != nil {
  80. st.Errorf("[%s] assert error: %v", tc.name, err)
  81. }
  82. }
  83. })
  84. }
  85. }