ingress-nginx-helm/test/e2e/annotations/limitconnections.go

106 lines
2.7 KiB
Go
Raw Normal View History

/*
Copyright 2020 The Kubernetes Authors.
Licensed 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 annotations
import (
"fmt"
"strconv"
"strings"
2020-07-31 03:59:09 +00:00
"sync"
"github.com/onsi/ginkgo/v2"
"github.com/stretchr/testify/assert"
"k8s.io/ingress-nginx/test/e2e/framework"
)
var _ = framework.DescribeAnnotation("Annotation - limit-connections", func() {
f := framework.NewDefaultFramework("limit-connections")
ginkgo.BeforeEach(func() {
f.NewSlowEchoDeployment()
})
ginkgo.It("should limit-connections", func() {
host := "limit-connections"
ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.SlowEchoService, 80, nil)
f.EnsureIngress(ing)
f.WaitForNginxServer(host, func(server string) bool {
return strings.Contains(server, fmt.Sprintf("server_name %s ;", host))
})
// limit connections
connectionLimit := 8
2020-07-31 03:59:09 +00:00
annotations := map[string]string{
"nginx.ingress.kubernetes.io/limit-connections": strconv.Itoa(connectionLimit),
}
ing.SetAnnotations(annotations)
f.UpdateIngress(ing)
f.WaitForNginxConfiguration(func(server string) bool {
return strings.Contains(server, "limit_conn") && strings.Contains(server, fmt.Sprintf("conn %v;", connectionLimit))
})
2020-07-31 03:59:09 +00:00
requests := 20
results := make(chan int, requests)
2020-07-31 03:59:09 +00:00
worker := func(wg *sync.WaitGroup) {
defer wg.Done()
2020-08-20 02:00:30 +00:00
defer ginkgo.GinkgoRecover()
2020-07-31 03:59:09 +00:00
resp := f.HTTPTestClient().
GET("/sleep/10").
WithHeader("Host", host).
Expect().
Raw()
if resp != nil {
results <- resp.StatusCode
}
}
2020-07-31 03:59:09 +00:00
var wg sync.WaitGroup
for currentRequest := 0; currentRequest < requests; currentRequest++ {
wg.Add(1)
go worker(&wg)
}
wg.Wait()
ok := 0
failed := 0
errors := 0
close(results)
for status := range results {
switch status {
case 200:
ok++
case 503:
failed++
default:
errors++
}
}
2020-07-31 03:59:09 +00:00
assert.Equal(ginkgo.GinkgoT(), connectionLimit, ok, "expecting the ok (200) requests to be equal to the connection limit")
assert.Equal(ginkgo.GinkgoT(), requests-connectionLimit, failed, "expecting the failed (503) requests to be the total requests - connection limit")
assert.Equal(ginkgo.GinkgoT(), 0, errors, "expecting failed (other than 503) requests to ber zero")
})
})