|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package v2 |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "fmt" |
| 23 | + "time" |
| 24 | + |
| 25 | + . "github.com/onsi/ginkgo/v2" |
| 26 | + . "github.com/onsi/gomega" |
| 27 | + "k8s.io/apimachinery/pkg/types" |
| 28 | + |
| 29 | + apiv2 "github.com/apache/apisix-ingress-controller/api/v2" |
| 30 | + "github.com/apache/apisix-ingress-controller/test/e2e/framework" |
| 31 | + "github.com/apache/apisix-ingress-controller/test/e2e/scaffold" |
| 32 | +) |
| 33 | + |
| 34 | +var _ = Describe("Test ApisixUpstream", Label("apisix.apache.org", "v2", "apisixupstream"), func() { |
| 35 | + var ( |
| 36 | + s = scaffold.NewDefaultScaffold() |
| 37 | + applier = framework.NewApplier(s.GinkgoT, s.K8sClient, s.CreateResourceFromString) |
| 38 | + ) |
| 39 | + BeforeEach(func() { |
| 40 | + By("create GatewayProxy") |
| 41 | + err := s.CreateResourceFromString(s.GetGatewayProxySpec()) |
| 42 | + Expect(err).NotTo(HaveOccurred(), "creating GatewayProxy") |
| 43 | + time.Sleep(5 * time.Second) |
| 44 | + |
| 45 | + By("create IngressClass") |
| 46 | + err = s.CreateResourceFromStringWithNamespace(s.GetIngressClassYaml(), "") |
| 47 | + Expect(err).NotTo(HaveOccurred(), "creating IngressClass") |
| 48 | + time.Sleep(5 * time.Second) |
| 49 | + }) |
| 50 | + |
| 51 | + Context("Health Check", func() { |
| 52 | + It("active and passive", func() { |
| 53 | + auWithHealthcheck := ` |
| 54 | +apiVersion: apisix.apache.org/v2 |
| 55 | +kind: ApisixUpstream |
| 56 | +metadata: |
| 57 | + name: active |
| 58 | +spec: |
| 59 | + ingressClassName: %s |
| 60 | + externalNodes: |
| 61 | + - type: Domain |
| 62 | + name: httpbin-service-e2e-test |
| 63 | + - type: Domain |
| 64 | + name: invalid.httpbin.host |
| 65 | + - type: Domain |
| 66 | + name: invalid1.httpbin.host |
| 67 | + retries: 1 |
| 68 | + healthCheck: |
| 69 | + active: |
| 70 | + type: http |
| 71 | + httpPath: /ip |
| 72 | + healthy: |
| 73 | + httpCodes: [200] |
| 74 | + interval: 1s |
| 75 | + unhealthy: |
| 76 | + httpFailures: 2 |
| 77 | + interval: 1s |
| 78 | + passive: |
| 79 | + healthy: |
| 80 | + httpCodes: [200] |
| 81 | + unhealthy: |
| 82 | + httpCodes: [502] |
| 83 | +` |
| 84 | + applier.MustApplyAPIv2(types.NamespacedName{Namespace: s.Namespace(), Name: "active"}, |
| 85 | + &apiv2.ApisixUpstream{}, fmt.Sprintf(auWithHealthcheck, s.Namespace())) |
| 86 | + |
| 87 | + ar := ` |
| 88 | +apiVersion: apisix.apache.org/v2 |
| 89 | +kind: ApisixRoute |
| 90 | +metadata: |
| 91 | + name: httpbin-route |
| 92 | +spec: |
| 93 | + ingressClassName: %s |
| 94 | + http: |
| 95 | + - name: rule1 |
| 96 | + match: |
| 97 | + hosts: |
| 98 | + - httpbin.org |
| 99 | + paths: |
| 100 | + - /* |
| 101 | + backends: |
| 102 | + - serviceName: httpbin-service-e2e-test |
| 103 | + servicePort: 80 |
| 104 | +` |
| 105 | + applier.MustApplyAPIv2(types.NamespacedName{Namespace: s.Namespace(), Name: "httpbin-route"}, |
| 106 | + &apiv2.ApisixRoute{}, fmt.Sprintf(ar, s.Namespace())) |
| 107 | + |
| 108 | + By("triggering the health check") |
| 109 | + s.RequestAssert(&scaffold.RequestAssert{ |
| 110 | + Method: "GET", |
| 111 | + Path: "/ip", |
| 112 | + Host: "httpbin.org", |
| 113 | + }) |
| 114 | + time.Sleep(2 * time.Second) |
| 115 | + |
| 116 | + ups, err := s.Deployer.DefaultDataplaneResource().Upstream().List(context.Background()) |
| 117 | + Expect(err).ToNot(HaveOccurred(), "listing upstreams") |
| 118 | + Expect(ups).To(HaveLen(1), "the number of upstreams") |
| 119 | + Expect(ups[0].Nodes).To(HaveLen(3), "the number of upstream nodes") |
| 120 | + Expect(ups[0].Checks).ToNot(BeNil(), "the healthcheck configuration") |
| 121 | + Expect(ups[0].Checks.Active).ToNot(BeNil(), "the active healthcheck configuration") |
| 122 | + Expect(ups[0].Checks.Active.Healthy).ToNot(BeNil(), "the active healthy configuration") |
| 123 | + Expect(ups[0].Checks.Active.Unhealthy).ToNot(BeNil(), "the active unhealthy configuration") |
| 124 | + Expect(ups[0].Checks.Active.Healthy.Interval).To(Equal(1), "the healthy interval") |
| 125 | + Expect(ups[0].Checks.Active.Healthy.HTTPStatuses).To(Equal([]int{200}), "the healthy http status") |
| 126 | + Expect(ups[0].Checks.Active.Unhealthy.Interval).To(Equal(1), "the unhealthy interval") |
| 127 | + Expect(ups[0].Checks.Active.Unhealthy.HTTPFailures).To(Equal(2), "the unhealthy http failures") |
| 128 | + Expect(ups[0].Checks.Passive).ToNot(BeNil(), "the passive healthcheck configuration") |
| 129 | + Expect(ups[0].Checks.Passive.Healthy).ToNot(BeNil(), "the passive healthy configuration") |
| 130 | + Expect(ups[0].Checks.Passive.Unhealthy).ToNot(BeNil(), "the passive unhealthy configuration") |
| 131 | + Expect(ups[0].Checks.Passive.Healthy.HTTPStatuses).To(Equal([]int{200}), "the passive healthy http status") |
| 132 | + Expect(ups[0].Checks.Passive.Unhealthy.HTTPStatuses).To(Equal([]int{502}), "the passive unhealthy http status") |
| 133 | + |
| 134 | + for range 100 { |
| 135 | + s.NewAPISIXClient().GET("/ip").WithHost("httpbin.org").Expect().Status(200) |
| 136 | + } |
| 137 | + }) |
| 138 | + }) |
| 139 | +}) |
0 commit comments