mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
Add testutil daemon.WithResolvConf
Signed-off-by: Rob Murray <rob.murray@docker.com>
This commit is contained in:
@@ -2,7 +2,6 @@ package network
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
@@ -11,22 +10,10 @@ import (
|
||||
|
||||
const DNSRespAddr = "10.11.12.13"
|
||||
|
||||
// WriteTempResolvConf writes a resolv.conf that only contains a single
|
||||
// nameserver line, with address addr.
|
||||
// It returns the name of the temp file. The temp file will be deleted
|
||||
// automatically by a t.Cleanup().
|
||||
func WriteTempResolvConf(t *testing.T, addr string) string {
|
||||
t.Helper()
|
||||
// Not using t.TempDir() here because in rootless mode, while the temporary
|
||||
// directory gets mode 0777, it's a subdir of an 0700 directory owned by root.
|
||||
// So, it's not accessible by the daemon.
|
||||
f, err := os.CreateTemp("", "resolv.conf")
|
||||
assert.NilError(t, err)
|
||||
t.Cleanup(func() { os.Remove(f.Name()) })
|
||||
err = f.Chmod(0o644)
|
||||
assert.NilError(t, err)
|
||||
f.Write([]byte("nameserver " + addr + "\n"))
|
||||
return f.Name()
|
||||
// GenResolvConf generates a resolv.conf that only contains a single
|
||||
// nameserver line, with address addr, and returns the file content.
|
||||
func GenResolvConf(addr string) string {
|
||||
return "nameserver " + addr + "\n"
|
||||
}
|
||||
|
||||
// StartDaftDNS starts and returns a really, really daft DNS server that only
|
||||
|
||||
@@ -102,8 +102,10 @@ func TestExtDNSInIPv6OnlyNw(t *testing.T) {
|
||||
network.StartDaftDNS(t, "127.0.0.1")
|
||||
|
||||
// Set up a temp resolv.conf pointing at that DNS server, and a daemon using it.
|
||||
tmpFileName := network.WriteTempResolvConf(t, "127.0.0.1")
|
||||
d := daemon.New(t, daemon.WithEnvVars("DOCKER_TEST_RESOLV_CONF_PATH="+tmpFileName), daemon.WithExperimental())
|
||||
d := daemon.New(t,
|
||||
daemon.WithResolvConf(network.GenResolvConf("127.0.0.1")),
|
||||
daemon.WithExperimental(),
|
||||
)
|
||||
d.StartWithBusybox(ctx, t)
|
||||
defer d.Stop(t)
|
||||
|
||||
|
||||
@@ -569,8 +569,7 @@ func TestIPVlanDNS(t *testing.T) {
|
||||
|
||||
net.StartDaftDNS(t, "127.0.0.1")
|
||||
|
||||
tmpFileName := net.WriteTempResolvConf(t, "127.0.0.1")
|
||||
d := daemon.New(t, daemon.WithEnvVars("DOCKER_TEST_RESOLV_CONF_PATH="+tmpFileName))
|
||||
d := daemon.New(t, daemon.WithResolvConf(net.GenResolvConf("127.0.0.1")))
|
||||
d.StartWithBusybox(ctx, t)
|
||||
t.Cleanup(func() { d.Stop(t) })
|
||||
c := d.NewClientT(t)
|
||||
|
||||
@@ -566,8 +566,7 @@ func TestMACVlanDNS(t *testing.T) {
|
||||
|
||||
net.StartDaftDNS(t, "127.0.0.1")
|
||||
|
||||
tmpFileName := net.WriteTempResolvConf(t, "127.0.0.1")
|
||||
d := daemon.New(t, daemon.WithEnvVars("DOCKER_TEST_RESOLV_CONF_PATH="+tmpFileName))
|
||||
d := daemon.New(t, daemon.WithResolvConf(net.GenResolvConf("127.0.0.1")))
|
||||
d.StartWithBusybox(ctx, t)
|
||||
t.Cleanup(func() { d.Stop(t) })
|
||||
c := d.NewClientT(t)
|
||||
|
||||
@@ -25,9 +25,7 @@ func TestResolvConfLocalhostIPv6(t *testing.T) {
|
||||
|
||||
ctx := setupTest(t)
|
||||
|
||||
tmpFileName := network.WriteTempResolvConf(t, "127.0.0.53")
|
||||
|
||||
d := daemon.New(t, daemon.WithEnvVars("DOCKER_TEST_RESOLV_CONF_PATH="+tmpFileName))
|
||||
d := daemon.New(t, daemon.WithResolvConf(network.GenResolvConf("127.0.0.53")))
|
||||
d.StartWithBusybox(ctx, t)
|
||||
defer d.Stop(t)
|
||||
|
||||
@@ -51,7 +49,7 @@ func TestResolvConfLocalhostIPv6(t *testing.T) {
|
||||
Force: true,
|
||||
})
|
||||
|
||||
output := strings.ReplaceAll(result.Stdout.String(), tmpFileName, "RESOLV.CONF")
|
||||
output := strings.ReplaceAll(result.Stdout.String(), d.ResolvConfPathOverride, "RESOLV.CONF")
|
||||
assert.Check(t, is.Equal(output, `# Generated by Docker Engine.
|
||||
# This file can be edited; Docker Engine will not make further changes once it
|
||||
# has been modified.
|
||||
@@ -79,8 +77,7 @@ func TestInternalNetworkDNS(t *testing.T) {
|
||||
network.StartDaftDNS(t, "127.0.0.1")
|
||||
|
||||
// Set up a temp resolv.conf pointing at that DNS server, and a daemon using it.
|
||||
tmpFileName := network.WriteTempResolvConf(t, "127.0.0.1")
|
||||
d := daemon.New(t, daemon.WithEnvVars("DOCKER_TEST_RESOLV_CONF_PATH="+tmpFileName))
|
||||
d := daemon.New(t, daemon.WithResolvConf(network.GenResolvConf("127.0.0.1")))
|
||||
d.StartWithBusybox(ctx, t)
|
||||
defer d.Stop(t)
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -88,6 +89,8 @@ type Daemon struct {
|
||||
usernsRemap string
|
||||
rootlessUser *user.User
|
||||
rootlessXDGRuntimeDir string
|
||||
resolvConfContent string
|
||||
ResolvConfPathOverride string // Path to a replacement for "/etc/resolv.conf", or empty.
|
||||
|
||||
// swarm related field
|
||||
swarmListenAddr string
|
||||
@@ -146,6 +149,15 @@ func NewDaemon(workingDir string, ops ...Option) (*Daemon, error) {
|
||||
op(d)
|
||||
}
|
||||
|
||||
if len(d.resolvConfContent) > 0 {
|
||||
path := filepath.Join(d.Folder, "resolv.conf")
|
||||
if err := os.WriteFile(path, []byte(d.resolvConfContent), 0644); err != nil {
|
||||
return nil, fmt.Errorf("failed to write docker resolv.conf to %q: %v", path, err)
|
||||
}
|
||||
d.extraEnv = append(d.extraEnv, "DOCKER_TEST_RESOLV_CONF_PATH="+path)
|
||||
d.ResolvConfPathOverride = path
|
||||
}
|
||||
|
||||
if d.rootlessUser != nil {
|
||||
if err := os.Chmod(SockRoot, 0o777); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -135,3 +135,11 @@ func WithEnvVars(vars ...string) Option {
|
||||
d.extraEnv = append(d.extraEnv, vars...)
|
||||
}
|
||||
}
|
||||
|
||||
// WithResolvConf allows a test to provide content for a resolv.conf file to be used
|
||||
// as the basis for resolv.conf in the container, instead of the host's /etc/resolv.conf.
|
||||
func WithResolvConf(content string) Option {
|
||||
return func(d *Daemon) {
|
||||
d.resolvConfContent = content
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user