Update unit tests to use in-memory lock manager

Signed-off-by: Matthew Heon <matthew.heon@gmail.com>
This commit is contained in:
Matthew Heon
2018-09-23 14:02:36 -04:00
committed by Matthew Heon
parent 35361595f3
commit 625c7e18ef
5 changed files with 573 additions and 549 deletions

View File

@ -3,20 +3,19 @@ package libpod
import (
"encoding/json"
"net"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
"github.com/containers/storage"
"github.com/containers/libpod/libpod/lock"
"github.com/cri-o/ocicni/pkg/ocicni"
"github.com/opencontainers/runtime-tools/generate"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func getTestContainer(id, name, locksDir string) (*Container, error) {
func getTestContainer(id, name string, manager lock.Manager) (*Container, error) {
ctr := &Container{
config: &Config{
ID: id,
@ -90,18 +89,18 @@ func getTestContainer(id, name, locksDir string) (*Container, error) {
ctr.config.Labels["test"] = "testing"
// Must make lockfile or container will error on being retrieved from DB
lockPath := filepath.Join(locksDir, id)
lock, err := storage.GetLockfile(lockPath)
// Allocate a lock for the container
lock, err := manager.AllocateLock()
if err != nil {
return nil, err
}
ctr.lock = lock
ctr.config.LockID = lock.ID()
return ctr, nil
}
func getTestPod(id, name, locksDir string) (*Pod, error) {
func getTestPod(id, name string, manager lock.Manager) (*Pod, error) {
pod := &Pod{
config: &PodConfig{
ID: id,
@ -115,38 +114,39 @@ func getTestPod(id, name, locksDir string) (*Pod, error) {
valid: true,
}
lockPath := filepath.Join(locksDir, id)
lock, err := storage.GetLockfile(lockPath)
// Allocate a lock for the pod
lock, err := manager.AllocateLock()
if err != nil {
return nil, err
}
pod.lock = lock
pod.config.LockID = lock.ID()
return pod, nil
}
func getTestCtrN(n, lockPath string) (*Container, error) {
return getTestContainer(strings.Repeat(n, 32), "test"+n, lockPath)
func getTestCtrN(n string, manager lock.Manager) (*Container, error) {
return getTestContainer(strings.Repeat(n, 32), "test"+n, manager)
}
func getTestCtr1(lockPath string) (*Container, error) {
return getTestCtrN("1", lockPath)
func getTestCtr1(manager lock.Manager) (*Container, error) {
return getTestCtrN("1", manager)
}
func getTestCtr2(lockPath string) (*Container, error) {
return getTestCtrN("2", lockPath)
func getTestCtr2(manager lock.Manager) (*Container, error) {
return getTestCtrN("2", manager)
}
func getTestPodN(n, lockPath string) (*Pod, error) {
return getTestPod(strings.Repeat(n, 32), "test"+n, lockPath)
func getTestPodN(n string, manager lock.Manager) (*Pod, error) {
return getTestPod(strings.Repeat(n, 32), "test"+n, manager)
}
func getTestPod1(lockPath string) (*Pod, error) {
return getTestPodN("1", lockPath)
func getTestPod1(manager lock.Manager) (*Pod, error) {
return getTestPodN("1", manager)
}
func getTestPod2(lockPath string) (*Pod, error) {
return getTestPodN("2", lockPath)
func getTestPod2(manager lock.Manager) (*Pod, error) {
return getTestPodN("2", manager)
}
// This horrible hack tests if containers are equal in a way that should handle
@ -174,6 +174,8 @@ func testContainersEqual(t *testing.T, a, b *Container, allowedEmpty bool) {
assert.Equal(t, a.valid, b.valid)
assert.Equal(t, a.lock.ID(), b.lock.ID())
aConfigJSON, err := json.Marshal(a.config)
assert.NoError(t, err)
err = json.Unmarshal(aConfigJSON, aConfig)
@ -223,6 +225,8 @@ func testPodsEqual(t *testing.T, a, b *Pod, allowedEmpty bool) {
assert.Equal(t, a.valid, b.valid)
assert.Equal(t, a.lock.ID(), b.lock.ID())
assert.EqualValues(t, a.config, b.config)
if allowedEmpty {

View File

@ -1,10 +1,9 @@
package libpod
import (
"io/ioutil"
"os"
"testing"
"github.com/containers/libpod/libpod/lock"
"github.com/stretchr/testify/assert"
)
@ -17,11 +16,12 @@ func TestBuildContainerGraphNoCtrsIsEmpty(t *testing.T) {
}
func TestBuildContainerGraphOneCtr(t *testing.T) {
tmpDir, err := ioutil.TempDir("", tmpDirPrefix)
assert.NoError(t, err)
defer os.RemoveAll(tmpDir)
manager, err := lock.NewInMemoryManager(16)
if err != nil {
t.Fatalf("Error setting up locks: %v", err)
}
ctr1, err := getTestCtr1(tmpDir)
ctr1, err := getTestCtr1(manager)
assert.NoError(t, err)
graph, err := buildContainerGraph([]*Container{ctr1})
@ -39,13 +39,14 @@ func TestBuildContainerGraphOneCtr(t *testing.T) {
}
func TestBuildContainerGraphTwoCtrNoEdge(t *testing.T) {
tmpDir, err := ioutil.TempDir("", tmpDirPrefix)
assert.NoError(t, err)
defer os.RemoveAll(tmpDir)
manager, err := lock.NewInMemoryManager(16)
if err != nil {
t.Fatalf("Error setting up locks: %v", err)
}
ctr1, err := getTestCtr1(tmpDir)
ctr1, err := getTestCtr1(manager)
assert.NoError(t, err)
ctr2, err := getTestCtr2(tmpDir)
ctr2, err := getTestCtr2(manager)
assert.NoError(t, err)
graph, err := buildContainerGraph([]*Container{ctr1, ctr2})
@ -64,13 +65,14 @@ func TestBuildContainerGraphTwoCtrNoEdge(t *testing.T) {
}
func TestBuildContainerGraphTwoCtrOneEdge(t *testing.T) {
tmpDir, err := ioutil.TempDir("", tmpDirPrefix)
assert.NoError(t, err)
defer os.RemoveAll(tmpDir)
manager, err := lock.NewInMemoryManager(16)
if err != nil {
t.Fatalf("Error setting up locks: %v", err)
}
ctr1, err := getTestCtr1(tmpDir)
ctr1, err := getTestCtr1(manager)
assert.NoError(t, err)
ctr2, err := getTestCtr2(tmpDir)
ctr2, err := getTestCtr2(manager)
assert.NoError(t, err)
ctr2.config.UserNsCtr = ctr1.config.ID
@ -85,13 +87,14 @@ func TestBuildContainerGraphTwoCtrOneEdge(t *testing.T) {
}
func TestBuildContainerGraphTwoCtrCycle(t *testing.T) {
tmpDir, err := ioutil.TempDir("", tmpDirPrefix)
assert.NoError(t, err)
defer os.RemoveAll(tmpDir)
manager, err := lock.NewInMemoryManager(16)
if err != nil {
t.Fatalf("Error setting up locks: %v", err)
}
ctr1, err := getTestCtr1(tmpDir)
ctr1, err := getTestCtr1(manager)
assert.NoError(t, err)
ctr2, err := getTestCtr2(tmpDir)
ctr2, err := getTestCtr2(manager)
assert.NoError(t, err)
ctr2.config.UserNsCtr = ctr1.config.ID
ctr1.config.NetNsCtr = ctr2.config.ID
@ -101,15 +104,16 @@ func TestBuildContainerGraphTwoCtrCycle(t *testing.T) {
}
func TestBuildContainerGraphThreeCtrNoEdges(t *testing.T) {
tmpDir, err := ioutil.TempDir("", tmpDirPrefix)
assert.NoError(t, err)
defer os.RemoveAll(tmpDir)
manager, err := lock.NewInMemoryManager(16)
if err != nil {
t.Fatalf("Error setting up locks: %v", err)
}
ctr1, err := getTestCtr1(tmpDir)
ctr1, err := getTestCtr1(manager)
assert.NoError(t, err)
ctr2, err := getTestCtr2(tmpDir)
ctr2, err := getTestCtr2(manager)
assert.NoError(t, err)
ctr3, err := getTestCtrN("3", tmpDir)
ctr3, err := getTestCtrN("3", manager)
assert.NoError(t, err)
graph, err := buildContainerGraph([]*Container{ctr1, ctr2, ctr3})
@ -132,15 +136,16 @@ func TestBuildContainerGraphThreeCtrNoEdges(t *testing.T) {
}
func TestBuildContainerGraphThreeContainersTwoInCycle(t *testing.T) {
tmpDir, err := ioutil.TempDir("", tmpDirPrefix)
assert.NoError(t, err)
defer os.RemoveAll(tmpDir)
manager, err := lock.NewInMemoryManager(16)
if err != nil {
t.Fatalf("Error setting up locks: %v", err)
}
ctr1, err := getTestCtr1(tmpDir)
ctr1, err := getTestCtr1(manager)
assert.NoError(t, err)
ctr2, err := getTestCtr2(tmpDir)
ctr2, err := getTestCtr2(manager)
assert.NoError(t, err)
ctr3, err := getTestCtrN("3", tmpDir)
ctr3, err := getTestCtrN("3", manager)
assert.NoError(t, err)
ctr1.config.UserNsCtr = ctr2.config.ID
ctr2.config.IPCNsCtr = ctr1.config.ID
@ -150,15 +155,16 @@ func TestBuildContainerGraphThreeContainersTwoInCycle(t *testing.T) {
}
func TestBuildContainerGraphThreeContainersCycle(t *testing.T) {
tmpDir, err := ioutil.TempDir("", tmpDirPrefix)
assert.NoError(t, err)
defer os.RemoveAll(tmpDir)
manager, err := lock.NewInMemoryManager(16)
if err != nil {
t.Fatalf("Error setting up locks: %v", err)
}
ctr1, err := getTestCtr1(tmpDir)
ctr1, err := getTestCtr1(manager)
assert.NoError(t, err)
ctr2, err := getTestCtr2(tmpDir)
ctr2, err := getTestCtr2(manager)
assert.NoError(t, err)
ctr3, err := getTestCtrN("3", tmpDir)
ctr3, err := getTestCtrN("3", manager)
assert.NoError(t, err)
ctr1.config.UserNsCtr = ctr2.config.ID
ctr2.config.IPCNsCtr = ctr3.config.ID
@ -169,15 +175,16 @@ func TestBuildContainerGraphThreeContainersCycle(t *testing.T) {
}
func TestBuildContainerGraphThreeContainersNoCycle(t *testing.T) {
tmpDir, err := ioutil.TempDir("", tmpDirPrefix)
assert.NoError(t, err)
defer os.RemoveAll(tmpDir)
manager, err := lock.NewInMemoryManager(16)
if err != nil {
t.Fatalf("Error setting up locks: %v", err)
}
ctr1, err := getTestCtr1(tmpDir)
ctr1, err := getTestCtr1(manager)
assert.NoError(t, err)
ctr2, err := getTestCtr2(tmpDir)
ctr2, err := getTestCtr2(manager)
assert.NoError(t, err)
ctr3, err := getTestCtrN("3", tmpDir)
ctr3, err := getTestCtrN("3", manager)
assert.NoError(t, err)
ctr1.config.UserNsCtr = ctr2.config.ID
ctr1.config.NetNsCtr = ctr3.config.ID
@ -194,17 +201,18 @@ func TestBuildContainerGraphThreeContainersNoCycle(t *testing.T) {
}
func TestBuildContainerGraphFourContainersNoEdges(t *testing.T) {
tmpDir, err := ioutil.TempDir("", tmpDirPrefix)
assert.NoError(t, err)
defer os.RemoveAll(tmpDir)
manager, err := lock.NewInMemoryManager(16)
if err != nil {
t.Fatalf("Error setting up locks: %v", err)
}
ctr1, err := getTestCtr1(tmpDir)
ctr1, err := getTestCtr1(manager)
assert.NoError(t, err)
ctr2, err := getTestCtr2(tmpDir)
ctr2, err := getTestCtr2(manager)
assert.NoError(t, err)
ctr3, err := getTestCtrN("3", tmpDir)
ctr3, err := getTestCtrN("3", manager)
assert.NoError(t, err)
ctr4, err := getTestCtrN("4", tmpDir)
ctr4, err := getTestCtrN("4", manager)
assert.NoError(t, err)
graph, err := buildContainerGraph([]*Container{ctr1, ctr2, ctr3, ctr4})
@ -231,18 +239,20 @@ func TestBuildContainerGraphFourContainersNoEdges(t *testing.T) {
}
func TestBuildContainerGraphFourContainersTwoInCycle(t *testing.T) {
tmpDir, err := ioutil.TempDir("", tmpDirPrefix)
assert.NoError(t, err)
defer os.RemoveAll(tmpDir)
manager, err := lock.NewInMemoryManager(16)
if err != nil {
t.Fatalf("Error setting up locks: %v", err)
}
ctr1, err := getTestCtr1(tmpDir)
ctr1, err := getTestCtr1(manager)
assert.NoError(t, err)
ctr2, err := getTestCtr2(tmpDir)
ctr2, err := getTestCtr2(manager)
assert.NoError(t, err)
ctr3, err := getTestCtrN("3", tmpDir)
ctr3, err := getTestCtrN("3", manager)
assert.NoError(t, err)
ctr4, err := getTestCtrN("4", tmpDir)
ctr4, err := getTestCtrN("4", manager)
assert.NoError(t, err)
ctr1.config.IPCNsCtr = ctr2.config.ID
ctr2.config.UserNsCtr = ctr1.config.ID
@ -251,18 +261,20 @@ func TestBuildContainerGraphFourContainersTwoInCycle(t *testing.T) {
}
func TestBuildContainerGraphFourContainersAllInCycle(t *testing.T) {
tmpDir, err := ioutil.TempDir("", tmpDirPrefix)
assert.NoError(t, err)
defer os.RemoveAll(tmpDir)
manager, err := lock.NewInMemoryManager(16)
if err != nil {
t.Fatalf("Error setting up locks: %v", err)
}
ctr1, err := getTestCtr1(tmpDir)
ctr1, err := getTestCtr1(manager)
assert.NoError(t, err)
ctr2, err := getTestCtr2(tmpDir)
ctr2, err := getTestCtr2(manager)
assert.NoError(t, err)
ctr3, err := getTestCtrN("3", tmpDir)
ctr3, err := getTestCtrN("3", manager)
assert.NoError(t, err)
ctr4, err := getTestCtrN("4", tmpDir)
ctr4, err := getTestCtrN("4", manager)
assert.NoError(t, err)
ctr1.config.IPCNsCtr = ctr2.config.ID
ctr2.config.UserNsCtr = ctr3.config.ID
ctr3.config.NetNsCtr = ctr4.config.ID
@ -273,18 +285,20 @@ func TestBuildContainerGraphFourContainersAllInCycle(t *testing.T) {
}
func TestBuildContainerGraphFourContainersNoneInCycle(t *testing.T) {
tmpDir, err := ioutil.TempDir("", tmpDirPrefix)
assert.NoError(t, err)
defer os.RemoveAll(tmpDir)
manager, err := lock.NewInMemoryManager(16)
if err != nil {
t.Fatalf("Error setting up locks: %v", err)
}
ctr1, err := getTestCtr1(tmpDir)
ctr1, err := getTestCtr1(manager)
assert.NoError(t, err)
ctr2, err := getTestCtr2(tmpDir)
ctr2, err := getTestCtr2(manager)
assert.NoError(t, err)
ctr3, err := getTestCtrN("3", tmpDir)
ctr3, err := getTestCtrN("3", manager)
assert.NoError(t, err)
ctr4, err := getTestCtrN("4", tmpDir)
ctr4, err := getTestCtrN("4", manager)
assert.NoError(t, err)
ctr1.config.IPCNsCtr = ctr2.config.ID
ctr1.config.NetNsCtr = ctr3.config.ID
ctr2.config.UserNsCtr = ctr3.config.ID

View File

@ -84,7 +84,7 @@ func (m *InMemoryManager) AllocateLock() (Locker, error) {
// RetrieveLock retrieves a lock from the manager.
func (m *InMemoryManager) RetrieveLock(id uint32) (Locker, error) {
if id >= m.numLocks {
return nil, errors.Errorf("given lock ID %d is too large - this manager only supports lock indexes up to %d", id, m.numLocks - 1)
return nil, errors.Errorf("given lock ID %d is too large - this manager only supports lock indexes up to %d", id, m.numLocks-1)
}
return m.locks[id], nil

View File

@ -17,8 +17,10 @@ import (
// to test without actually having multiple processes...
// We can at least verify that the locks work within the local process.
// 4 * BITMAP_SIZE to ensure we have to traverse bitmaps
var numLocks uint32 = 4 * BitmapSize
var (
// 4 * BITMAP_SIZE to ensure we have to traverse bitmaps
numLocks = 4 * BitmapSize
)
const lockPath = "/libpod_test"

File diff suppressed because it is too large Load Diff