-
Notifications
You must be signed in to change notification settings - Fork 366
chore: add test cases for external service #2500
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
Merged
Revolyssup
merged 6 commits into
apache:master
from
Revolyssup:revolyssup/external-service-test
Aug 5, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ import ( | |
| "github.com/gorilla/websocket" | ||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| "github.com/stretchr/testify/assert" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "k8s.io/apimachinery/pkg/util/wait" | ||
|
|
||
|
|
@@ -802,6 +803,7 @@ spec: | |
| } | ||
| }) | ||
| }) | ||
|
|
||
| Context("Test ApisixRoute sync during startup", func() { | ||
| const route = ` | ||
| apiVersion: apisix.apache.org/v2 | ||
|
|
@@ -1041,4 +1043,263 @@ spec: | |
| Expect(string(msg)).To(Equal(testMessage), "message content verification") | ||
| }) | ||
| }) | ||
|
|
||
| Context("Test ApisixRoute with External Services", func() { | ||
| const ( | ||
| externalServiceName = "ext-httpbin" | ||
| upstreamName = "httpbin-upstream" | ||
| routeName = "httpbin-route" | ||
| ) | ||
|
|
||
| createExternalService := func(externalName string) { | ||
| By(fmt.Sprintf("create ExternalName service: %s -> %s", externalServiceName, externalName)) | ||
| svcSpec := fmt.Sprintf(` | ||
| apiVersion: v1 | ||
| kind: Service | ||
| metadata: | ||
| name: %s | ||
| spec: | ||
| type: ExternalName | ||
| externalName: %s | ||
| `, externalServiceName, externalName) | ||
| err := s.CreateResourceFromString(svcSpec) | ||
| Expect(err).ShouldNot(HaveOccurred(), "creating ExternalName service") | ||
| } | ||
|
|
||
| createApisixUpstream := func(externalType apiv2.ApisixUpstreamExternalType, name string) { | ||
| By(fmt.Sprintf("create ApisixUpstream: type=%s, name=%s", externalType, name)) | ||
| upstreamSpec := fmt.Sprintf(` | ||
| apiVersion: apisix.apache.org/v2 | ||
| kind: ApisixUpstream | ||
| metadata: | ||
| name: %s | ||
| spec: | ||
| externalNodes: | ||
| - type: %s | ||
| name: %s | ||
| `, upstreamName, externalType, name) | ||
| var upstream apiv2.ApisixUpstream | ||
| applier.MustApplyAPIv2( | ||
| types.NamespacedName{Namespace: s.Namespace(), Name: upstreamName}, | ||
| &upstream, | ||
| upstreamSpec, | ||
| ) | ||
| } | ||
|
|
||
| createApisixRoute := func() { | ||
| By("create ApisixRoute referencing ApisixUpstream") | ||
| routeSpec := fmt.Sprintf(` | ||
| apiVersion: apisix.apache.org/v2 | ||
| kind: ApisixRoute | ||
| metadata: | ||
| name: %s | ||
| spec: | ||
| ingressClassName: apisix | ||
| http: | ||
| - name: rule1 | ||
| match: | ||
| hosts: | ||
| - httpbin.org | ||
| paths: | ||
| - /ip | ||
| upstreams: | ||
| - name: %s | ||
| `, routeName, upstreamName) | ||
| var route apiv2.ApisixRoute | ||
| applier.MustApplyAPIv2( | ||
| types.NamespacedName{Namespace: s.Namespace(), Name: routeName}, | ||
| &route, | ||
| routeSpec, | ||
| ) | ||
| } | ||
|
|
||
| createApisixRouteWithHostRewrite := func(host string) { | ||
| By("create ApisixRoute with host rewrite") | ||
| routeSpec := fmt.Sprintf(` | ||
| apiVersion: apisix.apache.org/v2 | ||
| kind: ApisixRoute | ||
| metadata: | ||
| name: %s | ||
| spec: | ||
| ingressClassName: apisix | ||
| http: | ||
| - name: rule1 | ||
| match: | ||
| hosts: | ||
| - httpbin.org | ||
| paths: | ||
| - /ip | ||
| upstreams: | ||
| - name: %s | ||
| plugins: | ||
| - name: proxy-rewrite | ||
| enable: true | ||
| config: | ||
| host: %s | ||
| `, routeName, upstreamName, host) | ||
| var route apiv2.ApisixRoute | ||
| applier.MustApplyAPIv2( | ||
| types.NamespacedName{Namespace: s.Namespace(), Name: routeName}, | ||
| &route, | ||
| routeSpec, | ||
| ) | ||
| } | ||
|
|
||
| verifyAccess := func() { | ||
| By("verify access to external service") | ||
| request := func() int { | ||
| return s.NewAPISIXClient().GET("/ip"). | ||
| WithHost("httpbin.org"). | ||
| Expect().Raw().StatusCode | ||
| } | ||
| Eventually(request).WithTimeout(30 * time.Second).ProbeEvery(2 * time.Second). | ||
| Should(Equal(http.StatusOK)) | ||
| } | ||
|
|
||
| It("access third-party service directly", func() { | ||
| createApisixUpstream(apiv2.ExternalTypeDomain, "httpbin.org") | ||
| createApisixRoute() | ||
| verifyAccess() | ||
| }) | ||
|
|
||
| It("access third-party service with host rewrite", func() { | ||
| createApisixUpstream(apiv2.ExternalTypeDomain, "httpbin.org") | ||
| createApisixRouteWithHostRewrite("httpbin.org") | ||
| verifyAccess() | ||
| }) | ||
|
|
||
| It("access external domain via ExternalName service", func() { | ||
| createExternalService("httpbin.org") | ||
| createApisixUpstream(apiv2.ExternalTypeService, externalServiceName) | ||
| createApisixRoute() | ||
| verifyAccess() | ||
| }) | ||
|
|
||
| It("access in-cluster service via ExternalName", func() { | ||
| By("create temporary httpbin service") | ||
|
|
||
| By("get FQDN of temporary service") | ||
| fqdn := fmt.Sprintf("%s.%s.svc.cluster.local", "httpbin-service-e2e-test", s.Namespace()) | ||
|
|
||
| By("setup external service and route") | ||
| createExternalService(fqdn) | ||
| createApisixUpstream(apiv2.ExternalTypeService, externalServiceName) | ||
| createApisixRoute() | ||
| verifyAccess() | ||
| }) | ||
|
|
||
| Context("complex scenarios", func() { | ||
| It("multiple external services in one upstream", func() { | ||
| By("create ApisixUpstream with multiple external nodes") | ||
| upstreamSpec := ` | ||
| apiVersion: apisix.apache.org/v2 | ||
| kind: ApisixUpstream | ||
| metadata: | ||
| name: httpbin-upstream | ||
| spec: | ||
| externalNodes: | ||
| - type: Domain | ||
| name: httpbin.org | ||
| - type: Domain | ||
| name: postman-echo.com | ||
| ` | ||
| var upstream apiv2.ApisixUpstream | ||
| applier.MustApplyAPIv2( | ||
| types.NamespacedName{Namespace: s.Namespace(), Name: upstreamName}, | ||
| &upstream, | ||
| upstreamSpec, | ||
| ) | ||
|
|
||
| createApisixRoute() | ||
|
|
||
| By("verify access to multiple services") | ||
| time.Sleep(7 * time.Second) | ||
AlinsRan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| hasEtag := false // postman-echo.com | ||
| hasNoEtag := false // httpbin.org | ||
| for range 20 { | ||
| headers := s.NewAPISIXClient().GET("/ip"). | ||
| WithHeader("Host", "httpbin.org"). | ||
| WithHeader("X-Foo", "bar"). | ||
| Expect(). | ||
| Headers().Raw() | ||
| if _, ok := headers["Etag"]; ok { | ||
| hasEtag = true | ||
| } else { | ||
| hasNoEtag = true | ||
| } | ||
| if hasEtag && hasNoEtag { | ||
| break | ||
| } | ||
| } | ||
| assert.True(GinkgoT(), hasEtag && hasNoEtag, "both httpbin and postman should be accessed at least once") | ||
| }) | ||
|
|
||
| It("should be able to use backends and upstreams together", func() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://github.com/apache/apisix-ingress-controller/blob/master/test/e2e/crds/v2/route.go#L516-L545 They are very similar, I suggest integrating them into one context to avoid redundant use cases. |
||
| upstreamSpec := ` | ||
| apiVersion: apisix.apache.org/v2 | ||
| kind: ApisixUpstream | ||
| metadata: | ||
| name: httpbin-upstream | ||
| spec: | ||
| externalNodes: | ||
| - type: Domain | ||
| name: postman-echo.com | ||
| ` | ||
| var upstream apiv2.ApisixUpstream | ||
| applier.MustApplyAPIv2( | ||
| types.NamespacedName{Namespace: s.Namespace(), Name: upstreamName}, | ||
| &upstream, | ||
| upstreamSpec, | ||
| ) | ||
| By("create ApisixRoute with both backends and upstreams") | ||
| routeSpec := fmt.Sprintf(` | ||
| apiVersion: apisix.apache.org/v2 | ||
| kind: ApisixRoute | ||
| metadata: | ||
| name: %s | ||
| spec: | ||
| ingressClassName: apisix | ||
| http: | ||
| - name: rule1 | ||
| match: | ||
| hosts: | ||
| - httpbin.org | ||
| paths: | ||
| - /ip | ||
| backends: | ||
| - serviceName: httpbin-service-e2e-test | ||
| servicePort: 80 | ||
| resolveGranularity: service | ||
| upstreams: | ||
| - name: %s | ||
| `, routeName, upstreamName) | ||
| var route apiv2.ApisixRoute | ||
| applier.MustApplyAPIv2( | ||
| types.NamespacedName{Namespace: s.Namespace(), Name: routeName}, | ||
| &route, | ||
| routeSpec, | ||
| ) | ||
| By("verify access to multiple services") | ||
| time.Sleep(7 * time.Second) | ||
| hasEtag := false // postman-echo.com | ||
| hasNoEtag := false // httpbin.org | ||
| for range 20 { | ||
| headers := s.NewAPISIXClient().GET("/ip"). | ||
| WithHeader("Host", "httpbin.org"). | ||
| WithHeader("X-Foo", "bar"). | ||
| Expect(). | ||
| Headers().Raw() | ||
| if _, ok := headers["Etag"]; ok { | ||
| hasEtag = true | ||
| } else { | ||
| hasNoEtag = true | ||
| } | ||
| if hasEtag && hasNoEtag { | ||
| break | ||
| } | ||
| } | ||
| assert.True(GinkgoT(), hasEtag && hasNoEtag, "both httpbin and postman should be accessed at least once") | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two services are actually not very stable. We can consider whether to replace them in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ref: https://github.com/apache/apisix-ingress-controller/blob/master/test/e2e/crds/v2/route.go#L493C3-L493C15
Please refer to it and try to use local services instead of external services as they are always unstable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this particular test is for testing external domains other than kubernetes service.