github.com/vipernet-xyz/tm@v0.34.24/test/e2e/pkg/infrastructure.go (about)

     1  package e2e
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net"
     7  	"os"
     8  )
     9  
    10  const (
    11  	dockerIPv4CIDR = "10.186.73.0/24"
    12  	dockerIPv6CIDR = "fd80:b10c::/48"
    13  
    14  	globalIPv4CIDR = "0.0.0.0/0"
    15  )
    16  
    17  // InfrastructureData contains the relevant information for a set of existing
    18  // infrastructure that is to be used for running a testnet.
    19  type InfrastructureData struct {
    20  
    21  	// Provider is the name of infrastructure provider backing the testnet.
    22  	// For example, 'docker' if it is running locally in a docker network or
    23  	// 'digital-ocean', 'aws', 'google', etc. if it is from a cloud provider.
    24  	Provider string `json:"provider"`
    25  
    26  	// Instances is a map of all of the machine instances on which to run
    27  	// processes for a testnet.
    28  	// The key of the map is the name of the instance, which each must correspond
    29  	// to the names of one of the testnet nodes defined in the testnet manifest.
    30  	Instances map[string]InstanceData `json:"instances"`
    31  
    32  	// Network is the CIDR notation range of IP addresses that all of the instances'
    33  	// IP addresses are expected to be within.
    34  	Network string `json:"network"`
    35  }
    36  
    37  // InstanceData contains the relevant information for a machine instance backing
    38  // one of the nodes in the testnet.
    39  type InstanceData struct {
    40  	IPAddress net.IP `json:"ip_address"`
    41  }
    42  
    43  func NewDockerInfrastructureData(m Manifest) (InfrastructureData, error) {
    44  	netAddress := dockerIPv4CIDR
    45  	if m.IPv6 {
    46  		netAddress = dockerIPv6CIDR
    47  	}
    48  	_, ipNet, err := net.ParseCIDR(netAddress)
    49  	if err != nil {
    50  		return InfrastructureData{}, fmt.Errorf("invalid IP network address %q: %w", netAddress, err)
    51  	}
    52  	ipGen := newIPGenerator(ipNet)
    53  	ifd := InfrastructureData{
    54  		Provider:  "docker",
    55  		Instances: make(map[string]InstanceData),
    56  		Network:   netAddress,
    57  	}
    58  	for name := range m.Nodes {
    59  		ifd.Instances[name] = InstanceData{
    60  			IPAddress: ipGen.Next(),
    61  		}
    62  	}
    63  	return ifd, nil
    64  }
    65  
    66  func InfrastructureDataFromFile(p string) (InfrastructureData, error) {
    67  	ifd := InfrastructureData{}
    68  	b, err := os.ReadFile(p)
    69  	if err != nil {
    70  		return InfrastructureData{}, err
    71  	}
    72  	err = json.Unmarshal(b, &ifd)
    73  	if err != nil {
    74  		return InfrastructureData{}, err
    75  	}
    76  	if ifd.Network == "" {
    77  		ifd.Network = globalIPv4CIDR
    78  	}
    79  	return ifd, nil
    80  }