From 3fdb6fd7a0fdaf391f0d4ccdb4c3637c810b40bf Mon Sep 17 00:00:00 2001 From: chen Date: Wed, 8 Nov 2023 14:19:08 +0800 Subject: [PATCH] add more test --- cmd/client/commandv2/convert/nginx/cmd.go | 3 + .../commandv2/convert/nginx/convert_test.go | 4 +- .../commandv2/convert/nginx/env_test.go | 77 +++++++++++++++++ cmd/client/commandv2/convert/nginx/parse.go | 1 + .../commandv2/convert/nginx/parse_test.go | 84 +++++++++++++++++++ 5 files changed, 167 insertions(+), 2 deletions(-) diff --git a/cmd/client/commandv2/convert/nginx/cmd.go b/cmd/client/commandv2/convert/nginx/cmd.go index e223458632..7fdb976813 100644 --- a/cmd/client/commandv2/convert/nginx/cmd.go +++ b/cmd/client/commandv2/convert/nginx/cmd.go @@ -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) diff --git a/cmd/client/commandv2/convert/nginx/convert_test.go b/cmd/client/commandv2/convert/nginx/convert_test.go index 299c86dc69..87bedeb623 100644 --- a/cmd/client/commandv2/convert/nginx/convert_test.go +++ b/cmd/client/commandv2/convert/nginx/convert_test.go @@ -132,7 +132,7 @@ servers: type: prefix backend: servers: - - server: http://localhost:9090 + - server: https://localhost:9090 weight: 1 setHeaders: Connection: $connection_upgrade @@ -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} { diff --git a/cmd/client/commandv2/convert/nginx/env_test.go b/cmd/client/commandv2/convert/nginx/env_test.go index bc01e74607..85223cdce4 100644 --- a/cmd/client/commandv2/convert/nginx/env_test.go +++ b/cmd/client/commandv2/convert/nginx/env_test.go @@ -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) + } + } + } +} diff --git a/cmd/client/commandv2/convert/nginx/parse.go b/cmd/client/commandv2/convert/nginx/parse.go index 6ba59f4692..0cf43e7176 100644 --- a/cmd/client/commandv2/convert/nginx/parse.go +++ b/cmd/client/commandv2/convert/nginx/parse.go @@ -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 diff --git a/cmd/client/commandv2/convert/nginx/parse_test.go b/cmd/client/commandv2/convert/nginx/parse_test.go index 3e88b2a71e..12d3d61f5b 100644 --- a/cmd/client/commandv2/convert/nginx/parse_test.go +++ b/cmd/client/commandv2/convert/nginx/parse_test.go @@ -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) + } +}