Add roundtrip test

This commit is contained in:
Ricardo Katz 2024-07-21 21:53:03 +00:00
parent 62522a3d49
commit fce3efdb94
7 changed files with 1340 additions and 13 deletions

View file

@ -20,7 +20,7 @@ import (
ngx_crossplane "github.com/nginxinc/nginx-go-crossplane"
)
func (c *crossplaneTemplate) buildConfig() {
func (c *CrossplaneTemplate) buildConfig() {
// Write basic directives
config := &ngx_crossplane.Config{
Parsed: ngx_crossplane.Directives{

View file

@ -34,15 +34,17 @@ Unsupported directives:
// On this case we will try to use the go ngx_crossplane to write the template instead of the template renderer
type crossplaneTemplate struct {
type CrossplaneTemplate struct {
options *ngx_crossplane.BuildOptions
config *ngx_crossplane.Config
tplConfig *config.TemplateConfig
mimeFile string
}
func NewCrossplaneTemplate() *crossplaneTemplate {
func NewCrossplaneTemplate() *CrossplaneTemplate {
lua := ngx_crossplane.Lua{}
return &crossplaneTemplate{
return &CrossplaneTemplate{
mimeFile: "/etc/nginx/mime.types",
options: &ngx_crossplane.BuildOptions{
Builders: []ngx_crossplane.RegisterBuilder{
lua.RegisterBuilder(),
@ -51,7 +53,11 @@ func NewCrossplaneTemplate() *crossplaneTemplate {
}
}
func (c *crossplaneTemplate) Write(conf *config.TemplateConfig) ([]byte, error) {
func (c *CrossplaneTemplate) SetMimeFile(file string) {
c.mimeFile = file
}
func (c *CrossplaneTemplate) Write(conf *config.TemplateConfig) ([]byte, error) {
c.tplConfig = conf
// build root directives

View file

@ -16,7 +16,25 @@ limitations under the License.
package crossplane_test
import "testing"
import (
"os"
"testing"
ngx_crossplane "github.com/nginxinc/nginx-go-crossplane"
"github.com/stretchr/testify/require"
"k8s.io/ingress-nginx/internal/ingress/controller/config"
"k8s.io/ingress-nginx/internal/ingress/controller/template/crossplane"
"k8s.io/ingress-nginx/pkg/apis/ingress"
)
const mockMimeTypes = `
types {
text/html html htm shtml;
text/css css;
text/xml xml;
}
`
// TestCrossplaneTemplate should be a roundtrip test.
// We should initialize the scenarios based on the template configuration
@ -24,5 +42,45 @@ import "testing"
// if the directives matches
// we should ignore line numbers and comments
func TestCrossplaneTemplate(t *testing.T) {
// implement
lua := ngx_crossplane.Lua{}
options := ngx_crossplane.ParseOptions{
ErrorOnUnknownDirectives: true,
StopParsingOnError: true,
IgnoreDirectives: []string{"more_clear_headers"},
DirectiveSources: []ngx_crossplane.MatchFunc{ngx_crossplane.DefaultDirectivesMatchFunc, ngx_crossplane.LuaDirectivesMatchFn},
LexOptions: ngx_crossplane.LexOptions{
Lexers: []ngx_crossplane.RegisterLexer{lua.RegisterLexer()},
},
}
defaultCertificate := &ingress.SSLCert{
PemFileName: "bla.crt",
PemCertKey: "bla.key",
}
mimeFile, err := os.CreateTemp("", "")
require.NoError(t, err)
_, err = mimeFile.WriteString(mockMimeTypes)
require.NoError(t, err)
require.NoError(t, mimeFile.Close())
t.Run("it should be able to marshall and unmarshall the current configuration", func(t *testing.T) {
tplConfig := &config.TemplateConfig{
Cfg: config.NewDefault(),
}
tplConfig.Cfg.DefaultSSLCertificate = defaultCertificate
tpl := crossplane.NewCrossplaneTemplate()
tpl.SetMimeFile(mimeFile.Name())
content, err := tpl.Write(tplConfig)
require.NoError(t, err)
tmpFile, err := os.CreateTemp("", "")
require.NoError(t, err)
_, err = tmpFile.Write(content)
require.NoError(t, err)
require.NoError(t, tmpFile.Close())
_, err = ngx_crossplane.Parse(tmpFile.Name(), &options)
require.NoError(t, err)
})
}

View file

@ -20,7 +20,7 @@ import (
ngx_crossplane "github.com/nginxinc/nginx-go-crossplane"
)
func (c *crossplaneTemplate) buildEvents() {
func (c *CrossplaneTemplate) buildEvents() {
events := &ngx_crossplane.Directive{
Directive: "events",
Block: ngx_crossplane.Directives{

View file

@ -24,11 +24,11 @@ import (
ngx_crossplane "github.com/nginxinc/nginx-go-crossplane"
)
func (c *crossplaneTemplate) initHTTPDirectives() ngx_crossplane.Directives {
func (c *CrossplaneTemplate) initHTTPDirectives() ngx_crossplane.Directives {
cfg := c.tplConfig.Cfg
httpBlock := ngx_crossplane.Directives{
buildDirective("lua_package_path", "/etc/nginx/lua/?.lua;;"),
buildDirective("include", "/etc/nginx/mime.types"),
buildDirective("include", c.mimeFile),
buildDirective("default_type", cfg.DefaultType),
buildDirective("real_ip_recursive", "on"),
buildDirective("aio", "threads"),
@ -46,7 +46,7 @@ func (c *crossplaneTemplate) initHTTPDirectives() ngx_crossplane.Directives {
buildDirective("proxy_temp_path", "/tmp/nginx/proxy-temp"),
buildDirective("client_header_buffer_size", cfg.ClientHeaderBufferSize),
buildDirective("client_header_timeout", seconds(cfg.ClientHeaderTimeout)),
buildDirective("large_client_header_buffers", cfg.LargeClientHeaderBuffers),
buildDirective("large_client_header_buffers", strings.Split(cfg.LargeClientHeaderBuffers, " ")),
buildDirective("client_body_buffer_size", cfg.ClientBodyBufferSize),
buildDirective("client_body_timeout", seconds(cfg.ClientBodyTimeout)),
buildDirective("types_hash_max_size", "2048"),
@ -64,6 +64,7 @@ func (c *crossplaneTemplate) initHTTPDirectives() ngx_crossplane.Directives {
buildDirective("uninitialized_variable_warn", "off"),
buildDirective("server_name_in_redirect", "off"),
buildDirective("port_in_redirect", "off"),
buildDirective("http2_max_concurrent_streams", cfg.HTTP2MaxConcurrentStreams),
buildDirective("ssl_protocols", strings.Split(cfg.SSLProtocols, " ")),
buildDirective("ssl_early_data", cfg.SSLEarlyData),
buildDirective("ssl_session_tickets", cfg.SSLSessionTickets),
@ -80,7 +81,7 @@ func (c *crossplaneTemplate) initHTTPDirectives() ngx_crossplane.Directives {
return httpBlock
}
func (c *crossplaneTemplate) buildHTTP() {
func (c *CrossplaneTemplate) buildHTTP() {
cfg := c.tplConfig.Cfg
httpBlock := c.initHTTPDirectives()
httpBlock = append(httpBlock, buildLuaSharedDictionaries(&c.tplConfig.Cfg)...)
@ -121,7 +122,7 @@ func (c *crossplaneTemplate) buildHTTP() {
httpBlock = append(httpBlock, buildDirective("gzip_vary", "on"))
if cfg.GzipDisable != "" {
httpBlock = append(httpBlock, buildDirective("gzip_disable", strings.Split(cfg.GzipDisable, "")))
httpBlock = append(httpBlock, buildDirective("gzip_disable", strings.Split(cfg.GzipDisable, " ")))
}
}

View file

@ -0,0 +1,8 @@
This directory contains the following files:
* nginx.tmpl - Should be used to track migrated directives. We will test later to see
if the ending result of it is the same as the one parsed by go-crossplane
* various nginx.conf - Will be used to see if the template parser reacts as
expected, creating files that matches and can be parsed by go-crossplane
TODO: move files to embed.FS

File diff suppressed because it is too large Load diff