From 0e38815387ef69c661c78690bc97a190268a4092 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Mon, 18 Mar 2024 22:04:32 +0100 Subject: [PATCH] utils: add test for the new function Signed-off-by: Giuseppe Scrivano --- pkg/util/utils_test.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/pkg/util/utils_test.go b/pkg/util/utils_test.go index c412df5ffb..cb13c49fa6 100644 --- a/pkg/util/utils_test.go +++ b/pkg/util/utils_test.go @@ -7,6 +7,7 @@ import ( "time" "github.com/containers/storage/pkg/idtools" + stypes "github.com/containers/storage/types" ruser "github.com/moby/sys/user" "github.com/opencontainers/runtime-spec/specs-go" "github.com/stretchr/testify/assert" @@ -588,3 +589,36 @@ func TestConvertTimeout(t *testing.T) { timeout = ConvertTimeout(-100) assert.Equal(t, uint(math.MaxUint32), timeout) } + +func TestGetRootlessKeepIDMapping(t *testing.T) { + tests := []struct { + uid, gid int + uids, gids []idtools.IDMap + expectedOptions *stypes.IDMappingOptions + expectedUID, expectedGID int + expectedError error + }{ + { + uid: 1000, + gid: 1000, + uids: []idtools.IDMap{}, + gids: []idtools.IDMap{}, + expectedOptions: &stypes.IDMappingOptions{ + HostUIDMapping: false, + HostGIDMapping: false, + UIDMap: []idtools.IDMap{{ContainerID: 1000, HostID: 0, Size: 1}}, + GIDMap: []idtools.IDMap{{ContainerID: 1000, HostID: 0, Size: 1}}, + }, + expectedUID: 1000, + expectedGID: 1000, + }, + } + + for _, test := range tests { + options, uid, gid, err := getRootlessKeepIDMapping(test.uid, test.gid, test.uids, test.gids) + assert.Nil(t, err) + assert.Equal(t, test.expectedOptions, options) + assert.Equal(t, test.expectedUID, uid) + assert.Equal(t, test.expectedGID, gid) + } +}