Skip to content

Commit 91d18b6

Browse files
committed
more tests
1 parent c66c358 commit 91d18b6

File tree

4 files changed

+38
-33
lines changed

4 files changed

+38
-33
lines changed

internal/controller/apisixroute_controller.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
corev1 "k8s.io/api/core/v1"
2323
discoveryv1 "k8s.io/api/discovery/v1"
2424
networkingv1 "k8s.io/api/networking/v1"
25-
"k8s.io/apimachinery/pkg/api/errors"
2625
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2726
"k8s.io/apimachinery/pkg/runtime"
2827
"k8s.io/apimachinery/pkg/types"
@@ -124,7 +123,7 @@ func (r *ApisixRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request)
124123
}
125124
if err = r.Provider.Update(ctx, tctx, &ar); err != nil {
126125
err = ReasonError{
127-
Reason: "SyncFailed",
126+
Reason: string(apiv2.ConditionReasonSyncFailed),
128127
Message: err.Error(),
129128
}
130129
r.Log.Error(err, "failed to process", "apisixroute", ar)
@@ -382,10 +381,10 @@ func (r *ApisixRouteReconciler) getDefaultIngressClass() (*networkingv1.IngressC
382381
return &ic, nil
383382
}
384383
}
385-
return nil, &errors.StatusError{ErrStatus: metav1.Status{
386-
Reason: metav1.StatusReasonNotFound,
384+
return nil, ReasonError{
385+
Reason: string(metav1.StatusReasonNotFound),
387386
Message: "default ingress class not found or dose not match the controller",
388-
}}
387+
}
389388
}
390389

391390
// processIngressClassParameters processes the IngressClass parameters that reference GatewayProxy

internal/provider/adc/adc.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func (d *adcClient) Delete(ctx context.Context, obj client.Object) error {
201201
var resourceTypes []string
202202
var labels map[string]string
203203
switch obj.(type) {
204-
case *gatewayv1.HTTPRoute:
204+
case *gatewayv1.HTTPRoute, *apiv2.ApisixRoute:
205205
resourceTypes = append(resourceTypes, "service")
206206
labels = label.GenLabel(obj)
207207
case *gatewayv1.Gateway:
@@ -213,10 +213,7 @@ func (d *adcClient) Delete(ctx context.Context, obj client.Object) error {
213213
resourceTypes = append(resourceTypes, "consumer")
214214
labels = label.GenLabel(obj)
215215
case *networkingv1.IngressClass:
216-
// delete all resources
217-
case *apiv2.ApisixRoute:
218-
resourceTypes = append(resourceTypes, "service")
219-
labels = label.GenLabel(obj)
216+
// delete all resources
220217
case *apiv2.ApisixGlobalRule:
221218
resourceTypes = append(resourceTypes, "global_rule")
222219
labels = label.GenLabel(obj)

internal/provider/adc/translator/apisixroute.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ func (t *Translator) TranslateApisixRoute(tctx *provider.TranslateContext, ar *a
4444
}
4545

4646
var plugins = make(adc.Plugins)
47-
// todo: need unit test or e2e test
4847
for _, plugin := range http.Plugins {
4948
if !plugin.Enable {
5049
continue
@@ -143,7 +142,7 @@ func (t *Translator) TranslateApisixRoute(tctx *provider.TranslateContext, ar *a
143142
// translate to adc.Service
144143
service.Name = adc.ComposeServiceNameWithRule(ar.Namespace, ar.Name, fmt.Sprintf("%d", ruleIndex))
145144
service.ID = id.GenID(service.Name)
146-
service.Labels = ar.Labels
145+
service.Labels = label.GenLabel(ar)
147146
service.Hosts = http.Match.Hosts
148147
service.Upstream = upstream
149148
service.Routes = []*adc.Route{route}

test/e2e/apisix/route.go

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,21 @@ var _ = Describe("Test ApisixRoute", func() {
3535
)
3636

3737
Context("Test ApisixRoute", func() {
38-
const apisixRouteSpec = `
38+
BeforeEach(func() {
39+
By("create GatewayProxy")
40+
gatewayProxy := fmt.Sprintf(gatewayProxyYaml, s.Deployer.GetAdminEndpoint(), s.AdminKey())
41+
err := s.CreateResourceFromStringWithNamespace(gatewayProxy, "default")
42+
Expect(err).NotTo(HaveOccurred(), "creating GatewayProxy")
43+
time.Sleep(5 * time.Second)
44+
45+
By("create IngressClass")
46+
err = s.CreateResourceFromStringWithNamespace(ingressClassYaml, "")
47+
Expect(err).NotTo(HaveOccurred(), "creating IngressClass")
48+
time.Sleep(5 * time.Second)
49+
})
50+
51+
It("Basic tests", func() {
52+
const apisixRouteSpec = `
3953
apiVersion: apisix.apache.org/v2
4054
kind: ApisixRoute
4155
metadata:
@@ -48,35 +62,31 @@ spec:
4862
hosts:
4963
- httpbin
5064
paths:
51-
- /get
65+
- %s
5266
backends:
5367
- serviceName: httpbin-service-e2e-test
5468
servicePort: 80
5569
`
70+
request := func(path string) int {
71+
return s.NewAPISIXClient().GET(path).WithHost("httpbin").Expect().Raw().StatusCode
72+
}
5673

57-
BeforeEach(func() {
58-
By("create GatewayProxy")
59-
gatewayProxy := fmt.Sprintf(gatewayProxyYaml, s.Deployer.GetAdminEndpoint(), s.AdminKey())
60-
err := s.CreateResourceFromStringWithNamespace(gatewayProxy, "default")
61-
Expect(err).NotTo(HaveOccurred(), "creating GatewayProxy")
62-
time.Sleep(5 * time.Second)
63-
64-
By("create IngressClass")
65-
err = s.CreateResourceFromStringWithNamespace(ingressClassYaml, "")
66-
Expect(err).NotTo(HaveOccurred(), "creating IngressClass")
67-
time.Sleep(5 * time.Second)
68-
})
69-
70-
It("Basic tests", func() {
7174
By("apply ApisixRoute")
7275
var apisixRoute apiv2.ApisixRoute
73-
applier.MustApplyAPIv2(types.NamespacedName{Namespace: s.Namespace(), Name: "default"}, &apisixRoute, apisixRouteSpec)
76+
applier.MustApplyAPIv2(types.NamespacedName{Namespace: s.Namespace(), Name: "default"}, &apisixRoute, fmt.Sprintf(apisixRouteSpec, "/get"))
7477

7578
By("verify ApisixRoute works")
76-
request := func() int {
77-
return s.NewAPISIXClient().GET("/get").WithHost("httpbin").Expect().Raw().StatusCode
78-
}
79-
Eventually(request).WithTimeout(8 * time.Second).ProbeEvery(time.Second).Should(Equal(http.StatusOK))
79+
Eventually(request).WithArguments("/get").WithTimeout(8 * time.Second).ProbeEvery(time.Second).Should(Equal(http.StatusOK))
80+
81+
By("update ApisixRoute")
82+
applier.MustApplyAPIv2(types.NamespacedName{Namespace: s.Namespace(), Name: "default"}, &apisixRoute, fmt.Sprintf(apisixRouteSpec, "/headers"))
83+
Eventually(request).WithArguments("/get").WithTimeout(8 * time.Second).ProbeEvery(time.Second).Should(Equal(http.StatusNotFound))
84+
s.NewAPISIXClient().GET("/headers").WithHost("httpbin").Expect().Status(http.StatusOK)
85+
86+
By("delete ApisixRoute")
87+
err := s.DeleteResource("ApisixRoute", "default")
88+
Expect(err).ShouldNot(HaveOccurred(), "deleting ApisixRoute")
89+
Eventually(request).WithArguments("/headers").WithTimeout(8 * time.Second).ProbeEvery(time.Second).Should(Equal(http.StatusNotFound))
8090
})
8191

8292
It("Test plugins in ApisixRoute", func() {

0 commit comments

Comments
 (0)