diff options
author | adrianvillin <avillin@cisco.com> | 2024-02-13 06:00:02 -0500 |
---|---|---|
committer | Florin Coras <florin.coras@gmail.com> | 2024-03-12 16:48:46 +0000 |
commit | 28bd8f07d33bdd70c2cac03b70911a9069e4bd50 (patch) | |
tree | 102bab11aa225339995a4dd07ea8332d1fce2212 /extras/hs-test/address_allocator.go | |
parent | 7a2961523ef6d58d718a9efb7809a212bb0b58b1 (diff) |
hs-test: experimental support for multiple test instances
- appending PID to interface and container names
- added a check for used IP addresses
- TestEnvoyProxyHttpTcp and TestTcpWithLoss are broken when running multiple instances
Type: test
Change-Id: Ib917079ec7cf851dee59ff9c67e74f48c7c5e3c6
Signed-off-by: adrianvillin <avillin@cisco.com>
Diffstat (limited to 'extras/hs-test/address_allocator.go')
-rw-r--r-- | extras/hs-test/address_allocator.go | 60 |
1 files changed, 58 insertions, 2 deletions
diff --git a/extras/hs-test/address_allocator.go b/extras/hs-test/address_allocator.go index c08f88958dc..72bc298fd3f 100644 --- a/extras/hs-test/address_allocator.go +++ b/extras/hs-test/address_allocator.go @@ -1,11 +1,20 @@ package main -import "fmt" +import ( + "errors" + "fmt" + "os" + "strings" + + "github.com/edwarnicke/exechelper" +) type AddressCounter = int type Ip4AddressAllocator struct { networks map[int]AddressCounter + chosenOctet int + assignedIps []string } func (a *Ip4AddressAllocator) AddNetwork(networkNumber int) { @@ -28,12 +37,59 @@ func (a *Ip4AddressAllocator) NewIp4InterfaceAddress(inputNetworkNumber ...int) return "", fmt.Errorf("no available IPv4 addresses") } - address := fmt.Sprintf("10.10.%v.%v/24", networkNumber, numberOfAddresses) + address, err := a.createIpAddress(networkNumber, numberOfAddresses) + a.networks[networkNumber] = numberOfAddresses + 1 + return address + "/24", err +} + +// Creates a file every time an IP is assigned: used to keep track of addresses in use. +// If an address is not in use, 'counter' is then copied to 'chosenOctet' and it is used for the remaining tests. +// Also checks host IP addresses. +func (a *Ip4AddressAllocator) createIpAddress(networkNumber int, numberOfAddresses int) (string, error){ + hostIps, _ := exechelper.CombinedOutput("ip a") + counter := 10 + var address string + + for { + if a.chosenOctet != 0 { + address = fmt.Sprintf("10.%v.%v.%v", a.chosenOctet, networkNumber, numberOfAddresses) + file, err := os.Create(address) + if err != nil{ + return "", errors.New("unable to create file: " + fmt.Sprint(err)) + } + file.Close() + break + } else { + address = fmt.Sprintf("10.%v.%v.%v", counter, networkNumber, numberOfAddresses) + _, err := os.Stat(address) + if err == nil || strings.Contains(string(hostIps), address) { + counter++ + } else if os.IsNotExist(err) { + file, err := os.Create(address) + if err != nil{ + return "", errors.New("unable to create file: " + fmt.Sprint(err)) + } + file.Close() + a.chosenOctet = counter + break + } else { + return "", errors.New("an error occured while checking if a file exists: " + fmt.Sprint(err)) + } + } + } + + a.assignedIps = append(a.assignedIps, address) return address, nil } +func (a *Ip4AddressAllocator) deleteIpAddresses(){ + for ip := range a.assignedIps{ + os.Remove(a.assignedIps[ip]) + } +} + func NewIp4AddressAllocator() *Ip4AddressAllocator { var ip4AddrAllocator = new(Ip4AddressAllocator) ip4AddrAllocator.networks = make(map[int]AddressCounter) |