1
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-16 21:56:40 -05:00

caddytest: Update Caddyfile tests for formatting, HTTP-only blocks

Previous commit improved the Caddyfile adapter so it doesn't unnecessarily add names to "skip" in "auto_https" when the server is already HTTP-only.

This commit updates the tests to reflect that change, while also fixing the Caddyfile formatting in many of the tests.

We also print the line number of the divergence between input and formatted version in Caddyfile adapt warnings - very useful for finding initial formatting problems.
This commit is contained in:
Matthew Holt 2021-01-19 14:21:11 -07:00
parent d68cff8eb6
commit 160d199999
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5
18 changed files with 103 additions and 100 deletions

View file

@ -53,13 +53,9 @@ func (a Adapter) Adapt(body []byte, options map[string]interface{}) ([]byte, []c
}
// lint check: see if input was properly formatted; sometimes messy files files parse
// successfully but result in logical errors because the Caddyfile is a bad format
// TODO: also perform this check on imported files
if !bytes.Equal(Format(body), body) {
warnings = append(warnings, caddyconfig.Warning{
File: filename,
Message: "file is not formatted with 'caddy fmt'",
})
// successfully but result in logical errors (the Caddyfile is a bad format, I'm sorry)
if warning, different := formattingDifference(filename, body); different {
warnings = append(warnings, warning)
}
result, err := json.Marshal(cfg)
@ -67,6 +63,32 @@ func (a Adapter) Adapt(body []byte, options map[string]interface{}) ([]byte, []c
return result, warnings, err
}
// formattingDifference returns a warning and true if the formatted version
// is any different from the input; empty warning and false otherwise.
// TODO: also perform this check on imported files
func formattingDifference(filename string, body []byte) (caddyconfig.Warning, bool) {
formatted := Format(body)
if bytes.Equal(formatted, body) {
return caddyconfig.Warning{}, false
}
// find where the difference is
line := 1
for i, ch := range body {
if i >= len(formatted) || ch != formatted[i] {
break
}
if ch == '\n' {
line++
}
}
return caddyconfig.Warning{
File: filename,
Line: line,
Message: "input is not formatted with 'caddy fmt'",
}, true
}
// Unmarshaler is a type that can unmarshal
// Caddyfile tokens to set itself up for a
// JSON encoding. The goal of an unmarshaler

View file

@ -327,7 +327,7 @@ func (tc *Tester) AssertRedirect(requestURI string, expectedToLocation string, e
}
// CompareAdapt adapts a config and then compares it against an expected result
func CompareAdapt(t *testing.T, rawConfig string, adapterName string, expectedResponse string) bool {
func CompareAdapt(t *testing.T, filename, rawConfig string, adapterName string, expectedResponse string) bool {
cfgAdapter := caddyconfig.GetAdapter(adapterName)
if cfgAdapter == nil {
@ -353,7 +353,7 @@ func CompareAdapt(t *testing.T, rawConfig string, adapterName string, expectedRe
if len(warnings) > 0 {
for _, w := range warnings {
t.Logf("warning: directive: %s : %s", w.Directive, w.Message)
t.Logf("warning: %s:%d: %s: %s", filename, w.Line, w.Directive, w.Message)
}
}
@ -388,7 +388,7 @@ func CompareAdapt(t *testing.T, rawConfig string, adapterName string, expectedRe
// AssertAdapt adapts a config and then tests it against an expected result
func AssertAdapt(t *testing.T, rawConfig string, adapterName string, expectedResponse string) {
ok := CompareAdapt(t, rawConfig, adapterName, expectedResponse)
ok := CompareAdapt(t, "Caddyfile", rawConfig, adapterName, expectedResponse)
if !ok {
t.Fail()
}

View file

@ -64,12 +64,7 @@ http://bar.com {
],
"terminal": true
}
],
"automatic_https": {
"skip": [
"bar.com"
]
}
},
"srv2": {
"listen": [

View file

@ -46,14 +46,9 @@ http://a.caddy.localhost {
],
"terminal": true
}
],
"automatic_https": {
"skip": [
"a.caddy.localhost"
]
}
}
}
}
}
}

View file

@ -1,4 +1,5 @@
localhost
request_body {
max_size 1MB
}

View file

@ -1,5 +1,4 @@
# issue #3953
{
cert_issuer zerossl api_key
}
@ -58,14 +57,9 @@ http://example.net {
],
"terminal": true
}
],
"automatic_https": {
"skip": [
"example.net"
]
}
}
}
},
"tls": {
"automation": {

View file

@ -75,12 +75,7 @@ http://b.b https://b.b:8443 {
],
"terminal": true
}
],
"automatic_https": {
"skip": [
"b.b"
]
}
},
"srv2": {
"listen": [

View file

@ -32,14 +32,15 @@ func TestCaddyfileAdaptToJSON(t *testing.T) {
}
// split the Caddyfile (first) and JSON (second) parts
// (append newline to Caddyfile to match formatter expectations)
parts := strings.Split(string(data), "----------")
caddyfile, json := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
caddyfile, json := strings.TrimSpace(parts[0])+"\n", strings.TrimSpace(parts[1])
// replace windows newlines in the json with unix newlines
json = winNewlines.ReplaceAllString(json, "\n")
// run the test
ok := caddytest.CompareAdapt(t, caddyfile, "caddyfile", json)
ok := caddytest.CompareAdapt(t, filename, caddyfile, "caddyfile", json)
if !ok {
t.Errorf("failed to adapt %s", filename)
}

View file

@ -529,6 +529,9 @@ func cmdAdaptConfig(fl Flags) (int, error) {
adaptedConfig = prettyBuf.Bytes()
}
// print result to stdout
fmt.Println(string(adaptedConfig))
// print warnings to stderr
for _, warn := range warnings {
msg := warn.Message
@ -538,9 +541,6 @@ func cmdAdaptConfig(fl Flags) (int, error) {
fmt.Fprintf(os.Stderr, "[WARNING][%s] %s:%d: %s\n", adaptCmdAdapterFlag, warn.File, warn.Line, msg)
}
// print result to stdout
fmt.Println(string(adaptedConfig))
// validate output if requested
if adaptCmdValidateFlag {
var cfg *caddy.Config