2020-10-09 00:12:29 +00:00
|
|
|
package ingress
|
2020-10-06 17:12:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
2020-10-20 14:29:13 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-10-06 17:12:52 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-10-20 14:29:13 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2020-10-20 17:00:34 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/cmd/cloudflared/config"
|
2020-10-06 17:12:52 +00:00
|
|
|
)
|
|
|
|
|
2020-10-15 21:41:03 +00:00
|
|
|
func TestParseUnixSocket(t *testing.T) {
|
|
|
|
rawYAML := `
|
|
|
|
ingress:
|
|
|
|
- service: unix:/tmp/echo.sock
|
|
|
|
`
|
|
|
|
ing, err := ParseIngressDryRun(MustReadIngress(rawYAML))
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, ok := ing.Rules[0].Service.(UnixSocketPath)
|
|
|
|
require.True(t, ok)
|
|
|
|
}
|
|
|
|
|
2020-10-20 14:29:13 +00:00
|
|
|
func Test_parseIngress(t *testing.T) {
|
|
|
|
localhost8000 := MustParseURL(t, "https://localhost:8000")
|
|
|
|
localhost8001 := MustParseURL(t, "https://localhost:8001")
|
2020-10-15 21:41:03 +00:00
|
|
|
defaultConfig := SetConfig(OriginRequestFromYAML(config.OriginRequestConfig{}), config.OriginRequestConfig{})
|
|
|
|
require.Equal(t, defaultKeepAliveConnections, defaultConfig.KeepAliveConnections)
|
2020-10-20 14:29:13 +00:00
|
|
|
type args struct {
|
|
|
|
rawYAML string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2020-10-15 21:41:03 +00:00
|
|
|
want []Rule
|
2020-10-20 14:29:13 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Empty file",
|
|
|
|
args: args{rawYAML: ""},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Multiple rules",
|
|
|
|
args: args{rawYAML: `
|
|
|
|
ingress:
|
|
|
|
- hostname: tunnel1.example.com
|
|
|
|
service: https://localhost:8000
|
|
|
|
- hostname: "*"
|
|
|
|
service: https://localhost:8001
|
|
|
|
`},
|
2020-10-15 21:41:03 +00:00
|
|
|
want: []Rule{
|
2020-10-20 14:29:13 +00:00
|
|
|
{
|
|
|
|
Hostname: "tunnel1.example.com",
|
2020-10-15 21:41:03 +00:00
|
|
|
Service: &URL{URL: localhost8000},
|
|
|
|
Config: defaultConfig,
|
2020-10-20 14:29:13 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Hostname: "*",
|
2020-10-15 21:41:03 +00:00
|
|
|
Service: &URL{URL: localhost8001},
|
|
|
|
Config: defaultConfig,
|
2020-10-20 14:29:13 +00:00
|
|
|
},
|
2020-10-15 21:41:03 +00:00
|
|
|
},
|
2020-10-20 14:29:13 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Extra keys",
|
|
|
|
args: args{rawYAML: `
|
|
|
|
ingress:
|
|
|
|
- hostname: "*"
|
|
|
|
service: https://localhost:8000
|
|
|
|
extraKey: extraValue
|
|
|
|
`},
|
2020-10-15 21:41:03 +00:00
|
|
|
want: []Rule{
|
2020-10-20 14:29:13 +00:00
|
|
|
{
|
|
|
|
Hostname: "*",
|
2020-10-15 21:41:03 +00:00
|
|
|
Service: &URL{URL: localhost8000},
|
|
|
|
Config: defaultConfig,
|
2020-10-20 14:29:13 +00:00
|
|
|
},
|
2020-10-15 21:41:03 +00:00
|
|
|
},
|
2020-10-20 14:29:13 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Hostname can be omitted",
|
|
|
|
args: args{rawYAML: `
|
|
|
|
ingress:
|
|
|
|
- service: https://localhost:8000
|
|
|
|
`},
|
2020-10-15 21:41:03 +00:00
|
|
|
want: []Rule{
|
2020-10-20 14:29:13 +00:00
|
|
|
{
|
2020-10-15 21:41:03 +00:00
|
|
|
Service: &URL{URL: localhost8000},
|
|
|
|
Config: defaultConfig,
|
2020-10-20 14:29:13 +00:00
|
|
|
},
|
2020-10-15 21:41:03 +00:00
|
|
|
},
|
2020-10-20 14:29:13 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid service",
|
|
|
|
args: args{rawYAML: `
|
|
|
|
ingress:
|
|
|
|
- hostname: "*"
|
|
|
|
service: https://local host:8000
|
|
|
|
`},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Last rule isn't catchall",
|
|
|
|
args: args{rawYAML: `
|
|
|
|
ingress:
|
|
|
|
- hostname: example.com
|
|
|
|
service: https://localhost:8000
|
|
|
|
`},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "First rule is catchall",
|
|
|
|
args: args{rawYAML: `
|
|
|
|
ingress:
|
|
|
|
- service: https://localhost:8000
|
|
|
|
- hostname: example.com
|
|
|
|
service: https://localhost:8000
|
|
|
|
`},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Catch-all rule can't have a path",
|
|
|
|
args: args{rawYAML: `
|
|
|
|
ingress:
|
|
|
|
- service: https://localhost:8001
|
|
|
|
path: /subpath1/(.*)/subpath2
|
|
|
|
`},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid regex",
|
|
|
|
args: args{rawYAML: `
|
|
|
|
ingress:
|
|
|
|
- hostname: example.com
|
|
|
|
service: https://localhost:8000
|
|
|
|
path: "*/subpath2"
|
|
|
|
- service: https://localhost:8001
|
|
|
|
`},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Service must have a scheme",
|
|
|
|
args: args{rawYAML: `
|
|
|
|
ingress:
|
|
|
|
- service: localhost:8000
|
|
|
|
`},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Wildcard not at start",
|
|
|
|
args: args{rawYAML: `
|
|
|
|
ingress:
|
|
|
|
- hostname: "test.*.example.com"
|
|
|
|
service: https://localhost:8000
|
|
|
|
`},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Service can't have a path",
|
|
|
|
args: args{rawYAML: `
|
|
|
|
ingress:
|
|
|
|
- service: https://localhost:8000/static/
|
|
|
|
`},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2020-10-15 21:41:03 +00:00
|
|
|
got, err := ParseIngressDryRun(MustReadIngress(tt.args.rawYAML))
|
2020-10-20 14:29:13 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2020-10-15 21:41:03 +00:00
|
|
|
t.Errorf("ParseIngressDryRun() error = %v, wantErr %v", err, tt.wantErr)
|
2020-10-20 14:29:13 +00:00
|
|
|
return
|
|
|
|
}
|
2020-10-15 21:41:03 +00:00
|
|
|
assert.Equal(t, tt.want, got.Rules)
|
2020-10-20 14:29:13 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-10-07 21:34:53 +00:00
|
|
|
|
2020-10-20 14:29:13 +00:00
|
|
|
func MustParseURL(t *testing.T, rawURL string) *url.URL {
|
2020-10-07 21:34:53 +00:00
|
|
|
u, err := url.Parse(rawURL)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
2020-10-20 14:29:13 +00:00
|
|
|
func BenchmarkFindMatch(b *testing.B) {
|
|
|
|
rulesYAML := `
|
|
|
|
ingress:
|
|
|
|
- hostname: tunnel1.example.com
|
|
|
|
service: https://localhost:8000
|
|
|
|
- hostname: tunnel2.example.com
|
|
|
|
service: https://localhost:8001
|
|
|
|
- hostname: "*"
|
|
|
|
service: https://localhost:8002
|
|
|
|
`
|
|
|
|
|
2020-10-15 21:41:03 +00:00
|
|
|
ing, err := ParseIngressDryRun(MustReadIngress(rulesYAML))
|
2020-10-20 14:29:13 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Error(err)
|
|
|
|
}
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
ing.FindMatchingRule("tunnel1.example.com", "")
|
|
|
|
ing.FindMatchingRule("tunnel2.example.com", "")
|
|
|
|
ing.FindMatchingRule("tunnel3.example.com", "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 17:00:34 +00:00
|
|
|
func MustReadIngress(s string) *config.Configuration {
|
|
|
|
var conf config.Configuration
|
|
|
|
err := yaml.Unmarshal([]byte(s), &conf)
|
2020-10-20 14:29:13 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-10-20 17:00:34 +00:00
|
|
|
return &conf
|
2020-10-20 14:29:13 +00:00
|
|
|
}
|