diff --git a/internal/server/model/handler.go b/internal/server/model/handler.go index 0899934..2410ce6 100644 --- a/internal/server/model/handler.go +++ b/internal/server/model/handler.go @@ -13,9 +13,8 @@ type Handler struct { // ID is unique identifier of the request. ID string - // Route is a reference to the original route that matched this - // request. - Route *Route + // Route is the original route that matched this request. + Route // Writing is a mutex that prevents two goroutines from writing at // the same time in the response. diff --git a/internal/server/user/spawn.go b/internal/server/user/spawn/spawn.go similarity index 75% rename from internal/server/user/spawn.go rename to internal/server/user/spawn/spawn.go index 48495d8..dc73fb0 100644 --- a/internal/server/user/spawn.go +++ b/internal/server/user/spawn/spawn.go @@ -1,6 +1,7 @@ -package user +package spawn import ( + "errors" "io" "os" "os/exec" @@ -10,7 +11,10 @@ import ( "github.com/BBVA/kapow/internal/server/model" ) -func spawn(h *model.Handler, out io.Writer) error { +func Spawn(h *model.Handler, out io.Writer) error { + if h.Route.Entrypoint == "" { + return errors.New("Entrypoint cannot be empty") + } args, err := shlex.Split(h.Route.Entrypoint) if err != nil { return err diff --git a/internal/server/user/spawn_test.go b/internal/server/user/spawn/spawn_test.go similarity index 85% rename from internal/server/user/spawn_test.go rename to internal/server/user/spawn/spawn_test.go index a8b5116..76d0517 100644 --- a/internal/server/user/spawn_test.go +++ b/internal/server/user/spawn/spawn_test.go @@ -1,4 +1,4 @@ -package user +package spawn import ( "bytes" @@ -34,7 +34,7 @@ func locateJailLover() string { } func TestSpawnRetursErrorWhenEntrypointIsBad(t *testing.T) { - r := &model.Route{ + r := model.Route{ Entrypoint: "/bin/this_executable_is_not_likely_to_exist", } @@ -42,14 +42,14 @@ func TestSpawnRetursErrorWhenEntrypointIsBad(t *testing.T) { Route: r, } - err := spawn(h, nil) + err := Spawn(h, nil) if err == nil { t.Error("Bad executable not reported") } } func TestSpawnReturnsNilWhenEntrypointIsGood(t *testing.T) { - r := &model.Route{ + r := model.Route{ Entrypoint: locateJailLover(), } @@ -57,14 +57,14 @@ func TestSpawnReturnsNilWhenEntrypointIsGood(t *testing.T) { Route: r, } - err := spawn(h, nil) + err := Spawn(h, nil) if err != nil { t.Error("Good executable reported") } } func TestSpawnWritesToStdout(t *testing.T) { - r := &model.Route{ + r := model.Route{ Entrypoint: locateJailLover(), } @@ -74,7 +74,7 @@ func TestSpawnWritesToStdout(t *testing.T) { out := &bytes.Buffer{} - _ = spawn(h, out) + _ = Spawn(h, out) jldata := decodeJailLover(out.Bytes()) @@ -85,7 +85,7 @@ func TestSpawnWritesToStdout(t *testing.T) { } func TestSpawnSetsKapowURLEnvVar(t *testing.T) { - r := &model.Route{ + r := model.Route{ Entrypoint: locateJailLover(), } @@ -95,7 +95,7 @@ func TestSpawnSetsKapowURLEnvVar(t *testing.T) { out := &bytes.Buffer{} - _ = spawn(h, out) + _ = Spawn(h, out) jldata := decodeJailLover(out.Bytes()) @@ -105,7 +105,7 @@ func TestSpawnSetsKapowURLEnvVar(t *testing.T) { } func TestSpawnSetsKapowHandlerIDEnvVar(t *testing.T) { - r := &model.Route{ + r := model.Route{ Entrypoint: locateJailLover(), } @@ -116,7 +116,7 @@ func TestSpawnSetsKapowHandlerIDEnvVar(t *testing.T) { out := &bytes.Buffer{} - _ = spawn(h, out) + _ = Spawn(h, out) jldata := decodeJailLover(out.Bytes()) @@ -126,7 +126,7 @@ func TestSpawnSetsKapowHandlerIDEnvVar(t *testing.T) { } func TestSpawnRunsOKEntrypointsWithAParam(t *testing.T) { - r := &model.Route{ + r := model.Route{ Entrypoint: locateJailLover() + " -foo", } @@ -136,7 +136,7 @@ func TestSpawnRunsOKEntrypointsWithAParam(t *testing.T) { out := &bytes.Buffer{} - _ = spawn(h, out) + _ = Spawn(h, out) jldata := decodeJailLover(out.Bytes()) @@ -146,7 +146,7 @@ func TestSpawnRunsOKEntrypointsWithAParam(t *testing.T) { } func TestSpawnRunsOKEntrypointWithArgWithSpace(t *testing.T) { - r := &model.Route{ + r := model.Route{ Entrypoint: locateJailLover() + ` "foo bar"`, } @@ -156,7 +156,7 @@ func TestSpawnRunsOKEntrypointWithArgWithSpace(t *testing.T) { out := &bytes.Buffer{} - _ = spawn(h, out) + _ = Spawn(h, out) jldata := decodeJailLover(out.Bytes()) @@ -166,7 +166,7 @@ func TestSpawnRunsOKEntrypointWithArgWithSpace(t *testing.T) { } func TestSpawnErrorsWhenEntrypointIsInvalidShell(t *testing.T) { - r := &model.Route{ + r := model.Route{ Entrypoint: locateJailLover() + ` "`, } @@ -176,7 +176,7 @@ func TestSpawnErrorsWhenEntrypointIsInvalidShell(t *testing.T) { out := &bytes.Buffer{} - err := spawn(h, out) + err := Spawn(h, out) if err == nil { t.Error("Invalid args not reported") @@ -184,7 +184,7 @@ func TestSpawnErrorsWhenEntrypointIsInvalidShell(t *testing.T) { } func TestSpawnRunsOKEntrypointWithMultipleArgs(t *testing.T) { - r := &model.Route{ + r := model.Route{ Entrypoint: locateJailLover() + " foo bar", } @@ -194,7 +194,7 @@ func TestSpawnRunsOKEntrypointWithMultipleArgs(t *testing.T) { out := &bytes.Buffer{} - _ = spawn(h, out) + _ = Spawn(h, out) jldata := decodeJailLover(out.Bytes()) @@ -204,7 +204,7 @@ func TestSpawnRunsOKEntrypointWithMultipleArgs(t *testing.T) { } func TestSpawnRunsOKEntrypointAndCommand(t *testing.T) { - r := &model.Route{ + r := model.Route{ Entrypoint: locateJailLover() + " foo bar", Command: "baz qux", } @@ -215,7 +215,7 @@ func TestSpawnRunsOKEntrypointAndCommand(t *testing.T) { out := &bytes.Buffer{} - _ = spawn(h, out) + _ = Spawn(h, out) jldata := decodeJailLover(out.Bytes()) @@ -223,3 +223,16 @@ func TestSpawnRunsOKEntrypointAndCommand(t *testing.T) { t.Error("Malformed cmdline") } } + +func TestSpawnReturnsErrorIfEntrypointNotSet(t *testing.T) { + r := model.Route{} + h := &model.Handler{ + Route: r, + } + + err := Spawn(h, nil) + + if err == nil { + t.Error("Spawn() did not report entrypoint not set") + } +}