Skip to content

Commit

Permalink
Merge pull request #982 from spidernet-io/bz/e2e/S00001
Browse files Browse the repository at this point in the history
Add e2e case S00001
  • Loading branch information
weizhoublue authored Dec 7, 2023
2 parents b22d19f + fefc530 commit 633a9e7
Show file tree
Hide file tree
Showing 10 changed files with 707 additions and 0 deletions.
5 changes: 5 additions & 0 deletions test/doc/egressendpointslice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# E2E Cases for EgressEndpointSlice

| Case ID | Title | Priority | Smoke | Status | Other |
|---------|--------------------------------------------------------------------------------------------------------------------------------------|----------|-------|--------|-------|
| S00001 | After repeatedly creating and deleting `pods` matched by a specific `policy`, check the egress IP addresses of all `pods` | p2 | false | | |
14 changes: 14 additions & 0 deletions test/doc/egressendpointslice_zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!--
# E2E Cases for EgressEndpointSlice
| Case ID | Title | Priority | Smoke | Status | Other |
|---------|--------------------------------------------------------------------------------------------------------------------------------------|----------|-------|--------|-------|
| S00001 | After repeatedly creating and deleting `pods` matched by a specific `policy`, check the egress IP addresses of all `pods` | p2 | false | | |
-->

# EgressEndpointSlice E2E 用例

| Case ID | Title | Priority | Smoke | Status | Other |
|---------|----------------------------------------------------------------------------------------|----------|-------|--------|-------|
| S00001 | 不断增删 `policy` 所匹配到的 `pods`,几轮增删后,判断所有 `pods` 的出口 `IP` | p2 | false | | |
39 changes: 39 additions & 0 deletions test/e2e/common/check_eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,42 @@ func CheckPodsEgressIP(ctx context.Context, cfg *Config, p2p map[*corev1.Pod]*eg
}
return nil
}

func CheckDeployEgressIP(
ctx context.Context, cli client.Client,
cfg *Config, egressConfig config.FileConfig,
deploy *appsv1.Deployment, ipv4, ipv6 string, expectUsedEip bool) error {

list := &corev1.PodList{}
labels := &metav1.LabelSelector{MatchLabels: deploy.Spec.Template.Labels}
selector, err := metav1.LabelSelectorAsSelector(labels)
if err != nil {
return err
}
err = cli.List(ctx, list, &client.ListOptions{
LabelSelector: selector,
Namespace: deploy.Namespace,
})
if err != nil {
return err
}

for _, pod := range list.Items {
// check v4
if egressConfig.EnableIPv4 {
err = CheckPodEgressIP(ctx, cfg, pod, ipv4, cfg.ServerAIPv4, expectUsedEip)
if err != nil {
return err
}
}

// check v6
if egressConfig.EnableIPv6 {
err = CheckPodEgressIP(ctx, cfg, pod, ipv6, cfg.ServerAIPv6, expectUsedEip)
if err != nil {
return err
}
}
}
return nil
}
107 changes: 107 additions & 0 deletions test/e2e/common/deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright 2022 Authors of spidernet-io
// SPDX-License-Identifier: Apache-2.0

package common

import (
"context"
"fmt"
"time"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apiserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"

e2eerr "github.com/spidernet-io/egressgateway/test/e2e/err"
)

func CreateDeploy(ctx context.Context, cli client.Client, name string, image string, repolicas int) (*appsv1.Deployment, error) {
ctx, cancel := context.WithTimeout(ctx, time.Second*20)
defer cancel()

var terminationGracePeriodSeconds int64 = 0

label := map[string]string{"app": name}
res := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: name,
Labels: label,
},
Spec: appsv1.DeploymentSpec{
Replicas: ptr.To[int32](int32(repolicas)),
Selector: &metav1.LabelSelector{MatchLabels: label},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: label},
Spec: corev1.PodSpec{
TerminationGracePeriodSeconds: &terminationGracePeriodSeconds,
Containers: []corev1.Container{{Name: name,
Image: image,
ImagePullPolicy: corev1.PullIfNotPresent,
Command: []string{"/bin/sh", "-c", "sleep infinity"},
}},
},
},
},
}

err := cli.Create(ctx, res)
if err != nil {
return nil, err
}

for {
select {
case <-ctx.Done():
_ = DeleteObj(context.Background(), cli, res)
return nil, fmt.Errorf("create DaemonSet time out")
default:
err := cli.Get(ctx, types.NamespacedName{Namespace: res.Namespace, Name: res.Name}, res)
if err != nil {
return nil, err
}

a := *res.Spec.Replicas
b := res.Status.ReadyReplicas

if a == b && b > 0 {
return res, nil
}

time.Sleep(time.Second / 2)
}
}
}

func WaitDeployDeleted(ctx context.Context, cli client.Client, deploy *appsv1.Deployment, timeout time.Duration) error {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

err := DeleteObj(ctx, cli, deploy)
if err != nil {
if apiserrors.IsNotFound(err) {
return nil
}
return err
}

dp := new(appsv1.Deployment)

for {
select {
case <-ctx.Done():
return e2eerr.ErrTimeout
default:
err = cli.Get(ctx, types.NamespacedName{Name: deploy.Name, Namespace: deploy.Namespace}, dp)
if !apiserrors.IsNotFound(err) {
time.Sleep(time.Second / 2)
continue
}
return nil
}
}
}
Loading

0 comments on commit 633a9e7

Please sign in to comment.