Skip to content

Commit

Permalink
utils: new conversion method
Browse files Browse the repository at this point in the history
Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Feb 1, 2023
1 parent 0e949bf commit cbb45a6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,19 @@ func IDtoolsToRuntimeSpec(idMaps []idtools.IDMap) (convertedIDMap []specs.LinuxI
return convertedIDMap
}

// RuntimeSpecToIDtoolsTo converts runtime spec to the one of the idtools ID mapping
func RuntimeSpecToIDtools(idMaps []specs.LinuxIDMapping) (convertedIDMap []idtools.IDMap) {
for _, idmap := range idMaps {
tempIDMap := idtools.IDMap{
ContainerID: int(idmap.ContainerID),
HostID: int(idmap.HostID),
Size: int(idmap.Size),
}
convertedIDMap = append(convertedIDMap, tempIDMap)
}
return convertedIDMap
}

func LookupUser(name string) (*user.User, error) {
// Assume UID lookup first, if it fails look up by username
if u, err := user.LookupId(name); err == nil {
Expand Down
28 changes: 28 additions & 0 deletions pkg/util/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -88,3 +89,30 @@ func TestParseInputTime(t *testing.T) {

assert.Equal(t, expected, tm)
}

func TestConvertMappings(t *testing.T) {
start := []specs.LinuxIDMapping{
{
ContainerID: 1,
HostID: 2,
Size: 3,
},
{
ContainerID: 4,
HostID: 5,
Size: 6,
},
}

converted := RuntimeSpecToIDtools(start)

convertedBack := IDtoolsToRuntimeSpec(converted)

assert.Equal(t, len(start), len(convertedBack))

for i := range start {
assert.Equal(t, start[i].ContainerID, convertedBack[i].ContainerID)
assert.Equal(t, start[i].HostID, convertedBack[i].HostID)
assert.Equal(t, start[i].Size, convertedBack[i].Size)
}
}

0 comments on commit cbb45a6

Please sign in to comment.