Remove the concept of a root dir out of engine

This makes the engine more general purpose so that we can
use it and the job routing functionality for reexec'ing our binary
Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)

Conflicts:
	integration/runtime_test.go
This commit is contained in:
Michael Crosby
2014-04-22 19:04:03 -07:00
parent 817494d009
commit 672edfe807
8 changed files with 38 additions and 100 deletions
+3 -39
View File
@@ -5,9 +5,7 @@ import (
"fmt"
"github.com/dotcloud/docker/utils"
"io"
"log"
"os"
"runtime"
"sort"
"strings"
)
@@ -37,7 +35,6 @@ func unregister(name string) {
// It acts as a store for *containers*, and allows manipulation of these
// containers by executing *jobs*.
type Engine struct {
root string
handlers map[string]Handler
hack Hack // data for temporary hackery (see hack.go)
id string
@@ -47,10 +44,6 @@ type Engine struct {
Logging bool
}
func (eng *Engine) Root() string {
return eng.root
}
func (eng *Engine) Register(name string, handler Handler) error {
_, exists := eng.handlers[name]
if exists {
@@ -60,38 +53,9 @@ func (eng *Engine) Register(name string, handler Handler) error {
return nil
}
// New initializes a new engine managing the directory specified at `root`.
// `root` is used to store containers and any other state private to the engine.
// Changing the contents of the root without executing a job will cause unspecified
// behavior.
func New(root string) (*Engine, error) {
// Check for unsupported architectures
if runtime.GOARCH != "amd64" {
return nil, fmt.Errorf("The docker runtime currently only supports amd64 (not %s). This will change in the future. Aborting.", runtime.GOARCH)
}
// Check for unsupported kernel versions
// FIXME: it would be cleaner to not test for specific versions, but rather
// test for specific functionalities.
// Unfortunately we can't test for the feature "does not cause a kernel panic"
// without actually causing a kernel panic, so we need this workaround until
// the circumstances of pre-3.8 crashes are clearer.
// For details see http://github.com/dotcloud/docker/issues/407
if k, err := utils.GetKernelVersion(); err != nil {
log.Printf("WARNING: %s\n", err)
} else {
if utils.CompareKernelVersion(k, &utils.KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0}) < 0 {
if os.Getenv("DOCKER_NOWARN_KERNEL_VERSION") == "" {
log.Printf("WARNING: You are running linux kernel version %s, which might be unstable running docker. Please upgrade your kernel to 3.8.0.", k.String())
}
}
}
if err := os.MkdirAll(root, 0700); err != nil && !os.IsExist(err) {
return nil, err
}
// New initializes a new engine.
func New() (*Engine, error) {
eng := &Engine{
root: root,
handlers: make(map[string]Handler),
id: utils.RandomString(),
Stdout: os.Stdout,
@@ -113,7 +77,7 @@ func New(root string) (*Engine, error) {
}
func (eng *Engine) String() string {
return fmt.Sprintf("%s|%s", eng.Root(), eng.id[:8])
return fmt.Sprintf("%s", eng.id[:8])
}
// Commands returns a list of all currently registered commands,
-42
View File
@@ -2,10 +2,6 @@ package engine
import (
"bytes"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"testing"
)
@@ -67,7 +63,6 @@ func TestJob(t *testing.T) {
func TestEngineCommands(t *testing.T) {
eng := newTestEngine(t)
defer os.RemoveAll(eng.Root())
handler := func(job *Job) Status { return StatusOK }
eng.Register("foo", handler)
eng.Register("bar", handler)
@@ -83,44 +78,9 @@ func TestEngineCommands(t *testing.T) {
}
}
func TestEngineRoot(t *testing.T) {
tmp, err := ioutil.TempDir("", "docker-test-TestEngineCreateDir")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)
// We expect Root to resolve to an absolute path.
// FIXME: this should not be necessary.
// Until the above FIXME is implemented, let's check for the
// current behavior.
tmp, err = filepath.EvalSymlinks(tmp)
if err != nil {
t.Fatal(err)
}
tmp, err = filepath.Abs(tmp)
if err != nil {
t.Fatal(err)
}
dir := path.Join(tmp, "dir")
eng, err := New(dir)
if err != nil {
t.Fatal(err)
}
if st, err := os.Stat(dir); err != nil {
t.Fatal(err)
} else if !st.IsDir() {
t.Fatalf("engine.New() created something other than a directory at %s", dir)
}
if r := eng.Root(); r != dir {
t.Fatalf("Expected: %v\nReceived: %v", dir, r)
}
}
func TestEngineString(t *testing.T) {
eng1 := newTestEngine(t)
defer os.RemoveAll(eng1.Root())
eng2 := newTestEngine(t)
defer os.RemoveAll(eng2.Root())
s1 := eng1.String()
s2 := eng2.String()
if eng1 == eng2 {
@@ -130,7 +90,6 @@ func TestEngineString(t *testing.T) {
func TestEngineLogf(t *testing.T) {
eng := newTestEngine(t)
defer os.RemoveAll(eng.Root())
input := "Test log line"
if n, err := eng.Logf("%s\n", input); err != nil {
t.Fatal(err)
@@ -141,7 +100,6 @@ func TestEngineLogf(t *testing.T) {
func TestParseJob(t *testing.T) {
eng := newTestEngine(t)
defer os.RemoveAll(eng.Root())
// Verify that the resulting job calls to the right place
var called bool
eng.Register("echo", func(job *Job) Status {
+1 -6
View File
@@ -1,18 +1,13 @@
package engine
import (
"github.com/dotcloud/docker/utils"
"testing"
)
var globalTestID string
func newTestEngine(t *testing.T) *Engine {
tmp, err := utils.TestDirectory("")
if err != nil {
t.Fatal(err)
}
eng, err := New(tmp)
eng, err := New()
if err != nil {
t.Fatal(err)
}
-6
View File
@@ -1,13 +1,11 @@
package engine
import (
"os"
"testing"
)
func TestJobStatusOK(t *testing.T) {
eng := newTestEngine(t)
defer os.RemoveAll(eng.Root())
eng.Register("return_ok", func(job *Job) Status { return StatusOK })
err := eng.Job("return_ok").Run()
if err != nil {
@@ -17,7 +15,6 @@ func TestJobStatusOK(t *testing.T) {
func TestJobStatusErr(t *testing.T) {
eng := newTestEngine(t)
defer os.RemoveAll(eng.Root())
eng.Register("return_err", func(job *Job) Status { return StatusErr })
err := eng.Job("return_err").Run()
if err == nil {
@@ -27,7 +24,6 @@ func TestJobStatusErr(t *testing.T) {
func TestJobStatusNotFound(t *testing.T) {
eng := newTestEngine(t)
defer os.RemoveAll(eng.Root())
eng.Register("return_not_found", func(job *Job) Status { return StatusNotFound })
err := eng.Job("return_not_found").Run()
if err == nil {
@@ -37,7 +33,6 @@ func TestJobStatusNotFound(t *testing.T) {
func TestJobStdoutString(t *testing.T) {
eng := newTestEngine(t)
defer os.RemoveAll(eng.Root())
// FIXME: test multiple combinations of output and status
eng.Register("say_something_in_stdout", func(job *Job) Status {
job.Printf("Hello world\n")
@@ -59,7 +54,6 @@ func TestJobStdoutString(t *testing.T) {
func TestJobStderrString(t *testing.T) {
eng := newTestEngine(t)
defer os.RemoveAll(eng.Root())
// FIXME: test multiple combinations of output and status
eng.Register("say_something_in_stderr", func(job *Job) Status {
job.Errorf("Warning, something might happen\nHere it comes!\nOh no...\nSomething happened\n")