Skip to content

Commit

Permalink
tests: update e2e
Browse files Browse the repository at this point in the history
Signed-off-by: l1b0k <[email protected]>
  • Loading branch information
l1b0k committed Dec 23, 2024
1 parent b3b0ca3 commit 17d9143
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 613 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ vet: ## Run go vet against code.

.PHONY: test
test: manifests generate fmt vet envtest datapath-test## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test -race --tags "$(GO_BUILD_TAGS)" $$(go list ./... | grep -Ev '/e2e|/mocks|/generated|/apis|/examples|/tests|/rpc') -coverprofile coverage.txt
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test -race --tags "$(GO_BUILD_TAGS)" $$(go list ./... | grep -Ev '/e2e|/mocks|/generated|/apis|/examples|/tests|/rpc|/windows') -coverprofile coverage.txt

.PHONY: lint
lint: golangci-lint ## Run golangci-lint linter & yamllint
Expand Down
55 changes: 25 additions & 30 deletions tests/connecctive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ import (
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/e2e-framework/klient"
"sigs.k8s.io/e2e-framework/klient/k8s"
"sigs.k8s.io/e2e-framework/klient/wait"
"sigs.k8s.io/e2e-framework/klient/wait/conditions"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/features"
)

var resourceKey struct{}

func getStack() []string {
var r []string
if testIPv4 {
Expand Down Expand Up @@ -63,19 +61,19 @@ func TestConnective(t *testing.T) {
{
name: "trunk pod",
podFunc: func(pod *Pod) *Pod {
return pod.WithLabels(map[string]string{"trunk": "enable"})
return pod.WithLabels(map[string]string{"netplan": "default"})
},
},
{
name: "trunk pod alinux2",
podFunc: func(pod *Pod) *Pod {
return pod.WithLabels(map[string]string{"trunk": "enable"}).WithNodeAffinity(map[string]string{"e2e-os": "alinux2"})
return pod.WithLabels(map[string]string{"netplan": "default"}).WithNodeAffinity(map[string]string{"e2e-os": "alinux2"})
},
},
{
name: "trunk pod alinux3",
podFunc: func(pod *Pod) *Pod {
return pod.WithLabels(map[string]string{"trunk": "enable"}).WithNodeAffinity(map[string]string{"e2e-os": "alinux3"})
return pod.WithLabels(map[string]string{"netplan": "default"}).WithNodeAffinity(map[string]string{"e2e-os": "alinux3"})
},
},
}
Expand All @@ -86,7 +84,7 @@ func TestConnective(t *testing.T) {

hairpin := features.New(fmt.Sprintf("PodConnective/hairpin-%s", name)).
Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
var objs []k8s.Object
var objs []client.Object

server := fn(NewPod("server", config.Namespace()).
WithLabels(map[string]string{"app": "server"}).
Expand All @@ -110,7 +108,7 @@ func TestConnective(t *testing.T) {
}
objs = append(objs, svc.Service)
}
ctx = context.WithValue(ctx, resourceKey, objs)
ctx = SaveResources(ctx, objs...)
return ctx
}).
Assess("Pod can access own service", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
Expand Down Expand Up @@ -145,7 +143,7 @@ func TestConnective(t *testing.T) {

podSameNode := features.New(fmt.Sprintf("PodConnective/podSameNode-%s", name)).
Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
var objs []k8s.Object
var objs []client.Object

server := fn(NewPod("server", config.Namespace()).
WithLabels(map[string]string{"app": "server"}).
Expand Down Expand Up @@ -182,8 +180,7 @@ func TestConnective(t *testing.T) {
}
objs = append(objs, svc.Service)
}
ctx = context.WithValue(ctx, resourceKey, objs)

ctx = SaveResources(ctx, objs...)
return ctx
}).
Assess("Pod can access server", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
Expand Down Expand Up @@ -225,7 +222,7 @@ func TestConnective(t *testing.T) {

podDifferentNode := features.New(fmt.Sprintf("PodConnective/podDifferentNode-%s", name)).
Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
var objs []k8s.Object
var objs []client.Object

server := fn(NewPod("server", config.Namespace()).
WithLabels(map[string]string{"app": "server"}).
Expand Down Expand Up @@ -262,7 +259,7 @@ func TestConnective(t *testing.T) {
}
objs = append(objs, svc.Service)
}
ctx = context.WithValue(ctx, resourceKey, objs)
ctx = SaveResources(ctx, objs...)

return ctx
}).
Expand Down Expand Up @@ -305,7 +302,7 @@ func TestConnective(t *testing.T) {

hostToPodSameNode := features.New(fmt.Sprintf("PodConnective/hostToSameNode-%s", name)).
Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
var objs []k8s.Object
var objs []client.Object

server := fn(NewPod("server", config.Namespace()).
WithLabels(map[string]string{"app": "server"}).
Expand Down Expand Up @@ -344,7 +341,7 @@ func TestConnective(t *testing.T) {
}
objs = append(objs, svc.Service)
}
ctx = context.WithValue(ctx, resourceKey, objs)
ctx = SaveResources(ctx, objs...)

return ctx
}).
Expand Down Expand Up @@ -427,19 +424,19 @@ func TestNetworkPolicy(t *testing.T) {
{
name: "trunk pod",
podFunc: func(pod *Pod) *Pod {
return pod.WithLabels(map[string]string{"trunk": "enable"})
return pod.WithLabels(map[string]string{"netplan": "default"})
},
},
{
name: "trunk pod alinux2",
podFunc: func(pod *Pod) *Pod {
return pod.WithLabels(map[string]string{"trunk": "enable"}).WithNodeAffinity(map[string]string{"e2e-os": "alinux2"})
return pod.WithLabels(map[string]string{"netplan": "default"}).WithNodeAffinity(map[string]string{"e2e-os": "alinux2"})
},
},
{
name: "trunk pod alinux3",
podFunc: func(pod *Pod) *Pod {
return pod.WithLabels(map[string]string{"trunk": "enable"}).WithNodeAffinity(map[string]string{"e2e-os": "alinux3"})
return pod.WithLabels(map[string]string{"netplan": "default"}).WithNodeAffinity(map[string]string{"e2e-os": "alinux3"})
},
},
}
Expand All @@ -449,7 +446,7 @@ func TestNetworkPolicy(t *testing.T) {

healthCheck := features.New(fmt.Sprintf("NetworkPolicy/PodHealthCheck-%s", name)).
Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
var objs []k8s.Object
var objs []client.Object

policy := NewNetworkPolicy("default-deny-ingress", config.Namespace()).
WithPolicyType(networkingv1.PolicyTypeIngress)
Expand All @@ -469,7 +466,7 @@ func TestNetworkPolicy(t *testing.T) {
}
}

ctx = context.WithValue(ctx, resourceKey, objs)
ctx = SaveResources(ctx, objs...)

return ctx
}).
Expand All @@ -486,7 +483,7 @@ func TestNetworkPolicy(t *testing.T) {

denyIngressSameNode := features.New(fmt.Sprintf("NetworkPolicy/DenyIngressSameNode-%s", name)).
Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
var objs []k8s.Object
var objs []client.Object

policy := NewNetworkPolicy("deny-ingress", config.Namespace()).
WithPolicyType(networkingv1.PolicyTypeIngress).
Expand Down Expand Up @@ -525,7 +522,7 @@ func TestNetworkPolicy(t *testing.T) {
}
}

ctx = context.WithValue(ctx, resourceKey, objs)
ctx = SaveResources(ctx, objs...)

return ctx
}).
Expand Down Expand Up @@ -557,7 +554,7 @@ func TestNetworkPolicy(t *testing.T) {

denyIngressotherNode := features.New(fmt.Sprintf("NetworkPolicy/denyIngressotherNode-%s", name)).
Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
var objs []k8s.Object
var objs []client.Object

policy := NewNetworkPolicy("deny-ingress", config.Namespace()).
WithPolicyType(networkingv1.PolicyTypeIngress).
Expand Down Expand Up @@ -596,7 +593,7 @@ func TestNetworkPolicy(t *testing.T) {
}
}

ctx = context.WithValue(ctx, resourceKey, objs)
ctx = SaveResources(ctx, objs...)

return ctx
}).
Expand Down Expand Up @@ -628,7 +625,7 @@ func TestNetworkPolicy(t *testing.T) {

denyEgressSameNode := features.New(fmt.Sprintf("NetworkPolicy/denyEgressSameNode-%s", name)).
Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
var objs []k8s.Object
var objs []client.Object

policy := NewNetworkPolicy("deny-ingress", config.Namespace()).
WithPolicyType(networkingv1.PolicyTypeEgress).
Expand Down Expand Up @@ -660,8 +657,7 @@ func TestNetworkPolicy(t *testing.T) {
}
}

ctx = context.WithValue(ctx, resourceKey, objs)

ctx = SaveResources(ctx, objs...)
return ctx
}).
Assess("Check ingress policy", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
Expand All @@ -685,7 +681,7 @@ func TestNetworkPolicy(t *testing.T) {

denyEgressOtherNode := features.New(fmt.Sprintf("NetworkPolicy/denyEgressOtherNode-%s", name)).
Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
var objs []k8s.Object
var objs []client.Object

policy := NewNetworkPolicy("deny-ingress", config.Namespace()).
WithPolicyType(networkingv1.PolicyTypeEgress).
Expand Down Expand Up @@ -717,8 +713,7 @@ func TestNetworkPolicy(t *testing.T) {
}
}

ctx = context.WithValue(ctx, resourceKey, objs)

ctx = SaveResources(ctx, objs...)
return ctx
}).
Assess("Check ingress policy", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
Expand Down
45 changes: 18 additions & 27 deletions tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
"testing"
"time"

"github.com/samber/lo"
k8sErr "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"

"go.uber.org/atomic"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -61,13 +63,17 @@ var (
isFailed atomic.Bool

nginxImage string

defaultPodNetworkingName string
)

func init() {
flag.StringVar(&repo, "repo", "registry.cn-hangzhou.aliyuncs.com/build-test", "image repo")
flag.StringVar(&timeout, "timeout", "2m", "2m")
flag.StringVar(&vSwitchIDs, "vswitch-ids", "", "extra vSwitchIDs")
flag.StringVar(&securityGroupIDs, "security-group-ids", "", "extra securityGroupIDs")

defaultPodNetworkingName = "default-pn"
}

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -99,21 +105,15 @@ func TestMain(m *testing.M) {
setPodNetworking,
)
testenv.AfterEachFeature(func(ctx context.Context, config *envconf.Config, t *testing.T, feature features.Feature) (context.Context, error) {
objs, ok := ctx.Value(resourceKey).([]k8s.Object)
if !ok {
return ctx, nil
}
for _, obj := range objs {
_ = config.Client().Resources().Delete(ctx, obj)
lo.ForEach(ResourcesFromCtx(ctx), func(item client.Object, index int) {
_ = config.Client().Resources().Delete(ctx, item)

err = wait.For(conditions.New(config.Client().Resources()).ResourceDeleted(obj), wait.WithContext(ctx),
wait.WithTimeout(parsedTimeout),
wait.WithInterval(1*time.Second))
err := wait.For(conditions.New(config.Client().Resources()).ResourceDeleted(item),
wait.WithInterval(1*time.Second), wait.WithImmediate(), wait.WithTimeout(1*time.Minute))
if err != nil {
t.Error(err)
t.FailNow()
t.Fatal("failed waiting for pods to be deleted", err)
}
}
})
return ctx, nil
})

Expand All @@ -124,6 +124,7 @@ func TestMain(m *testing.M) {

pn := &networkv1beta1.PodNetworking{}
pn.Name = "trunk"
pn.Spec.ENIOptions = networkv1beta1.ENIOptions{ENIAttachType: networkv1beta1.ENIOptionTypeTrunk}

_ = config.Client().Resources().Delete(ctx, pn)
return ctx, nil
Expand Down Expand Up @@ -223,9 +224,10 @@ func setPodNetworking(ctx context.Context, config *envconf.Config) (context.Cont
}

pn := &networkv1beta1.PodNetworking{}
pn.Name = "trunk"
pn.Name = defaultPodNetworkingName
pn.Spec.ENIOptions = networkv1beta1.ENIOptions{ENIAttachType: networkv1beta1.ENIOptionTypeTrunk}
pn.Spec.Selector = networkv1beta1.Selector{PodSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{"trunk": "enable"},
MatchLabels: map[string]string{"netplan": "default"},
}}
if securityGroupIDs != "" {
pn.Spec.SecurityGroupIDs = strings.Split(securityGroupIDs, ",")
Expand All @@ -241,19 +243,7 @@ func setPodNetworking(ctx context.Context, config *envconf.Config) (context.Cont
}
}

err = wait.For(func(ctx context.Context) (bool, error) {
pn := &networkv1beta1.PodNetworking{}
err := config.Client().Resources().Get(ctx, "trunk", "", pn)
if err != nil {
return false, err
}
if pn.Status.Status != networkv1beta1.NetworkingStatusReady {
return false, nil
}
return true, nil
},
wait.WithTimeout(10*time.Second),
wait.WithInterval(1*time.Second))
err = WaitPodNetworkingReady(pn.Name, config.Client())

return ctx, err
}
Expand All @@ -269,6 +259,7 @@ func patchNamespace(ctx context.Context, config *envconf.Config) (context.Contex
"labels": map[string]interface{}{
"ns": config.Namespace(),
"node-local-dns-injection": "enabled",
"ns-trunking": "true",
},
},
})
Expand Down
3 changes: 2 additions & 1 deletion tests/stress/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"path/filepath"
"testing"

"github.com/AliyunContainerService/terway/tests/utils"
"k8s.io/client-go/kubernetes/scheme"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/e2e-framework/pkg/env"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/envfuncs"

"github.com/AliyunContainerService/terway/tests/utils"
)

var (
Expand Down
Loading

0 comments on commit 17d9143

Please sign in to comment.