knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/test/e2e_flags.go (about)

     1  /*
     2  Copyright 2018 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // This file contains logic to encapsulate flags which are needed to specify
    18  // what cluster, etc. to use for e2e tests.
    19  
    20  package test
    21  
    22  import (
    23  	"bytes"
    24  	"flag"
    25  	"text/template"
    26  
    27  	env "knative.dev/pkg/environment"
    28  	testenv "knative.dev/pkg/test/environment"
    29  	"knative.dev/pkg/test/logging"
    30  )
    31  
    32  // Flags holds the command line flags or defaults for settings in the user's environment.
    33  // See EnvironmentFlags for a list of supported fields.
    34  // Deprecated: use test/flags.Flags()
    35  var Flags = initializeFlags()
    36  
    37  // EnvironmentFlags define the flags that are needed to run the e2e tests.
    38  // Deprecated: use test/flags.Flags() or injection.Flags()
    39  type EnvironmentFlags struct {
    40  	env.ClientConfig
    41  	testenv.TestClientConfig
    42  }
    43  
    44  func initializeFlags() *EnvironmentFlags {
    45  	f := new(EnvironmentFlags)
    46  
    47  	f.ClientConfig.InitFlags(flag.CommandLine)
    48  	f.TestClientConfig.InitFlags(flag.CommandLine)
    49  
    50  	return f
    51  }
    52  
    53  // SetupLoggingFlags initializes a logger for tests.
    54  // TODO(coryrc): Remove once other repos are moved to call logging.InitializeLogger() directly
    55  func SetupLoggingFlags() {
    56  	logging.InitializeLogger()
    57  }
    58  
    59  // ImagePath is a helper function to transform an image name into an image reference that can be pulled.
    60  func ImagePath(name string) string {
    61  	tpl, err := template.New("image").Parse(Flags.ImageTemplate)
    62  	if err != nil {
    63  		panic("could not parse image template: " + err.Error())
    64  	}
    65  
    66  	var buf bytes.Buffer
    67  	if err := tpl.Execute(&buf, struct {
    68  		Repository string
    69  		Name       string
    70  		Tag        string
    71  	}{
    72  		Repository: Flags.DockerRepo,
    73  		Name:       name,
    74  		Tag:        Flags.Tag,
    75  	}); err != nil {
    76  		panic("could not apply the image template: " + err.Error())
    77  	}
    78  	return buf.String()
    79  }