Introduce -reusenamespace test flag (#1383)

* Introduce -reusenamespace test flag

* allows for reusing test namespaces that were created in advance
(possibly by a cluster admininstrator)

* Fix gofmt
This commit is contained in:
Martin Gencur 2021-07-14 09:18:30 +02:00 committed by GitHub
parent 0c3e236006
commit 804d021e13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View File

@ -28,6 +28,7 @@ var Flags = InitializeFlags()
// ClientFlags define the flags that are needed to run the e2e tests.
type ClientFlags struct {
DockerConfigJSON string
ReuseNamespace bool
}
// InitializeFlags initializes the client's flags
@ -37,6 +38,9 @@ func InitializeFlags() *ClientFlags {
dockerConfigJSON := os.Getenv("DOCKER_CONFIG_JSON")
flag.StringVar(&f.DockerConfigJSON, "dockerconfigjson", dockerConfigJSON,
"Provide the path to Docker configuration file in json format. Defaults to $DOCKER_CONFIG_JSON")
// Might be useful in restricted environments where namespaces need to be
// created by a user with increased privileges (admin).
flag.BoolVar(&f.ReuseNamespace, "reusenamespace", false, "Whether to re-use namespace for test if it already exists.")
return &f
}

View File

@ -44,9 +44,14 @@ type KnTest struct {
// NewKnTest creates a new KnTest object
func NewKnTest() (*KnTest, error) {
ns := ""
// try next 20 namespace before giving up creating a namespace if it already exists
// Try next 20 namespace before giving up creating a namespace if it already exists
for i := 0; i < 20; i++ {
ns = NextNamespace()
if Flags.ReuseNamespace {
// Re-using existing namespace, no need to create it.
// The namespace is supposed to be created in advance.
break
}
err := CreateNamespace(ns)
if err == nil {
break
@ -77,6 +82,10 @@ func NewKnTest() (*KnTest, error) {
// Teardown clean up
func (test *KnTest) Teardown() error {
// If we're reusing existing namespaces leave the deletion to the creator.
if Flags.ReuseNamespace {
return nil
}
return DeleteNamespace(test.namespace)
}