Skip to content

Commit

Permalink
adds unit test and e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
slayer321 committed Oct 16, 2024
1 parent 80e4d8e commit 2990344
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
47 changes: 47 additions & 0 deletions pkg/providers/translation/apisix_upstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
103 changes: 103 additions & 0 deletions test/e2e/suite-features/upstream_keep_alive_pool.go
Original file line number Diff line number Diff line change
@@ -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)
})
})

0 comments on commit 2990344

Please sign in to comment.