Add sessionGetAll to StorageAPI

This commit is contained in:
Marcos Lilljedahl
2017-07-18 18:17:42 -03:00
parent 56e2899dcf
commit 9293a4f8d0
4 changed files with 46 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ import "github.com/play-with-docker/play-with-docker/pwd/types"
type mockStorage struct { type mockStorage struct {
sessionGet func(sessionId string) (*types.Session, error) sessionGet func(sessionId string) (*types.Session, error)
sessionGetAll func() (map[string]*types.Session, error)
sessionPut func(s *types.Session) error sessionPut func(s *types.Session) error
sessionCount func() (int, error) sessionCount func() (int, error)
sessionDelete func(sessionId string) error sessionDelete func(sessionId string) error
@@ -22,6 +23,14 @@ func (m *mockStorage) SessionGet(sessionId string) (*types.Session, error) {
} }
return nil, nil return nil, nil
} }
func (m *mockStorage) SessionGetAll() (map[string]*types.Session, error) {
if m.sessionGetAll != nil {
return m.sessionGetAll()
}
return nil, nil
}
func (m *mockStorage) SessionPut(s *types.Session) error { func (m *mockStorage) SessionPut(s *types.Session) error {
if m.sessionPut != nil { if m.sessionPut != nil {
return m.sessionPut(s) return m.sessionPut(s)

View File

@@ -27,6 +27,14 @@ func (store *storage) SessionGet(sessionId string) (*types.Session, error) {
return s, nil return s, nil
} }
func (store *storage) SessionGetAll() (map[string]*types.Session, error) {
store.rw.Lock()
defer store.rw.Unlock()
return store.db, nil
}
func (store *storage) SessionPut(s *types.Session) error { func (store *storage) SessionPut(s *types.Session) error {
store.rw.Lock() store.rw.Lock()
defer store.rw.Unlock() defer store.rw.Unlock()

View File

@@ -74,6 +74,34 @@ func TestSessionGet(t *testing.T) {
assert.Equal(t, expectedSession, loadedSession) assert.Equal(t, expectedSession, loadedSession)
} }
func TestSessionGetAll(t *testing.T) {
s1 := &types.Session{Id: "session1"}
s2 := &types.Session{Id: "session2"}
sessions := map[string]*types.Session{}
sessions[s1.Id] = s1
sessions[s2.Id] = s2
tmpfile, err := ioutil.TempFile("", "pwd")
if err != nil {
log.Fatal(err)
}
encoder := json.NewEncoder(tmpfile)
err = encoder.Encode(&sessions)
assert.Nil(t, err)
tmpfile.Close()
defer os.Remove(tmpfile.Name())
storage, err := NewFileStorage(tmpfile.Name())
assert.Nil(t, err)
loadedSessions, err := storage.SessionGetAll()
assert.Nil(t, err)
assert.Equal(t, s1, loadedSessions[s1.Id])
assert.Equal(t, s2, loadedSessions[s2.Id])
}
func TestInstanceFindByIP(t *testing.T) { func TestInstanceFindByIP(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "pwd") tmpfile, err := ioutil.TempFile("", "pwd")
if err != nil { if err != nil {

View File

@@ -13,6 +13,7 @@ type StorageApi interface {
SessionPut(*types.Session) error SessionPut(*types.Session) error
SessionCount() (int, error) SessionCount() (int, error)
SessionDelete(string) error SessionDelete(string) error
SessionGetAll() (map[string]*types.Session, error)
InstanceFindByAlias(sessionPrefix, alias string) (*types.Instance, error) InstanceFindByAlias(sessionPrefix, alias string) (*types.Instance, error)
// Should have the session id too, soon // Should have the session id too, soon