Skip to content

Commit 7ed64d8

Browse files
zijun726911Zijun Wang
andauthored
Add e2e traffic test Setup (#215)
* - Improved Test framework, adding VPC Lattice resource thorough clean up logic in the AfterSuite() - Trace each test run created k8s and VPC Lattice resource, properly clean up them in AfterSuite() - Add more retry and wait by Eventually(), base on vpc lattice api throttling setting and async api time spent - Add HeaderMatch httproute integ-test case * Delete header_match_httproute test case * Add traffic test setup in the test framework * Add LatticeAssignedDomainName const --------- Co-authored-by: Zijun Wang <zijunw@amazon.com>
1 parent ce43aaa commit 7ed64d8

File tree

7 files changed

+148
-14
lines changed

7 files changed

+148
-14
lines changed

controllers/httproute_controller.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ type HTTPRouteReconciler struct {
6363
}
6464

6565
const (
66-
httpRouteFinalizer = "httproute.k8s.aws/resources"
66+
httpRouteFinalizer = "httproute.k8s.aws/resources"
67+
LatticeAssignedDomainName = "application-networking.k8s.aws/lattice-assigned-domain-name"
6768
)
6869

6970
func NewHttpRouteReconciler(cloud aws.Cloud, client client.Client, scheme *runtime.Scheme, eventRecorder record.EventRecorder,
@@ -274,7 +275,7 @@ func (r *HTTPRouteReconciler) updateHTTPRouteStatus(ctx context.Context, dns str
274275
httproute.ObjectMeta.Annotations = make(map[string]string)
275276
}
276277

277-
httproute.ObjectMeta.Annotations["application-networking.k8s.aws/lattice-assigned-domain-name"] = dns
278+
httproute.ObjectMeta.Annotations[LatticeAssignedDomainName] = dns
278279

279280
if err := r.Client.Patch(ctx, httproute, client.MergeFrom(httprouteOld)); err != nil {
280281
glog.V(2).Infof("updateHTTPRouteStatus: Patch() received err %v \n", err)

test/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ require (
5252
github.com/json-iterator/go v1.1.12 // indirect
5353
github.com/mailru/easyjson v0.7.6 // indirect
5454
github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect
55+
github.com/moby/spdystream v0.2.0 // indirect
5556
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
5657
github.com/modern-go/reflect2 v1.0.2 // indirect
5758
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect

test/go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hC
9090
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
9191
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
9292
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
93+
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
9394
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
9495
github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
9596
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
@@ -164,6 +165,7 @@ github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZ
164165
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
165166
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
166167
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
168+
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153 h1:yUdfgN0XgIJw7foRItutHYUIhlcKzcSf5vDpdhQAKTc=
167169
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
168170
github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
169171
github.com/emicklei/go-restful v2.9.5+incompatible/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
@@ -509,6 +511,7 @@ github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0Qu
509511
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
510512
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
511513
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
514+
github.com/moby/spdystream v0.2.0 h1:cjW1zVyyoiM0T7b6UoySUFqzXMoqRckQtXwGPiBhOM8=
512515
github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c=
513516
github.com/moby/term v0.0.0-20210610120745-9d4ed1856297/go.mod h1:vgPCkQMyxTZ7IDy8SXRufE172gr8+K/JE/7hHFxHW3A=
514517
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6/go.mod h1:E2VnQOmVuvZB6UYnnDB0qG5Nq/1tD9acaOpo6xmt0Kw=

test/pkg/test/framework.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ package test
22

33
import (
44
"context"
5+
"github.com/aws/aws-application-networking-k8s/controllers"
56
"github.com/aws/aws-application-networking-k8s/pkg/config"
67
"github.com/aws/aws-application-networking-k8s/pkg/model/lattice"
78
"github.com/aws/aws-sdk-go/aws"
89
"github.com/aws/aws-sdk-go/aws/awserr"
10+
"k8s.io/apimachinery/pkg/runtime"
11+
"k8s.io/apimachinery/pkg/types"
12+
"k8s.io/client-go/rest"
913
"log"
1014
"os"
1115
"reflect"
@@ -51,6 +55,9 @@ var (
5155

5256
type Framework struct {
5357
client.Client
58+
ctx context.Context
59+
k8sScheme *runtime.Scheme
60+
controllerRuntimeConfig *rest.Config
5461
LatticeClient services.Lattice
5562
TestCasesCreatedServiceNetworkNames map[string]bool //key: ServiceNetworkName; value: not in use, meaningless
5663
TestCasesCreatedServiceNames map[string]bool //key: ServiceName; value not in use, meaningless
@@ -61,9 +68,13 @@ func NewFramework(ctx context.Context) *Framework {
6168
var scheme = scheme.Scheme
6269
lo.Must0(v1beta1.Install(scheme))
6370
lo.Must0(v1alpha1.Install(scheme))
71+
controllerRuntimeConfig := controllerruntime.GetConfigOrDie()
6472
framework := &Framework{
65-
Client: lo.Must(client.New(controllerruntime.GetConfigOrDie(), client.Options{Scheme: scheme})),
73+
Client: lo.Must(client.New(controllerRuntimeConfig, client.Options{Scheme: scheme})),
6674
LatticeClient: services.NewDefaultLattice(session.Must(session.NewSession()), config.Region), // region is currently hardcoded
75+
ctx: ctx,
76+
k8sScheme: scheme,
77+
controllerRuntimeConfig: controllerRuntimeConfig,
6778
TestCasesCreatedServiceNetworkNames: make(map[string]bool),
6879
TestCasesCreatedServiceNames: make(map[string]bool),
6980
TestCasesCreatedTargetGroupNames: make(map[string]bool),
@@ -471,3 +482,10 @@ func (env *Framework) DeleteAllFrameworkTracedTargetGroups(ctx aws.Context) {
471482
}
472483
env.TestCasesCreatedServiceNames = make(map[string]bool)
473484
}
485+
486+
func (env *Framework) GetVpcLatticeServiceDns(httpRouteName string, httpRouteNamespace string) string {
487+
httproute := v1beta1.HTTPRoute{}
488+
env.Get(env.ctx, types.NamespacedName{Name: httpRouteName, Namespace: httpRouteNamespace}, &httproute)
489+
vpcLatticeServiceDns := httproute.Annotations[controllers.LatticeAssignedDomainName]
490+
return vpcLatticeServiceDns
491+
}

test/pkg/test/httpapp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (env *Framework) NewHttpApp(options HTTPAppOptions) (*appsv1.Deployment, *v
4444
Image: "public.ecr.aws/x2j8p8w7/http-server:latest",
4545
Env: []v1.EnvVar{{
4646
Name: "PodName",
47-
Value: options.Name,
47+
Value: options.Name + " handler pod",
4848
}},
4949
}},
5050
},

test/pkg/test/pod_manager.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package test
2+
3+
import (
4+
"bytes"
5+
appsv1 "k8s.io/api/apps/v1"
6+
v1 "k8s.io/api/core/v1"
7+
"k8s.io/apimachinery/pkg/labels"
8+
"k8s.io/apimachinery/pkg/runtime"
9+
"k8s.io/apimachinery/pkg/runtime/serializer"
10+
"k8s.io/apimachinery/pkg/types"
11+
"k8s.io/client-go/kubernetes/scheme"
12+
"k8s.io/client-go/rest"
13+
"k8s.io/client-go/tools/remotecommand"
14+
"log"
15+
"net/http"
16+
"sigs.k8s.io/controller-runtime/pkg/client"
17+
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
18+
)
19+
20+
// https://github.com/aws/amazon-vpc-cni-k8s/blob/7eeb2a9ab437887f77de30a5eab20bb42742df06/test/framework/resources/k8s/resources/pod.go#L188
21+
func (env *Framework) PodExec(namespace string, podName string, cmd string) (string, string, error) {
22+
log.Printf("PodExec() [namespace: %v] [podName: %v] [command: %v] \n", namespace, podName, cmd)
23+
restClient, err := env.getRestClientForPod(namespace, podName)
24+
if err != nil {
25+
return "", "", err
26+
}
27+
command := []string{
28+
"sh",
29+
"-c",
30+
cmd,
31+
}
32+
execOptions := &v1.PodExecOptions{
33+
Stdout: true,
34+
Stderr: true,
35+
Command: command,
36+
}
37+
38+
restClient.Get()
39+
req := restClient.Post().
40+
Resource("pods").
41+
Name(podName).
42+
Namespace(namespace).
43+
SubResource("exec").
44+
VersionedParams(execOptions, runtime.NewParameterCodec(scheme.Scheme))
45+
46+
exec, err := remotecommand.NewSPDYExecutor(env.controllerRuntimeConfig, http.MethodPost, req.URL())
47+
if err != nil {
48+
return "", "", err
49+
}
50+
51+
var stdout, stderr bytes.Buffer
52+
err = exec.StreamWithContext(env.ctx, remotecommand.StreamOptions{
53+
Stdout: &stdout,
54+
Stderr: &stderr,
55+
})
56+
stdoutStr := stdout.String()
57+
stderrStr := stderr.String()
58+
log.Println("stdout: ", stdoutStr)
59+
log.Println("stderr: ", stderrStr)
60+
log.Println("err: ", err)
61+
return stdoutStr, stderrStr, err
62+
}
63+
64+
func (env *Framework) GetPodsByDeploymentName(deploymentName string, deploymentNamespce string) []v1.Pod {
65+
deployment := appsv1.Deployment{}
66+
env.Get(env.ctx, types.NamespacedName{Name: deploymentName, Namespace: deploymentNamespce}, &deployment)
67+
pods := &v1.PodList{}
68+
log.Println("deployment.Spec.Selector.MatchLabels:", deployment.Spec.Selector.MatchLabels)
69+
env.List(env.ctx, pods, client.MatchingLabelsSelector{
70+
Selector: labels.SelectorFromSet(deployment.Spec.Selector.MatchLabels),
71+
})
72+
return pods.Items
73+
}
74+
75+
func (env *Framework) getRestClientForPod(namespace string, name string) (rest.Interface, error) {
76+
pod := &v1.Pod{}
77+
err := env.Get(env.ctx, types.NamespacedName{
78+
Namespace: namespace,
79+
Name: name,
80+
}, pod)
81+
if err != nil {
82+
return nil, err
83+
}
84+
85+
gkv, err := apiutil.GVKForObject(pod, env.k8sScheme)
86+
if err != nil {
87+
return nil, err
88+
}
89+
return apiutil.RESTClientForGVK(gkv, false, env.controllerRuntimeConfig, serializer.NewCodecFactory(env.k8sScheme))
90+
}

test/suites/integration/httproute_test.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package integration
22

33
import (
4+
"fmt"
45
"github.com/aws/aws-application-networking-k8s/pkg/latticestore"
56
"github.com/aws/aws-application-networking-k8s/test/pkg/test"
67
"github.com/aws/aws-sdk-go/service/vpclattice"
78
. "github.com/onsi/ginkgo/v2"
89
. "github.com/onsi/gomega"
910
"github.com/samber/lo"
1011
v1 "k8s.io/api/core/v1"
12+
"k8s.io/apimachinery/pkg/types"
1113
"log"
1214
"os"
1315
"time"
@@ -100,18 +102,38 @@ var _ = Describe("HTTPRoute", func() {
100102
httprouteRules := pathMatchHttpRoute.Spec.Rules
101103

102104
g.Expect(err).To(BeNil())
103-
log.Println("*httprouteRules[0].Matches[0].Path.Value", *(httprouteRules[0].Matches[0].Path.Value))
104-
log.Println("*rule0.Match.HttpMatch.PathMatch.Match.Prefix", *(rule0.Match.HttpMatch.PathMatch.Match.Prefix))
105-
106-
g.Expect([]string{
105+
retrievedRules := []string{
107106
*rule0.Match.HttpMatch.PathMatch.Match.Prefix,
108-
*rule1.Match.HttpMatch.PathMatch.Match.Prefix}).To(
109-
ContainElements(
110-
*httprouteRules[0].Matches[0].Path.Value,
111-
*httprouteRules[1].Matches[0].Path.Value))
107+
*rule1.Match.HttpMatch.PathMatch.Match.Prefix}
108+
expectedRules := []string{*httprouteRules[0].Matches[0].Path.Value,
109+
*httprouteRules[1].Matches[0].Path.Value}
110+
log.Println("retrievedRules", retrievedRules)
111+
log.Println("expectedRules", expectedRules)
112+
113+
g.Expect(retrievedRules).To(
114+
ContainElements(expectedRules))
112115
}).WithOffset(1).Should(Succeed())
116+
time.Sleep(30 * time.Second) //Need to wait for config propagate to VPC lattice dataplane
117+
118+
log.Println("Verifying traffic")
119+
dnsName := testFramework.GetVpcLatticeServiceDns(pathMatchHttpRoute.Name, pathMatchHttpRoute.Namespace)
120+
121+
testFramework.Get(ctx, types.NamespacedName{Name: deployment1.Name, Namespace: deployment1.Namespace}, deployment1)
113122

114-
//TODO: test traffic in integ-test https://stackoverflow.com/questions/43314689/example-of-exec-in-k8ss-pod-by-using-go-client
123+
//get the pods of deployment1
124+
pods := testFramework.GetPodsByDeploymentName(deployment1.Name, deployment1.Namespace)
125+
Expect(len(pods)).To(BeEquivalentTo(1))
126+
log.Println("pods[0].Name:", pods[0].Name)
127+
128+
cmd1 := fmt.Sprintf("curl %s/pathmatch0", dnsName)
129+
stdout, _, err := testFramework.PodExec(pods[0].Namespace, pods[0].Name, cmd1)
130+
Expect(err).To(BeNil())
131+
Expect(stdout).To(ContainSubstring("test-v1 handler pod"))
132+
133+
cmd2 := fmt.Sprintf("curl %s/pathmatch1", dnsName)
134+
stdout, _, err = testFramework.PodExec(pods[0].Namespace, pods[0].Name, cmd2)
135+
Expect(err).To(BeNil())
136+
Expect(stdout).To(ContainSubstring("test-v2 handler pod"))
115137

116138
testFramework.ExpectDeleted(ctx,
117139
gateway,
@@ -130,6 +152,5 @@ var _ = Describe("HTTPRoute", func() {
130152
deployment2)
131153

132154
})
133-
134155
})
135156
})

0 commit comments

Comments
 (0)