From 78717ce5b0a940df0ef91a19e8389b11505a344d Mon Sep 17 00:00:00 2001 From: Mark Sargent <99003+sarge@users.noreply.github.com> Date: Sat, 4 Apr 2020 16:02:46 +1300 Subject: [PATCH] chore: add adapt tests. fix load failure not failing tests (#3222) * add adaption tests. fix load failure not failing tests * removed unnecessary assignment --- caddytest/caddytest.go | 57 +++- caddytest/integration/caddyfile_adapt_test.go | 285 ++++++++++++++++++ go.mod | 1 + go.sum | 2 + 4 files changed, 344 insertions(+), 1 deletion(-) create mode 100644 caddytest/integration/caddyfile_adapt_test.go diff --git a/caddytest/caddytest.go b/caddytest/caddytest.go index 0f2799f2..ce70f77e 100644 --- a/caddytest/caddytest.go +++ b/caddytest/caddytest.go @@ -19,6 +19,8 @@ import ( "testing" "time" + "github.com/aryann/difflib" + "github.com/caddyserver/caddy/v2/caddyconfig" caddycmd "github.com/caddyserver/caddy/v2/cmd" // plug in Caddy modules here @@ -58,7 +60,8 @@ func timeElapsed(start time.Time, name string) { // InitServer this will configure the server with a configurion of a specific // type. The configType must be either "json" or the adapter type. func InitServer(t *testing.T, rawConfig string, configType string) { - if err := initServer(t, rawConfig, configType); errors.Is(err, &configLoadError{}) { + + if err := initServer(t, rawConfig, configType); err != nil { t.Logf("failed to load config: %s", err) t.Fail() } @@ -316,3 +319,55 @@ func AssertRedirect(t *testing.T, requestURI string, expectedToLocation string, return resp } + +// AssertAdapt adapts a config and then tests it against an expected result +func AssertAdapt(t *testing.T, rawConfig string, adapterName string, expectedResponse string) { + + cfgAdapter := caddyconfig.GetAdapter(adapterName) + if cfgAdapter == nil { + t.Errorf("unrecognized config adapter '%s'", adapterName) + return + } + + options := make(map[string]interface{}) + options["pretty"] = "true" + + result, warnings, err := cfgAdapter.Adapt([]byte(rawConfig), options) + if err != nil { + t.Errorf("adapting config using %s adapter: %v", adapterName, err) + return + } + + if len(warnings) > 0 { + for _, w := range warnings { + t.Logf("warning: directive: %s : %s", w.Directive, w.Message) + } + } + + diff := difflib.Diff( + strings.Split(expectedResponse, "\n"), + strings.Split(string(result), "\n")) + + // scan for failure + failed := false + for _, d := range diff { + if d.Delta != difflib.Common { + failed = true + break + } + } + + if failed { + for _, d := range diff { + switch d.Delta { + case difflib.Common: + fmt.Printf(" %s\n", d.Payload) + case difflib.LeftOnly: + fmt.Printf(" - %s\n", d.Payload) + case difflib.RightOnly: + fmt.Printf(" + %s\n", d.Payload) + } + } + t.Fail() + } +} diff --git a/caddytest/integration/caddyfile_adapt_test.go b/caddytest/integration/caddyfile_adapt_test.go new file mode 100644 index 00000000..bff325be --- /dev/null +++ b/caddytest/integration/caddyfile_adapt_test.go @@ -0,0 +1,285 @@ +package integration + +import ( + "testing" + + "github.com/caddyserver/caddy/v2/caddytest" +) + +func TestHttpOnlyOnLocalhost(t *testing.T) { + caddytest.AssertAdapt(t, ` + localhost:80 { + respond /version 200 { + body "hello from localhost" + } + } + `, "caddyfile", `{ + "apps": { + "http": { + "servers": { + "srv0": { + "listen": [ + ":80" + ], + "routes": [ + { + "match": [ + { + "host": [ + "localhost" + ] + } + ], + "handle": [ + { + "handler": "subroute", + "routes": [ + { + "handle": [ + { + "body": "hello from localhost", + "handler": "static_response", + "status_code": 200 + } + ], + "match": [ + { + "path": [ + "/version" + ] + } + ] + } + ] + } + ], + "terminal": true + } + ] + } + } + } + } +}`) +} + +func TestHttpOnlyOnAnyAddress(t *testing.T) { + caddytest.AssertAdapt(t, ` + :80 { + respond /version 200 { + body "hello from localhost" + } + } + `, "caddyfile", `{ + "apps": { + "http": { + "servers": { + "srv0": { + "listen": [ + ":80" + ], + "routes": [ + { + "match": [ + { + "path": [ + "/version" + ] + } + ], + "handle": [ + { + "body": "hello from localhost", + "handler": "static_response", + "status_code": 200 + } + ] + } + ] + } + } + } + } +}`) +} + +func TestHttpsOnDomain(t *testing.T) { + caddytest.AssertAdapt(t, ` + a.caddy.localhost { + respond /version 200 { + body "hello from localhost" + } + } + `, "caddyfile", `{ + "apps": { + "http": { + "servers": { + "srv0": { + "listen": [ + ":443" + ], + "routes": [ + { + "match": [ + { + "host": [ + "a.caddy.localhost" + ] + } + ], + "handle": [ + { + "handler": "subroute", + "routes": [ + { + "handle": [ + { + "body": "hello from localhost", + "handler": "static_response", + "status_code": 200 + } + ], + "match": [ + { + "path": [ + "/version" + ] + } + ] + } + ] + } + ], + "terminal": true + } + ] + } + } + } + } +}`) +} + +func TestHttpOnlyOnDomain(t *testing.T) { + caddytest.AssertAdapt(t, ` + http://a.caddy.localhost { + respond /version 200 { + body "hello from localhost" + } + } + `, "caddyfile", `{ + "apps": { + "http": { + "servers": { + "srv0": { + "listen": [ + ":80" + ], + "routes": [ + { + "match": [ + { + "host": [ + "a.caddy.localhost" + ] + } + ], + "handle": [ + { + "handler": "subroute", + "routes": [ + { + "handle": [ + { + "body": "hello from localhost", + "handler": "static_response", + "status_code": 200 + } + ], + "match": [ + { + "path": [ + "/version" + ] + } + ] + } + ] + } + ], + "terminal": true + } + ], + "automatic_https": { + "skip": [ + "a.caddy.localhost" + ] + } + } + } + } + } +}`) +} + +func TestHttpOnlyOnNonStandardPort(t *testing.T) { + caddytest.AssertAdapt(t, ` + http://a.caddy.localhost:81 { + respond /version 200 { + body "hello from localhost" + } + } + `, "caddyfile", `{ + "apps": { + "http": { + "servers": { + "srv0": { + "listen": [ + ":81" + ], + "routes": [ + { + "match": [ + { + "host": [ + "a.caddy.localhost" + ] + } + ], + "handle": [ + { + "handler": "subroute", + "routes": [ + { + "handle": [ + { + "body": "hello from localhost", + "handler": "static_response", + "status_code": 200 + } + ], + "match": [ + { + "path": [ + "/version" + ] + } + ] + } + ] + } + ], + "terminal": true + } + ], + "automatic_https": { + "skip": [ + "a.caddy.localhost" + ] + } + } + } + } + } +}`) +} diff --git a/go.mod b/go.mod index cba7d67d..4964196b 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.14 require ( github.com/Masterminds/sprig/v3 v3.0.2 github.com/alecthomas/chroma v0.7.2-0.20200305040604-4f3623dce67a + github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a github.com/caddyserver/certmagic v0.10.10 github.com/cenkalti/backoff/v4 v4.0.2 // indirect github.com/dustin/go-humanize v1.0.1-0.20200219035652-afde56e7acac diff --git a/go.sum b/go.sum index 98bea164..7c8ca5b6 100644 --- a/go.sum +++ b/go.sum @@ -106,6 +106,8 @@ github.com/antlr/antlr4 v0.0.0-20190819145818-b43a4c3a8015 h1:StuiJFxQUsxSCzcby6 github.com/antlr/antlr4 v0.0.0-20190819145818-b43a4c3a8015/go.mod h1:T7PbCXFs94rrTttyxjbyT5+/1V8T2TYDejxUfHJjw1Y= github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= +github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a h1:pv34s756C4pEXnjgPfGYgdhg/ZdajGhyOvzx8k+23nw= +github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a/go.mod h1:DAHtR1m6lCRdSC2Tm3DSWRPvIPr6xNKyeHdqDQSQT+A= github.com/asaskevich/govalidator v0.0.0-20180315120708-ccb8e960c48f/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY= github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY= github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=