test(plugin): improve tests for auto (#7348)

This commit is contained in:
Ville Vesilehto
2025-06-05 00:37:52 +03:00
committed by GitHub
parent 11774d9e98
commit ddb74cdcf4
7 changed files with 644 additions and 24 deletions

View File

@@ -1,6 +1,7 @@
package auto
import (
"fmt"
"testing"
"time"
@@ -8,6 +9,7 @@ import (
)
func TestAutoParse(t *testing.T) {
t.Parallel()
tests := []struct {
inputFileRules string
shouldErr bool
@@ -93,6 +95,13 @@ func TestAutoParse(t *testing.T) {
}`,
true, "/tmp", "${1}", ``, 60 * time.Second,
},
// non-existent directory.
{
`auto example.org {
directory /foobar/coredns * {1}
}`,
true, "/tmp", "${1}", ``, 60 * time.Second,
},
// unexpected argument.
{
`auto example.org {
@@ -100,34 +109,54 @@ func TestAutoParse(t *testing.T) {
}`,
true, "/tmp", "${1}", ``, 60 * time.Second,
},
// upstream directive should not error and should consume args
{
`auto example.org {
directory /tmp
upstream 8.8.8.8 1.1.1.1
}`,
false, "/tmp", "${1}", `db\.(.*)`, 60 * time.Second,
},
// upstream directive with no args should not error
{
`auto example.org {
directory /tmp
upstream
}`,
false, "/tmp", "${1}", `db\.(.*)`, 60 * time.Second,
},
}
for i, test := range tests {
c := caddy.NewTestController("dns", test.inputFileRules)
a, err := autoParse(c)
t.Run(fmt.Sprintf("test_%d", i), func(t *testing.T) {
t.Parallel()
c := caddy.NewTestController("dns", test.inputFileRules)
a, err := autoParse(c)
if err == nil && test.shouldErr {
t.Fatalf("Test %d expected errors, but got no error", i)
} else if err != nil && !test.shouldErr {
t.Fatalf("Test %d expected no errors, but got '%v'", i, err)
} else if !test.shouldErr {
if a.directory != test.expectedDirectory {
t.Fatalf("Test %d expected %v, got %v", i, test.expectedDirectory, a.directory)
if err == nil && test.shouldErr {
t.Fatalf("Test %d expected errors, but got no error", i)
} else if err != nil && !test.shouldErr {
t.Fatalf("Test %d expected no errors, but got '%v'", i, err)
} else if !test.shouldErr {
if a.directory != test.expectedDirectory {
t.Fatalf("Test %d expected %v, got %v", i, test.expectedDirectory, a.directory)
}
if a.template != test.expectedTempl {
t.Fatalf("Test %d expected %v, got %v", i, test.expectedTempl, a.template)
}
if a.re.String() != test.expectedRe {
t.Fatalf("Test %d expected %v, got %v", i, test.expectedRe, a.re)
}
if a.ReloadInterval != test.expectedReloadInterval {
t.Fatalf("Test %d expected %v, got %v", i, test.expectedReloadInterval, a.ReloadInterval)
}
}
if a.template != test.expectedTempl {
t.Fatalf("Test %d expected %v, got %v", i, test.expectedTempl, a.template)
}
if a.re.String() != test.expectedRe {
t.Fatalf("Test %d expected %v, got %v", i, test.expectedRe, a.re)
}
if a.ReloadInterval != test.expectedReloadInterval {
t.Fatalf("Test %d expected %v, got %v", i, test.expectedReloadInterval, a.ReloadInterval)
}
}
})
}
}
func TestSetupReload(t *testing.T) {
t.Parallel()
tests := []struct {
name string
config string
@@ -168,6 +197,7 @@ func TestSetupReload(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ctr := caddy.NewTestController("dns", tt.config)
if err := setup(ctr); (err != nil) != tt.wantErr {
t.Errorf("Error: setup() error = %v, wantErr %v", err, tt.wantErr)