From f6e900f468e2714b1959f703042eed2f318c9409 Mon Sep 17 00:00:00 2001 From: Fabio Bertinatto Date: Fri, 30 Oct 2020 10:25:41 +0100 Subject: [PATCH] e2e: add custom timeouts support in external storage drivers --- test/e2e/storage/external/BUILD | 1 + test/e2e/storage/external/external.go | 46 +++++++++++++++++++++++ test/e2e/storage/testsuites/testdriver.go | 13 +++++++ 3 files changed, 60 insertions(+) diff --git a/test/e2e/storage/external/BUILD b/test/e2e/storage/external/BUILD index a469aff19e1..e6d2f68ea2c 100644 --- a/test/e2e/storage/external/BUILD +++ b/test/e2e/storage/external/BUILD @@ -23,6 +23,7 @@ go_library( "//test/e2e/storage/utils:go_default_library", "//vendor/github.com/onsi/ginkgo:go_default_library", "//vendor/github.com/pkg/errors:go_default_library", + "//vendor/k8s.io/klog/v2:go_default_library", ], ) diff --git a/test/e2e/storage/external/external.go b/test/e2e/storage/external/external.go index fbd7678f5c1..51e48e3e1d2 100644 --- a/test/e2e/storage/external/external.go +++ b/test/e2e/storage/external/external.go @@ -18,8 +18,10 @@ package external import ( "context" + "encoding/json" "flag" "io/ioutil" + "time" "github.com/pkg/errors" @@ -30,6 +32,7 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/client-go/kubernetes/scheme" + klog "k8s.io/klog/v2" "k8s.io/kubernetes/test/e2e/framework" e2econfig "k8s.io/kubernetes/test/e2e/framework/config" e2epod "k8s.io/kubernetes/test/e2e/framework/pod" @@ -128,6 +131,11 @@ type driverDefinition struct { // Can be left empty. Most drivers should not need this and instead // use topology to ensure that pods land on the right node(s). ClientNodeName string + + // Timeouts contains the custom timeouts used during the test execution. + // The values specified here will override the default values specified in + // the framework.TimeoutContext struct. + Timeouts map[string]string } func init() { @@ -213,6 +221,8 @@ var _ testsuites.SnapshottableTestDriver = &driverDefinition{} // And for ephemeral volumes. var _ testsuites.EphemeralTestDriver = &driverDefinition{} +var _ testsuites.CustomTimeoutsTestDriver = &driverDefinition{} + // runtime.DecodeInto needs a runtime.Object but doesn't do any // deserialization of it and therefore none of the methods below need // an implementation. @@ -302,6 +312,42 @@ func (d *driverDefinition) GetDynamicProvisionStorageClass(e2econfig *testsuites return testsuites.GetStorageClass(sc.Provisioner, sc.Parameters, sc.VolumeBindingMode, f.Namespace.Name, "e2e-sc") } +func (d *driverDefinition) GetTimeouts() *framework.TimeoutContext { + timeouts := framework.NewTimeoutContextWithDefaults() + if d.Timeouts == nil { + return timeouts + } + + // Use a temporary map to hold the timeouts specified in the manifest file + c := make(map[string]time.Duration) + for k, v := range d.Timeouts { + duration, err := time.ParseDuration(v) + if err != nil { + // We can't use ExpectNoError() because his method can be called out of an It(), + // so we simply log the error and return the default timeouts. + klog.Errorf("Could not parse duration for key %s, will use default values: %v", k, err) + return timeouts + } + c[k] = duration + } + + // Convert the temporary map holding the custom timeouts to JSON + t, err := json.Marshal(c) + if err != nil { + klog.Errorf("Could not marshal custom timeouts, will use default values: %v", err) + return timeouts + } + + // Override the default timeouts with the custom ones + err = json.Unmarshal(t, &timeouts) + if err != nil { + klog.Errorf("Could not unmarshal custom timeouts, will use default values: %v", err) + return timeouts + } + + return timeouts +} + func loadSnapshotClass(filename string) (*unstructured.Unstructured, error) { data, err := ioutil.ReadFile(filename) if err != nil { diff --git a/test/e2e/storage/testsuites/testdriver.go b/test/e2e/storage/testsuites/testdriver.go index f1f70d6218a..5618264d92b 100644 --- a/test/e2e/storage/testsuites/testdriver.go +++ b/test/e2e/storage/testsuites/testdriver.go @@ -129,6 +129,19 @@ type SnapshottableTestDriver interface { GetSnapshotClass(config *PerTestConfig) *unstructured.Unstructured } +// CustomTimeoutsTestDriver represents an interface fo a TestDriver that supports custom timeouts. +type CustomTimeoutsTestDriver interface { + TestDriver + GetTimeouts() *framework.TimeoutContext +} + +func getDriverTimeouts(driver TestDriver) *framework.TimeoutContext { + if d, ok := driver.(CustomTimeoutsTestDriver); ok { + return d.GetTimeouts() + } + return framework.NewTimeoutContextWithDefaults() +} + // Capability represents a feature that a volume plugin supports type Capability string