Skip to content

Add testutil daemon.WithResolvConf#49132

Merged
thaJeztah merged 1 commit intomoby:masterfrom
robmry:withresolvconf
Jan 3, 2025
Merged

Add testutil daemon.WithResolvConf#49132
thaJeztah merged 1 commit intomoby:masterfrom
robmry:withresolvconf

Conversation

@robmry
Copy link
Copy Markdown
Contributor

@robmry robmry commented Dec 19, 2024

- What I did

Add daemon.WithResolvConf to tidy up tests that were setting env var DOCKER_TEST_RESOLV_CONF_PATH.

- How I did it

- How to verify it

- Description for the changelog

- A picture of a cute animal (not mandatory but encouraged)

@robmry robmry added area/networking Networking area/testing kind/refactor PR's that refactor, or clean-up code area/networking/dns Networking labels Dec 19, 2024
@robmry robmry added this to the 28.0.0 milestone Dec 19, 2024
@robmry robmry self-assigned this Dec 19, 2024
Comment on lines +105 to +108
d := daemon.New(t,
daemon.WithResolvConf(network.WriteTempResolvConf(t, "127.0.0.1")),
daemon.WithExperimental(),
)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh, this now makes it really tempting to combine those (not sure if we should), as it looks like WriteTempResolvConf is always used in combination with daemon.WithResolvConf

daemon.WithResolver(t, "127.0.0.1")

There is some special handling in that utility for permissions;

// 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)

I wonder if (as we're passing the Daemon struct to these options) if putting it somewhere inside the daemon's storage location would work for that; in that case, we'd keep all things related to that daemon instance together;

dest := os.Getenv("DOCKER_INTEGRATION_DAEMON_DEST")
if dest == "" {
dest = os.Getenv("DEST")
}
dest = filepath.Join(dest, t.Name())

func NewDaemon(workingDir string, ops ...Option) (*Daemon, error) {
storageDriver := os.Getenv("DOCKER_GRAPHDRIVER")
if err := os.MkdirAll(SockRoot, 0o700); err != nil {
return nil, errors.Wrapf(err, "failed to create daemon socket root %q", SockRoot)
}
id := "d" + stringid.TruncateID(stringid.GenerateRandomID())
dir := filepath.Join(workingDir, id)
daemonFolder, err := filepath.Abs(dir)
if err != nil {
return nil, err
}
daemonRoot := filepath.Join(daemonFolder, "root")
if err := os.MkdirAll(daemonRoot, 0o755); err != nil {
return nil, errors.Wrapf(err, "failed to create daemon root %q", daemonRoot)
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh, this now makes it really tempting to combine those (not sure if we should), as it looks like WriteTempResolvConf is always used in combination with daemon.WithResolvConf

That'd take away the possibility of giving the daemon a more complicated resolv.conf (or a deliberately broken one).

I thought about having StartDaftDNS write its own resolv.conf, returning the filename. Then, in four of the five cases, it'd be ... daemon.WithResolvConf(network.StartDaftDNS(t, "127.0.0.1")). (In the other case, no dummy resolver gets started, the test just needs its own resolv.conf.)

But, same thing there really - it'd limit the resolv.conf, without buying much.

I wonder if (as we're passing the Daemon struct to these options) if putting it somewhere inside the daemon's storage location would work for that; in that case, we'd keep all things related to that daemon instance together;

I suppose WithResolvConf could take a []byte to write to a resolv.conf in the daemon's Folder, instead of the location of a file (so the file doesn't need to be created before the NewDaemon has created Folder). I'll try that...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose WithResolvConf could take a []byte to write to a resolv.conf in the daemon's Folder, instead of the location of a file (so the file doesn't need to be created before the NewDaemon has created Folder). I'll try that...

Oh, that might work yes!

And, yes, I was also considering "would we need more complex ones?" so yeah, I can agree.

It's mostly brainstorming a bit here, knowing that this is not a critical/urgent PR, so just looking what shape it could take to make it clean and flexible enough 😄 ❤️

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've had a go at that ... I think it's better.

@robmry
Copy link
Copy Markdown
Contributor Author

robmry commented Dec 19, 2024

Unrelated Windows test failures ...

=== FAIL: github.com/docker/docker/integration/container TestWaitRestartedContainer/not-running (60.38s)
    wait_test.go:205: assertion failed: error is not nil: error during connect: Post "http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.48/containers/create": EOF
=== FAIL: github.com/docker/docker/integration/build TestBuildWithRemoveAndForceRemove/successful_build_with_no_removal (70.35s)
    build_test.go:114: assertion failed: error is not nil: unexpected EOF

@robmry robmry marked this pull request as ready for review December 19, 2024 12:22
Copy link
Copy Markdown
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - left one more suggestion in case you think it makes sense (but not a blocker)


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(t, "127.0.0.1")))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh, very unrelated, but we should probably avoid using net as alias, as it would be clashing with Go's net package;

net "github.com/docker/docker/integration/internal/network"

Comment thread testutil/daemon/ops.go Outdated
Comment on lines +141 to +143
func WithResolvConf(content []byte) Option {
return func(d *Daemon) {
d.resolvConfContent = content
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker; wondering if string as arguments (and converting to []byte internally) would make these utilities easier to use. Or at least, I often find myself creating a string, and then having to convert it to []byte, because strings tend to be easier to produce 😅

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Copy Markdown
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@thaJeztah thaJeztah merged commit 0cd495c into moby:master Jan 3, 2025
@robmry robmry deleted the withresolvconf branch January 28, 2025 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants