From 0c11171b7e4bdbe5ffa6213bd9d1b07e541792b8 Mon Sep 17 00:00:00 2001 From: nilskch Date: Wed, 8 Mar 2023 11:59:59 +0100 Subject: [PATCH] add tests for probe errors and ExecProbeTimeout --- pkg/probe/exec/errors_test.go | 51 +++++++++++++++++++++++++++++++++++ pkg/probe/exec/exec_test.go | 30 +++++++++++++-------- 2 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 pkg/probe/exec/errors_test.go diff --git a/pkg/probe/exec/errors_test.go b/pkg/probe/exec/errors_test.go new file mode 100644 index 00000000000..e6c8f584b3e --- /dev/null +++ b/pkg/probe/exec/errors_test.go @@ -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) + } + } +} diff --git a/pkg/probe/exec/exec_test.go b/pkg/probe/exec/exec_test.go index 6083da3af98..5724228d2fc 100644 --- a/pkg/probe/exec/exec_test.go +++ b/pkg/probe/exec/exec_test.go @@ -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,