mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 08:14:18 -04:00
Add pkg/fall for Fallthrough (#1355)
* Add pkg/fall for Fallthrough Move this into it's own package to facilitate tests. Important bug was fixed: make the names fully qualified. Add fall package to hosts, reverse, etcd, and fix kubernetes and any tests. The k8s tests are still as-is, might need a future cleanup.
This commit is contained in:
77
plugin/pkg/fall/fall.go
Normal file
77
plugin/pkg/fall/fall.go
Normal file
@@ -0,0 +1,77 @@
|
||||
// Package fall handles the fallthrough logic used in plugins that support it.
|
||||
package fall
|
||||
|
||||
import (
|
||||
"github.com/coredns/coredns/plugin"
|
||||
)
|
||||
|
||||
// F can be nil to allow for no fallthrough, empty allow all zones to fallthrough or
|
||||
// contain a zone list that is checked.
|
||||
type F []string
|
||||
|
||||
// New returns a new F.
|
||||
func New() *F { return new(F) }
|
||||
|
||||
// Through will check if we should fallthrough for qname. Note that we've named the
|
||||
// variable in each plugin "Fall", so this then reads Fall.Through().
|
||||
func (f *F) Through(qname string) bool {
|
||||
if f == nil {
|
||||
return false
|
||||
}
|
||||
if len(*f) == 0 {
|
||||
return true
|
||||
}
|
||||
zone := plugin.Zones(*f).Matches(qname)
|
||||
return zone != ""
|
||||
}
|
||||
|
||||
// SetZones will set zones in f.
|
||||
func (f *F) SetZones(zones []string) {
|
||||
for i := range zones {
|
||||
zones[i] = plugin.Host(zones[i]).Normalize()
|
||||
}
|
||||
*f = zones
|
||||
}
|
||||
|
||||
// Example returns an F with example.org. as the zone name.
|
||||
var Example = func() *F {
|
||||
f := F([]string{"example.org."})
|
||||
return &f
|
||||
}()
|
||||
|
||||
// Zero returns a zero valued F.
|
||||
var Zero = func() *F {
|
||||
f := F([]string{})
|
||||
return &f
|
||||
}
|
||||
|
||||
// IsNil returns true is f is nil.
|
||||
func (f *F) IsNil() bool { return f == nil }
|
||||
|
||||
// IsZero returns true is f is zero (and not nil).
|
||||
func (f *F) IsZero() bool {
|
||||
if f == nil {
|
||||
return false
|
||||
}
|
||||
return len(*f) == 0
|
||||
}
|
||||
|
||||
// Equal returns true if f and g are equal. Only useful in tests, The (possible) zones
|
||||
// are *not* checked.
|
||||
func (f *F) Equal(g *F) bool {
|
||||
if f.IsNil() {
|
||||
if g.IsNil() {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
if f.IsZero() {
|
||||
if g.IsZero() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if len(*f) != len(*g) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
48
plugin/pkg/fall/fall_test.go
Normal file
48
plugin/pkg/fall/fall_test.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package fall
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestIsNil(t *testing.T) {
|
||||
var f *F
|
||||
if !f.IsNil() {
|
||||
t.Errorf("F should be nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsZero(t *testing.T) {
|
||||
f := New()
|
||||
if !f.IsZero() {
|
||||
t.Errorf("F should be zero")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFallThroughExample(t *testing.T) {
|
||||
if !Example.Through("example.org.") {
|
||||
t.Errorf("example.org. should fall through")
|
||||
}
|
||||
if Example.Through("example.net.") {
|
||||
t.Errorf("example.net. should not fall through")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFallthrough(t *testing.T) {
|
||||
var fall *F
|
||||
if fall.Through("foo.com.") {
|
||||
t.Errorf("Expected false, got true for nil fallthrough")
|
||||
}
|
||||
|
||||
fall = New()
|
||||
if !fall.Through("foo.net.") {
|
||||
t.Errorf("Expected true, got false for all zone fallthrough")
|
||||
}
|
||||
|
||||
fall.SetZones([]string{"foo.com", "bar.com"})
|
||||
|
||||
if fall.Through("foo.net.") {
|
||||
t.Errorf("Expected false, got true for non-matching fallthrough zone")
|
||||
}
|
||||
|
||||
if !fall.Through("bar.com.") {
|
||||
t.Errorf("Expected true, got false for matching fallthrough zone")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user