-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from FMotalleb/tests
Global Config Tests
- Loading branch information
Showing
5 changed files
with
279 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
package config_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/FMotalleb/dns-reverse-proxy-docker/lib/config" | ||
"github.com/FMotalleb/dns-reverse-proxy-docker/lib/config/globals" | ||
"github.com/FMotalleb/dns-reverse-proxy-docker/lib/provider" | ||
"github.com/FMotalleb/dns-reverse-proxy-docker/lib/rule" | ||
) | ||
|
||
func makeArray[T any](args ...T) []T { | ||
arr := make([]T, 0) | ||
for _, item := range args { | ||
arr = append(arr, item) | ||
} | ||
return arr | ||
} | ||
func TestValidateFailingOnEmptyConfig(t *testing.T) { | ||
|
||
DNSConfig := config.Config{} | ||
defer func() { | ||
err := recover() | ||
if err == nil { | ||
t.Error("config is missing mandatory parts it must fail (missing panic)") | ||
} | ||
}() | ||
_ = DNSConfig.Validate() | ||
t.Error("config is missing mandatory parts it must fail") | ||
} | ||
func TestValidateFailingOnEmptyDefaultProviderConfig(t *testing.T) { | ||
|
||
DNSConfig := config.Config{ | ||
Global: globals.CoreConfiguration{ | ||
Address: ":53", | ||
AllowTransfer: makeArray("0.0.0.0"), | ||
// DefaultProviders: makeArray("cf"), | ||
}, | ||
} | ||
defer func() { | ||
err := recover() | ||
if err == nil { | ||
t.Error("config is missing mandatory parts it must fail (missing panic)") | ||
} | ||
}() | ||
_ = DNSConfig.Validate() | ||
t.Error("config is missing mandatory parts it must fail") | ||
} | ||
|
||
func TestValidateFailOnMissingProviderInProviderList(t *testing.T) { | ||
|
||
DNSConfig := config.Config{ | ||
Global: globals.CoreConfiguration{ | ||
Address: ":53", | ||
AllowTransfer: makeArray("0.0.0.0"), | ||
DefaultProviders: makeArray("cf"), | ||
}, | ||
} | ||
defer func() { | ||
err := recover() | ||
if err == nil { | ||
t.Error("config is missing mandatory parts it must fail (missing panic)") | ||
} | ||
}() | ||
_ = DNSConfig.Validate() | ||
t.Error("config is missing mandatory parts it must fail") | ||
} | ||
|
||
func TestValidatePassOnCompleteConfig(t *testing.T) { | ||
DNSConfig := config.Config{ | ||
Global: globals.CoreConfiguration{ | ||
Address: ":53", | ||
AllowTransfer: makeArray("0.0.0.0"), | ||
DefaultProviders: makeArray("cf"), | ||
}, | ||
Providers: makeArray(provider.Provider{ | ||
Name: "cf", | ||
IP: makeArray("1.1.1.1:53"), | ||
}), | ||
Rules: makeArray(rule.Rule{ | ||
Matcher: "regex", | ||
MatcherParams: makeArray(".*"), | ||
Resolvers: makeArray("cf"), | ||
}), | ||
} | ||
isValid := DNSConfig.Validate() | ||
if !isValid { | ||
t.Error("config is carrying mandatory parts it must pass") | ||
} | ||
} | ||
func TestValidateFailOnMissingProviderConfig(t *testing.T) { | ||
|
||
DNSConfig := config.Config{ | ||
Global: globals.CoreConfiguration{ | ||
Address: ":53", | ||
AllowTransfer: makeArray("0.0.0.0"), | ||
DefaultProviders: makeArray("cf"), | ||
}, | ||
Providers: makeArray(provider.Provider{ | ||
Name: "cf", | ||
IP: makeArray("1.1.1.1"), | ||
}), | ||
Rules: makeArray(rule.Rule{ | ||
Matcher: "regex", | ||
MatcherParams: makeArray(".*"), | ||
Resolvers: makeArray("cf"), | ||
}), | ||
} | ||
defer func() { | ||
err := recover() | ||
if err == nil { | ||
t.Error("config is missing mandatory parts it must fail (missing panic)") | ||
} | ||
}() | ||
_ = DNSConfig.Validate() | ||
t.Error("config is missing mandatory parts it must fail") | ||
} | ||
|
||
func TestValidateFailOnMissingRuleConfig(t *testing.T) { | ||
|
||
DNSConfig := config.Config{ | ||
Global: globals.CoreConfiguration{ | ||
Address: ":53", | ||
AllowTransfer: makeArray("0.0.0.0"), | ||
DefaultProviders: makeArray("cf"), | ||
}, | ||
Providers: makeArray(provider.Provider{ | ||
Name: "cf", | ||
IP: makeArray("1.1.1.1:53"), | ||
}), | ||
Rules: makeArray(rule.Rule{ | ||
Matcher: "regex", | ||
MatcherParams: makeArray("**"), | ||
Resolvers: makeArray("cf"), | ||
}), | ||
} | ||
defer func() { | ||
err := recover() | ||
if err == nil { | ||
t.Error("config is missing mandatory parts it must fail (missing panic)") | ||
} | ||
}() | ||
_ = DNSConfig.Validate() | ||
t.Error("config is missing mandatory parts it must fail") | ||
} | ||
func TestGetRulePass(t *testing.T) { | ||
DNSConfig := config.Config{ | ||
Global: globals.CoreConfiguration{ | ||
Address: ":53", | ||
AllowTransfer: makeArray("0.0.0.0"), | ||
DefaultProviders: makeArray("cf"), | ||
}, | ||
Providers: makeArray(provider.Provider{ | ||
Name: "cf", | ||
IP: makeArray("1.1.1.1:53"), | ||
}), | ||
Rules: makeArray(rule.Rule{ | ||
Matcher: "regex", | ||
MatcherParams: makeArray(".*"), | ||
Resolvers: makeArray("cf"), | ||
}), | ||
} | ||
rule := DNSConfig.FindRuleFor("test.com") | ||
if rule == nil { | ||
t.Error("config is carrying mandatory parts it must pass") | ||
} | ||
} | ||
func TestGetRuleFail(t *testing.T) { | ||
DNSConfig := config.Config{ | ||
Global: globals.CoreConfiguration{ | ||
Address: ":53", | ||
AllowTransfer: makeArray("0.0.0.0"), | ||
DefaultProviders: makeArray("cf"), | ||
}, | ||
Providers: makeArray(provider.Provider{ | ||
Name: "cf", | ||
IP: makeArray("1.1.1.1:53"), | ||
}), | ||
Rules: makeArray(rule.Rule{ | ||
Matcher: "regex", | ||
MatcherParams: makeArray(".*\\.org"), | ||
Resolvers: makeArray("cf"), | ||
}), | ||
} | ||
rule := DNSConfig.FindRuleFor("test.com") | ||
if rule != nil { | ||
t.Error("config is carrying mandatory parts it must pass") | ||
} | ||
} |
Oops, something went wrong.