|
| 1 | +/* |
| 2 | +Copyright 2021 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 scope |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + . "github.com/onsi/gomega" |
| 24 | + |
| 25 | + runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" |
| 26 | + runtimecatalog "sigs.k8s.io/cluster-api/internal/runtime/catalog" |
| 27 | +) |
| 28 | + |
| 29 | +func TestHookResponseTracker_AggregateRetryAfter(t *testing.T) { |
| 30 | + nonBlockingBeforeClusterCreateResponse := &runtimehooksv1.BeforeClusterCreateResponse{ |
| 31 | + CommonRetryResponse: runtimehooksv1.CommonRetryResponse{ |
| 32 | + RetryAfterSeconds: int32(0), |
| 33 | + CommonResponse: runtimehooksv1.CommonResponse{ |
| 34 | + Status: runtimehooksv1.ResponseStatusSuccess, |
| 35 | + }, |
| 36 | + }, |
| 37 | + } |
| 38 | + blockingBeforeClusterCreateResponse := &runtimehooksv1.BeforeClusterCreateResponse{ |
| 39 | + CommonRetryResponse: runtimehooksv1.CommonRetryResponse{ |
| 40 | + RetryAfterSeconds: int32(10), |
| 41 | + CommonResponse: runtimehooksv1.CommonResponse{ |
| 42 | + Status: runtimehooksv1.ResponseStatusSuccess, |
| 43 | + }, |
| 44 | + }, |
| 45 | + } |
| 46 | + |
| 47 | + nonBlockingBeforeClusterUpgradeResponse := &runtimehooksv1.BeforeClusterUpgradeResponse{ |
| 48 | + CommonRetryResponse: runtimehooksv1.CommonRetryResponse{ |
| 49 | + RetryAfterSeconds: int32(0), |
| 50 | + CommonResponse: runtimehooksv1.CommonResponse{ |
| 51 | + Status: runtimehooksv1.ResponseStatusSuccess, |
| 52 | + }, |
| 53 | + }, |
| 54 | + } |
| 55 | + blockingBeforeClusterUpgradeResponse := &runtimehooksv1.BeforeClusterUpgradeResponse{ |
| 56 | + CommonRetryResponse: runtimehooksv1.CommonRetryResponse{ |
| 57 | + RetryAfterSeconds: int32(5), |
| 58 | + CommonResponse: runtimehooksv1.CommonResponse{ |
| 59 | + Status: runtimehooksv1.ResponseStatusSuccess, |
| 60 | + }, |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + t.Run("AggregateRetryAfter should return non zero value if there are any blocking hook responses", func(t *testing.T) { |
| 65 | + g := NewWithT(t) |
| 66 | + |
| 67 | + hrt := NewHookResponseTracker() |
| 68 | + hrt.Add(runtimehooksv1.BeforeClusterCreate, blockingBeforeClusterCreateResponse) |
| 69 | + hrt.Add(runtimehooksv1.BeforeClusterUpgrade, nonBlockingBeforeClusterUpgradeResponse) |
| 70 | + |
| 71 | + g.Expect(hrt.AggregateRetryAfter()).To(Equal(time.Duration(10) * time.Second)) |
| 72 | + }) |
| 73 | + t.Run("AggregateRetryAfter should return zero value if there are no blocking hook responses", func(t *testing.T) { |
| 74 | + g := NewWithT(t) |
| 75 | + |
| 76 | + hrt := NewHookResponseTracker() |
| 77 | + hrt.Add(runtimehooksv1.BeforeClusterCreate, nonBlockingBeforeClusterCreateResponse) |
| 78 | + hrt.Add(runtimehooksv1.BeforeClusterUpgrade, nonBlockingBeforeClusterUpgradeResponse) |
| 79 | + |
| 80 | + g.Expect(hrt.AggregateRetryAfter()).To(Equal(time.Duration(0))) |
| 81 | + }) |
| 82 | + t.Run("AggregateRetryAfter should return the lowest non-zero value if there are multiple blocking hook responses", func(t *testing.T) { |
| 83 | + g := NewWithT(t) |
| 84 | + |
| 85 | + hrt := NewHookResponseTracker() |
| 86 | + hrt.Add(runtimehooksv1.BeforeClusterCreate, blockingBeforeClusterCreateResponse) |
| 87 | + hrt.Add(runtimehooksv1.BeforeClusterUpgrade, blockingBeforeClusterUpgradeResponse) |
| 88 | + |
| 89 | + g.Expect(hrt.AggregateRetryAfter()).To(Equal(time.Duration(5) * time.Second)) |
| 90 | + }) |
| 91 | +} |
| 92 | + |
| 93 | +func TestHookResponseTracker_AggregateMessage(t *testing.T) { |
| 94 | + nonBlockingBeforeClusterCreateResponse := &runtimehooksv1.BeforeClusterCreateResponse{ |
| 95 | + CommonRetryResponse: runtimehooksv1.CommonRetryResponse{ |
| 96 | + RetryAfterSeconds: int32(0), |
| 97 | + CommonResponse: runtimehooksv1.CommonResponse{ |
| 98 | + Status: runtimehooksv1.ResponseStatusSuccess, |
| 99 | + }, |
| 100 | + }, |
| 101 | + } |
| 102 | + blockingBeforeClusterCreateResponse := &runtimehooksv1.BeforeClusterCreateResponse{ |
| 103 | + CommonRetryResponse: runtimehooksv1.CommonRetryResponse{ |
| 104 | + RetryAfterSeconds: int32(10), |
| 105 | + CommonResponse: runtimehooksv1.CommonResponse{ |
| 106 | + Status: runtimehooksv1.ResponseStatusSuccess, |
| 107 | + }, |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + nonBlockingBeforeClusterUpgradeResponse := &runtimehooksv1.BeforeClusterUpgradeResponse{ |
| 112 | + CommonRetryResponse: runtimehooksv1.CommonRetryResponse{ |
| 113 | + RetryAfterSeconds: int32(0), |
| 114 | + CommonResponse: runtimehooksv1.CommonResponse{ |
| 115 | + Status: runtimehooksv1.ResponseStatusSuccess, |
| 116 | + }, |
| 117 | + }, |
| 118 | + } |
| 119 | + blockingBeforeClusterUpgradeResponse := &runtimehooksv1.BeforeClusterUpgradeResponse{ |
| 120 | + CommonRetryResponse: runtimehooksv1.CommonRetryResponse{ |
| 121 | + RetryAfterSeconds: int32(5), |
| 122 | + CommonResponse: runtimehooksv1.CommonResponse{ |
| 123 | + Status: runtimehooksv1.ResponseStatusSuccess, |
| 124 | + }, |
| 125 | + }, |
| 126 | + } |
| 127 | + |
| 128 | + t.Run("AggregateMessage should return a message with the names of all the blocking hooks", func(t *testing.T) { |
| 129 | + g := NewWithT(t) |
| 130 | + |
| 131 | + hrt := NewHookResponseTracker() |
| 132 | + hrt.Add(runtimehooksv1.BeforeClusterCreate, blockingBeforeClusterCreateResponse) |
| 133 | + hrt.Add(runtimehooksv1.BeforeClusterUpgrade, blockingBeforeClusterUpgradeResponse) |
| 134 | + |
| 135 | + g.Expect(hrt.AggregateMessage()).To(ContainSubstring(runtimecatalog.HookName(runtimehooksv1.BeforeClusterCreate))) |
| 136 | + g.Expect(hrt.AggregateMessage()).To(ContainSubstring(runtimecatalog.HookName(runtimehooksv1.BeforeClusterUpgrade))) |
| 137 | + }) |
| 138 | + t.Run("AggregateMessage should return empty string if there are no blocking hook responses", func(t *testing.T) { |
| 139 | + g := NewWithT(t) |
| 140 | + |
| 141 | + hrt := NewHookResponseTracker() |
| 142 | + hrt.Add(runtimehooksv1.BeforeClusterCreate, nonBlockingBeforeClusterCreateResponse) |
| 143 | + hrt.Add(runtimehooksv1.BeforeClusterUpgrade, nonBlockingBeforeClusterUpgradeResponse) |
| 144 | + |
| 145 | + g.Expect(hrt.AggregateMessage()).To(Equal("")) |
| 146 | + }) |
| 147 | +} |
0 commit comments