Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow uid:gid to be specified during container creation #363

Merged
merged 2 commits into from Oct 22, 2021

Conversation

bamsammich
Copy link
Contributor

Fixes #361

Adds a User field to ContainerRequest and includes that value in the container config so that uid[:gid] can be specified for the container.

@@ -675,6 +675,7 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque
Labels: req.Labels,
Cmd: req.Cmd,
Hostname: req.Hostname,
User: req.User,
Copy link
Collaborator

Choose a reason for hiding this comment

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

What would happen if the req.User is empty? Will the docker client understand that as root?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

User is a string field, so it appears that an empty string equates to not setting a UID at all:

Confirmed with these tests:

package main

import (
	"bytes"
	"context"
	"fmt"

	"github.com/testcontainers/testcontainers-go"
	"github.com/testcontainers/testcontainers-go/wait"
)

func main() {
	tests := []testcontainers.ContainerRequest{
		{
			Image:      "debian:latest",
			Cmd:        []string{"sh", "-c", "id -u"},
			WaitingFor: wait.ForExit(),
		},
		{
			Image:      "debian:latest",
			User:       "60125",
			Cmd:        []string{"sh", "-c", "id -u"},
			WaitingFor: wait.ForExit(),
		},
	}

	for _, test := range tests {
		ctx := context.Background()
		c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
			ContainerRequest: test,
			Started:          true,
		})
		if err != nil {
			panic(err)
		}
		buf := new(bytes.Buffer)
		r, _ := c.Logs(ctx)
		buf.ReadFrom(r)
		fmt.Printf("User: expected: %s; got: %s\n", test.User, buf.String())
	}
}

Output:

2021/10/05 08:17:34 Starting container id: fa62829446d6 image: quay.io/testcontainers/ryuk:0.2.3
2021/10/05 08:17:35 Waiting for container id fa62829446d6 image: quay.io/testcontainers/ryuk:0.2.3
2021/10/05 08:17:35 Container is ready id: fa62829446d6 image: quay.io/testcontainers/ryuk:0.2.3
2021/10/05 08:17:35 Starting container id: 1b75176f40da image: debian:latest
2021/10/05 08:17:35 Waiting for container id 1b75176f40da image: debian:latest
2021/10/05 08:17:35 Container is ready id: 1b75176f40da image: debian:latest
User: expected: ; got: 0

2021/10/05 08:17:35 Starting container id: 063145d38a97 image: debian:latest
2021/10/05 08:17:36 Waiting for container id 063145d38a97 image: debian:latest
2021/10/05 08:17:36 Container is ready id: 063145d38a97 image: debian:latest
User: expected: 60125; got: 60125

Copy link
Collaborator

Choose a reason for hiding this comment

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

Would you mind adding two tests in your PR: one for adding the User, and another one without it?

Besides that, we could only set the User field if req.User is not empty

Copy link
Contributor Author

@bamsammich bamsammich Oct 5, 2021

Choose a reason for hiding this comment

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

@mdelapenya sure thing! Tests have been added.

@codecov
Copy link

codecov bot commented Oct 5, 2021

Codecov Report

Merging #363 (d69086e) into master (a8ca295) will increase coverage by 0.72%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #363      +/-   ##
==========================================
+ Coverage   61.91%   62.64%   +0.72%     
==========================================
  Files          15       15              
  Lines        1011     1012       +1     
==========================================
+ Hits          626      634       +8     
+ Misses        288      279       -9     
- Partials       97       99       +2     
Impacted Files Coverage Δ
container.go 85.29% <ø> (ø)
docker.go 66.25% <100.00%> (+1.53%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a5a9803...d69086e. Read the comment docs.

Comment on lines +1597 to +1599
if err != nil {
t.Fatal(err)
}

Choose a reason for hiding this comment

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

Nitpick: these if-statements can be replaced by calls to require.NoError(t, err) if you import github.com/stretchr/testify/require

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True. I used t.Fatal(err) here to match other tests for this package.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If you agree, would you mind sending the changes related to the require.NoError, or assert.Nil, in a follow up PR?

@bamsammich
Copy link
Contributor Author

@mdelapenya Is there anything else needed for this PR? Could the pending workflow be run?

Copy link
Collaborator

@mdelapenya mdelapenya left a comment

Choose a reason for hiding this comment

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

Thanks for this contribution! I'll wait for the CI for the final review, but LGTM

@mdelapenya
Copy link
Collaborator

@gianarb this is ready to go. Merging?

@gianarb gianarb merged commit 48ce5b9 into testcontainers:master Oct 22, 2021
@gianarb
Copy link
Collaborator

gianarb commented Oct 22, 2021

Thanks a lot!

@gianarb gianarb added the feature New functionality or new behaviors on the existing one label Oct 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality or new behaviors on the existing one
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow user and group ID specification in ContainerRequest
4 participants