istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/resource/flags.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package resource
    16  
    17  import (
    18  	"flag"
    19  	"fmt"
    20  	"os"
    21  	"strings"
    22  
    23  	"istio.io/istio/pkg/log"
    24  	"istio.io/istio/pkg/test/env"
    25  	"istio.io/istio/pkg/test/framework/config"
    26  	"istio.io/istio/pkg/test/framework/label"
    27  )
    28  
    29  var settingsFromCommandLine = DefaultSettings()
    30  
    31  // SettingsFromCommandLine returns settings obtained from command-line flags. config.Parse must be called before
    32  // calling this function.
    33  func SettingsFromCommandLine(testID string) (*Settings, error) {
    34  	if !config.Parsed() {
    35  		panic("config.Parse must be called before this function")
    36  	}
    37  
    38  	s := settingsFromCommandLine.Clone()
    39  	s.TestID = testID
    40  
    41  	f, err := label.ParseSelector(s.SelectorString)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	s.Selector = f
    46  
    47  	s.SkipMatcher, err = NewMatcher(s.SkipString)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	// NOTE: not using echo.VM, etc. here to avoid circular dependency.
    53  	if s.SkipVM {
    54  		s.SkipWorkloadClasses = append(s.SkipWorkloadClasses, "vm")
    55  	}
    56  	if s.SkipTProxy {
    57  		s.SkipWorkloadClasses = append(s.SkipWorkloadClasses, "tproxy")
    58  	}
    59  	// Allow passing a single CSV flag as well
    60  	normalized := make(ArrayFlags, 0)
    61  	for _, sk := range s.SkipWorkloadClasses {
    62  		normalized = append(normalized, strings.Split(sk, ",")...)
    63  	}
    64  	s.SkipWorkloadClasses = normalized
    65  
    66  	if s.Image.Hub == "" {
    67  		s.Image.Hub = env.HUB.ValueOrDefault("gcr.io/istio-testing")
    68  	}
    69  
    70  	if s.Image.Tag == "" {
    71  		s.Image.Tag = env.TAG.ValueOrDefault("latest")
    72  	}
    73  
    74  	if s.Image.Variant == "" {
    75  		s.Image.Variant = env.VARIANT.ValueOrDefault("")
    76  	}
    77  
    78  	if s.Image.PullPolicy == "" {
    79  		s.Image.PullPolicy = env.PULL_POLICY.ValueOrDefault("Always")
    80  	}
    81  
    82  	if s.EchoImage == "" {
    83  		s.EchoImage = env.ECHO_IMAGE.ValueOrDefault("")
    84  	}
    85  
    86  	if s.CustomGRPCEchoImage == "" {
    87  		s.CustomGRPCEchoImage = env.GRPC_ECHO_IMAGE.ValueOrDefault("")
    88  	}
    89  
    90  	if s.HelmRepo == "" {
    91  		s.HelmRepo = "https://istio-release.storage.googleapis.com/charts"
    92  	}
    93  
    94  	if err = validate(s); err != nil {
    95  		return nil, err
    96  	}
    97  
    98  	return s, nil
    99  }
   100  
   101  // validate checks that user has not passed invalid flag combinations to test framework.
   102  func validate(s *Settings) error {
   103  	if s.FailOnDeprecation && s.NoCleanup {
   104  		return fmt.Errorf("checking for deprecation occurs at cleanup level, thus flags -istio.test.nocleanup and" +
   105  			" -istio.test.deprecation_failure must not be used at the same time")
   106  	}
   107  
   108  	if s.Revision != "" {
   109  		if s.Revisions != nil {
   110  			return fmt.Errorf("cannot use --istio.test.revision and --istio.test.revisions at the same time," +
   111  				" --istio.test.revisions will take precedence and --istio.test.revision will be ignored")
   112  		}
   113  		// use Revision as the sole revision in RevVerMap
   114  		s.Revisions = RevVerMap{
   115  			s.Revision: "",
   116  		}
   117  	} else if s.Revisions != nil {
   118  		// TODO(Monkeyanator) remove once existing jobs are migrated to use compatibility flag.
   119  		s.Compatibility = true
   120  	}
   121  
   122  	if s.Revisions == nil && s.Compatibility {
   123  		return fmt.Errorf("cannot use --istio.test.compatibility without setting --istio.test.revisions")
   124  	}
   125  
   126  	if s.Image.Hub == "" || s.Image.Tag == "" {
   127  		return fmt.Errorf("values for Hub & Tag are not detected. Please supply them through command-line or via environment")
   128  	}
   129  
   130  	return nil
   131  }
   132  
   133  // init registers the command-line flags that we can exposed for "go test".
   134  func init() {
   135  	log.EnableKlogWithGoFlag()
   136  	flag.StringVar(&settingsFromCommandLine.BaseDir, "istio.test.work_dir", os.TempDir(),
   137  		"Local working directory for creating logs/temp files. If left empty, os.TempDir() is used.")
   138  
   139  	var env string
   140  	flag.StringVar(&env, "istio.test.env", "", "Deprecated. This flag does nothing")
   141  
   142  	flag.BoolVar(&settingsFromCommandLine.NoCleanup, "istio.test.nocleanup", settingsFromCommandLine.NoCleanup,
   143  		"Do not cleanup resources after test completion")
   144  
   145  	flag.BoolVar(&settingsFromCommandLine.CIMode, "istio.test.ci", settingsFromCommandLine.CIMode,
   146  		"Enable CI Mode. Additional logging and state dumping will be enabled.")
   147  
   148  	flag.StringVar(&settingsFromCommandLine.SelectorString, "istio.test.select", settingsFromCommandLine.SelectorString,
   149  		"Comma separated list of labels for selecting tests to run (e.g. 'foo,+bar-baz').")
   150  
   151  	flag.Var(&settingsFromCommandLine.SkipString, "istio.test.skip",
   152  		"Skip tests matching the regular expression. This follows the semantics of -test.run.")
   153  
   154  	flag.Var(&settingsFromCommandLine.SkipWorkloadClasses, "istio.test.skipWorkloads",
   155  		"Skips deploying and using workloads of the given comma-separated classes (e.g. vm, proxyless, etc.)")
   156  
   157  	flag.Var(&settingsFromCommandLine.OnlyWorkloadClasses, "istio.test.onlyWorkloads",
   158  		"Skips deploying and using workloads not included in the given comma-separated classes (e.g. vm, proxyless, etc.)")
   159  
   160  	flag.IntVar(&settingsFromCommandLine.Retries, "istio.test.retries", settingsFromCommandLine.Retries,
   161  		"Number of times to retry tests")
   162  
   163  	flag.BoolVar(&settingsFromCommandLine.StableNamespaces, "istio.test.stableNamespaces", settingsFromCommandLine.StableNamespaces,
   164  		"If set, will use consistent namespace rather than randomly generated. Useful with nocleanup to develop tests.")
   165  
   166  	flag.BoolVar(&settingsFromCommandLine.FailOnDeprecation, "istio.test.deprecation_failure", settingsFromCommandLine.FailOnDeprecation,
   167  		"Make tests fail if any usage of deprecated stuff (e.g. Envoy flags) is detected.")
   168  
   169  	flag.StringVar(&settingsFromCommandLine.Revision, "istio.test.revision", settingsFromCommandLine.Revision,
   170  		"If set to XXX, overwrite the default namespace label (istio-injection=enabled) with istio.io/rev=XXX.")
   171  
   172  	flag.BoolVar(&settingsFromCommandLine.SkipVM, "istio.test.skipVM", settingsFromCommandLine.SkipVM,
   173  		"Skip VM related parts in all tests.")
   174  
   175  	flag.BoolVar(&settingsFromCommandLine.SkipTProxy, "istio.test.skipTProxy", settingsFromCommandLine.SkipTProxy,
   176  		"Skip TProxy related parts in all tests.")
   177  
   178  	flag.BoolVar(&settingsFromCommandLine.Ambient, "istio.test.ambient", settingsFromCommandLine.Ambient,
   179  		"Indicate the use of ambient mesh.")
   180  
   181  	flag.BoolVar(&settingsFromCommandLine.PeerMetadataDiscovery, "istio.test.peer_metadata_discovery", settingsFromCommandLine.PeerMetadataDiscovery,
   182  		"Force the use of peer metadata discovery fallback for metadata exchange")
   183  
   184  	flag.BoolVar(&settingsFromCommandLine.AmbientEverywhere, "istio.test.ambient.everywhere", settingsFromCommandLine.AmbientEverywhere,
   185  		"Make Waypoint proxies the default instead of sidecar proxies for all echo apps. Must be used with istio.test.ambient")
   186  
   187  	flag.BoolVar(&settingsFromCommandLine.Compatibility, "istio.test.compatibility", settingsFromCommandLine.Compatibility,
   188  		"Transparently deploy echo instances pointing to each revision set in `Revisions`")
   189  
   190  	flag.Var(&settingsFromCommandLine.Revisions, "istio.test.revisions", "Istio CP revisions available to the test framework and their corresponding versions.")
   191  
   192  	flag.StringVar(&settingsFromCommandLine.Image.Hub, "istio.test.hub", settingsFromCommandLine.Image.Hub,
   193  		"Container registry hub to use")
   194  	flag.StringVar(&settingsFromCommandLine.Image.Tag, "istio.test.tag", settingsFromCommandLine.Image.Tag,
   195  		"Common Container tag to use when deploying container images")
   196  	flag.StringVar(&settingsFromCommandLine.Image.Variant, "istio.test.variant", settingsFromCommandLine.Image.Variant,
   197  		"Common Container variant to use when deploying container images")
   198  	flag.StringVar(&settingsFromCommandLine.Image.PullPolicy, "istio.test.pullpolicy", settingsFromCommandLine.Image.PullPolicy,
   199  		"Common image pull policy to use when deploying container images")
   200  	flag.StringVar(&settingsFromCommandLine.Image.PullSecret, "istio.test.imagePullSecret", settingsFromCommandLine.Image.PullSecret,
   201  		"Path to a file containing a DockerConfig secret use for test apps. This will be pushed to all created namespaces."+
   202  			"Secret should already exist when used with istio.test.stableNamespaces.")
   203  	flag.Uint64Var(&settingsFromCommandLine.MaxDumps, "istio.test.maxDumps", settingsFromCommandLine.MaxDumps,
   204  		"Maximum number of full test dumps that are allowed to occur within a test suite.")
   205  	flag.BoolVar(&settingsFromCommandLine.EnableDualStack, "istio.test.enableDualStack", settingsFromCommandLine.EnableDualStack,
   206  		"Deploy Istio with Dual Stack enabled.")
   207  	flag.StringVar(&settingsFromCommandLine.HelmRepo, "istio.test.helmRepo", settingsFromCommandLine.HelmRepo, "Helm repo to use to pull the charts.")
   208  	flag.BoolVar(&settingsFromCommandLine.GatewayConformanceStandardOnly, "istio.test.gatewayConformanceStandardOnly",
   209  		settingsFromCommandLine.GatewayConformanceStandardOnly,
   210  		"If set, only the standard gateway conformance tests will be run; tests relying on experimental resources will be skipped.")
   211  
   212  	flag.BoolVar(&settingsFromCommandLine.OpenShift, "istio.test.openshift", settingsFromCommandLine.OpenShift,
   213  		"Indicate the tests run in an OpenShift platform rather than in plain Kubernetes.")
   214  
   215  	initGatewayConformanceTimeouts()
   216  }
   217  
   218  func initGatewayConformanceTimeouts() {
   219  	flag.DurationVar(&settingsFromCommandLine.GatewayConformanceTimeoutConfig.CreateTimeout, "istio.test.gatewayConformance.createTimeout",
   220  		0, "Gateway conformance test timeout for waiting for creating a k8s resource.")
   221  	flag.DurationVar(&settingsFromCommandLine.GatewayConformanceTimeoutConfig.DeleteTimeout, "istio.test.gatewayConformance.deleteTimeout",
   222  		0, "Gateway conformance test timeout for getting a k8s resource.")
   223  	flag.DurationVar(&settingsFromCommandLine.GatewayConformanceTimeoutConfig.GetTimeout, "istio.test.gatewayConformance.geTimeout",
   224  		0, "Gateway conformance test timeout for getting a k8s resource.")
   225  	flag.DurationVar(&settingsFromCommandLine.GatewayConformanceTimeoutConfig.GatewayMustHaveAddress,
   226  		"istio.test.gatewayConformance.gatewayMustHaveAddressTimeout", 0,
   227  		"Gateway conformance test timeout for waiting for a Gateway to have an address.")
   228  	flag.DurationVar(&settingsFromCommandLine.GatewayConformanceTimeoutConfig.GatewayMustHaveCondition,
   229  		"istio.test.gatewayConformance.gatewayMustHaveConditionTimeout", 0,
   230  		"Gateway conformance test timeout for waiting for a Gateway to have a certain condition.")
   231  	flag.DurationVar(&settingsFromCommandLine.GatewayConformanceTimeoutConfig.GatewayStatusMustHaveListeners,
   232  		"istio.test.gatewayConformance.gatewayStatusMustHaveListenersTimeout", 0,
   233  		"Gateway conformance test timeout for waiting for a Gateway's status to have listeners.")
   234  	flag.DurationVar(&settingsFromCommandLine.GatewayConformanceTimeoutConfig.GatewayListenersMustHaveConditions,
   235  		"istio.test.gatewayConformance.gatewayListenersMustHaveConditionTimeout", 0,
   236  		"Gateway conformance test timeout for waiting for a Gateway's listeners to have certain conditions.")
   237  	flag.DurationVar(&settingsFromCommandLine.GatewayConformanceTimeoutConfig.GWCMustBeAccepted,
   238  		"istio.test.gatewayConformance.gatewayClassMustBeAcceptedTimeout",
   239  		0, "Gateway conformance test timeout for waiting for a GatewayClass to be accepted.")
   240  	flag.DurationVar(&settingsFromCommandLine.GatewayConformanceTimeoutConfig.HTTPRouteMustNotHaveParents,
   241  		"istio.test.gatewayConformance.httpRouteMustNotHaveParentsTimeout", 0,
   242  		"Gateway conformance test timeout for waiting for an HTTPRoute to either have no parents or a single parent that is not accepted.")
   243  	flag.DurationVar(&settingsFromCommandLine.GatewayConformanceTimeoutConfig.HTTPRouteMustHaveCondition,
   244  		"istio.test.gatewayConformance.httpRouteMustHaveConditionTimeout",
   245  		0, "Gateway conformance test timeout for waiting for an HTTPRoute to have a certain condition.")
   246  	flag.DurationVar(&settingsFromCommandLine.GatewayConformanceTimeoutConfig.TLSRouteMustHaveCondition,
   247  		"istio.test.gatewayConformance.tlsRouteMustHaveConditionTimeout", 0,
   248  		"Gateway conformance test timeout for waiting for an TLSRoute to have a certain condition.")
   249  	flag.DurationVar(&settingsFromCommandLine.GatewayConformanceTimeoutConfig.RouteMustHaveParents, "istio.test.gatewayConformance.routeMustHaveParentsTimeout",
   250  		0, "Gateway conformance test timeout for the the maximum time for an xRoute to have parents in status that match the expected parents.")
   251  	flag.DurationVar(&settingsFromCommandLine.GatewayConformanceTimeoutConfig.ManifestFetchTimeout, "istio.test.gatewayConformance.manifestFetchTimeout",
   252  		0, "Gateway conformance test timeout for the maximum time for getting content from a https:// URL.")
   253  	flag.DurationVar(&settingsFromCommandLine.GatewayConformanceTimeoutConfig.MaxTimeToConsistency, "istio.test.gatewayConformance.maxTimeToConsistency",
   254  		0, "Gateway conformance test setting for the maximum time for requiredConsecutiveSuccesses (default 3) requests to succeed in a row before failing the test.")
   255  	flag.DurationVar(&settingsFromCommandLine.GatewayConformanceTimeoutConfig.NamespacesMustBeReady,
   256  		"istio.test.gatewayConformance.namespacesMustBeReadyTimeout", 0,
   257  		"Gateway conformance test timeout for waiting for namespaces to be ready.")
   258  	flag.DurationVar(&settingsFromCommandLine.GatewayConformanceTimeoutConfig.RequestTimeout, "istio.test.gatewayConformance.requestTimeout",
   259  		0, "Gateway conformance test timeout for an HTTP request.")
   260  	flag.DurationVar(&settingsFromCommandLine.GatewayConformanceTimeoutConfig.LatestObservedGenerationSet,
   261  		"istio.test.gatewayConformance.latestObservedGenerationSetTimeout", 0,
   262  		"Gateway conformance test timeout for waiting for a latest observed generation to be set.")
   263  	flag.DurationVar(&settingsFromCommandLine.GatewayConformanceTimeoutConfig.DefaultTestTimeout, "istio.test.gatewayConformance.defaultTestTimeout",
   264  		0, "Default gateway conformance test case timeout.")
   265  	flag.IntVar(&settingsFromCommandLine.GatewayConformanceTimeoutConfig.RequiredConsecutiveSuccesses,
   266  		"istio.test.gatewayConformance.requiredConsecutiveSuccesses", 0,
   267  		"Gateway conformance test setting for the required number of consecutive successes before failing the test.")
   268  }