Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nginx conf #1130

Merged
merged 23 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add more test for convert
  • Loading branch information
suchen-sci committed Nov 7, 2023
commit 0d472e4d73a1a0b880ef4232337deed0133be6d0
5 changes: 4 additions & 1 deletion cmd/client/commandv2/common/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ func (p *PipelineSpec) SetFilters(filters []filters.Spec) {

// NewHTTPServerSpec returns a new HTTPServerSpec.
func NewHTTPServerSpec(name string) *HTTPServerSpec {
return &HTTPServerSpec{
spec := &HTTPServerSpec{
Name: name,
Kind: httpserver.Kind,
Spec: *getDefaultHTTPServerSpec(),
}
spec.Spec.Certs = map[string]string{}
spec.Spec.Keys = map[string]string{}
return spec
}

// NewPipelineSpec returns a new PipelineSpec.
Expand Down
6 changes: 4 additions & 2 deletions cmd/client/commandv2/convert/nginx/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,10 @@ func getWebsocketFilter(info *ProxyInfo) *httpproxy.WebSocketProxySpec {

func getProxyFilter(info *ProxyInfo) *httpproxy.Spec {
spec := common.NewProxyFilterSpec("proxy")
spec.Compression = &httpproxy.CompressionSpec{
MinLength: uint32(info.GzipMinLength),
if info.GzipMinLength != 0 {
spec.Compression = &httpproxy.CompressionSpec{
MinLength: uint32(info.GzipMinLength),
}
}
spec.Pools = []*httpproxy.ServerPoolSpec{{
BaseServerPoolSpec: getBaseServerPool(info),
Expand Down
201 changes: 201 additions & 0 deletions cmd/client/commandv2/convert/nginx/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import (
"strings"
"testing"

"github.com/megaease/easegress/v2/cmd/client/commandv2/common"
"github.com/megaease/easegress/v2/pkg/filters"
"github.com/megaease/easegress/v2/pkg/filters/builder"
"github.com/megaease/easegress/v2/pkg/protocols/httpprot"
"github.com/megaease/easegress/v2/pkg/util/codectool"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -74,3 +76,202 @@ func TestGetRequestAdaptor(t *testing.T) {
assert.Equal(v, h.Get(k), fmt.Sprintf("header %s", k))
}
}

func TestConvertConfig(t *testing.T) {
options := &Options{
Prefix: "test-convert",
}
options.init()
conf := `
servers:
- port: 8080
address: localhost
https: true
caCert: caCertBase64Str
certs:
cert1: cert1Base64Str
keys:
cert1: key1Base64Str
rules:
- hosts:
- value: www.example.com
isRegexp: false
- isRegexp: true
value: '.*\.example\.com'
paths:
- path: /apis
type: prefix
backend:
servers:
- server: http:https://localhost:8880
weight: 1
- server: http:https://localhost:8881
weight: 2
setHeaders:
X-Path: apis
gzipMinLength: 1000
- path: /exact
type: exact
backend:
servers:
- server: http:https://localhost:9999
weight: 1
- path: /regexp
type: regexp
backend:
servers:
- server: http:https://localhost:7777
weight: 1
- path: /case-insensitive-regexp
type: caseInsensitiveRegexp
backend:
servers:
- server: http:https://localhost:6666
weight: 1
`
config := &Config{}
err := codectool.Unmarshal([]byte(conf), config)
assert.Nil(t, err)
httpServers, pipelines, err := convertConfig(options, config)
assert.Nil(t, err)
assert.Equal(t, 1, len(httpServers))
assert.Equal(t, 4, len(pipelines))
serverYaml := `
name: test-convert-8080
kind: HTTPServer
https: true
caCertBase64: caCertBase64Str
certs:
cert1: cert1Base64Str
keys:
cert1: key1Base64Str
port: 8080
address: localhost
rules:
- hosts:
- value: www.example.com
isRegexp: false
- isRegexp: true
value: '.*\.example\.com'
paths:
- path: /exact
backend: test-convert-exact
- pathPrefix: /apis
backend: test-convert-apis
- pathRegexp: /regexp
backend: test-convert-regexp
- pathRegexp: (?i)/case-insensitive-regexp
backend: test-convert-caseinsensitiveregexp
`
expected := common.NewHTTPServerSpec("test-convert-8080")
err = codectool.UnmarshalYAML([]byte(serverYaml), expected)
assert.Nil(t, err)
assert.Equal(t, expected, httpServers[0])

pipelineApis := `
name: test-convert-apis
kind: Pipeline
filters:
- kind: RequestAdaptor
name: request-adaptor
template: |
header:
set:
X-Path: apis
- compression:
minLength: 1000
kind: Proxy
name: proxy
pools:
- loadBalance:
policy: weightedRandom
servers:
- url: http:https://localhost:8880
weight: 1
- url: http:https://localhost:8881
weight: 2
`
pipelineExact := `
name: test-convert-exact
kind: Pipeline
filters:
- kind: Proxy
name: proxy
pools:
- loadBalance:
policy: roundRobin
servers:
- url: http:https://localhost:9999
weight: 1
`
pipelineRegexp := `
name: test-convert-regexp
kind: Pipeline
filters:
- kind: Proxy
name: proxy
pools:
- loadBalance:
policy: roundRobin
servers:
- url: http:https://localhost:7777
weight: 1
`
pipelineCIReg := `
name: test-convert-caseinsensitiveregexp
kind: Pipeline
filters:
- kind: Proxy
name: proxy
pools:
- loadBalance:
policy: roundRobin
servers:
- url: http:https://localhost:6666
weight: 1
`
for i, yamlStr := range []string{pipelineApis, pipelineExact, pipelineRegexp, pipelineCIReg} {
spec := common.NewPipelineSpec("")
err = codectool.UnmarshalYAML([]byte(yamlStr), spec)
assert.Nil(t, err, i)
for j, f := range spec.Filters {
compareFilter(t, f, pipelines[i].Filters[j], fmt.Sprintf("%d filter in %d pipeline", j, i))
}
}
}

func compareFilter(t *testing.T, f1 map[string]interface{}, f2 map[string]interface{}, msg string) {
d1, err := codectool.MarshalYAML(f1)
assert.Nil(t, err, msg)
d2, err := codectool.MarshalYAML(f2)
assert.Nil(t, err, msg)

var specFn func() interface{}
switch f1["kind"] {
case "Proxy":
specFn = func() interface{} {
return common.NewProxyFilterSpec("")
}
s1 := common.NewProxyFilterSpec("")
err = codectool.UnmarshalYAML(d1, s1)
assert.Nil(t, err, msg)
s2 := common.NewProxyFilterSpec("")
err = codectool.Unmarshal(d2, s2)
assert.Nil(t, err, msg)
assert.Equal(t, s1, s2)
case "RequestAdaptor":
specFn = func() interface{} {
return common.NewRequestAdaptorFilterSpec("")
}
default:
t.Errorf("filter kind %s is not compared", f1["kind"])
return
}
s1 := specFn()
err = codectool.UnmarshalYAML(d1, s1)
assert.Nil(t, err, msg)
s2 := specFn()
err = codectool.Unmarshal(d2, s2)
assert.Nil(t, err, msg)
assert.Equal(t, s1, s2)
}
7 changes: 0 additions & 7 deletions cmd/client/commandv2/convert/nginx/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,8 @@ package nginx
import (
"fmt"
"strings"

"github.com/megaease/easegress/v2/pkg/util/codectool"
)

func directiveInfo(d *Directive) string {
return fmt.Sprintf("directive <%s %s> of %s:%d", d.Directive, strings.Join(d.Args, " "), d.File, d.Line)
}

func printYaml(v interface{}) {
b, _ := codectool.MarshalYAML(v)
fmt.Println(string(b))
}
Loading