Add tests for session manager

parent aeed9379
package sessionfile
import "testing"
const testFolderPrefix = "container-inventory-test"
func assertNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatalf("Unexpected error: %s", err.Error())
}
}
func assertNotNil(t *testing.T, v interface{}, msg string) {
t.Helper()
if v == nil {
t.Fatal(msg)
}
}
func assertSessionExists(t *testing.T, m *Manager, host, name string) {
t.Helper()
res, err := m.Exists(host, name)
assertNoError(t, err)
if !res {
t.Fatalf("Session %s/%s should have existed by now.", host, name)
}
}
package sessionfile
import (
"io/ioutil"
"os"
"testing"
"github.com/pkg/errors"
)
func TestSessionCreation(t *testing.T) {
dir, err := ioutil.TempDir(os.TempDir(), testFolderPrefix)
if err != nil {
t.Fatalf("Failed to create directory: %s", err.Error())
}
defer os.RemoveAll(dir)
m := NewManager(dir)
t.Run("Exists for missing", func(t *testing.T) {
result, err := m.Exists("host.com", "default")
assertNoError(t, err)
if result {
t.Fatal("Expected false, got true")
}
})
t.Run("Create new session", func(t *testing.T) {
s, err := m.CreateSession("custom", "host.com", "username", "password")
assertNoError(t, err)
assertNotNil(t, s, "Session object is nil")
assertSessionExists(t, m, "host.com", "custom")
})
t.Run("Get missing session", func(t *testing.T) {
s, err := m.GetSession("host.com", "doesnot-exist")
if !os.IsNotExist(errors.Cause(err)) {
t.Fatalf("Unexpected error returned: %s. Would have expected os.ErrNotExist as cause.", err)
}
if s != nil {
t.Fatalf("Would have expected nil-session, got %s instead.", s)
}
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment