summaryrefslogtreecommitdiff
path: root/caddytest/caddytest.go
diff options
context:
space:
mode:
authorMark Sargent <99003+sarge@users.noreply.github.com>2020-04-04 16:02:46 +1300
committerGitHub <noreply@github.com>2020-04-03 21:02:46 -0600
commit78717ce5b0a940df0ef91a19e8389b11505a344d (patch)
tree77a21a25534a93dac8011d68f723f750f745427c /caddytest/caddytest.go
parent3d6fc1e1b7f41ebbd0c7cb48280ce1a14a56f4d5 (diff)
chore: add adapt tests. fix load failure not failing tests (#3222)
* add adaption tests. fix load failure not failing tests * removed unnecessary assignment
Diffstat (limited to 'caddytest/caddytest.go')
-rw-r--r--caddytest/caddytest.go57
1 files changed, 56 insertions, 1 deletions
diff --git a/caddytest/caddytest.go b/caddytest/caddytest.go
index 0f2799f..ce70f77 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()
+ }
+}