config_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 imagepolicy
  14. import (
  15. "reflect"
  16. "testing"
  17. "time"
  18. )
  19. func TestConfigNormalization(t *testing.T) {
  20. tests := []struct {
  21. test string
  22. config imagePolicyWebhookConfig
  23. normalizedConfig imagePolicyWebhookConfig
  24. wantErr bool
  25. }{
  26. {
  27. test: "config within normal ranges",
  28. config: imagePolicyWebhookConfig{
  29. AllowTTL: ((minAllowTTL + maxAllowTTL) / 2) / time.Second,
  30. DenyTTL: ((minDenyTTL + maxDenyTTL) / 2) / time.Second,
  31. RetryBackoff: ((minRetryBackoff + maxRetryBackoff) / 2) / time.Millisecond,
  32. },
  33. normalizedConfig: imagePolicyWebhookConfig{
  34. AllowTTL: ((minAllowTTL + maxAllowTTL) / 2) / time.Second * time.Second,
  35. DenyTTL: ((minDenyTTL + maxDenyTTL) / 2) / time.Second * time.Second,
  36. RetryBackoff: (minRetryBackoff + maxRetryBackoff) / 2,
  37. },
  38. wantErr: false,
  39. },
  40. {
  41. test: "config below normal ranges, error",
  42. config: imagePolicyWebhookConfig{
  43. AllowTTL: minAllowTTL - time.Duration(1),
  44. DenyTTL: minDenyTTL - time.Duration(1),
  45. RetryBackoff: minRetryBackoff - time.Duration(1),
  46. },
  47. wantErr: true,
  48. },
  49. {
  50. test: "config above normal ranges, error",
  51. config: imagePolicyWebhookConfig{
  52. AllowTTL: time.Duration(1) + maxAllowTTL,
  53. DenyTTL: time.Duration(1) + maxDenyTTL,
  54. RetryBackoff: time.Duration(1) + maxRetryBackoff,
  55. },
  56. wantErr: true,
  57. },
  58. {
  59. test: "config wants default values",
  60. config: imagePolicyWebhookConfig{
  61. AllowTTL: useDefault,
  62. DenyTTL: useDefault,
  63. RetryBackoff: useDefault,
  64. },
  65. normalizedConfig: imagePolicyWebhookConfig{
  66. AllowTTL: defaultAllowTTL,
  67. DenyTTL: defaultDenyTTL,
  68. RetryBackoff: defaultRetryBackoff,
  69. },
  70. wantErr: false,
  71. },
  72. {
  73. test: "config wants disabled values",
  74. config: imagePolicyWebhookConfig{
  75. AllowTTL: disableTTL,
  76. DenyTTL: disableTTL,
  77. RetryBackoff: disableTTL,
  78. },
  79. normalizedConfig: imagePolicyWebhookConfig{
  80. AllowTTL: time.Duration(0),
  81. DenyTTL: time.Duration(0),
  82. RetryBackoff: time.Duration(0),
  83. },
  84. wantErr: false,
  85. },
  86. {
  87. test: "config within normal ranges for min values",
  88. config: imagePolicyWebhookConfig{
  89. AllowTTL: minAllowTTL / time.Second,
  90. DenyTTL: minDenyTTL / time.Second,
  91. RetryBackoff: minRetryBackoff,
  92. },
  93. normalizedConfig: imagePolicyWebhookConfig{
  94. AllowTTL: minAllowTTL,
  95. DenyTTL: minDenyTTL,
  96. RetryBackoff: minRetryBackoff * time.Millisecond,
  97. },
  98. wantErr: false,
  99. },
  100. {
  101. test: "config within normal ranges for max values",
  102. config: imagePolicyWebhookConfig{
  103. AllowTTL: maxAllowTTL / time.Second,
  104. DenyTTL: maxDenyTTL / time.Second,
  105. RetryBackoff: maxRetryBackoff / time.Millisecond,
  106. },
  107. normalizedConfig: imagePolicyWebhookConfig{
  108. AllowTTL: maxAllowTTL,
  109. DenyTTL: maxDenyTTL,
  110. RetryBackoff: maxRetryBackoff,
  111. },
  112. wantErr: false,
  113. },
  114. }
  115. for _, tt := range tests {
  116. err := normalizeWebhookConfig(&tt.config)
  117. if err == nil && tt.wantErr == true {
  118. t.Errorf("%s: expected error from normalization and didn't have one", tt.test)
  119. }
  120. if err != nil && tt.wantErr == false {
  121. t.Errorf("%s: unexpected error from normalization: %v", tt.test, err)
  122. }
  123. if err == nil && !reflect.DeepEqual(tt.config, tt.normalizedConfig) {
  124. t.Errorf("%s: expected config to be normalized. got: %v expected: %v", tt.test, tt.config, tt.normalizedConfig)
  125. }
  126. }
  127. }