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

@@ -27,6 +27,14 @@ func (store *storage) SessionGet(sessionId string) (*types.Session, error) {
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 {
store.rw.Lock()
defer store.rw.Unlock()

View File

@@ -74,6 +74,34 @@ func TestSessionGet(t *testing.T) {
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) {
tmpfile, err := ioutil.TempFile("", "pwd")
if err != nil {

View File

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