Fix unit tests and provide more realistic test environment

Signed-off-by: Matthew Heon <matthew.heon@gmail.com>
This commit is contained in:
Matthew Heon 2017-11-27 14:07:41 -05:00
parent e9298a533a
commit 43c51c58ad
1 changed files with 54 additions and 53 deletions

View File

@ -97,10 +97,10 @@ func testContainersEqual(a, b *Container) bool {
// Get an empty state for use in tests
// An empty Runtime is provided
func getEmptyState() (s State, p string, err error) {
func getEmptyState() (s State, p string, p2 string, err error) {
tmpDir, err := ioutil.TempDir("", "libpod_state_test_")
if err != nil {
return nil, "", err
return nil, "", "", err
}
defer func() {
if err != nil {
@ -109,27 +109,28 @@ func getEmptyState() (s State, p string, err error) {
}()
dbPath := filepath.Join(tmpDir, "db.sql")
lockPath := filepath.Join(tmpDir, "db.lck")
specsDir := filepath.Join(tmpDir, "specs")
lockDir := filepath.Join(tmpDir, "locks")
runtime := new(Runtime)
runtime.config = new(RuntimeConfig)
runtime.config.StorageConfig = storage.StoreOptions{}
state, err := NewSQLState(dbPath, lockPath, tmpDir, tmpDir, runtime)
state, err := NewSQLState(dbPath, specsDir, lockDir, runtime)
if err != nil {
return nil, "", err
return nil, "", "", err
}
return state, tmpDir, nil
return state, tmpDir, lockDir, nil
}
func TestAddAndGetContainer(t *testing.T) {
state, path, err := getEmptyState()
state, path, lockPath, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
testCtr, err := getTestContainer("0123456789ABCDEF0123456789ABCDEF", "test", path)
testCtr, err := getTestContainer("0123456789ABCDEF0123456789ABCDEF", "test", lockPath)
assert.NoError(t, err)
err = state.AddContainer(testCtr)
@ -146,14 +147,14 @@ func TestAddAndGetContainer(t *testing.T) {
}
func TestAddAndGetContainerFromMultiple(t *testing.T) {
state, path, err := getEmptyState()
state, path, lockPath, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
testCtr1, err := getTestContainer("11111111111111111111111111111111", "test1", path)
testCtr1, err := getTestContainer("11111111111111111111111111111111", "test1", lockPath)
assert.NoError(t, err)
testCtr2, err := getTestContainer("22222222222222222222222222222222", "test2", path)
testCtr2, err := getTestContainer("22222222222222222222222222222222", "test2", lockPath)
assert.NoError(t, err)
err = state.AddContainer(testCtr1)
@ -173,7 +174,7 @@ func TestAddAndGetContainerFromMultiple(t *testing.T) {
}
func TestAddInvalidContainerFails(t *testing.T) {
state, path, err := getEmptyState()
state, path, _, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
@ -183,14 +184,14 @@ func TestAddInvalidContainerFails(t *testing.T) {
}
func TestAddDuplicateIDFails(t *testing.T) {
state, path, err := getEmptyState()
state, path, lockPath, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
testCtr1, err := getTestContainer("11111111111111111111111111111111", "test1", path)
testCtr1, err := getTestContainer("11111111111111111111111111111111", "test1", lockPath)
assert.NoError(t, err)
testCtr2, err := getTestContainer(testCtr1.ID(), "test2", path)
testCtr2, err := getTestContainer(testCtr1.ID(), "test2", lockPath)
assert.NoError(t, err)
err = state.AddContainer(testCtr1)
@ -201,14 +202,14 @@ func TestAddDuplicateIDFails(t *testing.T) {
}
func TestAddDuplicateNameFails(t *testing.T) {
state, path, err := getEmptyState()
state, path, lockPath, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
testCtr1, err := getTestContainer("11111111111111111111111111111111", "test1", path)
testCtr1, err := getTestContainer("11111111111111111111111111111111", "test1", lockPath)
assert.NoError(t, err)
testCtr2, err := getTestContainer("22222222222222222222222222222222", testCtr1.Name(), path)
testCtr2, err := getTestContainer("22222222222222222222222222222222", testCtr1.Name(), lockPath)
assert.NoError(t, err)
err = state.AddContainer(testCtr1)
@ -219,7 +220,7 @@ func TestAddDuplicateNameFails(t *testing.T) {
}
func TestGetNonexistantContainerFails(t *testing.T) {
state, path, err := getEmptyState()
state, path, _, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
@ -229,7 +230,7 @@ func TestGetNonexistantContainerFails(t *testing.T) {
}
func TestGetContainerWithEmptyIDFails(t *testing.T) {
state, path, err := getEmptyState()
state, path, _, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
@ -239,7 +240,7 @@ func TestGetContainerWithEmptyIDFails(t *testing.T) {
}
func TestLookupContainerWithEmptyIDFails(t *testing.T) {
state, path, err := getEmptyState()
state, path, _, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
@ -249,7 +250,7 @@ func TestLookupContainerWithEmptyIDFails(t *testing.T) {
}
func TestLookupNonexistantContainerFails(t *testing.T) {
state, path, err := getEmptyState()
state, path, _, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
@ -258,12 +259,12 @@ func TestLookupNonexistantContainerFails(t *testing.T) {
}
func TestLookupContainerByFullID(t *testing.T) {
state, path, err := getEmptyState()
state, path, lockPath, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
testCtr, err := getTestContainer("0123456789ABCDEF0123456789ABCDEF", "test", path)
testCtr, err := getTestContainer("0123456789ABCDEF0123456789ABCDEF", "test", lockPath)
assert.NoError(t, err)
err = state.AddContainer(testCtr)
@ -280,12 +281,12 @@ func TestLookupContainerByFullID(t *testing.T) {
}
func TestLookupContainerByUniquePartialID(t *testing.T) {
state, path, err := getEmptyState()
state, path, lockPath, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
testCtr, err := getTestContainer("0123456789ABCDEF0123456789ABCDEF", "test", path)
testCtr, err := getTestContainer("0123456789ABCDEF0123456789ABCDEF", "test", lockPath)
assert.NoError(t, err)
err = state.AddContainer(testCtr)
@ -302,14 +303,14 @@ func TestLookupContainerByUniquePartialID(t *testing.T) {
}
func TestLookupContainerByNonUniquePartialIDFails(t *testing.T) {
state, path, err := getEmptyState()
state, path, lockPath, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
testCtr1, err := getTestContainer("00000000000000000000000000000000", "test1", path)
testCtr1, err := getTestContainer("00000000000000000000000000000000", "test1", lockPath)
assert.NoError(t, err)
testCtr2, err := getTestContainer("00000000000000000000000000000001", "test2", path)
testCtr2, err := getTestContainer("00000000000000000000000000000001", "test2", lockPath)
assert.NoError(t, err)
err = state.AddContainer(testCtr1)
@ -323,12 +324,12 @@ func TestLookupContainerByNonUniquePartialIDFails(t *testing.T) {
}
func TestLookupContainerByName(t *testing.T) {
state, path, err := getEmptyState()
state, path, lockPath, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
testCtr, err := getTestContainer("0123456789ABCDEF0123456789ABCDEF", "test", path)
testCtr, err := getTestContainer("0123456789ABCDEF0123456789ABCDEF", "test", lockPath)
assert.NoError(t, err)
err = state.AddContainer(testCtr)
@ -345,7 +346,7 @@ func TestLookupContainerByName(t *testing.T) {
}
func TestHasContainerEmptyIDFails(t *testing.T) {
state, path, err := getEmptyState()
state, path, _, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
@ -355,7 +356,7 @@ func TestHasContainerEmptyIDFails(t *testing.T) {
}
func TestHasContainerNoSuchContainerReturnsFalse(t *testing.T) {
state, path, err := getEmptyState()
state, path, _, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
@ -366,12 +367,12 @@ func TestHasContainerNoSuchContainerReturnsFalse(t *testing.T) {
}
func TestHasContainerFindsContainer(t *testing.T) {
state, path, err := getEmptyState()
state, path, lockPath, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
testCtr, err := getTestContainer("0123456789ABCDEF0123456789ABCDEF", "test", path)
testCtr, err := getTestContainer("0123456789ABCDEF0123456789ABCDEF", "test", lockPath)
assert.NoError(t, err)
err = state.AddContainer(testCtr)
@ -383,12 +384,12 @@ func TestHasContainerFindsContainer(t *testing.T) {
}
func TestSaveAndUpdateContainer(t *testing.T) {
state, path, err := getEmptyState()
state, path, lockPath, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
testCtr, err := getTestContainer("0123456789ABCDEF0123456789ABCDEF", "test", path)
testCtr, err := getTestContainer("0123456789ABCDEF0123456789ABCDEF", "test", lockPath)
assert.NoError(t, err)
err = state.AddContainer(testCtr)
@ -415,12 +416,12 @@ func TestSaveAndUpdateContainer(t *testing.T) {
}
func TestUpdateContainerNotInDatabaseReturnsError(t *testing.T) {
state, path, err := getEmptyState()
state, path, lockPath, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
testCtr, err := getTestContainer("0123456789ABCDEF0123456789ABCDEF", "test", path)
testCtr, err := getTestContainer("0123456789ABCDEF0123456789ABCDEF", "test", lockPath)
assert.NoError(t, err)
err = state.UpdateContainer(testCtr)
@ -429,7 +430,7 @@ func TestUpdateContainerNotInDatabaseReturnsError(t *testing.T) {
}
func TestUpdateInvalidContainerReturnsError(t *testing.T) {
state, path, err := getEmptyState()
state, path, _, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
@ -439,7 +440,7 @@ func TestUpdateInvalidContainerReturnsError(t *testing.T) {
}
func TestSaveInvalidContainerReturnsError(t *testing.T) {
state, path, err := getEmptyState()
state, path, _, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
@ -449,12 +450,12 @@ func TestSaveInvalidContainerReturnsError(t *testing.T) {
}
func TestSaveContainerNotInStateReturnsError(t *testing.T) {
state, path, err := getEmptyState()
state, path, lockPath, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
testCtr, err := getTestContainer("0123456789ABCDEF0123456789ABCDEF", "test", path)
testCtr, err := getTestContainer("0123456789ABCDEF0123456789ABCDEF", "test", lockPath)
assert.NoError(t, err)
err = state.SaveContainer(testCtr)
@ -462,12 +463,12 @@ func TestSaveContainerNotInStateReturnsError(t *testing.T) {
}
func TestRemoveContainer(t *testing.T) {
state, path, err := getEmptyState()
state, path, lockPath, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
testCtr, err := getTestContainer("0123456789ABCDEF0123456789ABCDEF", "test", path)
testCtr, err := getTestContainer("0123456789ABCDEF0123456789ABCDEF", "test", lockPath)
assert.NoError(t, err)
err = state.AddContainer(testCtr)
@ -486,12 +487,12 @@ func TestRemoveContainer(t *testing.T) {
}
func TestRemoveNonexistantContainerFails(t *testing.T) {
state, path, err := getEmptyState()
state, path, lockPath, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
testCtr, err := getTestContainer("0123456789ABCDEF0123456789ABCDEF", "test", path)
testCtr, err := getTestContainer("0123456789ABCDEF0123456789ABCDEF", "test", lockPath)
assert.NoError(t, err)
err = state.RemoveContainer(testCtr)
@ -499,7 +500,7 @@ func TestRemoveNonexistantContainerFails(t *testing.T) {
}
func TestGetAllContainersOnNewStateIsEmpty(t *testing.T) {
state, path, err := getEmptyState()
state, path, _, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
@ -510,12 +511,12 @@ func TestGetAllContainersOnNewStateIsEmpty(t *testing.T) {
}
func TestGetAllContainersWithOneContainer(t *testing.T) {
state, path, err := getEmptyState()
state, path, lockPath, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
testCtr, err := getTestContainer("0123456789ABCDEF0123456789ABCDEF", "test", path)
testCtr, err := getTestContainer("0123456789ABCDEF0123456789ABCDEF", "test", lockPath)
assert.NoError(t, err)
err = state.AddContainer(testCtr)
@ -533,14 +534,14 @@ func TestGetAllContainersWithOneContainer(t *testing.T) {
}
func TestGetAllContainersTwoContainers(t *testing.T) {
state, path, err := getEmptyState()
state, path, lockPath, err := getEmptyState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
testCtr1, err := getTestContainer("11111111111111111111111111111111", "test1", path)
testCtr1, err := getTestContainer("11111111111111111111111111111111", "test1", lockPath)
assert.NoError(t, err)
testCtr2, err := getTestContainer("22222222222222222222222222222222", "test2", path)
testCtr2, err := getTestContainer("22222222222222222222222222222222", "test2", lockPath)
assert.NoError(t, err)
err = state.AddContainer(testCtr1)