From f0cc053544b7e82127a49eb160c551aa20086746 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Tue, 3 Jan 2023 17:01:33 +0100 Subject: [PATCH] e2e framework: enable extending TimeoutContext If we were to add new fields in TimeoutContext, the current users of NewFrameworkWithCustomTimeouts might run into failures unless they get modified to also set those new fields. This is error-prone. A better approach is to let users of NewFrameworkWithCustomTimeouts override fields by setting just those and use the normal defaults for the others. --- test/e2e/framework/framework.go | 14 ++++- .../internal/unittests/framework_test.go | 52 +++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 test/e2e/framework/internal/unittests/framework_test.go diff --git a/test/e2e/framework/framework.go b/test/e2e/framework/framework.go index c8ba3aa0879..79050477fd5 100644 --- a/test/e2e/framework/framework.go +++ b/test/e2e/framework/framework.go @@ -27,6 +27,7 @@ import ( "math/rand" "os" "path" + "reflect" "strings" "time" @@ -144,10 +145,19 @@ type Options struct { GroupVersion *schema.GroupVersion } -// NewFrameworkWithCustomTimeouts makes a framework with with custom timeouts. +// NewFrameworkWithCustomTimeouts makes a framework with custom timeouts. +// For timeout values that are zero the normal default value continues to +// be used. func NewFrameworkWithCustomTimeouts(baseName string, timeouts *TimeoutContext) *Framework { f := NewDefaultFramework(baseName) - f.Timeouts = timeouts + in := reflect.ValueOf(timeouts).Elem() + out := reflect.ValueOf(f.Timeouts).Elem() + for i := 0; i < in.NumField(); i++ { + value := in.Field(i) + if !value.IsZero() { + out.Field(i).Set(value) + } + } return f } diff --git a/test/e2e/framework/internal/unittests/framework_test.go b/test/e2e/framework/internal/unittests/framework_test.go new file mode 100644 index 00000000000..30c8d8d0311 --- /dev/null +++ b/test/e2e/framework/internal/unittests/framework_test.go @@ -0,0 +1,52 @@ +/* +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 framework_test + +import ( + "reflect" + "testing" + "time" + + "github.com/stretchr/testify/assert" + + "k8s.io/kubernetes/test/e2e/framework" +) + +func TestNewFrameworkWithCustomTimeouts(t *testing.T) { + defaultF := framework.NewDefaultFramework("test") + customTimeouts := &framework.TimeoutContext{ + PodStart: 5 * time.Second, + PodDelete: time.Second, + } + customF := framework.NewFrameworkWithCustomTimeouts("test", customTimeouts) + + defaultF.Timeouts.PodStart = customTimeouts.PodStart + defaultF.Timeouts.PodDelete = customTimeouts.PodDelete + assert.Equal(t, customF.Timeouts, defaultF.Timeouts) +} + +func TestNewFramework(t *testing.T) { + f := framework.NewDefaultFramework("test") + + timeouts := reflect.ValueOf(f.Timeouts).Elem() + for i := 0; i < timeouts.NumField(); i++ { + value := timeouts.Field(i) + if value.IsZero() { + t.Errorf("%s in Framework.Timeouts was not set.", reflect.TypeOf(*f.Timeouts).Field(i).Name) + } + } +}