Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[e2e] Use Subnet UID to get NSX resources in the test #957

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions test/e2e/nsx_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ func SubnetCIDR(t *testing.T) {
assureSubnet(t, subnetTestNamespace, subnet.Name)
allocatedSubnet, err := testData.crdClientset.CrdV1alpha1().Subnets(subnetTestNamespace).Get(context.TODO(), subnet.Name, v1.GetOptions{})
require.NoError(t, err)
nsxSubnets := testData.fetchSubnetByNamespace(t, subnetTestNamespace, false)
subnetCRUID := string(allocatedSubnet.UID)
nsxSubnets := testData.fetchSubnetBySubnetUID(t, subnetCRUID)
require.Equal(t, 1, len(nsxSubnets))

targetCIDR := allocatedSubnet.Status.NetworkAddresses[0]
Expand All @@ -315,8 +316,11 @@ func SubnetCIDR(t *testing.T) {
return false, err
})
require.NoError(t, err)
nsxSubnets = testData.fetchSubnetByNamespace(t, subnetTestNamespace, true)
require.Equal(t, true, len(nsxSubnets) <= 1)
err = wait.PollUntilContextTimeout(context.TODO(), 1*time.Second, 100*time.Second, false, func(ctx context.Context) (bool, error) {
nsxSubnets = testData.fetchSubnetBySubnetUID(t, subnetCRUID)
return len(nsxSubnets) == 0 || *nsxSubnets[0].MarkedForDelete == true, nil
})
require.NoError(t, err)

subnet.Spec.IPAddresses = []string{targetCIDR}
_, err = testData.crdClientset.CrdV1alpha1().Subnets(subnetTestNamespace).Create(context.TODO(), subnet, v1.CreateOptions{})
Expand All @@ -330,7 +334,8 @@ func SubnetCIDR(t *testing.T) {
require.NoError(t, err)
require.Equal(t, targetCIDR, allocatedSubnet.Status.NetworkAddresses[0])

nsxSubnets = testData.fetchSubnetByNamespace(t, subnetTestNamespace, false)
newSubnetCRUID := string(allocatedSubnet.UID)
nsxSubnets = testData.fetchSubnetBySubnetUID(t, newSubnetCRUID)
require.Equal(t, 1, len(nsxSubnets))

err = testData.crdClientset.CrdV1alpha1().Subnets(subnetTestNamespace).Delete(context.TODO(), subnet.Name, v1.DeleteOptions{})
Expand All @@ -345,20 +350,17 @@ func SubnetCIDR(t *testing.T) {
})
require.NoError(t, err)

nsxSubnets = testData.fetchSubnetByNamespace(t, subnetTestNamespace, true)
require.Equal(t, true, len(nsxSubnets) <= 1)
assert.Eventually(t, func() bool {
nsxSubnets = testData.fetchSubnetBySubnetUID(t, newSubnetCRUID)
return len(nsxSubnets) == 0 || *nsxSubnets[0].MarkedForDelete == true
}, 100*time.Second, 1*time.Second)
}

func (data *TestData) fetchSubnetByNamespace(t *testing.T, ns string, isMarkForDelete bool) (res []model.VpcSubnet) {
tags := []string{common.TagScopeNamespace, ns}
func (data *TestData) fetchSubnetBySubnetUID(t *testing.T, subnetUID string) (res []model.VpcSubnet) {
tags := []string{common.TagScopeSubnetCRUID, subnetUID}
results, err := testData.queryResource(common.ResourceTypeSubnet, tags)
require.NoError(t, err)
subnets := transSearchResponsetoSubnet(results)
for _, subnet := range subnets {
if *subnet.MarkedForDelete == isMarkForDelete {
res = append(res, subnet)
}
}
res = transSearchResponsetoSubnet(results)
return
}

Expand Down
Loading