2016-11-10 22:56:29 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 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 template
|
|
|
|
|
|
|
|
import (
|
2018-06-21 14:50:57 +00:00
|
|
|
"fmt"
|
2018-05-16 18:08:43 +00:00
|
|
|
"reflect"
|
2016-11-10 22:56:29 +00:00
|
|
|
"testing"
|
2018-06-04 01:10:41 +00:00
|
|
|
"time"
|
2016-11-10 22:56:29 +00:00
|
|
|
|
2017-01-10 18:02:35 +00:00
|
|
|
"github.com/kylelemons/godebug/pretty"
|
2018-06-21 14:50:57 +00:00
|
|
|
"github.com/mitchellh/hashstructure"
|
2017-01-10 18:02:35 +00:00
|
|
|
|
2017-11-07 22:02:12 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/controller/config"
|
2016-11-10 22:56:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFilterErrors(t *testing.T) {
|
|
|
|
e := filterErrors([]int{200, 300, 345, 500, 555, 999})
|
|
|
|
if len(e) != 4 {
|
|
|
|
t.Errorf("expected 4 elements but %v returned", len(e))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-04 01:10:41 +00:00
|
|
|
func TestProxytTimeoutParsing(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
input string
|
|
|
|
expect time.Duration // duration in seconds
|
|
|
|
}{
|
|
|
|
"valid duration": {"35s", time.Duration(35) * time.Second},
|
|
|
|
"invalid duration": {"3zxs", time.Duration(5) * time.Second},
|
|
|
|
}
|
|
|
|
for n, tc := range testCases {
|
|
|
|
cfg := ReadConfig(map[string]string{"proxy-protocol-header-timeout": tc.input})
|
|
|
|
if cfg.ProxyProtocolHeaderTimeout.Seconds() != tc.expect.Seconds() {
|
|
|
|
t.Errorf("Testing %v. Expected %v seconds but got %v seconds", n, tc.expect, cfg.ProxyProtocolHeaderTimeout)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-10 22:56:29 +00:00
|
|
|
func TestMergeConfigMapToStruct(t *testing.T) {
|
2017-01-20 22:37:59 +00:00
|
|
|
conf := map[string]string{
|
2018-04-16 10:03:06 +00:00
|
|
|
"custom-http-errors": "300,400,demo",
|
|
|
|
"proxy-read-timeout": "1",
|
|
|
|
"proxy-send-timeout": "2",
|
|
|
|
"skip-access-log-urls": "/log,/demo,/test",
|
|
|
|
"use-proxy-protocol": "true",
|
|
|
|
"disable-access-log": "true",
|
|
|
|
"access-log-path": "/var/log/test/access.log",
|
|
|
|
"error-log-path": "/var/log/test/error.log",
|
|
|
|
"use-gzip": "true",
|
|
|
|
"enable-dynamic-tls-records": "false",
|
2018-07-09 00:30:59 +00:00
|
|
|
"gzip-level": "9",
|
2018-04-16 10:03:06 +00:00
|
|
|
"gzip-types": "text/html",
|
|
|
|
"proxy-real-ip-cidr": "1.1.1.1/8,2.2.2.2/24",
|
|
|
|
"bind-address": "1.1.1.1,2.2.2.2,3.3.3,2001:db8:a0b:12f0::1,3731:54:65fe:2::a7,33:33:33::33::33",
|
|
|
|
"worker-shutdown-timeout": "99s",
|
|
|
|
"nginx-status-ipv4-whitelist": "127.0.0.1,10.0.0.0/24",
|
|
|
|
"nginx-status-ipv6-whitelist": "::1,2001::/16",
|
|
|
|
"proxy-add-original-uri-header": "false",
|
2016-12-28 13:08:02 +00:00
|
|
|
}
|
2016-11-10 22:56:29 +00:00
|
|
|
def := config.NewDefault()
|
|
|
|
def.CustomHTTPErrors = []int{300, 400}
|
2017-02-09 23:20:12 +00:00
|
|
|
def.DisableAccessLog = true
|
2017-08-23 14:57:28 +00:00
|
|
|
def.AccessLogPath = "/var/log/test/access.log"
|
|
|
|
def.ErrorLogPath = "/var/log/test/error.log"
|
2016-12-28 13:08:02 +00:00
|
|
|
def.SkipAccessLogURLs = []string{"/log", "/demo", "/test"}
|
|
|
|
def.ProxyReadTimeout = 1
|
|
|
|
def.ProxySendTimeout = 2
|
|
|
|
def.EnableDynamicTLSRecords = false
|
|
|
|
def.UseProxyProtocol = true
|
2018-07-09 00:30:59 +00:00
|
|
|
def.GzipLevel = 9
|
2016-12-28 13:08:02 +00:00
|
|
|
def.GzipTypes = "text/html"
|
2017-07-06 20:17:46 +00:00
|
|
|
def.ProxyRealIPCIDR = []string{"1.1.1.1/8", "2.2.2.2/24"}
|
2017-08-25 02:24:32 +00:00
|
|
|
def.BindAddressIpv4 = []string{"1.1.1.1", "2.2.2.2"}
|
|
|
|
def.BindAddressIpv6 = []string{"[2001:db8:a0b:12f0::1]", "[3731:54:65fe:2::a7]"}
|
2017-08-29 09:49:18 +00:00
|
|
|
def.WorkerShutdownTimeout = "99s"
|
2018-03-28 12:27:34 +00:00
|
|
|
def.NginxStatusIpv4Whitelist = []string{"127.0.0.1", "10.0.0.0/24"}
|
|
|
|
def.NginxStatusIpv6Whitelist = []string{"::1", "2001::/16"}
|
2018-10-30 23:46:48 +00:00
|
|
|
def.ProxyAddOriginalURIHeader = false
|
2016-12-28 13:08:02 +00:00
|
|
|
|
2018-06-21 14:50:57 +00:00
|
|
|
hash, err := hashstructure.Hash(def, &hashstructure.HashOptions{
|
|
|
|
TagName: "json",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error obtaining hash: %v", err)
|
|
|
|
}
|
|
|
|
def.Checksum = fmt.Sprintf("%v", hash)
|
|
|
|
|
2016-12-28 13:08:02 +00:00
|
|
|
to := ReadConfig(conf)
|
2017-01-13 13:26:13 +00:00
|
|
|
if diff := pretty.Compare(to, def); diff != "" {
|
|
|
|
t.Errorf("unexpected diff: (-got +want)\n%s", diff)
|
2016-12-28 13:08:02 +00:00
|
|
|
}
|
2017-01-10 18:02:35 +00:00
|
|
|
|
2018-05-16 18:08:43 +00:00
|
|
|
to = ReadConfig(conf)
|
|
|
|
def.BindAddressIpv4 = []string{}
|
|
|
|
def.BindAddressIpv6 = []string{}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(to.BindAddressIpv4, []string{"1.1.1.1", "2.2.2.2"}) {
|
|
|
|
t.Errorf("unexpected bindAddressIpv4")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(to.BindAddressIpv6, []string{"[2001:db8:a0b:12f0::1]", "[3731:54:65fe:2::a7]"}) {
|
|
|
|
t.Logf("%v", to.BindAddressIpv6)
|
|
|
|
t.Errorf("unexpected bindAddressIpv6")
|
|
|
|
}
|
|
|
|
|
2017-01-10 18:02:35 +00:00
|
|
|
def = config.NewDefault()
|
2018-06-21 14:50:57 +00:00
|
|
|
hash, err = hashstructure.Hash(def, &hashstructure.HashOptions{
|
|
|
|
TagName: "json",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error obtaining hash: %v", err)
|
|
|
|
}
|
|
|
|
def.Checksum = fmt.Sprintf("%v", hash)
|
|
|
|
|
2017-01-20 22:37:59 +00:00
|
|
|
to = ReadConfig(map[string]string{})
|
2017-01-13 13:26:13 +00:00
|
|
|
if diff := pretty.Compare(to, def); diff != "" {
|
|
|
|
t.Errorf("unexpected diff: (-got +want)\n%s", diff)
|
2017-01-10 18:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def = config.NewDefault()
|
|
|
|
def.WhitelistSourceRange = []string{"1.1.1.1/32"}
|
2018-06-21 14:50:57 +00:00
|
|
|
|
|
|
|
hash, err = hashstructure.Hash(def, &hashstructure.HashOptions{
|
|
|
|
TagName: "json",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error obtaining hash: %v", err)
|
|
|
|
}
|
|
|
|
def.Checksum = fmt.Sprintf("%v", hash)
|
|
|
|
|
2017-01-20 22:37:59 +00:00
|
|
|
to = ReadConfig(map[string]string{
|
|
|
|
"whitelist-source-range": "1.1.1.1/32",
|
2017-01-10 18:02:35 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if diff := pretty.Compare(to, def); diff != "" {
|
|
|
|
t.Errorf("unexpected diff: (-got +want)\n%s", diff)
|
|
|
|
}
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
2017-04-28 20:41:53 +00:00
|
|
|
|
|
|
|
func TestDefaultLoadBalance(t *testing.T) {
|
2017-05-12 00:50:43 +00:00
|
|
|
conf := map[string]string{}
|
2017-04-28 20:41:53 +00:00
|
|
|
to := ReadConfig(conf)
|
2018-07-17 00:55:29 +00:00
|
|
|
if to.LoadBalanceAlgorithm != "" {
|
2017-04-28 20:41:53 +00:00
|
|
|
t.Errorf("default load balance algorithm wrong")
|
|
|
|
}
|
|
|
|
}
|