| 1 |
/* |
| 2 |
Copyright 2019 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 |
"testing" |
| 21 |
"time" |
| 22 |
|
| 23 |
testingclock "k8s.io/utils/clock/testing" |
| 24 |
) |
| 25 |
|
| 26 |
func TestDelayedHealthCheck(t *testing.T) { |
| 27 |
t.Run("test that liveness check returns true until the delay has elapsed", func(t *testing.T) { |
| 28 |
t0 := time.Unix(0, 0) |
| 29 |
c := testingclock.NewFakeClock(t0) |
| 30 |
doneCh := make(chan struct{}) |
| 31 |
|
| 32 |
healthCheck := delayedHealthCheck(postStartHookHealthz{"test", doneCh}, c, time.Duration(10)*time.Second) |
| 33 |
err := healthCheck.Check(nil) |
| 34 |
if err != nil { |
| 35 |
t.Errorf("Got %v, expected no error", err) |
| 36 |
} |
| 37 |
c.Step(10 * time.Second) |
| 38 |
err = healthCheck.Check(nil) |
| 39 |
if err != nil { |
| 40 |
t.Errorf("Got %v, expected no error", err) |
| 41 |
} |
| 42 |
c.Step(1 * time.Millisecond) |
| 43 |
err = healthCheck.Check(nil) |
| 44 |
if err == nil || err.Error() != "not finished" { |
| 45 |
t.Errorf("Got '%v', but expected error to be 'not finished'", err) |
| 46 |
} |
| 47 |
close(doneCh) |
| 48 |
err = healthCheck.Check(nil) |
| 49 |
if err != nil { |
| 50 |
t.Errorf("Got %v, expected no error", err) |
| 51 |
} |
| 52 |
}) |
| 53 |
t.Run("test that liveness check does not toggle false even if done channel is closed early", func(t *testing.T) { |
| 54 |
t0 := time.Unix(0, 0) |
| 55 |
c := testingclock.NewFakeClock(t0) |
| 56 |
|
| 57 |
doneCh := make(chan struct{}) |
| 58 |
|
| 59 |
healthCheck := delayedHealthCheck(postStartHookHealthz{"test", doneCh}, c, time.Duration(10)*time.Second) |
| 60 |
err := healthCheck.Check(nil) |
| 61 |
if err != nil { |
| 62 |
t.Errorf("Got %v, expected no error", err) |
| 63 |
} |
| 64 |
close(doneCh) |
| 65 |
c.Step(10 * time.Second) |
| 66 |
err = healthCheck.Check(nil) |
| 67 |
if err != nil { |
| 68 |
t.Errorf("Got %v, expected no error", err) |
| 69 |
} |
| 70 |
c.Step(1 * time.Millisecond) |
| 71 |
err = healthCheck.Check(nil) |
| 72 |
if err != nil { |
| 73 |
t.Errorf("Got %v, expected no error", err) |
| 74 |
} |
| 75 |
}) |
| 76 |
|
| 77 |
} |