New session now expects a struct with everything it needs and a context (#228)
to pass along
This commit is contained in:
committed by
Marcos Nils
parent
08f1ead2a9
commit
da6a55fb5c
@@ -1,6 +1,7 @@
|
||||
package pwd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -45,7 +46,8 @@ func TestClientNew(t *testing.T) {
|
||||
|
||||
playground := &types.Playground{Id: "foobar"}
|
||||
|
||||
session, err := p.SessionNew(playground, "", time.Hour, "", "", "")
|
||||
sConfig := types.SessionConfig{Playground: playground, UserId: "", Duration: time.Hour, Stack: "", StackName: "", ImageName: ""}
|
||||
session, err := p.SessionNew(context.Background(), sConfig)
|
||||
assert.Nil(t, err)
|
||||
|
||||
client := p.ClientNew("foobar", session)
|
||||
@@ -85,7 +87,8 @@ func TestClientCount(t *testing.T) {
|
||||
p.generator = _g
|
||||
playground := &types.Playground{Id: "foobar"}
|
||||
|
||||
session, err := p.SessionNew(playground, "", time.Hour, "", "", "")
|
||||
sConfig := types.SessionConfig{Playground: playground, UserId: "", Duration: time.Hour, Stack: "", StackName: "", ImageName: ""}
|
||||
session, err := p.SessionNew(context.Background(), sConfig)
|
||||
assert.Nil(t, err)
|
||||
|
||||
p.ClientNew("foobar", session)
|
||||
@@ -127,7 +130,8 @@ func TestClientResizeViewPort(t *testing.T) {
|
||||
p.generator = _g
|
||||
playground := &types.Playground{Id: "foobar"}
|
||||
|
||||
session, err := p.SessionNew(playground, "", time.Hour, "", "", "")
|
||||
sConfig := types.SessionConfig{Playground: playground, UserId: "", Duration: time.Hour, Stack: "", StackName: "", ImageName: ""}
|
||||
session, err := p.SessionNew(context.Background(), sConfig)
|
||||
assert.Nil(t, err)
|
||||
client := p.ClientNew("foobar", session)
|
||||
_s.On("ClientFindBySessionId", "aaaabbbbcccc").Return([]*types.Client{client}, nil)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package pwd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -74,7 +75,8 @@ func TestInstanceNew(t *testing.T) {
|
||||
|
||||
_s.On("PlaygroundGet", "foobar").Return(playground, nil)
|
||||
|
||||
session, err := p.SessionNew(playground, "", time.Hour, "", "", "")
|
||||
sConfig := types.SessionConfig{Playground: playground, UserId: "", Duration: time.Hour, Stack: "", StackName: "", ImageName: ""}
|
||||
session, err := p.SessionNew(context.Background(), sConfig)
|
||||
assert.Nil(t, err)
|
||||
|
||||
expectedInstance := types.Instance{
|
||||
@@ -143,7 +145,8 @@ func TestInstanceNew_WithNotAllowedImage(t *testing.T) {
|
||||
p.generator = _g
|
||||
|
||||
playground := &types.Playground{Id: "foobar"}
|
||||
session, err := p.SessionNew(playground, "", time.Hour, "", "", "")
|
||||
sConfig := types.SessionConfig{Playground: playground, UserId: "", Duration: time.Hour, Stack: "", StackName: "", ImageName: ""}
|
||||
session, err := p.SessionNew(context.Background(), sConfig)
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
@@ -213,7 +216,8 @@ func TestInstanceNew_WithCustomHostname(t *testing.T) {
|
||||
p.generator = _g
|
||||
|
||||
playground := &types.Playground{Id: "foobar"}
|
||||
session, err := p.SessionNew(playground, "", time.Hour, "", "", "")
|
||||
sConfig := types.SessionConfig{Playground: playground, UserId: "", Duration: time.Hour, Stack: "", StackName: "", ImageName: ""}
|
||||
session, err := p.SessionNew(context.Background(), sConfig)
|
||||
assert.Nil(t, err)
|
||||
|
||||
expectedInstance := types.Instance{
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package pwd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/play-with-docker/play-with-docker/pwd/types"
|
||||
"github.com/stretchr/testify/mock"
|
||||
@@ -13,8 +13,8 @@ type Mock struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *Mock) SessionNew(playground *types.Playground, userId string, duration time.Duration, stack string, stackName, imageName string) (*types.Session, error) {
|
||||
args := m.Called(duration, stack, stackName, imageName)
|
||||
func (m *Mock) SessionNew(ctx context.Context, config types.SessionConfig) (*types.Session, error) {
|
||||
args := m.Called(ctx, config)
|
||||
return args.Get(0).(*types.Session), args.Error(1)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package pwd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
@@ -72,7 +73,7 @@ func SessionNotEmpty(e error) bool {
|
||||
}
|
||||
|
||||
type PWDApi interface {
|
||||
SessionNew(playground *types.Playground, userId string, duration time.Duration, stack string, stackName, imageName string) (*types.Session, error)
|
||||
SessionNew(ctx context.Context, config types.SessionConfig) (*types.Session, error)
|
||||
SessionClose(session *types.Session) error
|
||||
SessionGetSmallestViewPort(sessionId string) types.ViewPort
|
||||
SessionDeployStack(session *types.Session) error
|
||||
|
||||
@@ -44,29 +44,30 @@ type SessionSetupInstanceConf struct {
|
||||
Tls bool `json:"tls"`
|
||||
}
|
||||
|
||||
func (p *pwd) SessionNew(playground *types.Playground, userId string, duration time.Duration, stack, stackName, imageName string) (*types.Session, error) {
|
||||
func (p *pwd) SessionNew(ctx context.Context, config types.SessionConfig) (*types.Session, error) {
|
||||
defer observeAction("SessionNew", time.Now())
|
||||
|
||||
s := &types.Session{}
|
||||
s.Id = p.generator.NewId()
|
||||
s.CreatedAt = time.Now()
|
||||
s.ExpiresAt = s.CreatedAt.Add(duration)
|
||||
s.ExpiresAt = s.CreatedAt.Add(config.Duration)
|
||||
s.Ready = true
|
||||
s.Stack = stack
|
||||
s.UserId = userId
|
||||
s.PlaygroundId = playground.Id
|
||||
s.Stack = config.Stack
|
||||
s.UserId = config.UserId
|
||||
s.PlaygroundId = config.Playground.Id
|
||||
|
||||
if s.Stack != "" {
|
||||
s.Ready = false
|
||||
}
|
||||
stackName := config.StackName
|
||||
if stackName == "" {
|
||||
stackName = "pwd"
|
||||
}
|
||||
s.StackName = stackName
|
||||
s.ImageName = imageName
|
||||
s.ImageName = config.ImageName
|
||||
|
||||
log.Printf("NewSession id=[%s]\n", s.Id)
|
||||
if err := p.sessionProvisioner.SessionNew(playground, s); err != nil {
|
||||
if err := p.sessionProvisioner.SessionNew(ctx, s); err != nil {
|
||||
log.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package pwd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -47,7 +48,8 @@ func TestSessionNew(t *testing.T) {
|
||||
before := time.Now()
|
||||
|
||||
playground := &types.Playground{Id: "foobar"}
|
||||
s, e := p.SessionNew(playground, "", time.Hour, "", "", "")
|
||||
sConfig := types.SessionConfig{Playground: playground, UserId: "", Duration: time.Hour, Stack: "", StackName: "", ImageName: ""}
|
||||
s, e := p.SessionNew(context.Background(), sConfig)
|
||||
assert.Nil(t, e)
|
||||
assert.NotNil(t, s)
|
||||
|
||||
@@ -58,7 +60,8 @@ func TestSessionNew(t *testing.T) {
|
||||
assert.WithinDuration(t, s.ExpiresAt, before.Add(time.Hour), time.Second)
|
||||
assert.True(t, s.Ready)
|
||||
|
||||
s, _ = p.SessionNew(playground, "", time.Hour, "stackPath", "stackName", "imageName")
|
||||
sConfig = types.SessionConfig{Playground: playground, UserId: "", Duration: time.Hour, Stack: "stackPath", StackName: "stackName", ImageName: "imageName"}
|
||||
s, _ = p.SessionNew(context.Background(), sConfig)
|
||||
|
||||
assert.Equal(t, "stackPath", s.Stack)
|
||||
assert.Equal(t, "stackName", s.StackName)
|
||||
@@ -133,7 +136,8 @@ func TestSessionSetup(t *testing.T) {
|
||||
|
||||
p := NewPWD(_f, _e, _s, sp, ipf)
|
||||
p.generator = _g
|
||||
s, e := p.SessionNew(time.Hour, "", "", "")
|
||||
sConfig := types.SessionConfig{Playground: playground, UserId: "", Duration: time.Hour, Stack: "", StackName: "", ImageName: ""}
|
||||
s, e := p.SessionNew(context.Background(), sConfig)
|
||||
assert.Nil(t, e)
|
||||
|
||||
err := p.SessionSetup(s, SessionSetupConf{
|
||||
|
||||
@@ -4,6 +4,15 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type SessionConfig struct {
|
||||
Playground *Playground
|
||||
UserId string
|
||||
Duration time.Duration
|
||||
Stack string
|
||||
StackName string
|
||||
ImageName string
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
Id string `json:"id" bson:"id"`
|
||||
CreatedAt time.Time `json:"created_at" bson:"created_at"`
|
||||
|
||||
Reference in New Issue
Block a user