Debian Codegraph @

pkg/storage/cacher/util_test.go

raw
1 /*
2 Copyright 2015 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package cacher
18
19 import (
20 "testing"
21 "time"
22 )
23
24 func TestHasPathPrefix(t *testing.T) {
25 validTestcases := []struct {
26 s string
27 prefix string
28 }{
29 // Exact matches
30 {"", ""},
31 {"a", "a"},
32 {"a/", "a/"},
33 {"a/../", "a/../"},
34
35 // Path prefix matches
36 {"a/b", "a"},
37 {"a/b", "a/"},
38 {"中文/", "中文"},
39 }
40 for i, tc := range validTestcases {
41 if !hasPathPrefix(tc.s, tc.prefix) {
42 t.Errorf(`%d: Expected hasPathPrefix("%s","%s") to be true`, i, tc.s, tc.prefix)
43 }
44 }
45
46 invalidTestcases := []struct {
47 s string
48 prefix string
49 }{
50 // Mismatch
51 {"a", "b"},
52
53 // Dir requirement
54 {"a", "a/"},
55
56 // Prefix mismatch
57 {"ns2", "ns"},
58 {"ns2", "ns/"},
59 {"中文文", "中文"},
60
61 // Ensure no normalization is applied
62 {"a/c/../b/", "a/b/"},
63 {"a/", "a/b/.."},
64 }
65 for i, tc := range invalidTestcases {
66 if hasPathPrefix(tc.s, tc.prefix) {
67 t.Errorf(`%d: Expected hasPathPrefix("%s","%s") to be false`, i, tc.s, tc.prefix)
68 }
69 }
70 }
71
72 func TestCalculateRetryAfterForUnreadyCache(t *testing.T) {
73 tests := []struct {
74 downtime time.Duration
75 expected int
76 }{
77 {downtime: 0 * time.Second, expected: 1},
78 {downtime: 1 * time.Second, expected: 1},
79 {downtime: 3 * time.Second, expected: 1},
80 {downtime: 5 * time.Second, expected: 1},
81 {downtime: 7 * time.Second, expected: 1},
82 {downtime: 10 * time.Second, expected: 1},
83 {downtime: 14 * time.Second, expected: 2},
84 {downtime: 20 * time.Second, expected: 3},
85 {downtime: 30 * time.Second, expected: 6},
86 {downtime: 40 * time.Second, expected: 11},
87 {downtime: 540 * time.Second, expected: 30},
88 }
89
90 for _, test := range tests {
91 t.Run(test.downtime.String(), func(t *testing.T) {
92 result := calculateRetryAfterForUnreadyCache(test.downtime)
93 if result != test.expected {
94 t.Errorf("for downtime %s, expected %d, but got %d", test.downtime, test.expected, result)
95 }
96 })
97 }
98 }