|  | 
|  | 1 | +package e2e | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"context" | 
|  | 5 | +	"fmt" | 
|  | 6 | +	"net" | 
|  | 7 | +	"path/filepath" | 
|  | 8 | +	"testing" | 
|  | 9 | +	"time" | 
|  | 10 | + | 
|  | 11 | +	"github.com/stretchr/testify/require" | 
|  | 12 | +	"k8s.io/apimachinery/pkg/api/errors" | 
|  | 13 | +	v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | 
|  | 14 | +	"k8s.io/apimachinery/pkg/util/wait" | 
|  | 15 | + | 
|  | 16 | +	"github.com/vmware-tanzu/nsx-operator/pkg/apis/vpc/v1alpha1" | 
|  | 17 | +) | 
|  | 18 | + | 
|  | 19 | +const ( | 
|  | 20 | +	externalCIDR   = "192.168.0.0/16" | 
|  | 21 | +	privateCIDR    = "172.26.0.0/16" | 
|  | 22 | +	privateTGWCIDR = "10.246.0.0/16" | 
|  | 23 | +	ns             = "test-ipaddress-allocation" | 
|  | 24 | +) | 
|  | 25 | + | 
|  | 26 | +func TestIPAddressAllocation(t *testing.T) { | 
|  | 27 | +	setupTest(t, ns) | 
|  | 28 | +	defer teardownTest(t, ns, defaultTimeout) | 
|  | 29 | +	t.Run("testIPAddressAllocationExternal", func(t *testing.T) { | 
|  | 30 | +		testIPAddressAllocation(t, "./manifest/testIPAddressAllocation/ipaddressallocation_external.yaml", externalCIDR) | 
|  | 31 | +	}) | 
|  | 32 | +	t.Run("testIPAddressAllocationPrivate", func(t *testing.T) { | 
|  | 33 | +		testIPAddressAllocation(t, "./manifest/testIPAddressAllocation/ipaddressallocation_private.yaml", privateCIDR) | 
|  | 34 | +	}) | 
|  | 35 | +	t.Run("testIPAddressAllocationPrivateTGW", func(t *testing.T) { | 
|  | 36 | +		testIPAddressAllocation(t, "./manifest/testIPAddressAllocation/ipaddressallocation_privatetgw.yaml", privateTGWCIDR) | 
|  | 37 | +	}) | 
|  | 38 | +} | 
|  | 39 | + | 
|  | 40 | +func testIPAddressAllocation(t *testing.T, yamlPath string, expectedCIDR string) { | 
|  | 41 | +	deadlineCtx, deadlineCancel := context.WithTimeout(context.Background(), defaultTimeout) | 
|  | 42 | +	defer deadlineCancel() | 
|  | 43 | + | 
|  | 44 | +	var err error | 
|  | 45 | + | 
|  | 46 | +	// Parse YAML to get CR's name | 
|  | 47 | +	ipAllocPath, _ := filepath.Abs(yamlPath) | 
|  | 48 | +	require.NoError(t, applyYAML(ipAllocPath, ns)) | 
|  | 49 | + | 
|  | 50 | +	// Assume the name is the same as defined in the respective YAML | 
|  | 51 | +	ipAllocName := getNameFromYAML(ipAllocPath) | 
|  | 52 | +	assureIPAddressAllocationReady(t, ns, ipAllocName) | 
|  | 53 | + | 
|  | 54 | +	// Check AllocationIPs | 
|  | 55 | +	assertAllocationCIDR(t, ns, ipAllocName, expectedCIDR) | 
|  | 56 | + | 
|  | 57 | +	// Delete IPAddressAllocation | 
|  | 58 | +	_ = deleteYAML(ipAllocPath, ns) | 
|  | 59 | + | 
|  | 60 | +	err = wait.PollUntilContextTimeout(deadlineCtx, 1*time.Second, defaultTimeout, false, func(ctx context.Context) (done bool, err error) { | 
|  | 61 | +		resp, err := testData.crdClientset.CrdV1alpha1().IPAddressAllocations(ns).Get(ctx, ipAllocName, v1.GetOptions{}) | 
|  | 62 | +		log.V(2).Info("Check resource", "resp", resp) | 
|  | 63 | +		if err != nil { | 
|  | 64 | +			if errors.IsNotFound(err) { | 
|  | 65 | +				return true, nil | 
|  | 66 | +			} | 
|  | 67 | +			return false, fmt.Errorf("error when waiting for IPAddressAllocation %s", ipAllocName) | 
|  | 68 | +		} | 
|  | 69 | +		return false, nil | 
|  | 70 | +	}) | 
|  | 71 | +	require.NoError(t, err) | 
|  | 72 | +} | 
|  | 73 | + | 
|  | 74 | +func assureIPAddressAllocationReady(t *testing.T, ns, ipAllocName string) { | 
|  | 75 | +	deadlineCtx, deadlineCancel := context.WithTimeout(context.Background(), defaultTimeout) | 
|  | 76 | +	defer deadlineCancel() | 
|  | 77 | +	err := wait.PollUntilContextTimeout(deadlineCtx, 1*time.Second, defaultTimeout, false, func(ctx context.Context) (done bool, err error) { | 
|  | 78 | +		resp, err := testData.crdClientset.CrdV1alpha1().IPAddressAllocations(ns).Get(context.Background(), ipAllocName, v1.GetOptions{}) | 
|  | 79 | +		log.V(2).Info("Get IPAddressAllocations", "Namespace", ns, "Name", ipAllocName) | 
|  | 80 | +		if err != nil { | 
|  | 81 | +			return false, fmt.Errorf("error when waiting for %s", ipAllocName) | 
|  | 82 | +		} | 
|  | 83 | +		for _, con := range resp.Status.Conditions { | 
|  | 84 | +			if con.Type == v1alpha1.Ready && resp.Status.AllocationIPs != "" { | 
|  | 85 | +				return true, nil | 
|  | 86 | +			} | 
|  | 87 | +		} | 
|  | 88 | +		return false, nil | 
|  | 89 | +	}) | 
|  | 90 | +	require.NoError(t, err) | 
|  | 91 | +} | 
|  | 92 | + | 
|  | 93 | +func assertAllocationCIDR(t *testing.T, ns, ipAllocName, expectedCIDR string) { | 
|  | 94 | +	_, expectedNet, err := net.ParseCIDR(expectedCIDR) | 
|  | 95 | +	require.NoError(t, err) | 
|  | 96 | + | 
|  | 97 | +	deadlineCtx, deadlineCancel := context.WithTimeout(context.Background(), defaultTimeout) | 
|  | 98 | +	defer deadlineCancel() | 
|  | 99 | +	err = wait.PollUntilContextTimeout(deadlineCtx, 1*time.Second, defaultTimeout, false, func(ctx context.Context) (bool, error) { | 
|  | 100 | +		resp, err := testData.crdClientset.CrdV1alpha1().IPAddressAllocations(ns).Get(context.Background(), ipAllocName, v1.GetOptions{}) | 
|  | 101 | +		if err != nil { | 
|  | 102 | +			return false, err | 
|  | 103 | +		} | 
|  | 104 | +		if resp.Status.AllocationIPs != "" { | 
|  | 105 | +			allocCIDR := resp.Status.AllocationIPs | 
|  | 106 | +			_, allocNet, err := net.ParseCIDR(allocCIDR) | 
|  | 107 | +			if err != nil { | 
|  | 108 | +				return false, err | 
|  | 109 | +			} | 
|  | 110 | +			if expectedNet.Contains(allocNet.IP) && allocNet.Contains(expectedNet.IP) { | 
|  | 111 | +				return true, nil | 
|  | 112 | +			} | 
|  | 113 | +			return true, nil | 
|  | 114 | +		} | 
|  | 115 | +		return false, nil | 
|  | 116 | +	}) | 
|  | 117 | +	require.NoError(t, err, "Failed to verify AllocationIPs CIDR for IPAddressAllocation %s", ipAllocName) | 
|  | 118 | +} | 
|  | 119 | + | 
|  | 120 | +func getNameFromYAML(yamlPath string) string { | 
|  | 121 | +	// Manually extract the CR's name from the filename, adjust logic if necessary | 
|  | 122 | +	switch filepath.Base(yamlPath) { | 
|  | 123 | +	case "ipaddressallocation_external.yaml": | 
|  | 124 | +		return "guestcluster-workers-b" | 
|  | 125 | +	case "ipaddressallocation_private.yaml": | 
|  | 126 | +		return "guestcluster-workers-a" | 
|  | 127 | +	case "ipaddressallocation_privatetgw.yaml": | 
|  | 128 | +		return "guestcluster-workers-c" | 
|  | 129 | +	default: | 
|  | 130 | +		panic("Unknown YAML file") | 
|  | 131 | +	} | 
|  | 132 | +} | 
0 commit comments