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 // 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 // successfully but result in logical errors (the Caddyfile is a bad format, I'm sorry)
// TODO: also perform this check on imported files if warning, different := formattingDifference(filename, body); different {
if !bytes.Equal(Format(body), body) { warnings = append(warnings, warning)
warnings = append(warnings, caddyconfig.Warning{
File: filename,
Message: "file is not formatted with 'caddy fmt'",
})
} }
result, err := json.Marshal(cfg) 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 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 // Unmarshaler is a type that can unmarshal
// Caddyfile tokens to set itself up for a // Caddyfile tokens to set itself up for a
// JSON encoding. The goal of an unmarshaler // 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 // 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) cfgAdapter := caddyconfig.GetAdapter(adapterName)
if cfgAdapter == nil { if cfgAdapter == nil {
@ -353,7 +353,7 @@ func CompareAdapt(t *testing.T, rawConfig string, adapterName string, expectedRe
if len(warnings) > 0 { if len(warnings) > 0 {
for _, w := range warnings { 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 // AssertAdapt adapts a config and then tests it against an expected result
func AssertAdapt(t *testing.T, rawConfig string, adapterName string, expectedResponse string) { 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 { if !ok {
t.Fail() t.Fail()
} }

View File

@ -9,7 +9,7 @@
} }
acme_ca https://example.com acme_ca https://example.com
acme_eab { acme_eab {
key_id 4K2scIVbBpNd-78scadB2g key_id 4K2scIVbBpNd-78scadB2g
mac_key abcdefghijklmnopqrstuvwx-abcdefghijklnopqrstuvwxyz12ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgh mac_key abcdefghijklmnopqrstuvwx-abcdefghijklnopqrstuvwxyz12ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgh
} }
acme_ca_root /path/to/ca.crt acme_ca_root /path/to/ca.crt

View File

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

View File

@ -18,7 +18,7 @@
} }
} }
foo.com { foo.com {
} }
---------- ----------

View File

@ -4,7 +4,7 @@
header ?John "von Neumann" header ?John "von Neumann"
header -Wolfram header -Wolfram
header { header {
Grace: "Hopper" # some users habitually suffix field names with a colon Grace: "Hopper" # some users habitually suffix field names with a colon
+Ray "Solomonoff" +Ray "Solomonoff"
?Tim "Berners-Lee" ?Tim "Berners-Lee"
defer defer

View File

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

View File

@ -1,10 +1,10 @@
:80 { :80 {
route { route {
# unused matchers should not panic # unused matchers should not panic
# see https://github.com/caddyserver/caddy/issues/3745 # see https://github.com/caddyserver/caddy/issues/3745
@matcher1 path /path1 @matcher1 path /path1
@matcher2 path /path2 @matcher2 path /path2
} }
} }
---------- ----------
{ {

View File

@ -1,15 +1,15 @@
:8884 :8884
php_fastcgi localhost:9000 { php_fastcgi localhost:9000 {
# some php_fastcgi-specific subdirectives # some php_fastcgi-specific subdirectives
split .php .php5 split .php .php5
env VAR1 value1 env VAR1 value1
env VAR2 value2 env VAR2 value2
root /var/www root /var/www
index index.php5 index index.php5
# passed through to reverse_proxy (directive order doesn't matter!) # passed through to reverse_proxy (directive order doesn't matter!)
lb_policy random lb_policy random
} }
---------- ----------
{ {

View File

@ -1,6 +1,7 @@
localhost localhost
request_body { request_body {
max_size 1MB max_size 1MB
} }
---------- ----------
{ {

View File

@ -1,7 +1,7 @@
:8884 :8884
reverse_proxy 127.0.0.1:65535 { reverse_proxy 127.0.0.1:65535 {
transport fastcgi transport fastcgi
} }
---------- ----------
{ {

View File

@ -1,38 +1,38 @@
:8884 :8884
reverse_proxy h2c://localhost:8080 reverse_proxy h2c://localhost:8080
---------- ----------
{ {
"apps": { "apps": {
"http": { "http": {
"servers": { "servers": {
"srv0": { "srv0": {
"listen": [ "listen": [
":8884" ":8884"
], ],
"routes": [ "routes": [
{ {
"handle": [ "handle": [
{ {
"handler": "reverse_proxy", "handler": "reverse_proxy",
"transport": { "transport": {
"protocol": "http", "protocol": "http",
"versions": [ "versions": [
"h2c", "h2c",
"2" "2"
] ]
}, },
"upstreams": [ "upstreams": [
{ {
"dial": "localhost:8080" "dial": "localhost:8080"
} }
] ]
} }
] ]
} }
] ]
} }
} }
} }
} }
} }

View File

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

View File

@ -3,7 +3,7 @@ localhost
respond "hello from localhost" respond "hello from localhost"
tls { tls {
client_auth { client_auth {
mode request mode request
trusted_ca_cert_file ../caddy.ca.cer trusted_ca_cert_file ../caddy.ca.cer
} }
} }

View File

@ -3,8 +3,8 @@ localhost
respond "hello from localhost" respond "hello from localhost"
tls { tls {
client_auth { client_auth {
mode request mode request
trusted_ca_cert MIIDSzCCAjOgAwIBAgIUfIRObjWNUA4jxQ/0x8BOCvE2Vw4wDQYJKoZIhvcNAQELBQAwFjEUMBIGA1UEAwwLRWFzeS1SU0EgQ0EwHhcNMTkwODI4MTYyNTU5WhcNMjkwODI1MTYyNTU5WjAWMRQwEgYDVQQDDAtFYXN5LVJTQSBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK5m5elxhQfMp/3aVJ4JnpN9PUSz6LlP6LePAPFU7gqohVVFVtDkChJAG3FNkNQNlieVTja/bgH9IcC6oKbROwdY1h0MvNV8AHHigvl03WuJD8g2ReVFXXwsnrPmKXCFzQyMI6TYk3m2gYrXsZOU1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H9IcC6oKbROwdY1h0MvNV8AHHigvl03WuJD8g2ReVFXXwsnrPmKXCFzQyMI6TYk3m2gYrXsZOU1GLnfMRC3KAMRgE2F45twOs9hqG169YJ6mM2eQjzjCHWI6S2/iUYvYxRkCOlYUbLsMD/AhgAf1plzg6LPqNxtdlwxZnA0ytgkmhK67HtzJu0+ovUCsMv0RwcMhsEo9T8nyFAGt9XLZ63X5WpBCTUApaAUhnG0XnerjmUWb6eUWw4zev54sEfY5F3x002iQaW6cECAwEAAaOBkDCBjTAdBgNVHQ4EFgQU4CBUbZsS2GaNIkGRz/cBsD5ivjswUQYDVR0jBEowSIAU4CBUbZsS2GaNIkGRz/cBsD5ivjuhGqQYMBYxFDASBgNVBAMMC0Vhc3ktUlNBIENBghR8hE5uNY1QDiPFD/THwE4K8TZXDjAMBgNVHRMEBTADAQH/MAsGA1UdDwQEAwIBBjANBgkqhkiG9w0BAQsFAAOCAQEAKB3V4HIzoiO/Ch6WMj9bLJ2FGbpkMrcb/Eq01hT5zcfKD66lVS1MlK+cRL446Z2b2KDP1oFyVs+qmrmtdwrWgD+nfe2sBmmIHo9m9KygMkEOfG3MghGTEcS+0cTKEcoHYWYyOqQh6jnedXY8Cdm4GM1hAc9MiL3/sqV8YCVSLNnkoNysmr06/rZ0MCUZPGUtRmfd0heWhrfzAKw2HLgX+RAmpOE2MZqWcjvqKGyaRiaZks4nJkP6521aC2Lgp0HhCz1j8/uQ5ldoDszCnu/iro0NAsNtudTMD+YoLQxLqdleIh6CW+illc2VdXwj7mn6J04yns9jfE2jRjW/yTLFuQ==
} }
} }
---------- ----------

View File

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

View File

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

View File

@ -529,6 +529,9 @@ func cmdAdaptConfig(fl Flags) (int, error) {
adaptedConfig = prettyBuf.Bytes() adaptedConfig = prettyBuf.Bytes()
} }
// print result to stdout
fmt.Println(string(adaptedConfig))
// print warnings to stderr // print warnings to stderr
for _, warn := range warnings { for _, warn := range warnings {
msg := warn.Message 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) 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 // validate output if requested
if adaptCmdValidateFlag { if adaptCmdValidateFlag {
var cfg *caddy.Config var cfg *caddy.Config