github.com/apache/beam/sdks/v2@v2.48.2/go/test/integration/flags.go (about)

     1  // Licensed to the Apache Software Foundation (ASF) under one or more
     2  // contributor license agreements.  See the NOTICE file distributed with
     3  // this work for additional information regarding copyright ownership.
     4  // The ASF licenses this file to You under the Apache License, Version 2.0
     5  // (the "License"); you may not use this file except in compliance with
     6  // the License.  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  package integration
    17  
    18  import (
    19  	"flag"
    20  	"fmt"
    21  	"strings"
    22  )
    23  
    24  // The following flags are flags used in one or more integration tests, and that
    25  // may be used by scripts that execute "go test ./sdks/go/test/integration/...".
    26  // Because any flags used with those commands are used for each package, every
    27  // integration test package must import these flags, even if they are not used.
    28  var (
    29  	// BootstrapServers is the address of the bootstrap servers for a Kafka
    30  	// cluster, used for Kafka IO tests.
    31  	BootstrapServers = flag.String("bootstrap_servers", "",
    32  		"URL of the bootstrap servers for the Kafka cluster. Should be accessible by the runner.")
    33  
    34  	// KafkaJar is a filepath to a jar for starting a Kafka cluster, used for
    35  	// Kafka IO tests.
    36  	KafkaJar = flag.String("kafka_jar", "",
    37  		"The filepath to a jar for starting up a Kafka cluster. Only used if boostrap_servers is unspecified.")
    38  
    39  	// KafkaJarTimeout attempts to apply an auto-shutdown timeout to the Kafka
    40  	// cluster jar. Only used for Kafka IO tests.
    41  	KafkaJarTimeout = flag.String("kafka_jar_timeout", "10m",
    42  		"Sets an auto-shutdown timeout to the Kafka cluster. "+
    43  			"Requires the timeout command to be present in Path, unless the value is set to \"\".")
    44  
    45  	// BigQueryDataset is the name of the dataset to create tables in for
    46  	// BigQuery integration tests.
    47  	BigQueryDataset = flag.String("bq_dataset", "",
    48  		"Name of the dataset to create tables in for BigQuery tests.")
    49  
    50  	// ExpansionJars contains elements in the form "label:jar" describing jar
    51  	// filepaths for expansion services to use in integration tests, and the
    52  	// corresponding labels. Once provided through this flag, those jars can
    53  	// be used in tests via the ExpansionServices struct.
    54  	ExpansionJars stringSlice
    55  
    56  	// ExpansionAddrs contains elements in the form "label:address" describing
    57  	// endpoints for expansion services to use in integration tests, and the
    58  	// corresponding labels. Once provided through this flag, those addresses
    59  	// can be used in tests via the ExpansionServices struct.
    60  	ExpansionAddrs stringSlice
    61  
    62  	// ExpansionTimeout attempts to apply an auto-shutdown timeout to any
    63  	// expansion services started by integration tests.
    64  	ExpansionTimeout = flag.Duration("expansion_timeout", 0,
    65  		"Sets an auto-shutdown timeout to any started expansion services. "+
    66  			"Requires the timeout command to be present in Path, unless the value is set to 0.")
    67  )
    68  
    69  func init() {
    70  	flag.Var(&ExpansionJars, "expansion_jar",
    71  		"Define jar locations for expansion services. Each entry consists of "+
    72  			"two values, an arbitrary label and a jar filepath, separated by a "+
    73  			"\":\", in the form \"label:jar\". Jars provided through this flag "+
    74  			"can be started by tests.")
    75  	flag.Var(&ExpansionAddrs, "expansion_addr",
    76  		"Define addresses for expansion services. Each entry consists of "+
    77  			"two values, an arbitrary label and an address, separated by a "+
    78  			"\":\", in the form \"label:address\". Addresses provided through "+
    79  			"this flag can be used as expansion addresses by tests.")
    80  }
    81  
    82  // GetExpansionJars gets all the jars given to --expansion_jar as a map of label to jar location.
    83  func GetExpansionJars() map[string]string {
    84  	ret := make(map[string]string)
    85  	for _, jar := range ExpansionJars {
    86  		splits := strings.SplitN(jar, ":", 2)
    87  		ret[splits[0]] = splits[1]
    88  	}
    89  	return ret
    90  }
    91  
    92  // GetExpansionAddrs gets all the addresses given to --expansion_addr as a map of label to address.
    93  func GetExpansionAddrs() map[string]string {
    94  	ret := make(map[string]string)
    95  	for _, addr := range ExpansionAddrs {
    96  		splits := strings.SplitN(addr, ":", 2)
    97  		ret[splits[0]] = splits[1]
    98  	}
    99  	return ret
   100  }
   101  
   102  // stringSlice is a flag.Value implementation for string slices, that allows
   103  // multiple strings to be assigned to one flag by specifying multiple instances
   104  // of the flag.
   105  //
   106  // Example:
   107  //
   108  //	var myFlags stringSlice
   109  //	flag.Var(&myFlags, "my_flag", "A list of flags")
   110  //
   111  // With the example above, the slice can be set to contain ["foo", "bar"]:
   112  //
   113  //	cmd -my_flag foo -my_flag bar
   114  type stringSlice []string
   115  
   116  // String implements the String method of flag.Value. This outputs the value
   117  // of the flag as a string.
   118  func (s *stringSlice) String() string {
   119  	return fmt.Sprintf("%v", *s)
   120  }
   121  
   122  // Set implements the Set method of flag.Value. This stores a string input to
   123  // the flag into a stringSlice representation.
   124  func (s *stringSlice) Set(value string) error {
   125  	*s = append(*s, value)
   126  	return nil
   127  }
   128  
   129  // Get returns the instance itself.
   130  func (s stringSlice) Get() any {
   131  	return s
   132  }