vitess.io/vitess@v0.16.2/go/testfiles/ports.go (about)

     1  /*
     2  Copyright 2019 The Vitess 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  package testfiles
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"strconv"
    23  )
    24  
    25  // This file contains helper methods and declarations so all unit
    26  // tests use different ports.
    27  //
    28  // We also use it to allocate Zookeeper server IDs.
    29  
    30  // Port definitions. Unit tests may run at the same time,
    31  // so they should not use the same ports.
    32  var (
    33  	// vtPortStart is the starting port for all tests.
    34  	vtPortStart = getPortStart()
    35  
    36  	// GoVtTopoEtcd2topoPort is used by the go/vt/topo/etcd2topo package.
    37  	// Takes two ports.
    38  	GoVtTopoEtcd2topoPort = vtPortStart
    39  
    40  	// GoVtTopoZk2topoPort is used by the go/vt/topo/zk2topo package.
    41  	// Takes three ports.
    42  	GoVtTopoZk2topoPort = GoVtTopoEtcd2topoPort + 2
    43  
    44  	// GoVtTopoConsultopoPort is used by the go/vt/topo/consultopo package.
    45  	// Takes four ports.
    46  	GoVtTopoConsultopoPort = GoVtTopoZk2topoPort + 3
    47  )
    48  
    49  // Zookeeper server ID definitions. Unit tests may run at the
    50  // same time, so they can't use the same Zookeeper server IDs.
    51  var (
    52  	// GoVtTopoZk2topoZkID is used by the go/vt/topo/zk2topo package.
    53  	GoVtTopoZk2topoZkID = 1
    54  )
    55  
    56  func getPortStart() int {
    57  	env := os.Getenv("VTPORTSTART")
    58  	if env == "" {
    59  		env = "6700"
    60  	}
    61  	portStart, err := strconv.Atoi(env)
    62  	if err != nil {
    63  		panic(fmt.Errorf("cannot parse VTPORTSTART: %v", err))
    64  	}
    65  	return portStart
    66  }