Skip to content

Commit e41507b

Browse files
committed
Add unit test to check wrong uid case
1 parent eb38750 commit e41507b

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

container_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,23 @@ func TestUser(t *testing.T) {
849849
if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
850850
t.Error(string(output))
851851
}
852+
853+
// Test an wrong username
854+
container, err = builder.Create(&Config{
855+
Image: GetTestImage(runtime).ID,
856+
Cmd: []string{"id"},
857+
858+
User: "unkownuser",
859+
},
860+
)
861+
if err != nil {
862+
t.Fatal(err)
863+
}
864+
defer runtime.Destroy(container)
865+
output, err = container.Output()
866+
if container.State.ExitCode == 0 {
867+
t.Fatal("Starting container with wrong uid should fail but it passed.")
868+
}
852869
}
853870

854871
func TestMultipleContainers(t *testing.T) {

utils/utils.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,9 @@ func ParseRepositoryTag(repos string) (string, string) {
702702
return repos, ""
703703
}
704704

705+
// UserLookup check if the given username or uid is present in /etc/passwd
706+
// and returns the user struct.
707+
// If the username is not found, an error is returned.
705708
func UserLookup(uid string) (*user.User, error) {
706709
file, err := ioutil.ReadFile("/etc/passwd")
707710
if err != nil {

0 commit comments

Comments
 (0)