Skip to content

Commit

Permalink
add more test
Browse files Browse the repository at this point in the history
  • Loading branch information
suchen-sci committed Nov 8, 2023
1 parent d966e40 commit 3fdb6fd
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cmd/client/commandv2/convert/nginx/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func Cmd() *cobra.Command {
if err != nil {
general.ExitWithErrorf("parse nginx.conf failed: %v", err)
}
for _, e := range payload.Errors {
general.Warnf("parse nginx.conf error: %v in %s of %s", e.Error, e.Line, e.File)
}
config, err := parsePayload(payload)
if err != nil {
general.ExitWithError(err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/client/commandv2/convert/nginx/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ servers:
type: prefix
backend:
servers:
- server: http://localhost:9090
- server: https://localhost:9090
weight: 1
setHeaders:
Connection: $connection_upgrade
Expand Down Expand Up @@ -252,7 +252,7 @@ filters:
- loadBalance:
policy: roundRobin
servers:
- url: ws://localhost:9090
- url: wss://localhost:9090
weight: 1
`
for i, yamlStr := range []string{pipelineApis, pipelineExact, pipelineRegexp, pipelineCIReg, pipelineWebsocket} {
Expand Down
77 changes: 77 additions & 0 deletions cmd/client/commandv2/convert/nginx/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,80 @@ func TestSplitAddressPort(t *testing.T) {
}
}
}

func newDirective(d string, args ...string) *Directive {
return &Directive{
Directive: d,
Args: args,
}
}

func TestEnvProcessErrors(t *testing.T) {
// gzip
{
testCases := []struct {
gzip *Directive
len *Directive
res int
}{
{gzip: newDirective("gzip", "invalid"), len: nil, res: 0},
{gzip: newDirective("gzip", "on"), len: nil, res: 20},
{gzip: newDirective("gzip", "on"), len: newDirective("gzip_min_length", "200"), res: 200},
{gzip: newDirective("gzip", "on"), len: newDirective("gzip_min_length", "invalid"), res: 20},
{gzip: newDirective("gzip", "on"), len: newDirective("gzip_min_length", "-1"), res: 20},
}
for i, tc := range testCases {
gzip := &GzipEnv{
Gzip: tc.gzip,
GzipMinLength: tc.len,
}
got := processGzip(gzip)
assert.Equal(t, tc.res, got, "case", i)
}
}

// ssl
{
certs := []*Directive{newDirective("ssl_certificate", "cert1"), newDirective("ssl_certificate", "cert2")}
keys := []*Directive{newDirective("ssl_certificate_key", "key1")}
_, _, err := processSSLCertificates(certs, keys)
assert.NotNil(t, err)

certs = []*Directive{newDirective("ssl_certificate", "cert1")}
keys = []*Directive{newDirective("ssl_certificate_key", "key1"), newDirective("ssl_certificate_key", "key2")}
_, _, err = processSSLCertificates(certs, keys)
assert.NotNil(t, err)

certs = []*Directive{newDirective("ssl_certificate", "cert1")}
keys = []*Directive{newDirective("ssl_certificate_key", "key1")}
_, _, err = processSSLCertificates(certs, keys)
assert.NotNil(t, err)
}

// server name
{
testCases := []struct {
server *Directive
hostValues []string
isRegexp []bool
err bool
}{
{server: newDirective("server_name", "~www.example.com$"), hostValues: []string{"www.example.com$"}, isRegexp: []bool{true}, err: false},
{server: newDirective("server_name", "~["), hostValues: []string{}, isRegexp: []bool{}, err: true},
{server: newDirective("server_name", "*.example.*"), hostValues: []string{}, isRegexp: []bool{}, err: true},
}
for i, tc := range testCases {
serverNames, err := processServerName(tc.server)
assert.Equal(t, len(tc.hostValues), len(serverNames), "case", i)
for i := 0; i < len(tc.hostValues); i++ {
assert.Equal(t, tc.hostValues[i], serverNames[i].Value, "case", i)
assert.Equal(t, tc.isRegexp[i], serverNames[i].IsRegexp, "case", i)
}
if tc.err {
assert.NotNil(t, err, "case", i)
} else {
assert.Nil(t, err, "case", i)
}
}
}
}
1 change: 1 addition & 0 deletions cmd/client/commandv2/convert/nginx/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func updateServers(servers []*Server, s *Server) ([]*Server, error) {
for k, v := range s.Keys {
servers[i].Keys[k] = v
}
servers[i].Rules = append(servers[i].Rules, s.Rules...)
return servers, nil
}
return append(servers, s), nil
Expand Down
84 changes: 84 additions & 0 deletions cmd/client/commandv2/convert/nginx/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,87 @@ servers:
assert.Equal(t, expected, config)
}
}

func TestUpdateServer(t *testing.T) {
tempDir := newTempTestDir(t)
defer tempDir.Clean()
{
nginxConf := `
events {}
http {
server {
listen 80;
location /apis {
proxy_pass http://localhost:8880;
}
}
server {
listen 80;
location /user {
proxy_pass http://localhost:9999;
}
}
}
`
file := tempDir.Create("nginx.conf", []byte(nginxConf))
payload, err := crossplane.Parse(file, &crossplane.ParseOptions{})
assert.Nil(t, err)
config, err := parsePayload(payload)
assert.Nil(t, err)

proxyInfo := `
servers:
- port: 80
rules:
- paths:
- path: /apis
type: prefix
backend:
servers:
- server: http://localhost:8880
weight: 1
- paths:
- path: /user
type: prefix
backend:
servers:
- server: http://localhost:9999
weight: 1
`
expected := &Config{}
err = codectool.UnmarshalYAML([]byte(proxyInfo), expected)
assert.Nil(t, err)
assert.Equal(t, expected, config)
}
{
servers := []*Server{
{
ServerBase: ServerBase{
Port: 80,
Certs: map[string]string{},
Keys: map[string]string{},
},
},
}
server := &Server{
ServerBase: ServerBase{
Port: 80,
HTTPS: true,
CaCert: "ca",
Certs: map[string]string{"cert": "cert"},
Keys: map[string]string{"cert": "key"},
},
}
servers, err := updateServers(servers, server)
assert.Nil(t, err)
assert.Equal(t, 1, len(servers))
got := servers[0]
assert.True(t, got.HTTPS)
assert.Equal(t, "ca", got.CaCert)
assert.Equal(t, map[string]string{"cert": "cert"}, got.Certs)
assert.Equal(t, map[string]string{"cert": "key"}, got.Keys)
}
}

0 comments on commit 3fdb6fd

Please sign in to comment.