git » gofer » commit e730253

config: Add tests to cover error cases

author Alberto Bertogli
2022-10-05 23:52:06 UTC
committer Alberto Bertogli
2022-10-09 11:34:34 UTC
parent a94f402684393b09a71dd69f06088e8cd171e6f1

config: Add tests to cover error cases

This patch adds various tests to the config package, to cover error and
corner cases.

config/config_test.go +163 -0

diff --git a/config/config_test.go b/config/config_test.go
index 496790b..9203b11 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -3,9 +3,12 @@ package config
 import (
 	"log"
 	"net/url"
+	"regexp"
+	"strings"
 	"testing"
 
 	"github.com/google/go-cmp/cmp"
+	"gopkg.in/yaml.v3"
 )
 
 func mustURL(r string) *URL {
@@ -93,4 +96,164 @@ https:
 	if diff := cmp.Diff(expected, *conf); diff != "" {
 		t.Errorf("configuration is not as expected (-want +got):\n%s", diff)
 	}
+
+	conf2, err := LoadString(conf.String())
+	if diff := cmp.Diff(expected, *conf2); diff != "" {
+		t.Errorf("configuration from string not as expected (-want +got):\n%s", diff)
+	}
+
+	// Check that we return an error when we can't access the file.
+	conf, err = Load("/doesnotexist")
+	if !(conf == nil && strings.Contains(err.Error(), "error reading")) {
+		t.Errorf("expected error reading non-existent file, got: %v / %v",
+			conf, err)
+	}
+}
+
+func TestCheck(t *testing.T) {
+	// routes must be set.
+	contents := `
+http:
+  ":http":
+`
+	expectErrs(t, `":http": missing routes`,
+		loadAndCheck(t, contents))
+
+	// no actions
+	contents = `
+http:
+  ":http":
+    routes:
+      "/":
+`
+	expectErrs(t, `":http": "/": action missing`,
+		loadAndCheck(t, contents))
+
+	// too many actions
+	contents = `
+http:
+  ":http":
+    routes:
+      "/":
+        file: "/dev/null"
+        dir: "/tmp"
+`
+	expectErrs(t, `":http": "/": too many actions set`,
+		loadAndCheck(t, contents))
+
+	// certs or autocerts must be set.
+	contents = `
+https:
+  ":https":
+    routes:
+      "/":
+        file: "/dev/null"
+`
+	expectErrs(t, `":https": certs or autocerts must be set`,
+		loadAndCheck(t, contents))
+
+	// diropts on a non-directory.
+	contents = `
+https:
+  ":https":
+    routes:
+      "/":
+        file: "/dev/null"
+        diropts:
+          exclude: ["everything"]
+`
+	expectErrs(t, `":https": certs or autocerts must be set`,
+		loadAndCheck(t, contents))
+
+	// reqlog reference (http).
+	contents = `
+https:
+  ":https":
+    certs: "/dev/null"
+    routes:
+      "/":
+        file: "/dev/null"
+    reqlog:
+      "/": "lalala"
+`
+	expectErrs(t, `":https": "/": unknown reqlog "lalala"`,
+		loadAndCheck(t, contents))
+
+	// reqlog reference (raw).
+	contents = `
+raw:
+  ":1234":
+    reqlog: "lalala"
+`
+	expectErrs(t, `":1234": unknown reqlog "lalala"`,
+		loadAndCheck(t, contents))
+}
+
+func loadAndCheck(t *testing.T, contents string) []error {
+	t.Helper()
+
+	conf, err := LoadString(contents)
+	if err != nil {
+		t.Errorf("error loading config: %v", err)
+	}
+
+	return conf.Check()
+}
+
+func expectErrs(t *testing.T, want string, got []error) {
+	t.Helper()
+
+	found := false
+	for _, e := range got {
+		if strings.Contains(e.Error(), want) {
+			found = true
+			break
+		}
+	}
+
+	if !found {
+		t.Errorf("expected %q, but got %v", want, got)
+	}
+}
+
+func TestRegexp(t *testing.T) {
+	re := Regexp{}
+	err := yaml.Unmarshal([]byte(`"ab.d"`), &re)
+	if err != nil {
+		t.Errorf("unexpected error: %v", err)
+	}
+	expected := Regexp{
+		orig:   "ab.d",
+		Regexp: regexp.MustCompile("^(?:ab.d)$"),
+	}
+	opts := cmp.Comparer(func(x, y Regexp) bool {
+		return x.orig == y.orig && x.String() == y.String()
+	})
+	if diff := cmp.Diff(expected, re, opts); diff != "" {
+		t.Errorf("unexpected regexp result (-want +got):\n%s", diff)
+	}
+
+	// Error: invalid regexp.
+	err = yaml.Unmarshal([]byte(`"*"`), &re)
+	if !strings.Contains(err.Error(), "error parsing regexp:") {
+		t.Errorf("expected error parsing regexp, got %v", err)
+	}
+}
+
+func TestURL(t *testing.T) {
+	u := URL{}
+	err := yaml.Unmarshal([]byte(`"http://a/b/c"`), &u)
+	if err != nil {
+		t.Errorf("unexpected error: %v", err)
+	}
+	expected, _ := url.Parse("http://a/b/c")
+	if diff := cmp.Diff(URL(*expected), u); diff != "" {
+		t.Errorf("unexpected regexp result (-want +got):\n%s", diff)
+	}
+
+	// Error: invalid URL.
+	err = yaml.Unmarshal([]byte(`":a"`), &u)
+	if !strings.Contains(err.Error(), "missing protocol scheme") {
+		t.Errorf("expected error parsing url, got %v", err)
+	}
 }