internal/server/model.Handler.Route is no longer a pointer

Also Spawn() now checks if entrypoint is not set

Co-authored-by: Roberto Abdelkader Martínez Pérez <robertomartinezp@gmail.com>
This commit is contained in:
pancho horrillo
2019-10-10 18:10:10 +02:00
parent 25d34bda0a
commit 2129c4a1f6
3 changed files with 42 additions and 26 deletions
+2 -3
View File
@@ -13,9 +13,8 @@ type Handler struct {
// ID is unique identifier of the request. // ID is unique identifier of the request.
ID string ID string
// Route is a reference to the original route that matched this // Route is the original route that matched this request.
// request. Route
Route *Route
// Writing is a mutex that prevents two goroutines from writing at // Writing is a mutex that prevents two goroutines from writing at
// the same time in the response. // the same time in the response.
@@ -1,6 +1,7 @@
package user package spawn
import ( import (
"errors"
"io" "io"
"os" "os"
"os/exec" "os/exec"
@@ -10,7 +11,10 @@ import (
"github.com/BBVA/kapow/internal/server/model" "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) args, err := shlex.Split(h.Route.Entrypoint)
if err != nil { if err != nil {
return err return err
@@ -1,4 +1,4 @@
package user package spawn
import ( import (
"bytes" "bytes"
@@ -34,7 +34,7 @@ func locateJailLover() string {
} }
func TestSpawnRetursErrorWhenEntrypointIsBad(t *testing.T) { func TestSpawnRetursErrorWhenEntrypointIsBad(t *testing.T) {
r := &model.Route{ r := model.Route{
Entrypoint: "/bin/this_executable_is_not_likely_to_exist", Entrypoint: "/bin/this_executable_is_not_likely_to_exist",
} }
@@ -42,14 +42,14 @@ func TestSpawnRetursErrorWhenEntrypointIsBad(t *testing.T) {
Route: r, Route: r,
} }
err := spawn(h, nil) err := Spawn(h, nil)
if err == nil { if err == nil {
t.Error("Bad executable not reported") t.Error("Bad executable not reported")
} }
} }
func TestSpawnReturnsNilWhenEntrypointIsGood(t *testing.T) { func TestSpawnReturnsNilWhenEntrypointIsGood(t *testing.T) {
r := &model.Route{ r := model.Route{
Entrypoint: locateJailLover(), Entrypoint: locateJailLover(),
} }
@@ -57,14 +57,14 @@ func TestSpawnReturnsNilWhenEntrypointIsGood(t *testing.T) {
Route: r, Route: r,
} }
err := spawn(h, nil) err := Spawn(h, nil)
if err != nil { if err != nil {
t.Error("Good executable reported") t.Error("Good executable reported")
} }
} }
func TestSpawnWritesToStdout(t *testing.T) { func TestSpawnWritesToStdout(t *testing.T) {
r := &model.Route{ r := model.Route{
Entrypoint: locateJailLover(), Entrypoint: locateJailLover(),
} }
@@ -74,7 +74,7 @@ func TestSpawnWritesToStdout(t *testing.T) {
out := &bytes.Buffer{} out := &bytes.Buffer{}
_ = spawn(h, out) _ = Spawn(h, out)
jldata := decodeJailLover(out.Bytes()) jldata := decodeJailLover(out.Bytes())
@@ -85,7 +85,7 @@ func TestSpawnWritesToStdout(t *testing.T) {
} }
func TestSpawnSetsKapowURLEnvVar(t *testing.T) { func TestSpawnSetsKapowURLEnvVar(t *testing.T) {
r := &model.Route{ r := model.Route{
Entrypoint: locateJailLover(), Entrypoint: locateJailLover(),
} }
@@ -95,7 +95,7 @@ func TestSpawnSetsKapowURLEnvVar(t *testing.T) {
out := &bytes.Buffer{} out := &bytes.Buffer{}
_ = spawn(h, out) _ = Spawn(h, out)
jldata := decodeJailLover(out.Bytes()) jldata := decodeJailLover(out.Bytes())
@@ -105,7 +105,7 @@ func TestSpawnSetsKapowURLEnvVar(t *testing.T) {
} }
func TestSpawnSetsKapowHandlerIDEnvVar(t *testing.T) { func TestSpawnSetsKapowHandlerIDEnvVar(t *testing.T) {
r := &model.Route{ r := model.Route{
Entrypoint: locateJailLover(), Entrypoint: locateJailLover(),
} }
@@ -116,7 +116,7 @@ func TestSpawnSetsKapowHandlerIDEnvVar(t *testing.T) {
out := &bytes.Buffer{} out := &bytes.Buffer{}
_ = spawn(h, out) _ = Spawn(h, out)
jldata := decodeJailLover(out.Bytes()) jldata := decodeJailLover(out.Bytes())
@@ -126,7 +126,7 @@ func TestSpawnSetsKapowHandlerIDEnvVar(t *testing.T) {
} }
func TestSpawnRunsOKEntrypointsWithAParam(t *testing.T) { func TestSpawnRunsOKEntrypointsWithAParam(t *testing.T) {
r := &model.Route{ r := model.Route{
Entrypoint: locateJailLover() + " -foo", Entrypoint: locateJailLover() + " -foo",
} }
@@ -136,7 +136,7 @@ func TestSpawnRunsOKEntrypointsWithAParam(t *testing.T) {
out := &bytes.Buffer{} out := &bytes.Buffer{}
_ = spawn(h, out) _ = Spawn(h, out)
jldata := decodeJailLover(out.Bytes()) jldata := decodeJailLover(out.Bytes())
@@ -146,7 +146,7 @@ func TestSpawnRunsOKEntrypointsWithAParam(t *testing.T) {
} }
func TestSpawnRunsOKEntrypointWithArgWithSpace(t *testing.T) { func TestSpawnRunsOKEntrypointWithArgWithSpace(t *testing.T) {
r := &model.Route{ r := model.Route{
Entrypoint: locateJailLover() + ` "foo bar"`, Entrypoint: locateJailLover() + ` "foo bar"`,
} }
@@ -156,7 +156,7 @@ func TestSpawnRunsOKEntrypointWithArgWithSpace(t *testing.T) {
out := &bytes.Buffer{} out := &bytes.Buffer{}
_ = spawn(h, out) _ = Spawn(h, out)
jldata := decodeJailLover(out.Bytes()) jldata := decodeJailLover(out.Bytes())
@@ -166,7 +166,7 @@ func TestSpawnRunsOKEntrypointWithArgWithSpace(t *testing.T) {
} }
func TestSpawnErrorsWhenEntrypointIsInvalidShell(t *testing.T) { func TestSpawnErrorsWhenEntrypointIsInvalidShell(t *testing.T) {
r := &model.Route{ r := model.Route{
Entrypoint: locateJailLover() + ` "`, Entrypoint: locateJailLover() + ` "`,
} }
@@ -176,7 +176,7 @@ func TestSpawnErrorsWhenEntrypointIsInvalidShell(t *testing.T) {
out := &bytes.Buffer{} out := &bytes.Buffer{}
err := spawn(h, out) err := Spawn(h, out)
if err == nil { if err == nil {
t.Error("Invalid args not reported") t.Error("Invalid args not reported")
@@ -184,7 +184,7 @@ func TestSpawnErrorsWhenEntrypointIsInvalidShell(t *testing.T) {
} }
func TestSpawnRunsOKEntrypointWithMultipleArgs(t *testing.T) { func TestSpawnRunsOKEntrypointWithMultipleArgs(t *testing.T) {
r := &model.Route{ r := model.Route{
Entrypoint: locateJailLover() + " foo bar", Entrypoint: locateJailLover() + " foo bar",
} }
@@ -194,7 +194,7 @@ func TestSpawnRunsOKEntrypointWithMultipleArgs(t *testing.T) {
out := &bytes.Buffer{} out := &bytes.Buffer{}
_ = spawn(h, out) _ = Spawn(h, out)
jldata := decodeJailLover(out.Bytes()) jldata := decodeJailLover(out.Bytes())
@@ -204,7 +204,7 @@ func TestSpawnRunsOKEntrypointWithMultipleArgs(t *testing.T) {
} }
func TestSpawnRunsOKEntrypointAndCommand(t *testing.T) { func TestSpawnRunsOKEntrypointAndCommand(t *testing.T) {
r := &model.Route{ r := model.Route{
Entrypoint: locateJailLover() + " foo bar", Entrypoint: locateJailLover() + " foo bar",
Command: "baz qux", Command: "baz qux",
} }
@@ -215,7 +215,7 @@ func TestSpawnRunsOKEntrypointAndCommand(t *testing.T) {
out := &bytes.Buffer{} out := &bytes.Buffer{}
_ = spawn(h, out) _ = Spawn(h, out)
jldata := decodeJailLover(out.Bytes()) jldata := decodeJailLover(out.Bytes())
@@ -223,3 +223,16 @@ func TestSpawnRunsOKEntrypointAndCommand(t *testing.T) {
t.Error("Malformed cmdline") 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")
}
}