2016-05-27 21:03:54 +00:00
|
|
|
/*
|
2016-09-08 11:02:39 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2016-05-27 21:03:54 +00:00
|
|
|
|
|
|
|
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 ratelimit
|
|
|
|
|
|
|
|
import (
|
2019-02-04 01:53:01 +00:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2016-05-27 21:03:54 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-07-16 19:19:59 +00:00
|
|
|
api "k8s.io/api/core/v1"
|
|
|
|
extensions "k8s.io/api/extensions/v1beta1"
|
2017-04-01 14:39:42 +00:00
|
|
|
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
|
|
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
2017-11-23 16:46:23 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/annotations/parser"
|
2017-11-07 22:02:12 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/defaults"
|
2017-11-08 20:58:57 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/resolver"
|
2016-05-27 21:03:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func buildIngress() *extensions.Ingress {
|
|
|
|
defaultBackend := extensions.IngressBackend{
|
|
|
|
ServiceName: "default-backend",
|
|
|
|
ServicePort: intstr.FromInt(80),
|
|
|
|
}
|
|
|
|
|
|
|
|
return &extensions.Ingress{
|
2017-04-01 14:39:42 +00:00
|
|
|
ObjectMeta: meta_v1.ObjectMeta{
|
2016-05-27 21:03:54 +00:00
|
|
|
Name: "foo",
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
},
|
|
|
|
Spec: extensions.IngressSpec{
|
|
|
|
Backend: &extensions.IngressBackend{
|
|
|
|
ServiceName: "default-backend",
|
|
|
|
ServicePort: intstr.FromInt(80),
|
|
|
|
},
|
|
|
|
Rules: []extensions.IngressRule{
|
|
|
|
{
|
|
|
|
Host: "foo.bar.com",
|
|
|
|
IngressRuleValue: extensions.IngressRuleValue{
|
|
|
|
HTTP: &extensions.HTTPIngressRuleValue{
|
|
|
|
Paths: []extensions.HTTPIngressPath{
|
|
|
|
{
|
|
|
|
Path: "/foo",
|
|
|
|
Backend: defaultBackend,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-13 06:52:20 +00:00
|
|
|
type mockBackend struct {
|
2017-11-08 20:58:57 +00:00
|
|
|
resolver.Mock
|
2017-08-13 06:52:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockBackend) GetDefaultBackend() defaults.Backend {
|
|
|
|
return defaults.Backend{
|
|
|
|
LimitRateAfter: 0,
|
|
|
|
LimitRate: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-27 21:03:54 +00:00
|
|
|
func TestWithoutAnnotations(t *testing.T) {
|
|
|
|
ing := buildIngress()
|
2017-08-13 06:52:20 +00:00
|
|
|
_, err := NewParser(mockBackend{}).Parse(ing)
|
2016-12-29 20:02:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("unexpected error with ingress without annotations")
|
2016-05-27 21:03:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 01:53:01 +00:00
|
|
|
func TestParseCIDRs(t *testing.T) {
|
|
|
|
cidr, _ := parseCIDRs("invalid.com")
|
|
|
|
if cidr != nil {
|
|
|
|
t.Errorf("expected %v but got %v", nil, cidr)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []string{"192.0.0.1", "192.0.1.0/24"}
|
|
|
|
cidr, err := parseCIDRs("192.0.0.1, 192.0.1.0/24")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error %v", err)
|
|
|
|
}
|
|
|
|
sort.Strings(cidr)
|
|
|
|
if !reflect.DeepEqual(expected, cidr) {
|
|
|
|
t.Errorf("expected %v but got %v", expected, cidr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRateLimiting(t *testing.T) {
|
2016-05-27 21:03:54 +00:00
|
|
|
ing := buildIngress()
|
|
|
|
|
|
|
|
data := map[string]string{}
|
2017-11-23 16:46:23 +00:00
|
|
|
data[parser.GetAnnotationWithPrefix("limit-connections")] = "0"
|
|
|
|
data[parser.GetAnnotationWithPrefix("limit-rps")] = "0"
|
|
|
|
data[parser.GetAnnotationWithPrefix("limit-rpm")] = "0"
|
2016-05-27 21:03:54 +00:00
|
|
|
ing.SetAnnotations(data)
|
|
|
|
|
2017-08-13 06:52:20 +00:00
|
|
|
_, err := NewParser(mockBackend{}).Parse(ing)
|
2016-12-29 20:02:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error with invalid limits (0)")
|
2016-05-27 21:03:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data = map[string]string{}
|
2017-11-23 16:46:23 +00:00
|
|
|
data[parser.GetAnnotationWithPrefix("limit-connections")] = "5"
|
|
|
|
data[parser.GetAnnotationWithPrefix("limit-rps")] = "100"
|
|
|
|
data[parser.GetAnnotationWithPrefix("limit-rpm")] = "10"
|
|
|
|
data[parser.GetAnnotationWithPrefix("limit-rate-after")] = "100"
|
|
|
|
data[parser.GetAnnotationWithPrefix("limit-rate")] = "10"
|
2017-08-13 06:52:20 +00:00
|
|
|
|
2016-05-27 21:03:54 +00:00
|
|
|
ing.SetAnnotations(data)
|
|
|
|
|
2017-08-13 06:52:20 +00:00
|
|
|
i, err := NewParser(mockBackend{}).Parse(ing)
|
2016-05-27 21:03:54 +00:00
|
|
|
if err != nil {
|
2016-12-29 20:02:06 +00:00
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
2017-11-07 16:36:51 +00:00
|
|
|
rateLimit, ok := i.(*Config)
|
2016-12-29 20:02:06 +00:00
|
|
|
if !ok {
|
|
|
|
t.Errorf("expected a RateLimit type")
|
2016-05-27 21:03:54 +00:00
|
|
|
}
|
|
|
|
if rateLimit.Connections.Limit != 5 {
|
2016-12-29 20:02:06 +00:00
|
|
|
t.Errorf("expected 5 in limit by ip but %v was returend", rateLimit.Connections)
|
2016-05-27 21:03:54 +00:00
|
|
|
}
|
|
|
|
if rateLimit.RPS.Limit != 100 {
|
2016-12-29 20:02:06 +00:00
|
|
|
t.Errorf("expected 100 in limit by rps but %v was returend", rateLimit.RPS)
|
2016-05-27 21:03:54 +00:00
|
|
|
}
|
2017-08-02 03:24:48 +00:00
|
|
|
if rateLimit.RPM.Limit != 10 {
|
|
|
|
t.Errorf("expected 10 in limit by rpm but %v was returend", rateLimit.RPM)
|
|
|
|
}
|
2017-08-13 06:52:20 +00:00
|
|
|
if rateLimit.LimitRateAfter != 100 {
|
|
|
|
t.Errorf("expected 100 in limit by limitrateafter but %v was returend", rateLimit.LimitRateAfter)
|
|
|
|
}
|
|
|
|
if rateLimit.LimitRate != 10 {
|
|
|
|
t.Errorf("expected 10 in limit by limitrate but %v was returend", rateLimit.LimitRate)
|
|
|
|
}
|
2016-05-27 21:03:54 +00:00
|
|
|
}
|