Repository: moby/moby
File: daemon/libnetwork/sandbox_dns_unix_test.go (Go)

Code before:
	assert.Check(t, is.Len(dnsOptionsList, 1))
	assert.Check(t, is.Equal("ndots:0", dnsOptionsList[0]))

	sb2.config.dnsOptionsList = []string{"ndots:foobar"}
	err = sb2.setupDNS()
	assert.NilError(t, err)
	err = sb2.rebuildDNS()
	assert.NilError(t, err)
	dnsOptionsList = getResolvConfOptions(t, sb2.config.resolvConfPath)
	assert.Check(t, is.DeepEqual([]string{"ndots:0"}, dnsOptionsList))

	sb2.config.dnsOptionsList = []string{"ndots:-1"}
	err = sb2.setupDNS()
	assert.NilError(t, err)
	err = sb2.rebuildDNS()
	assert.NilError(t, err)
	dnsOptionsList = getResolvConfOptions(t, sb2.config.resolvConfPath)
	assert.Check(t, is.DeepEqual([]string{"ndots:0"}, dnsOptionsList))
}

func TestNonHostNetDNSRestart(t *testing.T) {
	c, err := New(context.Background(), config.OptionDataDir(t.TempDir()))
	assert.NilError(t, err)

	// Step 1: Create initial sandbox (simulating first container start)
	sb, err := c.NewSandbox(context.Background(), "cnt1")
	assert.NilError(t, err)

	sb.startResolver(false)

	err = sb.setupDNS()
	assert.NilError(t, err)
	err = sb.rebuildDNS()
	assert.NilError(t, err)

	// Step 2: Simulate user manually overwriting the container's resolv.conf
	resolvConfPath := sb.config.resolvConfPath
	modifiedContent := []byte(`nameserver 1.1.1.1`)
	err = os.WriteFile(resolvConfPath, modifiedContent, 0644)
	assert.NilError(t, err)

	// Step 3: Delete the sandbox (simulating container stop)
	err = sb.Delete(context.Background())
	assert.NilError(t, err)

	// Step 4: Create a new sandbox (simulating container restart)
	sbRestart, err := c.NewSandbox(context.Background(), "cnt1",
		OptionResolvConfPath(resolvConfPath),
	)
	assert.NilError(t, err)
	defer func() {

Review comment (bug):
If the test fails and exits before getting to "step 3", `sb` needs to be deleted (or it'll leave the network namespace behind).

It's ok to call Delete twice, it'll just return an error. So you can just use a defer and ignore the error.

Code after:
	assert.Check(t, is.Len(dnsOptionsList, 1))
	assert.Check(t, is.Equal("ndots:0", dnsOptionsList[0]))

	sb2.config.dnsOptionsList = []string{"ndots:foobar"}
	err = sb2.setupDNS()
	assert.NilError(t, err)
	err = sb2.rebuildDNS()
	assert.NilError(t, err)
	dnsOptionsList = getResolvConfOptions(t, sb2.config.resolvConfPath)
	assert.Check(t, is.DeepEqual([]string{"ndots:0"}, dnsOptionsList))

	sb2.config.dnsOptionsList = []string{"ndots:-1"}
	err = sb2.setupDNS()
	assert.NilError(t, err)
	err = sb2.rebuildDNS()
	assert.NilError(t, err)
	dnsOptionsList = getResolvConfOptions(t, sb2.config.resolvConfPath)
	assert.Check(t, is.DeepEqual([]string{"ndots:0"}, dnsOptionsList))
}

func TestNonHostNetDNSRestart(t *testing.T) {
	c, err := New(context.Background(), config.OptionDataDir(t.TempDir()))
	assert.NilError(t, err)

	// Step 1: Create initial sandbox (simulating first container start)
	sb, err := c.NewSandbox(context.Background(), "cnt1")
	assert.NilError(t, err)

	defer func() {
		_ = sb.Delete(context.Background())
	}()

	sb.startResolver(false)

	err = sb.setupDNS()
	assert.NilError(t, err)
	err = sb.rebuildDNS()
	assert.NilError(t, err)

	// Step 2: Simulate user manually overwriting the container's resolv.conf
	resolvConfPath := sb.config.resolvConfPath
	modifiedContent := []byte(`nameserver 1.1.1.1`)
	err = os.WriteFile(resolvConfPath, modifiedContent, 0644)
	assert.NilError(t, err)

	// Step 3: Delete the sandbox (simulating container stop)
	err = sb.Delete(context.Background())
	assert.NilError(t, err)

	// Step 4: Create a new sandbox (simulating container restart)
	sbRestart, err := c.NewSandbox(context.Background(), "cnt1",