Compare commits

...

7 Commits

Author SHA1 Message Date
Mohammed Al Sahaf
1a67048217
Merge abdeadfdf4be22fd641bc5f0828befd59c42a64a into 8861eae22350d9e8f94653db951faf85a50a82da 2025-03-03 11:23:49 -05:00
baruchyahalom
8861eae223
caddytest: Support configuration defaults override (#6850) 2025-03-03 14:35:54 +00:00
Mohammed Al Sahaf
abdeadfdf4
Merge branch 'master' into interface-network-type 2024-10-12 14:58:38 +03:00
Mohammed Al Sahaf
9ebd7fa221
Merge branch 'master' into interface-network-type 2024-08-22 20:34:25 +03:00
Mohammed Al Sahaf
32d6798822
Merge branch 'master' into interface-network-type 2024-08-13 09:36:07 +03:00
Mohammed Al Sahaf
9c4654a638
Merge branch 'master' into interface-network-type 2024-08-12 10:04:06 +03:00
Mohammed Al Sahaf
490fd1d63d
core: add iface as network type
Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com>
2024-08-10 22:46:46 +03:00
3 changed files with 97 additions and 21 deletions

View File

@ -31,8 +31,8 @@ import (
_ "github.com/caddyserver/caddy/v2/modules/standard"
)
// Defaults store any configuration required to make the tests run
type Defaults struct {
// Config store any configuration required to make the tests run
type Config struct {
// Port we expect caddy to listening on
AdminPort int
// Certificates we expect to be loaded before attempting to run the tests
@ -44,7 +44,7 @@ type Defaults struct {
}
// Default testing values
var Default = Defaults{
var Default = Config{
AdminPort: 2999, // different from what a real server also running on a developer's machine might be
Certificates: []string{"/caddy.localhost.crt", "/caddy.localhost.key"},
TestRequestTimeout: 5 * time.Second,
@ -61,6 +61,7 @@ type Tester struct {
Client *http.Client
configLoaded bool
t testing.TB
config Config
}
// NewTester will create a new testing client with an attached cookie jar
@ -78,9 +79,29 @@ func NewTester(t testing.TB) *Tester {
},
configLoaded: false,
t: t,
config: Default,
}
}
// WithDefaultOverrides this will override the default test configuration with the provided values.
func (tc *Tester) WithDefaultOverrides(overrides Config) *Tester {
if overrides.AdminPort != 0 {
tc.config.AdminPort = overrides.AdminPort
}
if len(overrides.Certificates) > 0 {
tc.config.Certificates = overrides.Certificates
}
if overrides.TestRequestTimeout != 0 {
tc.config.TestRequestTimeout = overrides.TestRequestTimeout
tc.Client.Timeout = overrides.TestRequestTimeout
}
if overrides.LoadRequestTimeout != 0 {
tc.config.LoadRequestTimeout = overrides.LoadRequestTimeout
}
return tc
}
type configLoadError struct {
Response string
}
@ -113,7 +134,7 @@ func (tc *Tester) initServer(rawConfig string, configType string) error {
return nil
}
err := validateTestPrerequisites(tc.t)
err := validateTestPrerequisites(tc)
if err != nil {
tc.t.Skipf("skipping tests as failed integration prerequisites. %s", err)
return nil
@ -121,7 +142,7 @@ func (tc *Tester) initServer(rawConfig string, configType string) error {
tc.t.Cleanup(func() {
if tc.t.Failed() && tc.configLoaded {
res, err := http.Get(fmt.Sprintf("http://localhost:%d/config/", Default.AdminPort))
res, err := http.Get(fmt.Sprintf("http://localhost:%d/config/", tc.config.AdminPort))
if err != nil {
tc.t.Log("unable to read the current config")
return
@ -151,10 +172,10 @@ func (tc *Tester) initServer(rawConfig string, configType string) error {
tc.t.Logf("After: %s", rawConfig)
}
client := &http.Client{
Timeout: Default.LoadRequestTimeout,
Timeout: tc.config.LoadRequestTimeout,
}
start := time.Now()
req, err := http.NewRequest("POST", fmt.Sprintf("http://localhost:%d/load", Default.AdminPort), strings.NewReader(rawConfig))
req, err := http.NewRequest("POST", fmt.Sprintf("http://localhost:%d/load", tc.config.AdminPort), strings.NewReader(rawConfig))
if err != nil {
tc.t.Errorf("failed to create request. %s", err)
return err
@ -205,11 +226,11 @@ func (tc *Tester) ensureConfigRunning(rawConfig string, configType string) error
}
client := &http.Client{
Timeout: Default.LoadRequestTimeout,
Timeout: tc.config.LoadRequestTimeout,
}
fetchConfig := func(client *http.Client) any {
resp, err := client.Get(fmt.Sprintf("http://localhost:%d/config/", Default.AdminPort))
resp, err := client.Get(fmt.Sprintf("http://localhost:%d/config/", tc.config.AdminPort))
if err != nil {
return nil
}
@ -237,30 +258,30 @@ func (tc *Tester) ensureConfigRunning(rawConfig string, configType string) error
}
const initConfig = `{
admin localhost:2999
admin localhost:%d
}
`
// validateTestPrerequisites ensures the certificates are available in the
// designated path and Caddy sub-process is running.
func validateTestPrerequisites(t testing.TB) error {
func validateTestPrerequisites(tc *Tester) error {
// check certificates are found
for _, certName := range Default.Certificates {
for _, certName := range tc.config.Certificates {
if _, err := os.Stat(getIntegrationDir() + certName); errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("caddy integration test certificates (%s) not found", certName)
}
}
if isCaddyAdminRunning() != nil {
if isCaddyAdminRunning(tc) != nil {
// setup the init config file, and set the cleanup afterwards
f, err := os.CreateTemp("", "")
if err != nil {
return err
}
t.Cleanup(func() {
tc.t.Cleanup(func() {
os.Remove(f.Name())
})
if _, err := f.WriteString(initConfig); err != nil {
if _, err := f.WriteString(fmt.Sprintf(initConfig, tc.config.AdminPort)); err != nil {
return err
}
@ -271,23 +292,23 @@ func validateTestPrerequisites(t testing.TB) error {
}()
// wait for caddy to start serving the initial config
for retries := 10; retries > 0 && isCaddyAdminRunning() != nil; retries-- {
for retries := 10; retries > 0 && isCaddyAdminRunning(tc) != nil; retries-- {
time.Sleep(1 * time.Second)
}
}
// one more time to return the error
return isCaddyAdminRunning()
return isCaddyAdminRunning(tc)
}
func isCaddyAdminRunning() error {
func isCaddyAdminRunning(tc *Tester) error {
// assert that caddy is running
client := &http.Client{
Timeout: Default.LoadRequestTimeout,
Timeout: tc.config.LoadRequestTimeout,
}
resp, err := client.Get(fmt.Sprintf("http://localhost:%d/config/", Default.AdminPort))
resp, err := client.Get(fmt.Sprintf("http://localhost:%d/config/", tc.config.AdminPort))
if err != nil {
return fmt.Errorf("caddy integration test caddy server not running. Expected to be listening on localhost:%d", Default.AdminPort)
return fmt.Errorf("caddy integration test caddy server not running. Expected to be listening on localhost:%d", tc.config.AdminPort)
}
resp.Body.Close()

51
interface.go Normal file
View File

@ -0,0 +1,51 @@
package caddy
import (
"context"
"fmt"
"net"
"strings"
)
func init() {
RegisterNetwork("iface", getInterfaceListener)
RegisterNetwork("iface+tcp", getInterfaceListener)
RegisterNetwork("iface+udp", getInterfaceListener)
}
func getInterfaceListener(ctx context.Context, network, addr string, config net.ListenConfig) (any, error) {
// assuming addr = "interface+family:port"
// if family is missing, then assume tcp
family := "tcp"
parts := strings.Split(network, "+")
if len(parts) == 2 {
family = parts[1]
}
host, port, _ := net.SplitHostPort(addr)
iface, err := net.InterfaceByName(host)
if err != nil {
return nil, fmt.Errorf("interface %s not found", addr)
}
addrs, err := iface.Addrs()
if err != nil {
return nil, fmt.Errorf("error on obtaining interface %s addresses: %s", iface.Name, err)
}
if len(addrs) == 0 {
return nil, fmt.Errorf("interface %s has no addresses", iface.Name)
}
for _, addr := range addrs {
if face, ok := addr.(*net.IPNet); ok {
if ip4 := face.IP.To4(); ip4 != nil {
switch family {
case "tcp":
return net.Listen(family, net.JoinHostPort(ip4.String(), port))
case "udp":
return net.ListenPacket(family, net.JoinHostPort(ip4.String(), port))
default:
return net.Listen(family, net.JoinHostPort(ip4.String(), port))
}
}
}
}
return nil, fmt.Errorf("interface %s has no IPv4 addresses", iface.Name)
}

View File

@ -42,6 +42,10 @@ import (
"github.com/caddyserver/caddy/v2/modules/caddytls"
)
func init() {
RegisterNetworkHTTP3("iface", "iface+udp")
}
// Server describes an HTTP server.
type Server struct {
// Socket addresses to which to bind listeners. Accepts