More fixes
This commit is contained in:
@@ -48,7 +48,22 @@ func (store *storage) SessionPut(s *types.Session) error {
|
||||
return store.save()
|
||||
}
|
||||
|
||||
func (store *storage) InstanceFind(sessionId, ip string) (*types.Instance, error) {
|
||||
func (store *storage) InstanceGet(sessionId, name string) (*types.Instance, error) {
|
||||
store.rw.Lock()
|
||||
defer store.rw.Unlock()
|
||||
|
||||
s := store.db[sessionId]
|
||||
if s == nil {
|
||||
return nil, fmt.Errorf("%s", notFound)
|
||||
}
|
||||
i := s.Instances[name]
|
||||
if i == nil {
|
||||
return nil, fmt.Errorf("%s", notFound)
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
|
||||
func (store *storage) InstanceFindByIP(sessionId, ip string) (*types.Instance, error) {
|
||||
store.rw.Lock()
|
||||
defer store.rw.Unlock()
|
||||
|
||||
@@ -116,19 +131,6 @@ func (store *storage) InstanceCount() (int, error) {
|
||||
return ins, nil
|
||||
}
|
||||
|
||||
func (store *storage) ClientCount() (int, error) {
|
||||
store.rw.Lock()
|
||||
defer store.rw.Unlock()
|
||||
|
||||
var cli int
|
||||
|
||||
for _, s := range store.db {
|
||||
cli += len(s.Clients)
|
||||
}
|
||||
|
||||
return cli, nil
|
||||
}
|
||||
|
||||
func (store *storage) SessionDelete(sessionId string) error {
|
||||
store.rw.Lock()
|
||||
defer store.rw.Unlock()
|
||||
|
||||
@@ -102,7 +102,7 @@ func TestSessionGetAll(t *testing.T) {
|
||||
assert.Equal(t, s2, loadedSessions[s2.Id])
|
||||
}
|
||||
|
||||
func TestInstanceFind(t *testing.T) {
|
||||
func TestInstanceFindByIP(t *testing.T) {
|
||||
tmpfile, err := ioutil.TempFile("", "pwd")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -124,27 +124,50 @@ func TestInstanceFind(t *testing.T) {
|
||||
err = storage.SessionPut(s2)
|
||||
assert.Nil(t, err)
|
||||
|
||||
foundInstance, err := storage.InstanceFind("session1", "10.0.0.1")
|
||||
foundInstance, err := storage.InstanceFindByIP("session1", "10.0.0.1")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, i1, foundInstance)
|
||||
|
||||
foundInstance, err = storage.InstanceFind("session2", "10.1.0.1")
|
||||
foundInstance, err = storage.InstanceFindByIP("session2", "10.1.0.1")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, i2, foundInstance)
|
||||
|
||||
foundInstance, err = storage.InstanceFind("session3", "10.1.0.1")
|
||||
foundInstance, err = storage.InstanceFindByIP("session3", "10.1.0.1")
|
||||
assert.True(t, NotFound(err))
|
||||
assert.Nil(t, foundInstance)
|
||||
|
||||
foundInstance, err = storage.InstanceFind("session1", "10.1.0.1")
|
||||
foundInstance, err = storage.InstanceFindByIP("session1", "10.1.0.1")
|
||||
assert.True(t, NotFound(err))
|
||||
assert.Nil(t, foundInstance)
|
||||
|
||||
foundInstance, err = storage.InstanceFind("session1", "192.168.0.1")
|
||||
foundInstance, err = storage.InstanceFindByIP("session1", "192.168.0.1")
|
||||
assert.True(t, NotFound(err))
|
||||
assert.Nil(t, foundInstance)
|
||||
}
|
||||
|
||||
func TestInstanceGet(t *testing.T) {
|
||||
tmpfile, err := ioutil.TempFile("", "pwd")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
tmpfile.Close()
|
||||
os.Remove(tmpfile.Name())
|
||||
defer os.Remove(tmpfile.Name())
|
||||
|
||||
storage, err := NewFileStorage(tmpfile.Name())
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
i1 := &types.Instance{Name: "i1", IP: "10.0.0.1"}
|
||||
s1 := &types.Session{Id: "session1", Instances: map[string]*types.Instance{"i1": i1}}
|
||||
err = storage.SessionPut(s1)
|
||||
assert.Nil(t, err)
|
||||
|
||||
foundInstance, err := storage.InstanceGet("session1", "i1")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, i1, foundInstance)
|
||||
}
|
||||
|
||||
func TestInstanceCreate(t *testing.T) {
|
||||
tmpfile, err := ioutil.TempFile("", "pwd")
|
||||
if err != nil {
|
||||
|
||||
@@ -34,7 +34,12 @@ func (m *Mock) SessionGetAll() (map[string]*types.Session, error) {
|
||||
return args.Get(0).(map[string]*types.Session), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *Mock) InstanceFind(sessionId, ip string) (*types.Instance, error) {
|
||||
func (m *Mock) InstanceGet(sessionId, name string) (*types.Instance, error) {
|
||||
args := m.Called(sessionId, name)
|
||||
return args.Get(0).(*types.Instance), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *Mock) InstanceFindByIP(sessionId, ip string) (*types.Instance, error) {
|
||||
args := m.Called(sessionId, ip)
|
||||
return args.Get(0).(*types.Instance), args.Error(1)
|
||||
}
|
||||
|
||||
@@ -15,9 +15,9 @@ type StorageApi interface {
|
||||
SessionDelete(string) error
|
||||
SessionGetAll() (map[string]*types.Session, error)
|
||||
|
||||
InstanceFind(session, ip string) (*types.Instance, error)
|
||||
InstanceGet(sessionId, name string) (*types.Instance, error)
|
||||
InstanceFindByIP(session, ip string) (*types.Instance, error)
|
||||
InstanceCreate(sessionId string, instance *types.Instance) error
|
||||
InstanceDelete(sessionId, instanceName string) error
|
||||
|
||||
InstanceCount() (int, error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user