Tests are working again
This commit is contained in:
@@ -48,27 +48,12 @@ func (store *storage) SessionPut(s *types.Session) error {
|
||||
return store.save()
|
||||
}
|
||||
|
||||
func (store *storage) InstanceFindByIP(ip string) (*types.Instance, error) {
|
||||
store.rw.Lock()
|
||||
defer store.rw.Unlock()
|
||||
|
||||
for _, s := range store.db {
|
||||
for _, i := range s.Instances {
|
||||
if i.IP == ip {
|
||||
return i, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("%s", notFound)
|
||||
}
|
||||
|
||||
func (store *storage) InstanceFindByIPAndSession(sessionPrefix, ip string) (*types.Instance, error) {
|
||||
func (store *storage) InstanceFind(sessionId, ip string) (*types.Instance, error) {
|
||||
store.rw.Lock()
|
||||
defer store.rw.Unlock()
|
||||
|
||||
for id, s := range store.db {
|
||||
if strings.HasPrefix(id, sessionPrefix) {
|
||||
if strings.HasPrefix(id, sessionId[:8]) {
|
||||
for _, i := range s.Instances {
|
||||
if i.IP == ip {
|
||||
return i, nil
|
||||
@@ -80,23 +65,6 @@ func (store *storage) InstanceFindByIPAndSession(sessionPrefix, ip string) (*typ
|
||||
return nil, fmt.Errorf("%s", notFound)
|
||||
}
|
||||
|
||||
func (store *storage) InstanceFindByAlias(sessionPrefix, alias string) (*types.Instance, error) {
|
||||
store.rw.Lock()
|
||||
defer store.rw.Unlock()
|
||||
|
||||
for id, s := range store.db {
|
||||
if strings.HasPrefix(id, sessionPrefix) {
|
||||
for _, i := range s.Instances {
|
||||
if i.Alias == alias {
|
||||
return i, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("%s", notFound)
|
||||
}
|
||||
|
||||
func (store *storage) InstanceCreate(sessionId string, instance *types.Instance) 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 TestInstanceFindByIP(t *testing.T) {
|
||||
func TestInstanceFind(t *testing.T) {
|
||||
tmpfile, err := ioutil.TempFile("", "pwd")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -124,97 +124,23 @@ func TestInstanceFindByIP(t *testing.T) {
|
||||
err = storage.SessionPut(s2)
|
||||
assert.Nil(t, err)
|
||||
|
||||
foundInstance, err := storage.InstanceFindByIP("10.0.0.1")
|
||||
foundInstance, err := storage.InstanceFind("session1", "10.0.0.1")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, i1, foundInstance)
|
||||
|
||||
foundInstance, err = storage.InstanceFindByIP("10.1.0.1")
|
||||
foundInstance, err = storage.InstanceFind("session2", "10.1.0.1")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, i2, foundInstance)
|
||||
|
||||
foundInstance, err = storage.InstanceFindByIP("192.168.0.1")
|
||||
assert.True(t, NotFound(err))
|
||||
assert.Nil(t, foundInstance)
|
||||
}
|
||||
|
||||
func TestInstanceFindByIPAndSession(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"}
|
||||
i2 := &types.Instance{Name: "i2", IP: "10.1.0.1"}
|
||||
s1 := &types.Session{Id: "session1", Instances: map[string]*types.Instance{"i1": i1}}
|
||||
s2 := &types.Session{Id: "session2", Instances: map[string]*types.Instance{"i2": i2}}
|
||||
err = storage.SessionPut(s1)
|
||||
assert.Nil(t, err)
|
||||
err = storage.SessionPut(s2)
|
||||
assert.Nil(t, err)
|
||||
|
||||
foundInstance, err := storage.InstanceFindByIPAndSession("session1", "10.0.0.1")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, i1, foundInstance)
|
||||
|
||||
foundInstance, err = storage.InstanceFindByIPAndSession("session2", "10.1.0.1")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, i2, foundInstance)
|
||||
|
||||
foundInstance, err = storage.InstanceFindByIPAndSession("session3", "10.1.0.1")
|
||||
foundInstance, err = storage.InstanceFind("session3", "10.1.0.1")
|
||||
assert.True(t, NotFound(err))
|
||||
assert.Nil(t, foundInstance)
|
||||
|
||||
foundInstance, err = storage.InstanceFindByIPAndSession("session1", "10.1.0.1")
|
||||
foundInstance, err = storage.InstanceFind("session1", "10.1.0.1")
|
||||
assert.True(t, NotFound(err))
|
||||
assert.Nil(t, foundInstance)
|
||||
|
||||
foundInstance, err = storage.InstanceFindByIPAndSession("session1", "192.168.0.1")
|
||||
assert.True(t, NotFound(err))
|
||||
assert.Nil(t, foundInstance)
|
||||
}
|
||||
|
||||
func TestInstanceFindByAlias(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", Alias: "foo", IP: "10.0.0.1"}
|
||||
i2 := &types.Instance{Name: "i2", Alias: "foo", IP: "10.1.0.1"}
|
||||
s1 := &types.Session{Id: "session1", Instances: map[string]*types.Instance{"i1": i1}}
|
||||
s2 := &types.Session{Id: "session2", Instances: map[string]*types.Instance{"i2": i2}}
|
||||
err = storage.SessionPut(s1)
|
||||
assert.Nil(t, err)
|
||||
err = storage.SessionPut(s2)
|
||||
assert.Nil(t, err)
|
||||
|
||||
foundInstance, err := storage.InstanceFindByAlias("session1", "foo")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, i1, foundInstance)
|
||||
|
||||
foundInstance, err = storage.InstanceFindByAlias("session2", "foo")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, i2, foundInstance)
|
||||
|
||||
foundInstance, err = storage.InstanceFindByAlias("session1", "bar")
|
||||
assert.True(t, NotFound(err))
|
||||
assert.Nil(t, foundInstance)
|
||||
|
||||
foundInstance, err = storage.InstanceFindByAlias("session3", "foo")
|
||||
foundInstance, err = storage.InstanceFind("session1", "192.168.0.1")
|
||||
assert.True(t, NotFound(err))
|
||||
assert.Nil(t, foundInstance)
|
||||
}
|
||||
@@ -232,7 +158,7 @@ func TestInstanceCreate(t *testing.T) {
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
i1 := &types.Instance{Name: "i1", Alias: "foo", IP: "10.0.0.1"}
|
||||
i1 := &types.Instance{Name: "i1", IP: "10.0.0.1"}
|
||||
s1 := &types.Session{Id: "session1"}
|
||||
err = storage.SessionPut(s1)
|
||||
assert.Nil(t, err)
|
||||
@@ -260,8 +186,8 @@ func TestCounts(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
|
||||
c1 := &types.Client{}
|
||||
i1 := &types.Instance{Name: "i1", Alias: "foo", IP: "10.0.0.1"}
|
||||
i2 := &types.Instance{Name: "i2", Alias: "foo", IP: "10.1.0.1"}
|
||||
i1 := &types.Instance{Name: "i1", IP: "10.0.0.1"}
|
||||
i2 := &types.Instance{Name: "i2", IP: "10.1.0.1"}
|
||||
s1 := &types.Session{Id: "session1", Instances: map[string]*types.Instance{"i1": i1}}
|
||||
s2 := &types.Session{Id: "session2", Instances: map[string]*types.Instance{"i2": i2}}
|
||||
s3 := &types.Session{Id: "session3", Clients: []*types.Client{c1}}
|
||||
|
||||
55
storage/mock.go
Normal file
55
storage/mock.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"github.com/play-with-docker/play-with-docker/pwd/types"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type Mock struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *Mock) SessionGet(sessionId string) (*types.Session, error) {
|
||||
args := m.Called(sessionId)
|
||||
return args.Get(0).(*types.Session), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *Mock) SessionPut(session *types.Session) error {
|
||||
args := m.Called(session)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func (m *Mock) SessionCount() (int, error) {
|
||||
args := m.Called()
|
||||
return args.Int(0), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *Mock) SessionDelete(sessionId string) error {
|
||||
args := m.Called(sessionId)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func (m *Mock) SessionGetAll() (map[string]*types.Session, error) {
|
||||
args := m.Called()
|
||||
return args.Get(0).(map[string]*types.Session), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *Mock) InstanceFind(sessionId, ip string) (*types.Instance, error) {
|
||||
args := m.Called(sessionId, ip)
|
||||
return args.Get(0).(*types.Instance), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *Mock) InstanceCreate(sessionId string, instance *types.Instance) error {
|
||||
args := m.Called(sessionId, instance)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func (m *Mock) InstanceDelete(sessionId, instanceName string) error {
|
||||
args := m.Called(sessionId, instanceName)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func (m *Mock) InstanceCount() (int, error) {
|
||||
args := m.Called()
|
||||
return args.Int(0), args.Error(1)
|
||||
}
|
||||
@@ -15,10 +15,7 @@ type StorageApi interface {
|
||||
SessionDelete(string) error
|
||||
SessionGetAll() (map[string]*types.Session, error)
|
||||
|
||||
InstanceFindByAlias(sessionPrefix, alias string) (*types.Instance, error)
|
||||
// Should have the session id too, soon
|
||||
InstanceFindByIP(ip string) (*types.Instance, error)
|
||||
InstanceFindByIPAndSession(sessionPrefix, ip string) (*types.Instance, error)
|
||||
InstanceFind(session, ip string) (*types.Instance, error)
|
||||
InstanceCreate(sessionId string, instance *types.Instance) error
|
||||
InstanceDelete(sessionId, instanceName string) error
|
||||
|
||||
|
||||
Reference in New Issue
Block a user