mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 02:41:25 +00:00
Merge pull request #116306 from nilskch/add-unit-tests
add unit tests for probe errors and ExecProbeTimeout
This commit is contained in:
commit
7da203f60d
51
pkg/probe/exec/errors_test.go
Normal file
51
pkg/probe/exec/errors_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -23,6 +23,9 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"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"
|
"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.
|
elevenKilobyte := strings.Repeat("logs-123", 8*128*11) // 8*128*11=11264 = 11KB of text.
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
expectedStatus probe.Result
|
expectedStatus probe.Result
|
||||||
expectError bool
|
expectError bool
|
||||||
input string
|
execProbeTimeout bool
|
||||||
output string
|
input string
|
||||||
err error
|
output string
|
||||||
|
err error
|
||||||
}{
|
}{
|
||||||
// Ok
|
// Ok
|
||||||
{probe.Success, false, "OK", "OK", nil},
|
{probe.Success, false, true, "OK", "OK", nil},
|
||||||
// Ok
|
// Ok
|
||||||
{probe.Success, false, "OK", "OK", &fakeExitError{true, 0}},
|
{probe.Success, false, true, "OK", "OK", &fakeExitError{true, 0}},
|
||||||
// Ok - truncated output
|
// Ok - truncated output
|
||||||
{probe.Success, false, elevenKilobyte, tenKilobyte, nil},
|
{probe.Success, false, true, elevenKilobyte, tenKilobyte, nil},
|
||||||
// Run returns error
|
// Run returns error
|
||||||
{probe.Unknown, true, "", "", fmt.Errorf("test error")},
|
{probe.Unknown, true, true, "", "", fmt.Errorf("test error")},
|
||||||
// Unhealthy
|
// Unhealthy
|
||||||
{probe.Failure, false, "Fail", "", &fakeExitError{true, 1}},
|
{probe.Failure, false, true, "Fail", "", &fakeExitError{true, 1}},
|
||||||
// Timeout
|
// 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 {
|
for i, test := range tests {
|
||||||
|
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ExecProbeTimeout, test.execProbeTimeout)()
|
||||||
fake := FakeCmd{
|
fake := FakeCmd{
|
||||||
out: []byte(test.output),
|
out: []byte(test.output),
|
||||||
err: test.err,
|
err: test.err,
|
||||||
|
Loading…
Reference in New Issue
Block a user