readd docker environment tests
implement Exec()
This commit is contained in:
parent
8b43e3a43d
commit
5bba1c0d90
|
@ -199,8 +199,8 @@ func (env *dockerEnvironment) Start() error {
|
||||||
func (env *dockerEnvironment) Stop() error {
|
func (env *dockerEnvironment) Stop() error {
|
||||||
log.WithField("server", env.server.ID).Debug("Stopping service in docker environment")
|
log.WithField("server", env.server.ID).Debug("Stopping service in docker environment")
|
||||||
|
|
||||||
// TODO: Decide after what timeout to kill the container, currently 10min
|
// TODO: Decide after what timeout to kill the container, currently 30 seconds
|
||||||
timeout := time.Minute * 10
|
timeout := 30 * time.Second
|
||||||
if err := env.client.ContainerStop(context.TODO(), env.server.DockerContainer.ID, &timeout); err != nil {
|
if err := env.client.ContainerStop(context.TODO(), env.server.DockerContainer.ID, &timeout); err != nil {
|
||||||
log.WithError(err).Error("Failed to stop docker container")
|
log.WithError(err).Error("Failed to stop docker container")
|
||||||
return err
|
return err
|
||||||
|
@ -211,7 +211,7 @@ func (env *dockerEnvironment) Stop() error {
|
||||||
func (env *dockerEnvironment) Kill() error {
|
func (env *dockerEnvironment) Kill() error {
|
||||||
log.WithField("server", env.server.ID).Debug("Killing service in docker environment")
|
log.WithField("server", env.server.ID).Debug("Killing service in docker environment")
|
||||||
|
|
||||||
if err := env.client.ContainerKill(context.TODO(), env.server.DockerContainer.ID, "SIGKILL"); err != nil {
|
if err := env.client.ContainerKill(context.TODO(), env.server.DockerContainer.ID, "KILL"); err != nil {
|
||||||
log.WithError(err).Error("Failed to kill docker container")
|
log.WithError(err).Error("Failed to kill docker container")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -220,24 +220,26 @@ func (env *dockerEnvironment) Kill() error {
|
||||||
|
|
||||||
// Exec sends commands to the standard input of the docker container
|
// Exec sends commands to the standard input of the docker container
|
||||||
func (env *dockerEnvironment) Exec(command string) error {
|
func (env *dockerEnvironment) Exec(command string) error {
|
||||||
//log.Debug("Command: " + command)
|
log.Debug("Command: " + command)
|
||||||
//_, err := env.containerInput.Write([]byte(command + "\n"))
|
_, err := env.hires.Conn.Write([]byte(command + "\n"))
|
||||||
//return err
|
return err
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (env *dockerEnvironment) pullImage(ctx context.Context) error {
|
func (env *dockerEnvironment) pullImage(ctx context.Context) error {
|
||||||
// Split image repository and tag
|
// Split image repository and tag
|
||||||
imageParts := strings.Split(env.server.GetService().DockerImage, ":")
|
//imageParts := strings.Split(env.server.GetService().DockerImage, ":")
|
||||||
imageRepoParts := strings.Split(imageParts[0], "/")
|
//imageRepoParts := strings.Split(imageParts[0], "/")
|
||||||
if len(imageRepoParts) >= 3 {
|
//if len(imageRepoParts) >= 3 {
|
||||||
// TODO: Handle possibly required authentication
|
// TODO: Handle possibly required authentication
|
||||||
}
|
//}
|
||||||
|
|
||||||
// Pull docker image
|
// Pull docker image
|
||||||
log.WithField("image", env.server.GetService().DockerImage).Debug("Pulling docker image")
|
log.WithField("image", env.server.GetService().DockerImage).Debug("Pulling docker image")
|
||||||
|
|
||||||
rc, err := env.client.ImagePull(ctx, env.server.GetService().DockerImage, types.ImagePullOptions{})
|
rc, err := env.client.ImagePull(ctx, env.server.GetService().DockerImage, types.ImagePullOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
defer rc.Close()
|
defer rc.Close()
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,125 +1,133 @@
|
||||||
package control
|
package control
|
||||||
|
|
||||||
// func testServer() *ServerStruct {
|
import (
|
||||||
// return &ServerStruct{
|
"context"
|
||||||
// ID: "testuuid-something-something",
|
"fmt"
|
||||||
// service: &service{
|
"testing"
|
||||||
// DockerImage: "alpine:latest",
|
|
||||||
// },
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func TestNewDockerEnvironment(t *testing.T) {
|
"github.com/pterodactyl/wings/api/websockets"
|
||||||
// env, err := createTestDockerEnv(nil)
|
|
||||||
|
|
||||||
// assert.Nil(t, err)
|
"github.com/docker/docker/api/types"
|
||||||
// assert.NotNil(t, env)
|
"github.com/docker/docker/client"
|
||||||
// assert.NotNil(t, env.client)
|
"github.com/pterodactyl/wings/config"
|
||||||
// }
|
"github.com/spf13/viper"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
// func TestNewDockerEnvironmentExisting(t *testing.T) {
|
func testServer() *ServerStruct {
|
||||||
// eenv, _ := createTestDockerEnv(nil)
|
viper.SetDefault(config.DataPath, "./data")
|
||||||
// eenv.Create()
|
return &ServerStruct{
|
||||||
|
ID: "testuuid-something-something",
|
||||||
|
Service: &Service{
|
||||||
|
DockerImage: "alpine:latest",
|
||||||
|
},
|
||||||
|
StartupCommand: "/bin/ash echo hello && sleep 100",
|
||||||
|
websockets: websockets.NewCollection(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// env, err := createTestDockerEnv(eenv.server)
|
func TestNewDockerEnvironment(t *testing.T) {
|
||||||
|
env, err := createTestDockerEnv(nil)
|
||||||
|
|
||||||
// assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
// assert.NotNil(t, env)
|
assert.NotNil(t, env)
|
||||||
// assert.NotNil(t, env.container)
|
assert.NotNil(t, env.client)
|
||||||
|
}
|
||||||
|
|
||||||
// eenv.Destroy()
|
func TestNewDockerEnvironmentExisting(t *testing.T) {
|
||||||
// }
|
eenv, _ := createTestDockerEnv(nil)
|
||||||
|
eenv.Create()
|
||||||
|
|
||||||
// func TestCreateDockerEnvironment(t *testing.T) {
|
env, err := createTestDockerEnv(eenv.server)
|
||||||
// env, _ := createTestDockerEnv(nil)
|
|
||||||
|
|
||||||
// err := env.Create()
|
assert.Nil(t, err)
|
||||||
|
assert.NotNil(t, env)
|
||||||
|
assert.NotNil(t, env.server.DockerContainer)
|
||||||
|
|
||||||
// a := assert.New(t)
|
eenv.Destroy()
|
||||||
// a.Nil(err)
|
}
|
||||||
// a.NotNil(env.container)
|
|
||||||
// a.Equal(env.container.Name, "ptdl_testuuid")
|
|
||||||
|
|
||||||
// if err := env.client.RemoveContainer(docker.RemoveContainerOptions{
|
func TestCreateDockerEnvironment(t *testing.T) {
|
||||||
// ID: env.container.ID,
|
env, _ := createTestDockerEnv(nil)
|
||||||
// }); err != nil {
|
|
||||||
// fmt.Println(err)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func TestDestroyDockerEnvironment(t *testing.T) {
|
err := env.Create()
|
||||||
// env, _ := createTestDockerEnv(nil)
|
|
||||||
// env.Create()
|
|
||||||
|
|
||||||
// err := env.Destroy()
|
a := assert.New(t)
|
||||||
|
a.Nil(err)
|
||||||
|
a.NotNil(env.server.DockerContainer.ID)
|
||||||
|
|
||||||
// _, ierr := env.client.InspectContainer(env.container.ID)
|
if err := env.client.ContainerRemove(context.TODO(), env.server.DockerContainer.ID, types.ContainerRemoveOptions{}); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// assert.Nil(t, err)
|
func TestDestroyDockerEnvironment(t *testing.T) {
|
||||||
// assert.IsType(t, ierr, &docker.NoSuchContainer{})
|
env, _ := createTestDockerEnv(nil)
|
||||||
// }
|
env.Create()
|
||||||
|
|
||||||
// func TestStartDockerEnvironment(t *testing.T) {
|
err := env.Destroy()
|
||||||
// env, _ := createTestDockerEnv(nil)
|
|
||||||
// env.Create()
|
|
||||||
// err := env.Start()
|
|
||||||
|
|
||||||
// i, ierr := env.client.InspectContainer(env.container.ID)
|
_, ierr := env.client.ContainerInspect(context.TODO(), env.server.DockerContainer.ID)
|
||||||
|
|
||||||
// assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
// assert.Nil(t, ierr)
|
assert.True(t, client.IsErrNotFound(ierr))
|
||||||
// assert.True(t, i.State.Running)
|
}
|
||||||
|
|
||||||
// env.client.KillContainer(docker.KillContainerOptions{
|
func TestStartDockerEnvironment(t *testing.T) {
|
||||||
// ID: env.container.ID,
|
env, _ := createTestDockerEnv(nil)
|
||||||
// })
|
env.Create()
|
||||||
// env.Destroy()
|
err := env.Start()
|
||||||
// }
|
|
||||||
|
|
||||||
// func TestStopDockerEnvironment(t *testing.T) {
|
i, ierr := env.client.ContainerInspect(context.TODO(), env.server.DockerContainer.ID)
|
||||||
// env, _ := createTestDockerEnv(nil)
|
|
||||||
// env.Create()
|
|
||||||
// env.Start()
|
|
||||||
// err := env.Stop()
|
|
||||||
|
|
||||||
// i, ierr := env.client.InspectContainer(env.container.ID)
|
assert.Nil(t, err)
|
||||||
|
assert.Nil(t, ierr)
|
||||||
|
assert.True(t, i.State.Running)
|
||||||
|
|
||||||
// assert.Nil(t, err)
|
env.client.ContainerKill(context.TODO(), env.server.DockerContainer.ID, "KILL")
|
||||||
// assert.Nil(t, ierr)
|
env.Destroy()
|
||||||
// assert.False(t, i.State.Running)
|
}
|
||||||
|
|
||||||
// env.client.KillContainer(docker.KillContainerOptions{
|
func TestStopDockerEnvironment(t *testing.T) {
|
||||||
// ID: env.container.ID,
|
env, _ := createTestDockerEnv(nil)
|
||||||
// })
|
env.Create()
|
||||||
// env.Destroy()
|
env.Start()
|
||||||
// }
|
err := env.Stop()
|
||||||
|
|
||||||
// func TestKillDockerEnvironment(t *testing.T) {
|
i, ierr := env.client.ContainerInspect(context.TODO(), env.server.DockerContainer.ID)
|
||||||
// env, _ := createTestDockerEnv(nil)
|
|
||||||
// env.Create()
|
|
||||||
// env.Start()
|
|
||||||
// err := env.Kill()
|
|
||||||
|
|
||||||
// i, ierr := env.client.InspectContainer(env.container.ID)
|
assert.Nil(t, err)
|
||||||
|
assert.Nil(t, ierr)
|
||||||
|
assert.False(t, i.State.Running)
|
||||||
|
|
||||||
// assert.Nil(t, err)
|
env.client.ContainerKill(context.TODO(), env.server.DockerContainer.ID, "KILL")
|
||||||
// assert.Nil(t, ierr)
|
env.Destroy()
|
||||||
// assert.False(t, i.State.Running)
|
}
|
||||||
|
|
||||||
// env.client.KillContainer(docker.KillContainerOptions{
|
func TestKillDockerEnvironment(t *testing.T) {
|
||||||
// ID: env.container.ID,
|
env, _ := createTestDockerEnv(nil)
|
||||||
// })
|
env.Create()
|
||||||
// env.Destroy()
|
env.Start()
|
||||||
// }
|
err := env.Kill()
|
||||||
|
|
||||||
// func TestExecDockerEnvironment(t *testing.T) {
|
i, ierr := env.client.ContainerInspect(context.TODO(), env.server.DockerContainer.ID)
|
||||||
|
|
||||||
// }
|
assert.Nil(t, err)
|
||||||
|
assert.Nil(t, ierr)
|
||||||
|
assert.False(t, i.State.Running)
|
||||||
|
|
||||||
// func createTestDockerEnv(s *ServerStruct) (*dockerEnvironment, error) {
|
env.client.ContainerKill(context.TODO(), env.server.DockerContainer.ID, "KILL")
|
||||||
// if s == nil {
|
env.Destroy()
|
||||||
// s = testServer()
|
}
|
||||||
// }
|
|
||||||
// env, err := NewDockerEnvironment(s)
|
func TestExecDockerEnvironment(t *testing.T) {
|
||||||
// return env.(*dockerEnvironment), err
|
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
func createTestDockerEnv(s *ServerStruct) (*dockerEnvironment, error) {
|
||||||
|
if s == nil {
|
||||||
|
s = testServer()
|
||||||
|
}
|
||||||
|
env, err := NewDockerEnvironment(s)
|
||||||
|
return env.(*dockerEnvironment), err
|
||||||
|
}
|
||||||
|
|
|
@ -91,7 +91,12 @@ func (s *ServerStruct) Save() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ServerStruct) path() string {
|
func (s *ServerStruct) path() string {
|
||||||
return filepath.Join(viper.GetString(config.DataPath), constants.ServersPath, s.ID)
|
p, err := filepath.Abs(viper.GetString(config.DataPath))
|
||||||
|
if err != nil {
|
||||||
|
log.WithError(err).WithField("server", s.ID).Error("Failed to get absolute data path for server.")
|
||||||
|
p = viper.GetString(config.DataPath)
|
||||||
|
}
|
||||||
|
return filepath.Join(p, constants.ServersPath, s.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ServerStruct) dataPath() string {
|
func (s *ServerStruct) dataPath() string {
|
||||||
|
|
BIN
wings-api.paw
BIN
wings-api.paw
Binary file not shown.
Loading…
Reference in New Issue
Block a user