Skip to content

Commit aee88fb

Browse files
authored
tests: improve passwd/group parsing tests (#2045)
1 parent 3286a0b commit aee88fb

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

pkg/passwd/group_test.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022, 2023 Chainguard, Inc.
1+
// Copyright 2022-2026 Chainguard, Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ import (
1818
"bytes"
1919
"testing"
2020

21+
"github.com/stretchr/testify/assert"
2122
"github.com/stretchr/testify/require"
2223

2324
apkfs "chainguard.dev/apko/pkg/apk/fs"
@@ -28,15 +29,40 @@ func TestGroupParser(t *testing.T) {
2829
gf, err := ReadOrCreateGroupFile(fsys, "group")
2930
require.NoError(t, err)
3031

32+
require.NotEmpty(t, gf, "group file in existing testdata file should not be empty")
33+
34+
found_root := false
35+
found_daemon := false
36+
found_nobody := false
3137
for _, ge := range gf.Entries {
3238
if ge.GID == 0 {
33-
require.Equal(t, "root", ge.GroupName, "gid 0 is not root")
39+
assert.Equal(t, "root", ge.GroupName, "gid 0 is not root")
40+
assert.Equal(t, "x", ge.Password, "gid 0 password entry is not set to x")
41+
assert.Equal(t, []string{"root"}, ge.Members, "gid 0 members should just be the root user")
42+
found_root = true
43+
}
44+
45+
if ge.GID == 2 {
46+
assert.Equal(t, "daemon", ge.GroupName, "gid 2 is not daemon")
47+
assert.Equal(t, "x", ge.Password, "gid 2 password entry is not set to x")
48+
assert.ElementsMatch(t, []string{"root", "bin", "daemon"}, ge.Members, "gid 2 members should contain root, bin, and daemon users")
49+
found_daemon = true
3450
}
3551

3652
if ge.GID == 65534 {
37-
require.Equal(t, "nobody", ge.GroupName, "gid 65534 is not nobody")
53+
assert.Equal(t, "nobody", ge.GroupName, "gid 65534 is not nobody")
54+
assert.Equal(t, "x", ge.Password, "gid 65534 password entry is not set to x")
55+
// XXX if there's no users listed as group members, Parse
56+
// returns a list with one empty string as a result, not
57+
// sure if that's intended behavior.
58+
assert.Equal(t, []string{""}, ge.Members, "gid 65534 members should be empty")
59+
found_nobody = true
3860
}
3961
}
62+
63+
assert.True(t, found_root, "group file should contain the root group")
64+
assert.True(t, found_daemon, "group file should contain the daemon group")
65+
assert.True(t, found_nobody, "group file should contain the nobody group")
4066
}
4167

4268
func TestGroupWriter(t *testing.T) {

pkg/passwd/passwd_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022, 2023 Chainguard, Inc.
1+
// Copyright 2022-2026 Chainguard, Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ import (
1919
"os"
2020
"testing"
2121

22+
"github.com/stretchr/testify/assert"
2223
"github.com/stretchr/testify/require"
2324

2425
apkfs "chainguard.dev/apko/pkg/apk/fs"
@@ -34,16 +35,27 @@ func TestParser(t *testing.T) {
3435
require.NoError(t, err)
3536
uf, err := ReadOrCreateUserFile(fsys, "etc/passwd")
3637
require.NoError(t, err)
38+
require.NotEmpty(t, uf, "parsed passwd file should not be empty")
3739

40+
found_root := false
41+
found_nobody := false
3842
for _, ue := range uf.Entries {
3943
if ue.UID == 0 {
40-
require.Equal(t, "root", ue.UserName, "uid 0 is not root")
44+
assert.Equal(t, "root", ue.UserName, "uid 0 is not root")
45+
assert.Equal(t, "/bin/ash", ue.Shell, "uid 0 shell is not /bin/ash")
46+
assert.Equal(t, "/root", ue.HomeDir, "uid 0 homedir is not /root")
47+
found_root = true
4148
}
4249

4350
if ue.UID == 65534 {
44-
require.Equal(t, "nobody", ue.UserName, "uid 65534 is not nobody")
51+
assert.Equal(t, "nobody", ue.UserName, "uid 65534 is not nobody")
52+
assert.Equal(t, "/bin/false", ue.Shell, "uid 65534 shell is not /bin/false")
53+
assert.Equal(t, "/", ue.HomeDir, "uid 65534 homedir is not /")
54+
found_nobody = true
4555
}
4656
}
57+
assert.True(t, found_root, "passwd file should contain the root user")
58+
assert.True(t, found_nobody, "passwd file should contain the nobody user")
4759
}
4860

4961
func TestWriter(t *testing.T) {

0 commit comments

Comments
 (0)