Debian Codegraph @

pkg/server/storage_readiness_hook_test.go

raw
1 /*
2 Copyright 2024 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 server
18
19 import (
20 "context"
21 "fmt"
22 "testing"
23 "time"
24
25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26 "k8s.io/apimachinery/pkg/runtime"
27 )
28
29 type fakeReadinessStorage struct {
30 result error
31 }
32
33 func (s *fakeReadinessStorage) New() runtime.Object { return nil }
34 func (s *fakeReadinessStorage) Destroy() {}
35 func (s *fakeReadinessStorage) ReadinessCheck() error { return s.result }
36
37 func testGVR(index int) metav1.GroupVersionResource {
38 return metav1.GroupVersionResource{
39 Group: "group",
40 Version: "version",
41 Resource: fmt.Sprintf("resource-%d", index),
42 }
43 }
44
45 func TestStorageReadinessHook(t *testing.T) {
46 h := NewStorageReadinessHook(time.Second)
47
48 numChecks := 5
49 storages := make([]*fakeReadinessStorage, numChecks)
50 for i := 0; i < numChecks; i++ {
51 storages[i] = &fakeReadinessStorage{
52 result: fmt.Errorf("failed"),
53 }
54 h.RegisterStorage(testGVR(i), storages[i])
55 }
56
57 for i := 0; i < numChecks; i++ {
58 if ok := h.check(); ok {
59 t.Errorf("%d: unexpected check pass", i)
60 }
61 storages[i].result = nil
62 }
63 if ok := h.check(); !ok {
64 t.Errorf("unexpected check failure")
65 }
66 }
67
68 func TestStorageReadinessHookTimeout(t *testing.T) {
69 h := NewStorageReadinessHook(time.Second)
70
71 storage := &fakeReadinessStorage{
72 result: fmt.Errorf("failed"),
73 }
74 h.RegisterStorage(testGVR(0), storage)
75
76 ctx := context.Background()
77 hookCtx := PostStartHookContext{
78 LoopbackClientConfig: nil,
79 Context: ctx,
80 }
81 if err := h.Hook(hookCtx); err != nil {
82 t.Errorf("unexpected hook failure on timeout")
83 }
84 }