From 62b21daded2ea9b703a6e5fa0479e007016b1edb Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Mon, 24 Feb 2014 12:26:56 -0800 Subject: [PATCH 1/2] Engine: cleanup side effects between tests Docker-DCO-1.1-Signed-off-by: Solomon Hykes (github: shykes) --- engine/engine.go | 4 ++++ engine/engine_test.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/engine/engine.go b/engine/engine.go index 5814955fdd..946e8a9ece 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -29,6 +29,10 @@ func Register(name string, handler Handler) error { return nil } +func unregister(name string) { + delete(globalHandlers, name) +} + // The Engine is the core of Docker. // It acts as a store for *containers*, and allows manipulation of these // containers by executing *jobs*. diff --git a/engine/engine_test.go b/engine/engine_test.go index da59610727..90b61cda9a 100644 --- a/engine/engine_test.go +++ b/engine/engine_test.go @@ -16,6 +16,8 @@ func TestRegister(t *testing.T) { if err := Register("dummy1", nil); err == nil { t.Fatalf("Expecting error, got none") } + // Register is global so let's cleanup to avoid conflicts + defer unregister("dummy1") eng := newTestEngine(t) @@ -32,6 +34,7 @@ func TestRegister(t *testing.T) { if err := eng.Register("dummy2", nil); err == nil { t.Fatalf("Expecting error, got none") } + defer unregister("dummy2") } func TestJob(t *testing.T) { @@ -48,6 +51,7 @@ func TestJob(t *testing.T) { } eng.Register("dummy2", h) + defer unregister("dummy2") job2 := eng.Job("dummy2", "--level=awesome") if job2.handler == nil { From cd846ecb60ad936d58d70762ed91cffa906cef93 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Sat, 15 Feb 2014 15:06:21 -0800 Subject: [PATCH 2/2] Engine: builtin command 'commands' returns a list of registered commands Docker-DCO-1.1-Signed-off-by: Solomon Hykes (github: shykes) --- engine/engine.go | 18 ++++++++++++++++++ engine/engine_test.go | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/engine/engine.go b/engine/engine.go index 946e8a9ece..68e109e7f2 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -9,6 +9,7 @@ import ( "os" "path/filepath" "runtime" + "sort" "strings" ) @@ -110,6 +111,12 @@ func New(root string) (*Engine, error) { Stderr: os.Stderr, Stdin: os.Stdin, } + eng.Register("commands", func(job *Job) Status { + for _, name := range eng.commands() { + job.Printf("%s\n", name) + } + return StatusOK + }) // Copy existing global handlers for k, v := range globalHandlers { eng.handlers[k] = v @@ -121,6 +128,17 @@ func (eng *Engine) String() string { return fmt.Sprintf("%s|%s", eng.Root(), eng.id[:8]) } +// Commands returns a list of all currently registered commands, +// sorted alphabetically. +func (eng *Engine) commands() []string { + names := make([]string, 0, len(eng.handlers)) + for name := range eng.handlers { + names = append(names, name) + } + sort.Strings(names) + return names +} + // Job creates a new job which can later be executed. // This function mimics `Command` from the standard os/exec package. func (eng *Engine) Job(name string, args ...string) *Job { diff --git a/engine/engine_test.go b/engine/engine_test.go index 90b61cda9a..e922d8a875 100644 --- a/engine/engine_test.go +++ b/engine/engine_test.go @@ -1,6 +1,7 @@ package engine import ( + "bytes" "io/ioutil" "os" "path" @@ -63,6 +64,24 @@ 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) + eng.Register("echo", handler) + eng.Register("die", handler) + var output bytes.Buffer + commands := eng.Job("commands") + commands.Stdout.Add(&output) + commands.Run() + expected := "bar\ncommands\ndie\necho\nfoo\n" + if result := output.String(); result != expected { + t.Fatalf("Unexpected output:\nExpected = %v\nResult = %v\n", expected, result) + } +} + func TestEngineRoot(t *testing.T) { tmp, err := ioutil.TempDir("", "docker-test-TestEngineCreateDir") if err != nil {