Add first version of the windows ASG provider

This commit is contained in:
Marcos Lilljedahl
2017-08-04 21:54:03 -03:00
parent f810c0c92f
commit ed7cefcf9c
10 changed files with 388 additions and 29 deletions

View File

@@ -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()

View File

@@ -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 {

View File

@@ -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)

View File

@@ -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
}