Add first version of the windows ASG provider
This commit is contained in:
@@ -48,6 +48,19 @@ func (store *storage) SessionPut(s *types.Session) error {
|
||||
return store.save()
|
||||
}
|
||||
|
||||
func (store *storage) InstanceGetAllWindows() ([]*types.WindowsInstance, error) {
|
||||
store.rw.Lock()
|
||||
defer store.rw.Unlock()
|
||||
|
||||
instances := []*types.WindowsInstance{}
|
||||
|
||||
for _, s := range store.db {
|
||||
instances = append(instances, s.WindowsAssigned...)
|
||||
}
|
||||
|
||||
return instances, nil
|
||||
}
|
||||
|
||||
func (store *storage) InstanceGet(sessionId, name string) (*types.Instance, error) {
|
||||
store.rw.Lock()
|
||||
defer store.rw.Unlock()
|
||||
@@ -94,6 +107,20 @@ func (store *storage) InstanceCreate(sessionId string, instance *types.Instance)
|
||||
return store.save()
|
||||
}
|
||||
|
||||
func (store *storage) InstanceCreateWindows(instance *types.WindowsInstance) error {
|
||||
store.rw.Lock()
|
||||
defer store.rw.Unlock()
|
||||
|
||||
s, found := store.db[instance.SessionId]
|
||||
if !found {
|
||||
return fmt.Errorf("Session %s", notFound)
|
||||
}
|
||||
|
||||
s.WindowsAssigned = append(s.WindowsAssigned, instance)
|
||||
|
||||
return store.save()
|
||||
}
|
||||
|
||||
func (store *storage) InstanceDelete(sessionId, name string) error {
|
||||
store.rw.Lock()
|
||||
defer store.rw.Unlock()
|
||||
@@ -111,6 +138,25 @@ func (store *storage) InstanceDelete(sessionId, name string) error {
|
||||
return store.save()
|
||||
}
|
||||
|
||||
func (store *storage) InstanceDeleteWindows(sessionId, id string) error {
|
||||
store.rw.Lock()
|
||||
defer store.rw.Unlock()
|
||||
|
||||
s, found := store.db[sessionId]
|
||||
if !found {
|
||||
return fmt.Errorf("Session %s", notFound)
|
||||
}
|
||||
|
||||
for i, winst := range s.WindowsAssigned {
|
||||
if winst.ID == id {
|
||||
s.WindowsAssigned = append(s.WindowsAssigned[:i], s.WindowsAssigned[i+1:]...)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return store.save()
|
||||
}
|
||||
|
||||
func (store *storage) SessionCount() (int, error) {
|
||||
store.rw.Lock()
|
||||
defer store.rw.Unlock()
|
||||
|
||||
@@ -168,6 +168,31 @@ func TestInstanceGet(t *testing.T) {
|
||||
assert.Equal(t, i1, foundInstance)
|
||||
}
|
||||
|
||||
func TestInstanceGetAllWindows(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)
|
||||
w1 := []*types.WindowsInstance{{ID: "one"}, {ID: "two"}}
|
||||
w2 := []*types.WindowsInstance{{ID: "three"}, {ID: "four"}}
|
||||
s1 := &types.Session{Id: "session1", WindowsAssigned: w1}
|
||||
s2 := &types.Session{Id: "session2", WindowsAssigned: w2}
|
||||
err = storage.SessionPut(s1)
|
||||
err = storage.SessionPut(s2)
|
||||
assert.Nil(t, err)
|
||||
|
||||
allw, err := storage.InstanceGetAllWindows()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, allw, append(w1, w2...))
|
||||
}
|
||||
|
||||
func TestInstanceCreate(t *testing.T) {
|
||||
tmpfile, err := ioutil.TempFile("", "pwd")
|
||||
if err != nil {
|
||||
@@ -195,6 +220,56 @@ func TestInstanceCreate(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func TestInstanceCreateWindows(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)
|
||||
|
||||
s1 := &types.Session{Id: "session1"}
|
||||
i1 := &types.WindowsInstance{SessionId: s1.Id, ID: "some id"}
|
||||
err = storage.SessionPut(s1)
|
||||
assert.Nil(t, err)
|
||||
err = storage.InstanceCreateWindows(i1)
|
||||
assert.Nil(t, err)
|
||||
|
||||
loadedSession, err := storage.SessionGet("session1")
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Equal(t, i1, loadedSession.WindowsAssigned[0])
|
||||
}
|
||||
|
||||
func TestInstanceDeleteWindows(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)
|
||||
|
||||
s1 := &types.Session{Id: "session1", WindowsAssigned: []*types.WindowsInstance{{ID: "one"}}}
|
||||
err = storage.SessionPut(s1)
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = storage.InstanceDeleteWindows(s1.Id, "one")
|
||||
assert.Nil(t, err)
|
||||
|
||||
found, err := storage.SessionGet(s1.Id)
|
||||
assert.Equal(t, 0, len(found.WindowsAssigned))
|
||||
}
|
||||
|
||||
func TestCounts(t *testing.T) {
|
||||
tmpfile, err := ioutil.TempFile("", "pwd")
|
||||
if err != nil {
|
||||
|
||||
@@ -39,6 +39,11 @@ func (m *Mock) InstanceGet(sessionId, name string) (*types.Instance, error) {
|
||||
return args.Get(0).(*types.Instance), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *Mock) InstanceGetAllWindows() ([]*types.WindowsInstance, error) {
|
||||
args := m.Called()
|
||||
return args.Get(0).([]*types.WindowsInstance), 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)
|
||||
@@ -49,11 +54,21 @@ func (m *Mock) InstanceCreate(sessionId string, instance *types.Instance) error
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func (m *Mock) InstanceCreateWindows(instance *types.WindowsInstance) error {
|
||||
args := m.Called(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) InstanceDeleteWindows(sessionId, instanceId string) error {
|
||||
args := m.Called(sessionId, instanceId)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func (m *Mock) InstanceCount() (int, error) {
|
||||
args := m.Called()
|
||||
return args.Int(0), args.Error(1)
|
||||
|
||||
@@ -19,5 +19,8 @@ type StorageApi interface {
|
||||
InstanceFindByIP(session, ip string) (*types.Instance, error)
|
||||
InstanceCreate(sessionId string, instance *types.Instance) error
|
||||
InstanceDelete(sessionId, instanceName string) error
|
||||
InstanceDeleteWindows(sessionId, instanceId string) error
|
||||
InstanceCount() (int, error)
|
||||
InstanceGetAllWindows() ([]*types.WindowsInstance, error)
|
||||
InstanceCreateWindows(*types.WindowsInstance) error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user