Skip to content

Commit

Permalink
Migrate unit tests from cmd/podman into pkg/spec
Browse files Browse the repository at this point in the history
Several changes made in the interface of pkg/spec make
interacting with it without a runtime difficult to impossible,
so move the existing limited testing from cmd/podman (which
mostly tested pkg/spec) into pkg/spec itself where we can call
individual functions that don't break things.

Signed-off-by: Matthew Heon <matthew.heon@pm.me>
  • Loading branch information
mheon committed May 1, 2019
1 parent 9ee50fe commit 2e00d41
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 186 deletions.
162 changes: 0 additions & 162 deletions cmd/podman/run_test.go

This file was deleted.

2 changes: 0 additions & 2 deletions pkg/spec/containerconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ func (config *CreateConfig) MakeContainerConfig(runtime *libpod.Runtime, pod *li
return nil, nil, err
}

logrus.Debugf("got mounts as %v", mounts)

runtimeSpec, err := config.createConfigToOCISpec(runtime, mounts)
if err != nil {
return nil, nil, err
Expand Down
103 changes: 81 additions & 22 deletions pkg/spec/spec_test.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,98 @@
package createconfig

import (
"reflect"
"runtime"
"testing"

spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/containers/libpod/pkg/sysinfo"
"github.com/containers/storage"
"github.com/containers/storage/pkg/idtools"
"github.com/docker/go-units"
"github.com/stretchr/testify/assert"
)

func TestGetVolumeMountsOneVolume(t *testing.T) {
data := spec.Mount{
Destination: "/foobar",
Type: "bind",
Source: "/foobar",
Options: []string{"ro", "rbind", "rprivate"},
var (
sysInfo = sysinfo.New(true)
)

// Make createconfig to test with
func makeTestCreateConfig() *CreateConfig {
cc := new(CreateConfig)
cc.Resources = CreateResourceConfig{}
cc.IDMappings = new(storage.IDMappingOptions)
cc.IDMappings.UIDMap = []idtools.IDMap{}
cc.IDMappings.GIDMap = []idtools.IDMap{}

return cc
}

// TestPIDsLimit verifies the given pid-limit is correctly defined in the spec
func TestPIDsLimit(t *testing.T) {
// The default configuration of podman enables seccomp, which is not available on non-Linux systems.
// Thus, any tests that use the default seccomp setting would fail.
// Skip the tests on non-Linux platforms rather than explicitly disable seccomp in the test and possibly affect the test result.
if runtime.GOOS != "linux" {
t.Skip("seccomp, which is enabled by default, is only supported on Linux")
}
config := CreateConfig{
Volumes: []string{"/foobar:/foobar:ro"},
if !sysInfo.PidsLimit {
t.Skip("running test not supported by the host system")
}
specMount, _, err := config.getVolumeMounts()

cc := makeTestCreateConfig()
cc.Resources.PidsLimit = 22

spec, err := cc.createConfigToOCISpec(nil, nil)
assert.NoError(t, err)
assert.True(t, reflect.DeepEqual(data, specMount[data.Destination]))

assert.Equal(t, spec.Linux.Resources.Pids.Limit, int64(22))
}

func TestGetTmpfsMounts(t *testing.T) {
data := spec.Mount{
Destination: "/homer",
Type: "tmpfs",
Source: "tmpfs",
Options: []string{"rw", "size=787448k", "mode=1777"},
// TestBLKIOWeightDevice verifies the given blkio weight is correctly set in the
// spec.
func TestBLKIOWeightDevice(t *testing.T) {
// The default configuration of podman enables seccomp, which is not available on non-Linux systems.
// Thus, any tests that use the default seccomp setting would fail.
// Skip the tests on non-Linux platforms rather than explicitly disable seccomp in the test and possibly affect the test result.
if runtime.GOOS != "linux" {
t.Skip("seccomp, which is enabled by default, is only supported on Linux")
}
config := CreateConfig{
Tmpfs: []string{"/homer:rw,size=787448k,mode=1777"},
if !sysInfo.BlkioWeightDevice {
t.Skip("running test not supported by the host system")
}
tmpfsMount, err := config.getTmpfsMounts()

cc := makeTestCreateConfig()
cc.Resources.BlkioWeightDevice = []string{"/dev/zero:100"}

spec, err := cc.createConfigToOCISpec(nil, nil)
assert.NoError(t, err)
assert.True(t, reflect.DeepEqual(data, tmpfsMount[data.Destination]))

// /dev/zero is guaranteed 1,5 by the Linux kernel
assert.Equal(t, spec.Linux.Resources.BlockIO.WeightDevice[0].Major, int64(1))
assert.Equal(t, spec.Linux.Resources.BlockIO.WeightDevice[0].Minor, int64(5))
assert.Equal(t, *(spec.Linux.Resources.BlockIO.WeightDevice[0].Weight), uint16(100))
}

// TestMemorySwap verifies that the given swap memory limit is correctly set in
// the spec.
func TestMemorySwap(t *testing.T) {
// The default configuration of podman enables seccomp, which is not available on non-Linux systems.
// Thus, any tests that use the default seccomp setting would fail.
// Skip the tests on non-Linux platforms rather than explicitly disable seccomp in the test and possibly affect the test result.
if runtime.GOOS != "linux" {
t.Skip("seccomp, which is enabled by default, is only supported on Linux")
}
if !sysInfo.SwapLimit {
t.Skip("running test not supported by the host system")
}

swapLimit, err := units.RAMInBytes("45m")
assert.NoError(t, err)

cc := makeTestCreateConfig()
cc.Resources.MemorySwap = swapLimit

spec, err := cc.createConfigToOCISpec(nil, nil)
assert.NoError(t, err)

assert.Equal(t, *(spec.Linux.Resources.Memory.Swap), swapLimit)
}
39 changes: 39 additions & 0 deletions pkg/spec/storage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package createconfig

import (
"reflect"
"testing"

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

func TestGetVolumeMountsOneVolume(t *testing.T) {
data := spec.Mount{
Destination: "/foobar",
Type: "bind",
Source: "/tmp",
Options: []string{"ro", "rbind", "rprivate"},
}
config := CreateConfig{
Volumes: []string{"/tmp:/foobar:ro"},
}
specMount, _, err := config.getVolumeMounts()
assert.NoError(t, err)
assert.True(t, reflect.DeepEqual(data, specMount[data.Destination]))
}

func TestGetTmpfsMounts(t *testing.T) {
data := spec.Mount{
Destination: "/homer",
Type: "tmpfs",
Source: "tmpfs",
Options: []string{"rw", "size=787448k", "mode=1777"},
}
config := CreateConfig{
Tmpfs: []string{"/homer:rw,size=787448k,mode=1777"},
}
tmpfsMount, err := config.getTmpfsMounts()
assert.NoError(t, err)
assert.True(t, reflect.DeepEqual(data, tmpfsMount[data.Destination]))
}

0 comments on commit 2e00d41

Please sign in to comment.