Skip to content

Commit

Permalink
e2e: wait for subnet to meet specified condition (#2431)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzujian committed Mar 7, 2023
1 parent 810f7b9 commit e9017e2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
33 changes: 33 additions & 0 deletions test/e2e/framework/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,39 @@ func (c *SubnetClient) WaitToBeUpdated(subnet *apiv1.Subnet, timeout time.Durati
return false
}

// Wait waits the given timeout duration for the specified condition to be met.
func (c *SubnetClient) Wait(name string, cond func(s *apiv1.Subnet) (bool, error), condDesc string, interval, timeout time.Duration) (*apiv1.Subnet, error) {
var lastSubnet *apiv1.Subnet
err := wait.PollImmediate(interval, timeout, func() (bool, error) {
Logf("Waiting for subnet %s to meet condition %q", name, condDesc)
subnets, err := c.List(context.TODO(), metav1.ListOptions{})
if err != nil {
return handleWaitingAPIError(err, true, "listing subnets")
}
met := false
for _, subnet := range subnets.Items {
if subnet.Name == name {
if met, err = cond(&subnet); err != nil {
return false, fmt.Errorf("failed to check condition for subnet %s: %v", name, err)
}
lastSubnet = subnet.DeepCopy()
break
}
}

return met, nil
})
if err == nil {
return lastSubnet, nil
}
if IsTimeout(err) {
return lastSubnet, TimeoutError(fmt.Sprintf("timed out while waiting for subnet %s to meet condition %q", name, condDesc),
lastSubnet,
)
}
return lastSubnet, maybeTimeoutError(err, "waiting for subnet %s to meet condition %q", name, condDesc)
}

// WaitToDisappear waits the given timeout duration for the specified subnet to disappear.
func (c *SubnetClient) WaitToDisappear(name string, interval, timeout time.Duration) error {
var lastSubnet *apiv1.Subnet
Expand Down
29 changes: 20 additions & 9 deletions test/e2e/kube-ovn/subnet/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import (
"strings"
"time"

"github.com/onsi/ginkgo/v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/kubernetes/test/e2e/framework/deployment"
e2enode "k8s.io/kubernetes/test/e2e/framework/node"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"

apiv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
"github.com/kubeovn/kube-ovn/pkg/util"
"github.com/kubeovn/kube-ovn/test/e2e/framework"
"github.com/kubeovn/kube-ovn/test/e2e/framework/docker"
Expand Down Expand Up @@ -348,8 +349,12 @@ var _ = framework.Describe("[group:subnet]", func() {
framework.ExpectEmpty(subnet.Spec.AllowSubnets)

ginkgo.By("Validating subnet status fields")
time.Sleep(2 * time.Second)
framework.ExpectContainElement(gatewayNodes, subnet.Status.ActivateGateway)
subnet, err = subnetClient.Wait(subnetName, func(s *apiv1.Subnet) (bool, error) {
return gomega.ContainElement(s.Status.ActivateGateway).Match(gatewayNodes)
}, fmt.Sprintf("field .status.activateGateway contain element %v", gatewayNodes),
2*time.Second, time.Minute,
)
framework.ExpectNoError(err)
framework.ExpectZero(subnet.Status.V4UsingIPs)
framework.ExpectZero(subnet.Status.V6UsingIPs)

Expand Down Expand Up @@ -393,12 +398,18 @@ var _ = framework.Describe("[group:subnet]", func() {
framework.ExpectEqual(subnet.Status.ActivateGateway, gatewayNodes[0])
framework.ExpectConsistOf(strings.Split(subnet.Spec.GatewayNode, ","), gatewayNodes)

ginkgo.By("change subnet spec field enableEcmp to true")
ginkgo.By("Change subnet spec field enableEcmp to true")
modifiedSubnet := subnet.DeepCopy()
modifiedSubnet.Spec.EnableEcmp = true
subnet = subnetClient.PatchSync(subnet, modifiedSubnet)
time.Sleep(2 * time.Second)
framework.ExpectEmpty(subnet.Status.ActivateGateway)

ginkgo.By("Validating active gateway")
subnet, err = subnetClient.Wait(subnetName, func(s *apiv1.Subnet) (bool, error) {
return gomega.BeEmpty().Match(s.Status.ActivateGateway)
}, "field .status.activateGateway is empty",
2*time.Second, time.Minute,
)
framework.ExpectNoError(err)

execCmd := "kubectl ko nbctl --format=csv --data=bare --no-heading --columns=nexthops find logical-router-policy " + fmt.Sprintf("external_ids:subnet=%s", subnetName)
output, err := exec.Command("bash", "-c", execCmd).CombinedOutput()
Expand Down Expand Up @@ -921,14 +932,14 @@ var _ = framework.Describe("[group:subnet]", func() {
gatewayNodes = append(gatewayNodes, nodes.Items[i].Name)
}
modifiedSubnet := subnet.DeepCopy()
modifiedSubnet.Spec.GatewayType = kubeovnv1.GWCentralizedType
modifiedSubnet.Spec.GatewayType = apiv1.GWCentralizedType
modifiedSubnet.Spec.GatewayNode = strings.Join(gatewayNodes, ",")

subnet = subnetClient.PatchSync(subnet, modifiedSubnet)
eventClient = f.EventClient("default")
events := eventClient.WaitToHaveEvent("Subnet", subnetName, "Normal", "SubnetGatewayTypeChanged", "kube-ovn-controller", "")

message := fmt.Sprintf("subnet gateway type changes from %s to %s ", kubeovnv1.GWDistributedType, kubeovnv1.GWCentralizedType)
message := fmt.Sprintf("subnet gateway type changes from %s to %s ", apiv1.GWDistributedType, apiv1.GWCentralizedType)
found := false
for _, event := range events {
if strings.Contains(event.Message, message) {
Expand Down

0 comments on commit e9017e2

Please sign in to comment.