-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add e2e test case for IPAddressAllocation
- Loading branch information
1 parent
a1693cd
commit 14c4bc4
Showing
4 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
test/e2e/manifest/testIPAddressAllocation/ipaddressallocation_external.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
apiVersion: crd.nsx.vmware.com/v1alpha1 | ||
kind: IPAddressAllocation | ||
metadata: | ||
name: guestcluster-workers-b | ||
spec: | ||
ipAddressBlockVisibility: External | ||
allocationSize: 32 |
7 changes: 7 additions & 0 deletions
7
test/e2e/manifest/testIPAddressAllocation/ipaddressallocation_private.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
apiVersion: crd.nsx.vmware.com/v1alpha1 | ||
kind: IPAddressAllocation | ||
metadata: | ||
name: guestcluster-workers-a | ||
spec: | ||
ipAddressBlockVisibility: Private | ||
allocationSize: 32 |
7 changes: 7 additions & 0 deletions
7
test/e2e/manifest/testIPAddressAllocation/ipaddressallocation_privatetgw.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
apiVersion: crd.nsx.vmware.com/v1alpha1 | ||
kind: IPAddressAllocation | ||
metadata: | ||
name: guestcluster-workers-c | ||
spec: | ||
ipAddressBlockVisibility: PrivateTGW | ||
allocationSize: 32 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package e2e | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
|
||
"github.com/vmware-tanzu/nsx-operator/pkg/apis/vpc/v1alpha1" | ||
) | ||
|
||
const ( | ||
externalCIDR = "192.168.0.0/27" | ||
privateCIDR = "172.26.0.0/27" | ||
privateTGWCIDR = "10.246.0.0/27" | ||
) | ||
|
||
func TestIPAddressAllocation(t *testing.T) { | ||
t.Run("testIPAddressAllocationExternal", func(t *testing.T) { | ||
testIPAddressAllocation(t, "./manifest/testIPAddressAllocation/ipaddressallocation_external.yaml", externalCIDR) | ||
}) | ||
t.Run("testIPAddressAllocationPrivate", func(t *testing.T) { | ||
testIPAddressAllocation(t, "./manifest/testIPAddressAllocation/ipaddressallocation_private.yaml", privateCIDR) | ||
}) | ||
t.Run("testIPAddressAllocationPrivateTGW", func(t *testing.T) { | ||
testIPAddressAllocation(t, "./manifest/testIPAddressAllocation/ipaddressallocation_privatetgw.yaml", privateTGWCIDR) | ||
}) | ||
} | ||
|
||
func testIPAddressAllocation(t *testing.T, yamlPath string, expectedCIDR string) { | ||
deadlineCtx, deadlineCancel := context.WithTimeout(context.Background(), defaultTimeout) | ||
defer deadlineCancel() | ||
|
||
ns := "test-ipaddress-allocation" | ||
var err error | ||
|
||
setupTest(t, ns) | ||
defer teardownTest(t, ns, defaultTimeout) | ||
|
||
// Parse YAML to get CR's name | ||
ipAllocPath, _ := filepath.Abs(yamlPath) | ||
require.NoError(t, applyYAML(ipAllocPath, ns)) | ||
defer func(filename string, ns string) { | ||
_ = deleteYAML(filename, ns) | ||
}(ipAllocPath, ns) | ||
|
||
// Assume the name is the same as defined in the respective YAML | ||
ipAllocName := getNameFromYAML(ipAllocPath) | ||
assureIPAddressAllocationReady(t, ns, ipAllocName) | ||
|
||
// Check AllocationIPs | ||
assertAllocationCIDR(t, ns, ipAllocName, expectedCIDR) | ||
|
||
// Delete IPAddressAllocation | ||
_ = deleteYAML(ipAllocPath, ns) | ||
|
||
err = wait.PollUntilContextTimeout(deadlineCtx, 1*time.Second, defaultTimeout, false, func(ctx context.Context) (done bool, err error) { | ||
resp, err := testData.crdClientset.CrdV1alpha1().IPAddressAllocations(ns).Get(ctx, ipAllocName, v1.GetOptions{}) | ||
t.Logf("Check resource: %v", resp) | ||
if err != nil { | ||
if errors.IsNotFound(err) { | ||
return true, nil | ||
} | ||
return false, fmt.Errorf("error when waiting for IPAddressAllocation %s", ipAllocName) | ||
} | ||
return false, nil | ||
}) | ||
require.NoError(t, err) | ||
} | ||
|
||
func assureIPAddressAllocationReady(t *testing.T, ns, ipAllocName string) { | ||
deadlineCtx, deadlineCancel := context.WithTimeout(context.Background(), defaultTimeout) | ||
defer deadlineCancel() | ||
err := wait.PollUntilContextTimeout(deadlineCtx, 1*time.Second, defaultTimeout, false, func(ctx context.Context) (done bool, err error) { | ||
resp, err := testData.crdClientset.CrdV1alpha1().IPAddressAllocations(ns).Get(context.Background(), ipAllocName, v1.GetOptions{}) | ||
t.Logf("Get IPAddressAllocations: %v, Namespace: %s, Name: %s", resp, ns, ipAllocName) | ||
if err != nil { | ||
return false, fmt.Errorf("error when waiting for %s", ipAllocName) | ||
} | ||
for _, con := range resp.Status.Conditions { | ||
if con.Type == v1alpha1.Ready && resp.Status.AllocationIPs != "" { | ||
return true, nil | ||
} | ||
} | ||
return false, nil | ||
}) | ||
require.NoError(t, err) | ||
} | ||
|
||
func assertAllocationCIDR(t *testing.T, ns, ipAllocName, expectedCIDR string) { | ||
_, expectedNet, err := net.ParseCIDR(expectedCIDR) | ||
require.NoError(t, err) | ||
|
||
deadlineCtx, deadlineCancel := context.WithTimeout(context.Background(), defaultTimeout) | ||
defer deadlineCancel() | ||
err = wait.PollUntilContextTimeout(deadlineCtx, 1*time.Second, defaultTimeout, false, func(ctx context.Context) (bool, error) { | ||
resp, err := testData.crdClientset.CrdV1alpha1().IPAddressAllocations(ns).Get(context.Background(), ipAllocName, v1.GetOptions{}) | ||
if err != nil { | ||
return false, err | ||
} | ||
if resp.Status.AllocationIPs != "" { | ||
allocCIDR := resp.Status.AllocationIPs | ||
_, allocNet, err := net.ParseCIDR(allocCIDR) | ||
if err != nil { | ||
return false, err | ||
} | ||
if expectedNet.Contains(allocNet.IP) && allocNet.Contains(expectedNet.IP) { | ||
return true, nil | ||
} | ||
return true, nil | ||
} | ||
return false, nil | ||
}) | ||
require.NoError(t, err, "Failed to verify AllocationIPs CIDR for IPAddressAllocation %s", ipAllocName) | ||
} | ||
|
||
func getNameFromYAML(yamlPath string) string { | ||
// Manually extract the CR's name from the filename, adjust logic if necessary | ||
switch filepath.Base(yamlPath) { | ||
case "ipaddressallocation_external.yaml": | ||
return "guestcluster-workers-b" | ||
case "ipaddressallocation_private.yaml": | ||
return "guestcluster-workers-a" | ||
case "ipaddressallocation_privatetgw.yaml": | ||
return "guestcluster-workers-c" | ||
default: | ||
panic("Unknown YAML file") | ||
} | ||
} |