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