Files
terraform-provider-unifi/internal/provider/testing/test_helpers_network.go
Mateusz Filipowicz e7164c0460 feat: add DNS record resource and datasources (#25)
* add DNS record

* revamp tests

* lint

* cleanup

* feat dns test

* chore: add DNS Record tests

* linting

* f
2025-02-26 01:17:59 +01:00

69 lines
1.2 KiB
Go

package testing
import (
"github.com/apparentlymart/go-cidr/cidr"
mapset "github.com/deckarep/golang-set/v2"
"math"
"net"
"sync"
"testing"
)
const (
vlanMin = 2
vlanMax = 4095
)
var (
macInit sync.Once
macPool = mapset.NewSet[*net.HardwareAddr]()
network = &net.IPNet{
IP: net.IPv4(10, 0, 0, 0).To4(),
Mask: net.IPv4Mask(255, 0, 0, 0),
}
vlanLock sync.Mutex
vlanNext = vlanMin
)
func GetTestVLAN(t *testing.T) (*net.IPNet, int) {
vlanLock.Lock()
defer vlanLock.Unlock()
vlan := vlanNext
vlanNext++
subnet, err := cidr.Subnet(network, int(math.Ceil(math.Log2(vlanMax))), vlan)
if err != nil {
t.Error(err)
}
return subnet, vlan
}
func AllocateTestMac(t *testing.T) (string, func()) {
MarkAccTest(t)
macInit.Do(func() {
// for test MAC addresses, see https://tools.ietf.org/html/rfc7042#section-2.1.
for i := 0; i < 512; i++ {
mac := net.HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, byte(i)}
if ok := macPool.Add(&mac); !ok {
t.Fatal("Failed to add MAC to pool")
}
}
})
mac, ok := macPool.Pop()
if mac == nil || !ok {
t.Fatal("Unable to allocate test MAC")
}
unallocate := func() {
if ok := macPool.Add(mac); !ok {
t.Fatal("Failed to add MAC to pool")
}
}
return mac.String(), unallocate
}