refactor: reduce duplicate code
Signed-off-by: Ye Sijun <junnplus@gmail.com>
This commit is contained in:
@@ -18,6 +18,7 @@ package oci
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -30,6 +31,123 @@ import (
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// nolint:gosec
|
||||
func TestWithUserID(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
expectedPasswd := `root:x:0:0:root:/root:/bin/ash
|
||||
guest:x:405:100:guest:/dev/null:/sbin/nologin
|
||||
`
|
||||
td := t.TempDir()
|
||||
apply := fstest.Apply(
|
||||
fstest.CreateDir("/etc", 0777),
|
||||
fstest.CreateFile("/etc/passwd", []byte(expectedPasswd), 0777),
|
||||
)
|
||||
if err := apply.Apply(td); err != nil {
|
||||
t.Fatalf("failed to apply: %v", err)
|
||||
}
|
||||
c := containers.Container{ID: t.Name()}
|
||||
testCases := []struct {
|
||||
userID uint32
|
||||
expectedUID uint32
|
||||
expectedGID uint32
|
||||
}{
|
||||
{
|
||||
userID: 0,
|
||||
expectedUID: 0,
|
||||
expectedGID: 0,
|
||||
},
|
||||
{
|
||||
userID: 405,
|
||||
expectedUID: 405,
|
||||
expectedGID: 100,
|
||||
},
|
||||
{
|
||||
userID: 1000,
|
||||
expectedUID: 1000,
|
||||
expectedGID: 0,
|
||||
},
|
||||
}
|
||||
for _, testCase := range testCases {
|
||||
t.Run(fmt.Sprintf("user %d", testCase.userID), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := Spec{
|
||||
Version: specs.Version,
|
||||
Root: &specs.Root{
|
||||
Path: td,
|
||||
},
|
||||
Linux: &specs.Linux{},
|
||||
}
|
||||
err := WithUserID(testCase.userID)(context.Background(), nil, &c, &s)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, testCase.expectedUID, s.Process.User.UID)
|
||||
assert.Equal(t, testCase.expectedGID, s.Process.User.GID)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// nolint:gosec
|
||||
func TestWithUsername(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
expectedPasswd := `root:x:0:0:root:/root:/bin/ash
|
||||
guest:x:405:100:guest:/dev/null:/sbin/nologin
|
||||
`
|
||||
td := t.TempDir()
|
||||
apply := fstest.Apply(
|
||||
fstest.CreateDir("/etc", 0777),
|
||||
fstest.CreateFile("/etc/passwd", []byte(expectedPasswd), 0777),
|
||||
)
|
||||
if err := apply.Apply(td); err != nil {
|
||||
t.Fatalf("failed to apply: %v", err)
|
||||
}
|
||||
c := containers.Container{ID: t.Name()}
|
||||
testCases := []struct {
|
||||
user string
|
||||
expectedUID uint32
|
||||
expectedGID uint32
|
||||
err string
|
||||
}{
|
||||
{
|
||||
user: "root",
|
||||
expectedUID: 0,
|
||||
expectedGID: 0,
|
||||
},
|
||||
{
|
||||
user: "guest",
|
||||
expectedUID: 405,
|
||||
expectedGID: 100,
|
||||
},
|
||||
{
|
||||
user: "1000",
|
||||
err: "no users found",
|
||||
},
|
||||
{
|
||||
user: "unknown",
|
||||
err: "no users found",
|
||||
},
|
||||
}
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.user, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := Spec{
|
||||
Version: specs.Version,
|
||||
Root: &specs.Root{
|
||||
Path: td,
|
||||
},
|
||||
Linux: &specs.Linux{},
|
||||
}
|
||||
err := WithUsername(testCase.user)(context.Background(), nil, &c, &s)
|
||||
if err != nil {
|
||||
assert.EqualError(t, err, testCase.err)
|
||||
}
|
||||
assert.Equal(t, testCase.expectedUID, s.Process.User.UID)
|
||||
assert.Equal(t, testCase.expectedGID, s.Process.User.GID)
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// nolint:gosec
|
||||
func TestWithAdditionalGIDs(t *testing.T) {
|
||||
t.Parallel()
|
||||
@@ -54,7 +172,6 @@ sys:x:3:root,bin,adm
|
||||
c := containers.Container{ID: t.Name()}
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
user string
|
||||
expected []uint32
|
||||
}{
|
||||
|
||||
Reference in New Issue
Block a user