| 1 |
/* |
| 2 |
Copyright 2017 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 configuration |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"math" |
| 22 |
"sync" |
| 23 |
"testing" |
| 24 |
"time" |
| 25 |
|
| 26 |
"k8s.io/apimachinery/pkg/runtime" |
| 27 |
"k8s.io/apimachinery/pkg/util/wait" |
| 28 |
) |
| 29 |
|
| 30 |
func TestTolerateBootstrapFailure(t *testing.T) { |
| 31 |
var fakeGetSucceed bool |
| 32 |
var fakeGetSucceedLock sync.RWMutex |
| 33 |
fakeGetFn := func() (runtime.Object, error) { |
| 34 |
fakeGetSucceedLock.RLock() |
| 35 |
defer fakeGetSucceedLock.RUnlock() |
| 36 |
if fakeGetSucceed { |
| 37 |
return nil, nil |
| 38 |
} else { |
| 39 |
return nil, fmt.Errorf("this error shouldn't be exposed to caller") |
| 40 |
} |
| 41 |
} |
| 42 |
poller := newPoller(fakeGetFn) |
| 43 |
poller.bootstrapGracePeriod = 100 * time.Second |
| 44 |
poller.bootstrapRetries = math.MaxInt32 |
| 45 |
// set failureThreshold to 0 so that one single failure will set "ready" to false. |
| 46 |
poller.failureThreshold = 0 |
| 47 |
stopCh := make(chan struct{}) |
| 48 |
defer close(stopCh) |
| 49 |
go poller.Run(stopCh) |
| 50 |
go func() { |
| 51 |
// The test might have false negative, but won't be flaky |
| 52 |
timer := time.NewTimer(2 * time.Second) |
| 53 |
defer timer.Stop() |
| 54 |
<-timer.C |
| 55 |
fakeGetSucceedLock.Lock() |
| 56 |
defer fakeGetSucceedLock.Unlock() |
| 57 |
fakeGetSucceed = true |
| 58 |
}() |
| 59 |
|
| 60 |
done := make(chan struct{}) |
| 61 |
go func(t *testing.T) { |
| 62 |
_, err := poller.configuration() |
| 63 |
if err != nil { |
| 64 |
t.Errorf("unexpected error: %v", err) |
| 65 |
} |
| 66 |
close(done) |
| 67 |
}(t) |
| 68 |
<-done |
| 69 |
} |
| 70 |
|
| 71 |
func TestNotTolerateNonbootstrapFailure(t *testing.T) { |
| 72 |
fakeGetFn := func() (runtime.Object, error) { |
| 73 |
return nil, fmt.Errorf("this error should be exposed to caller") |
| 74 |
} |
| 75 |
poller := newPoller(fakeGetFn) |
| 76 |
poller.bootstrapGracePeriod = 1 * time.Second |
| 77 |
poller.interval = 1 * time.Millisecond |
| 78 |
stopCh := make(chan struct{}) |
| 79 |
defer close(stopCh) |
| 80 |
go poller.Run(stopCh) |
| 81 |
// to kick the bootstrap timer |
| 82 |
go poller.configuration() |
| 83 |
|
| 84 |
wait.PollInfinite(1*time.Second, func() (bool, error) { |
| 85 |
poller.lock.Lock() |
| 86 |
defer poller.lock.Unlock() |
| 87 |
return poller.bootstrapped, nil |
| 88 |
}) |
| 89 |
|
| 90 |
_, err := poller.configuration() |
| 91 |
if err == nil { |
| 92 |
t.Errorf("unexpected no error") |
| 93 |
} |
| 94 |
} |