From 2990344e4c2136f28c114d3b03ccb41280b6d5a1 Mon Sep 17 00:00:00 2001 From: slayer321 Date: Wed, 16 Oct 2024 22:46:26 +0530 Subject: [PATCH] adds unit test and e2e test --- .../translation/apisix_upstream_test.go | 47 ++++++++ .../upstream_keep_alive_pool.go | 103 ++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 test/e2e/suite-features/upstream_keep_alive_pool.go diff --git a/pkg/providers/translation/apisix_upstream_test.go b/pkg/providers/translation/apisix_upstream_test.go index 6723535e56..05c09e4921 100644 --- a/pkg/providers/translation/apisix_upstream_test.go +++ b/pkg/providers/translation/apisix_upstream_test.go @@ -410,6 +410,53 @@ func TestUpstreamRetriesAndTimeoutV2(t *testing.T) { }, ups.Timeout) } +func TestKeepAlivePoll(t *testing.T) { + tr := &translator{} + + keepAlive := &configv2.KeepAlivePool{ + Size: 0, + } + err := tr.translateKeepAlivePool(keepAlive, nil) + assert.Equal(t, &TranslateError{ + Field: "size", + Reason: "invalid value, must be one or greater", + }, err) + + var ups apisixv1.Upstream + + keepAlive = &configv2.KeepAlivePool{ + Size: 100, + IdleTimeout: -1, + } + err = tr.translateKeepAlivePool(keepAlive, &ups) + assert.Equal(t, &TranslateError{ + Field: "idle_timeout", + Reason: "invalid value", + }, err) + + keepAlive = &configv2.KeepAlivePool{ + Size: 100, + Request: -10, + } + err = tr.translateKeepAlivePool(keepAlive, &ups) + assert.Equal(t, &TranslateError{ + Field: "request", + Reason: "invalid value, must be one or greater", + }, err) + + keepAlive = &configv2.KeepAlivePool{ + Size: 100, + Request: 320, + IdleTimeout: int(time.Second * 60), + } + + err = tr.translateKeepAlivePool(keepAlive, &ups) + assert.Nil(t, err) + assert.Equal(t, keepAlive.IdleTimeout, ups.IdleTimeout) + assert.Equal(t, keepAlive.Size, ups.Size) + assert.Equal(t, keepAlive.Request, ups.Request) +} + func TestUpstreamPassHost(t *testing.T) { tr := &translator{} tests := []struct { diff --git a/test/e2e/suite-features/upstream_keep_alive_pool.go b/test/e2e/suite-features/upstream_keep_alive_pool.go new file mode 100644 index 0000000000..4f921bf8ab --- /dev/null +++ b/test/e2e/suite-features/upstream_keep_alive_pool.go @@ -0,0 +1,103 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package features + +import ( + "fmt" + "time" + + ginkgo "github.com/onsi/ginkgo/v2" + "github.com/stretchr/testify/assert" + + "github.com/apache/apisix-ingress-controller/test/e2e/scaffold" +) + +var _ = ginkgo.Describe("suite-features: upstream keep alive pool", func() { + suites := func(scaffoldFunc func() *scaffold.Scaffold) { + s := scaffoldFunc() + + routeTpl := ` +apiVersion: apisix.apache.org/v2 +kind: ApisixRoute +metadata: + name: httpbin-route +spec: + http: + - name: rule1 + match: + hosts: + - httpbin.org + paths: + - /* + backends: + - serviceName: %s + servicePort: %d +` + + ginkgo.It("is set to default value", func() { + backendSvc, backendPorts := s.DefaultHTTPBackend() + ar := fmt.Sprintf(routeTpl, backendSvc, backendPorts[0]) + err := s.CreateVersionedApisixResource(ar) + assert.Nil(ginkgo.GinkgoT(), err) + time.Sleep(5 * time.Second) + + au := fmt.Sprintf(` +apiVersion: apisix.apache.org/v2 +kind: ApisixUpstream +metadata: + name: %s +spec: + keepalive_pool: + idle_timeout: 60s + requests: 1000 + size: 320 +`, backendSvc) + err = s.CreateVersionedApisixResource(au) + assert.Nil(ginkgo.GinkgoT(), err, "create ApisixUpstream") + time.Sleep(2 * time.Second) + + ups, err := s.ListApisixUpstreams() + assert.Nil(ginkgo.GinkgoT(), err) + assert.Len(ginkgo.GinkgoT(), ups, 1) + assert.Equal(ginkgo.GinkgoT(), "320", ups[0].Size) + assert.Equal(ginkgo.GinkgoT(), "1000", ups[0].Request) + assert.Equal(ginkgo.GinkgoT(), "60", ups[0].IdleTimeout) + }) + + ginkgo.It("is set to invalid size", func() { + backendSvc, backendPorts := s.DefaultHTTPBackend() + ar := fmt.Sprintf(routeTpl, backendSvc, backendPorts[0]) + err := s.CreateVersionedApisixResource(ar) + assert.Nil(ginkgo.GinkgoT(), err) + time.Sleep(5 * time.Second) + + au := fmt.Sprintf(` +apiVersion: apisix.apache.org/v2 +kind: ApisixUpstream +metadata: + name: %s +spec: + keepalive_pool: + size: -20 +`, backendSvc) + err = s.CreateVersionedApisixResource(au) + assert.NotNil(ginkgo.GinkgoT(), err) + }) + + } + ginkgo.Describe("suite-features: scaffold v2", func() { + suites(scaffold.NewDefaultV2Scaffold) + }) +})