Merge pull request #116306 from nilskch/add-unit-tests

add unit tests for probe errors and ExecProbeTimeout
This commit is contained in:
Kubernetes Prow Robot 2023-03-09 22:43:55 -08:00 committed by GitHub
commit 7da203f60d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 11 deletions

View File

@ -0,0 +1,51 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package exec
import (
"errors"
"testing"
"time"
)
func TestErrors(t *testing.T) {
tests := []struct {
err error
timeout time.Duration
message string
}{
{
err: errors.New("some error message"),
timeout: time.Hour * 8,
message: "some error message",
},
}
for i, test := range tests {
testErr := NewTimeoutError(test.err, test.timeout)
if testErr == nil {
t.Errorf("[%d] expected error a TimeoutError, got nil", i)
}
if msg := testErr.Error(); msg != test.message {
t.Errorf("[%d] expected error message %q, got %q", i, test.message, msg)
}
if timeout := testErr.Timeout(); timeout != test.timeout {
t.Errorf("[%d] expected timeout %q, got %q", i, test.timeout, timeout)
}
}
}

View File

@ -23,6 +23,9 @@ import (
"testing"
"time"
utilfeature "k8s.io/apiserver/pkg/util/feature"
featuregatetesting "k8s.io/component-base/featuregate/testing"
"k8s.io/kubernetes/pkg/features"
"k8s.io/kubernetes/pkg/probe"
)
@ -107,26 +110,31 @@ func TestExec(t *testing.T) {
elevenKilobyte := strings.Repeat("logs-123", 8*128*11) // 8*128*11=11264 = 11KB of text.
tests := []struct {
expectedStatus probe.Result
expectError bool
input string
output string
err error
expectedStatus probe.Result
expectError bool
execProbeTimeout bool
input string
output string
err error
}{
// Ok
{probe.Success, false, "OK", "OK", nil},
{probe.Success, false, true, "OK", "OK", nil},
// Ok
{probe.Success, false, "OK", "OK", &fakeExitError{true, 0}},
{probe.Success, false, true, "OK", "OK", &fakeExitError{true, 0}},
// Ok - truncated output
{probe.Success, false, elevenKilobyte, tenKilobyte, nil},
{probe.Success, false, true, elevenKilobyte, tenKilobyte, nil},
// Run returns error
{probe.Unknown, true, "", "", fmt.Errorf("test error")},
{probe.Unknown, true, true, "", "", fmt.Errorf("test error")},
// Unhealthy
{probe.Failure, false, "Fail", "", &fakeExitError{true, 1}},
{probe.Failure, false, true, "Fail", "", &fakeExitError{true, 1}},
// Timeout
{probe.Failure, false, "", "command testcmd timed out", NewTimeoutError(fmt.Errorf("command testcmd timed out"), time.Second)},
{probe.Failure, false, true, "", "command testcmd timed out", NewTimeoutError(fmt.Errorf("command testcmd timed out"), time.Second)},
// ExecProbeTimeout
{probe.Unknown, true, false, "", "", NewTimeoutError(fmt.Errorf("command testcmd timed out"), time.Second)},
}
for i, test := range tests {
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ExecProbeTimeout, test.execProbeTimeout)()
fake := FakeCmd{
out: []byte(test.output),
err: test.err,