quota_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Copyright 2016 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 versioned
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. )
  20. func TestQuotaGenerate(t *testing.T) {
  21. hard := "cpu=10,memory=5G,pods=10,services=7"
  22. resourceQuotaSpecList, err := populateResourceListV1(hard)
  23. if err != nil {
  24. t.Errorf("unexpected error: %v", err)
  25. }
  26. tests := []struct {
  27. name string
  28. params map[string]interface{}
  29. expected *v1.ResourceQuota
  30. expectErr bool
  31. }{
  32. {
  33. name: "test-valid-use",
  34. params: map[string]interface{}{
  35. "name": "foo",
  36. "hard": hard,
  37. },
  38. expected: &v1.ResourceQuota{
  39. ObjectMeta: metav1.ObjectMeta{
  40. Name: "foo",
  41. },
  42. Spec: v1.ResourceQuotaSpec{Hard: resourceQuotaSpecList},
  43. },
  44. expectErr: false,
  45. },
  46. {
  47. name: "test-missing-required-param",
  48. params: map[string]interface{}{
  49. "name": "foo",
  50. },
  51. expectErr: true,
  52. },
  53. {
  54. name: "test-valid-scopes",
  55. params: map[string]interface{}{
  56. "name": "foo",
  57. "hard": hard,
  58. "scopes": "BestEffort,NotTerminating",
  59. },
  60. expected: &v1.ResourceQuota{
  61. ObjectMeta: metav1.ObjectMeta{
  62. Name: "foo",
  63. },
  64. Spec: v1.ResourceQuotaSpec{
  65. Hard: resourceQuotaSpecList,
  66. Scopes: []v1.ResourceQuotaScope{
  67. v1.ResourceQuotaScopeBestEffort,
  68. v1.ResourceQuotaScopeNotTerminating,
  69. },
  70. },
  71. },
  72. expectErr: false,
  73. },
  74. {
  75. name: "test-empty-scopes",
  76. params: map[string]interface{}{
  77. "name": "foo",
  78. "hard": hard,
  79. "scopes": "",
  80. },
  81. expected: &v1.ResourceQuota{
  82. ObjectMeta: metav1.ObjectMeta{
  83. Name: "foo",
  84. },
  85. Spec: v1.ResourceQuotaSpec{Hard: resourceQuotaSpecList},
  86. },
  87. expectErr: false,
  88. },
  89. {
  90. name: "test-invalid-scopes",
  91. params: map[string]interface{}{
  92. "name": "foo",
  93. "hard": hard,
  94. "scopes": "abc,",
  95. },
  96. expectErr: true,
  97. },
  98. }
  99. generator := ResourceQuotaGeneratorV1{}
  100. for _, tt := range tests {
  101. t.Run(tt.name, func(t *testing.T) {
  102. obj, err := generator.Generate(tt.params)
  103. if !tt.expectErr && err != nil {
  104. t.Errorf("%s: unexpected error: %v", tt.name, err)
  105. }
  106. if tt.expectErr && err != nil {
  107. return
  108. }
  109. if !reflect.DeepEqual(obj.(*v1.ResourceQuota), tt.expected) {
  110. t.Errorf("%s:\nexpected:\n%#v\nsaw:\n%#v", tt.name, tt.expected, obj.(*v1.ResourceQuota))
  111. }
  112. })
  113. }
  114. }