2016-03-21 19:06:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-06-09 20:33:26 +00:00
|
|
|
"io/ioutil"
|
2016-03-21 19:06:39 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-04-21 01:08:36 +00:00
|
|
|
"sort"
|
2016-03-21 19:06:39 +00:00
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/docker/containerd/api/grpc/types"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2016-06-09 20:33:26 +00:00
|
|
|
func (cs *ContainerdSuite) GetLogs() string {
|
|
|
|
b, _ := ioutil.ReadFile(cs.logFile.Name())
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
2016-03-21 19:06:39 +00:00
|
|
|
func (cs *ContainerdSuite) ListRunningContainers() ([]*types.Container, error) {
|
|
|
|
resp, err := cs.grpcClient.State(context.Background(), &types.StateRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp.Containers, nil
|
|
|
|
}
|
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
func (cs *ContainerdSuite) SignalContainerProcess(id string, procID string, sig uint32) error {
|
2016-03-21 19:06:39 +00:00
|
|
|
_, err := cs.grpcClient.Signal(context.Background(), &types.SignalRequest{
|
|
|
|
Id: id,
|
2016-06-03 22:00:49 +00:00
|
|
|
Pid: procID,
|
2016-03-21 19:06:39 +00:00
|
|
|
Signal: sig,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *ContainerdSuite) SignalContainer(id string, sig uint32) error {
|
|
|
|
return cs.SignalContainerProcess(id, "init", sig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *ContainerdSuite) KillContainer(id string) error {
|
|
|
|
return cs.SignalContainerProcess(id, "init", uint32(syscall.SIGKILL))
|
|
|
|
}
|
|
|
|
|
2016-06-09 20:33:26 +00:00
|
|
|
func (cs *ContainerdSuite) UpdateContainerResource(id string, rs *types.UpdateResource) error {
|
|
|
|
_, err := cs.grpcClient.UpdateContainer(context.Background(), &types.UpdateContainerRequest{
|
|
|
|
Id: id,
|
|
|
|
Pid: "init",
|
|
|
|
Status: "",
|
|
|
|
Resources: rs,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-03-21 19:06:39 +00:00
|
|
|
func (cs *ContainerdSuite) PauseContainer(id string) error {
|
|
|
|
_, err := cs.grpcClient.UpdateContainer(context.Background(), &types.UpdateContainerRequest{
|
|
|
|
Id: id,
|
|
|
|
Pid: "init",
|
|
|
|
Status: "paused",
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *ContainerdSuite) ResumeContainer(id string) error {
|
|
|
|
_, err := cs.grpcClient.UpdateContainer(context.Background(), &types.UpdateContainerRequest{
|
|
|
|
Id: id,
|
|
|
|
Pid: "init",
|
|
|
|
Status: "running",
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *ContainerdSuite) GetContainerStats(id string) (*types.StatsResponse, error) {
|
|
|
|
stats, err := cs.grpcClient.Stats(context.Background(), &types.StatsRequest{
|
|
|
|
Id: id,
|
|
|
|
})
|
|
|
|
return stats, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type stdio struct {
|
|
|
|
stdin string
|
|
|
|
stdout string
|
|
|
|
stderr string
|
|
|
|
stdinf *os.File
|
|
|
|
stdoutf *os.File
|
|
|
|
stderrf *os.File
|
|
|
|
stdoutBuffer bytes.Buffer
|
|
|
|
stderrBuffer bytes.Buffer
|
|
|
|
}
|
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
type ContainerProcess struct {
|
|
|
|
containerID string
|
2016-03-21 19:06:39 +00:00
|
|
|
pid string
|
|
|
|
bundle *Bundle
|
|
|
|
io stdio
|
|
|
|
eventsCh chan *types.Event
|
|
|
|
cs *ContainerdSuite
|
|
|
|
hasExited bool
|
|
|
|
}
|
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
func (c *ContainerProcess) openIo() (err error) {
|
2016-03-21 19:06:39 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
c.Cleanup()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
c.io.stdinf, err = os.OpenFile(c.io.stdin, os.O_RDWR, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.io.stdoutf, err = os.OpenFile(c.io.stdout, os.O_RDWR, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go io.Copy(&c.io.stdoutBuffer, c.io.stdoutf)
|
|
|
|
|
|
|
|
c.io.stderrf, err = os.OpenFile(c.io.stderr, os.O_RDWR, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go io.Copy(&c.io.stderrBuffer, c.io.stderrf)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
func (c *ContainerProcess) GetEventsChannel() chan *types.Event {
|
2016-03-28 18:27:12 +00:00
|
|
|
return c.eventsCh
|
|
|
|
}
|
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
func (c *ContainerProcess) GetNextEvent() *types.Event {
|
2016-03-28 18:27:12 +00:00
|
|
|
if c.hasExited {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-21 19:06:39 +00:00
|
|
|
e := <-c.eventsCh
|
|
|
|
|
|
|
|
if e.Type == "exit" && e.Pid == c.pid {
|
|
|
|
c.Cleanup()
|
|
|
|
c.hasExited = true
|
2016-03-28 18:27:12 +00:00
|
|
|
close(c.eventsCh)
|
2016-03-21 19:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
func (c *ContainerProcess) CloseStdin() error {
|
2016-03-21 19:06:39 +00:00
|
|
|
_, err := c.cs.grpcClient.UpdateProcess(context.Background(), &types.UpdateProcessRequest{
|
2016-06-03 22:00:49 +00:00
|
|
|
Id: c.containerID,
|
2016-03-21 19:06:39 +00:00
|
|
|
Pid: c.pid,
|
|
|
|
CloseStdin: true,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
func (c *ContainerProcess) Cleanup() {
|
2016-03-21 19:06:39 +00:00
|
|
|
for _, f := range []*os.File{
|
|
|
|
c.io.stdinf,
|
|
|
|
c.io.stdoutf,
|
|
|
|
c.io.stderrf,
|
|
|
|
} {
|
|
|
|
if f != nil {
|
|
|
|
f.Close()
|
|
|
|
f = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
func NewContainerProcess(cs *ContainerdSuite, bundle *Bundle, cid, pid string) (c *ContainerProcess, err error) {
|
|
|
|
c = &ContainerProcess{
|
|
|
|
containerID: cid,
|
2016-03-21 19:06:39 +00:00
|
|
|
pid: "init",
|
|
|
|
bundle: bundle,
|
|
|
|
eventsCh: make(chan *types.Event, 8),
|
|
|
|
cs: cs,
|
|
|
|
hasExited: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, path := range map[string]*string{
|
|
|
|
"stdin": &c.io.stdin,
|
|
|
|
"stdout": &c.io.stdout,
|
|
|
|
"stderr": &c.io.stderr,
|
|
|
|
} {
|
|
|
|
*path = filepath.Join(bundle.Path, "io", cid+"-"+pid+"-"+name)
|
|
|
|
if err = syscall.Mkfifo(*path, 0755); err != nil && !os.IsExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = c.openIo(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
func (cs *ContainerdSuite) StartContainerWithEventFilter(id, bundleName string, filter func(*types.Event)) (c *ContainerProcess, err error) {
|
2016-03-28 23:22:17 +00:00
|
|
|
bundle := GetBundle(bundleName)
|
|
|
|
if bundle == nil {
|
2016-03-21 19:06:39 +00:00
|
|
|
return nil, fmt.Errorf("No such bundle '%s'", bundleName)
|
|
|
|
}
|
|
|
|
|
2016-03-28 23:22:17 +00:00
|
|
|
c, err = NewContainerProcess(cs, bundle, id, "init")
|
2016-03-21 19:06:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r := &types.CreateContainerRequest{
|
|
|
|
Id: id,
|
|
|
|
BundlePath: filepath.Join(cs.cwd, bundle.Path),
|
|
|
|
Stdin: filepath.Join(cs.cwd, c.io.stdin),
|
|
|
|
Stdout: filepath.Join(cs.cwd, c.io.stdout),
|
|
|
|
Stderr: filepath.Join(cs.cwd, c.io.stderr),
|
|
|
|
}
|
|
|
|
|
2016-03-28 23:22:17 +00:00
|
|
|
if filter == nil {
|
|
|
|
filter = func(event *types.Event) {
|
|
|
|
c.eventsCh <- event
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cs.SetContainerEventFilter(id, filter)
|
2016-03-21 19:06:39 +00:00
|
|
|
|
|
|
|
if _, err := cs.grpcClient.CreateContainer(context.Background(), r); err != nil {
|
|
|
|
c.Cleanup()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
func (cs *ContainerdSuite) StartContainer(id, bundleName string) (c *ContainerProcess, err error) {
|
2016-03-28 23:22:17 +00:00
|
|
|
return cs.StartContainerWithEventFilter(id, bundleName, nil)
|
|
|
|
}
|
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
func (cs *ContainerdSuite) RunContainer(id, bundleName string) (c *ContainerProcess, err error) {
|
2016-03-21 19:06:39 +00:00
|
|
|
c, err = cs.StartContainer(id, bundleName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
e := c.GetNextEvent()
|
|
|
|
if e.Type == "exit" && e.Pid == "init" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
func (cs *ContainerdSuite) AddProcessToContainer(init *ContainerProcess, pid, cwd string, env, args []string, uid, gid uint32) (c *ContainerProcess, err error) {
|
|
|
|
c, err = NewContainerProcess(cs, init.bundle, init.containerID, pid)
|
2016-03-21 19:06:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pr := &types.AddProcessRequest{
|
2016-06-03 22:00:49 +00:00
|
|
|
Id: init.containerID,
|
2016-03-21 19:06:39 +00:00
|
|
|
Pid: pid,
|
|
|
|
Args: args,
|
|
|
|
Cwd: cwd,
|
|
|
|
Env: env,
|
|
|
|
User: &types.User{
|
|
|
|
Uid: uid,
|
|
|
|
Gid: gid,
|
|
|
|
},
|
|
|
|
Stdin: filepath.Join(cs.cwd, c.io.stdin),
|
|
|
|
Stdout: filepath.Join(cs.cwd, c.io.stdout),
|
|
|
|
Stderr: filepath.Join(cs.cwd, c.io.stderr),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = cs.grpcClient.AddProcess(context.Background(), pr)
|
|
|
|
if err != nil {
|
|
|
|
c.Cleanup()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
2016-04-21 01:08:36 +00:00
|
|
|
|
|
|
|
type containerSorter struct {
|
|
|
|
c []*types.Container
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *containerSorter) Len() int {
|
|
|
|
return len(s.c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *containerSorter) Swap(i, j int) {
|
|
|
|
s.c[i], s.c[j] = s.c[j], s.c[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *containerSorter) Less(i, j int) bool {
|
|
|
|
return s.c[i].Id < s.c[j].Id
|
|
|
|
}
|
|
|
|
|
|
|
|
func sortContainers(c []*types.Container) {
|
|
|
|
sort.Sort(&containerSorter{c})
|
|
|
|
}
|