|
| 1 | +/* |
| 2 | +* SPDX-FileCopyrightText: Copyright (c) <2024> NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +* SPDX-License-Identifier: Apache-2.0 |
| 4 | +* |
| 5 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +* you may not use this file except in compliance with the License. |
| 7 | +* You may obtain a copy of the License at |
| 8 | +* |
| 9 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +* |
| 11 | +* Unless required by applicable law or agreed to in writing, software |
| 12 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +* See the License for the specific language governing permissions and |
| 15 | +* limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package validator |
| 19 | + |
| 20 | +import ( |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + canaryv1 "github.com/nvidia/container-canary/internal/apis/v1" |
| 25 | + "github.com/nvidia/container-canary/internal/container" |
| 26 | + "github.com/rs/zerolog" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | +) |
| 29 | + |
| 30 | +func TestExecuteCheckSuccess(t *testing.T) { |
| 31 | + require := require.New(t) |
| 32 | + logger, buf := logger() |
| 33 | + |
| 34 | + start := time.Now() |
| 35 | + attempts := 0 |
| 36 | + checkFunc := func(c container.ContainerInterface, probe *canaryv1.Probe, e *zerolog.Event) (bool, error) { |
| 37 | + require.GreaterOrEqual(time.Since(start), (2+time.Duration(attempts))*time.Second) |
| 38 | + require.Less(time.Since(start), (2+time.Duration(attempts))*time.Second+100*time.Millisecond) |
| 39 | + attempts++ |
| 40 | + e.Str("data", "test") |
| 41 | + if attempts < 5 { |
| 42 | + return false, nil |
| 43 | + } |
| 44 | + return true, nil |
| 45 | + } |
| 46 | + |
| 47 | + check := canaryv1.Check{ |
| 48 | + Name: "test-check", |
| 49 | + Probe: canaryv1.Probe{ |
| 50 | + InitialDelaySeconds: 2, |
| 51 | + PeriodSeconds: 1, |
| 52 | + TimeoutSeconds: 60, |
| 53 | + SuccessThreshold: 3, |
| 54 | + FailureThreshold: 10, |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + expectedLog := "{\"level\":\"info\",\"check\":\"test-check\",\"attempt\":1,\"data\":\"test\",\"pass\":false}\n" + |
| 59 | + "{\"level\":\"info\",\"check\":\"test-check\",\"attempt\":2,\"data\":\"test\",\"pass\":false}\n" + |
| 60 | + "{\"level\":\"info\",\"check\":\"test-check\",\"attempt\":3,\"data\":\"test\",\"pass\":false}\n" + |
| 61 | + "{\"level\":\"info\",\"check\":\"test-check\",\"attempt\":4,\"data\":\"test\",\"pass\":false}\n" + |
| 62 | + "{\"level\":\"info\",\"check\":\"test-check\",\"attempt\":5,\"data\":\"test\",\"pass\":true}\n" + |
| 63 | + "{\"level\":\"info\",\"check\":\"test-check\",\"attempt\":6,\"data\":\"test\",\"pass\":true}\n" + |
| 64 | + "{\"level\":\"info\",\"check\":\"test-check\",\"attempt\":7,\"data\":\"test\",\"pass\":true}\n" |
| 65 | + |
| 66 | + result, err := executeCheck(checkFunc, logger, &dummyContainer{}, check) |
| 67 | + require.NoError(err) |
| 68 | + require.True(result) |
| 69 | + require.Equal(7, attempts) |
| 70 | + require.Equal(expectedLog, buf.String()) |
| 71 | +} |
| 72 | + |
| 73 | +func TestExecuteCheckFailureThreshold(t *testing.T) { |
| 74 | + require := require.New(t) |
| 75 | + logger, buf := logger() |
| 76 | + |
| 77 | + start := time.Now() |
| 78 | + attempts := 0 |
| 79 | + checkFunc := func(c container.ContainerInterface, probe *canaryv1.Probe, e *zerolog.Event) (bool, error) { |
| 80 | + require.GreaterOrEqual(time.Since(start), (2+time.Duration(attempts))*time.Second) |
| 81 | + require.Less(time.Since(start), (2+time.Duration(attempts))*time.Second+100*time.Millisecond) |
| 82 | + attempts++ |
| 83 | + e.Str("data", "test") |
| 84 | + if attempts < 5 { |
| 85 | + return false, nil |
| 86 | + } |
| 87 | + return true, nil |
| 88 | + } |
| 89 | + |
| 90 | + check := canaryv1.Check{ |
| 91 | + Name: "test-check", |
| 92 | + Probe: canaryv1.Probe{ |
| 93 | + InitialDelaySeconds: 2, |
| 94 | + PeriodSeconds: 1, |
| 95 | + TimeoutSeconds: 60, |
| 96 | + SuccessThreshold: 3, |
| 97 | + FailureThreshold: 4, |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + expectedLog := "{\"level\":\"info\",\"check\":\"test-check\",\"attempt\":1,\"data\":\"test\",\"pass\":false}\n" + |
| 102 | + "{\"level\":\"info\",\"check\":\"test-check\",\"attempt\":2,\"data\":\"test\",\"pass\":false}\n" + |
| 103 | + "{\"level\":\"info\",\"check\":\"test-check\",\"attempt\":3,\"data\":\"test\",\"pass\":false}\n" + |
| 104 | + "{\"level\":\"info\",\"check\":\"test-check\",\"attempt\":4,\"data\":\"test\",\"pass\":false}\n" |
| 105 | + |
| 106 | + result, err := executeCheck(checkFunc, logger, &dummyContainer{}, check) |
| 107 | + require.NoError(err) |
| 108 | + require.False(result) |
| 109 | + require.Equal(4, attempts) |
| 110 | + require.Equal(expectedLog, buf.String()) |
| 111 | +} |
| 112 | + |
| 113 | +func TestExecuteCheckTimeout(t *testing.T) { |
| 114 | + require := require.New(t) |
| 115 | + logger, buf := logger() |
| 116 | + |
| 117 | + start := time.Now() |
| 118 | + attempts := 0 |
| 119 | + checkFunc := func(c container.ContainerInterface, probe *canaryv1.Probe, e *zerolog.Event) (bool, error) { |
| 120 | + require.GreaterOrEqual(time.Since(start), (2+time.Duration(attempts))*time.Second) |
| 121 | + require.Less(time.Since(start), (2+time.Duration(attempts))*time.Second+100*time.Millisecond) |
| 122 | + attempts++ |
| 123 | + e.Str("data", "test") |
| 124 | + if attempts < 5 { |
| 125 | + return false, nil |
| 126 | + } |
| 127 | + return true, nil |
| 128 | + } |
| 129 | + |
| 130 | + check := canaryv1.Check{ |
| 131 | + Name: "test-check", |
| 132 | + Probe: canaryv1.Probe{ |
| 133 | + InitialDelaySeconds: 2, |
| 134 | + PeriodSeconds: 1, |
| 135 | + TimeoutSeconds: 4, |
| 136 | + SuccessThreshold: 3, |
| 137 | + FailureThreshold: 10, |
| 138 | + }, |
| 139 | + } |
| 140 | + |
| 141 | + expectedLog := "{\"level\":\"info\",\"check\":\"test-check\",\"attempt\":1,\"data\":\"test\",\"pass\":false}\n" + |
| 142 | + "{\"level\":\"info\",\"check\":\"test-check\",\"attempt\":2,\"data\":\"test\",\"pass\":false}\n" + |
| 143 | + "{\"level\":\"info\",\"check\":\"test-check\",\"attempt\":3,\"data\":\"test\",\"pass\":false}\n" + |
| 144 | + "{\"level\":\"info\",\"check\":\"test-check\",\"attempt\":4,\"data\":\"test\",\"pass\":false}\n" + |
| 145 | + "{\"level\":\"info\",\"check\":\"test-check\",\"attempt\":5,\"data\":\"test\",\"pass\":true}\n" |
| 146 | + |
| 147 | + result, err := executeCheck(checkFunc, logger, &dummyContainer{}, check) |
| 148 | + require.Error(err, "check timed out after 4 seconds") |
| 149 | + require.False(result) |
| 150 | + require.Equal(5, attempts) |
| 151 | + require.Equal(expectedLog, buf.String()) |
| 152 | +} |
0 commit comments