Add container model
This commit is contained in:
parent
c74397c6f1
commit
a0b56504d0
|
@ -0,0 +1,49 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
)
|
||||
|
||||
type Container struct {
|
||||
}
|
||||
|
||||
func GetId(containername string) (string, error) {
|
||||
containers, err := DockerApi().ContainerList(context.Background(), types.ContainerListOptions{All: true})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
for _, container := range containers {
|
||||
|
||||
for _, name := range container.Names {
|
||||
if "/"+containername == name {
|
||||
return container.ID, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", errors.New("Container not found")
|
||||
}
|
||||
|
||||
func ContainerStart(id string) error {
|
||||
return DockerApi().ContainerStart(context.Background(), id, types.ContainerStartOptions{})
|
||||
}
|
||||
|
||||
func ContainerStop(id string) error {
|
||||
return DockerApi().ContainerStop(context.Background(), id, nil)
|
||||
}
|
||||
|
||||
func ContainerLogs(id string, follow bool) (io.ReadCloser, error) {
|
||||
out, err := DockerApi().ContainerLogs(context.Background(), id, types.ContainerLogsOptions{
|
||||
ShowStdout: true,
|
||||
ShowStderr: true,
|
||||
Follow: follow,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
package models_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"magmise/models"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
)
|
||||
|
||||
func TestGetId(t *testing.T) {
|
||||
cleanup, container := ContainerSetup(t)
|
||||
defer cleanup()
|
||||
id, err := models.GetId("magmise_test")
|
||||
if id != container.ID {
|
||||
t.Fatal(err, "Result ", id, "Expected ", container.ID)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestContainer(t *testing.T) {
|
||||
cleanup, c := ContainerSetup(t)
|
||||
defer cleanup()
|
||||
err := models.ContainerStart(c.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
statusCh, errCh := models.DockerApi().ContainerWait(context.Background(), c.ID, container.WaitConditionNotRunning)
|
||||
select {
|
||||
case err := <-errCh:
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
case <-statusCh:
|
||||
}
|
||||
|
||||
logs, err := models.ContainerLogs(c.ID, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
buf.ReadFrom(logs)
|
||||
t.Log(buf.String())
|
||||
// var l []byte
|
||||
// logs.Read(l)
|
||||
// t.Log(l)
|
||||
if err := models.ContainerStop(c.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func ContainerSetup(t *testing.T) (func(), container.ContainerCreateCreatedBody) {
|
||||
_, err := models.DockerApi().ImagePull(context.Background(), "docker.io/library/alpine", types.ImagePullOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// get rid of old container
|
||||
id, _ := models.GetId("magmise_test")
|
||||
models.DockerApi().ContainerRemove(context.Background(), id, types.ContainerRemoveOptions{})
|
||||
container, err := models.DockerApi().ContainerCreate(context.Background(), &container.Config{
|
||||
Image: "docker.io/library/alpine", Cmd: []string{"echo", "hello world"}},
|
||||
nil, nil, nil, "magmise_test")
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
}
|
||||
// cleanup
|
||||
return func() {
|
||||
models.DockerApi().ContainerRemove(context.Background(), container.ID, types.ContainerRemoveOptions{})
|
||||
}, container
|
||||
}
|
Loading…
Reference in New Issue