fn: support for functions testing (#379)

* fn: add test framework

* fn: make routes creation smarter

* fn: add testframework examples

* fn: remove unnecessary dependency

* fn: update doc

* fn: fix consistenty between runff, runlocaltest and runremotetest
This commit is contained in:
C Cirello
2016-12-05 17:05:06 +01:00
committed by Seif Lotfy سيف لطفي
parent 49cc0f6533
commit 28f713ed11
17 changed files with 442 additions and 51 deletions

View File

@@ -3,6 +3,7 @@ package main
import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
@@ -46,12 +47,16 @@ func (r *runCmd) run(c *cli.Context) error {
image = ff.FullName()
}
return runff(image, stdin(), os.Stdout, os.Stderr, c.StringSlice("e"))
}
func runff(image string, stdin io.Reader, stdout, stderr io.Writer, restrictedEnv []string) error {
sh := []string{"docker", "run", "--rm", "-i"}
var env []string
detectedEnv := os.Environ()
if se := c.StringSlice("e"); len(se) > 0 {
detectedEnv = se
if len(restrictedEnv) > 0 {
detectedEnv = restrictedEnv
}
for _, e := range detectedEnv {
@@ -67,26 +72,9 @@ func (r *runCmd) run(c *cli.Context) error {
sh = append(sh, image)
cmd := exec.Command(sh[0], sh[1:]...)
// Check if stdin is being piped, and if not, create our own pipe with nothing in it
// http://stackoverflow.com/questions/22744443/check-if-there-is-something-to-read-on-stdin-in-golang
stat, err := os.Stdin.Stat()
if err != nil {
// On Windows, this gets an error if nothing is piped in.
// If something is piped in, it works fine.
// Turns out, this works just fine in our case as the piped stuff works properly and the non-piped doesn't hang either.
// See: https://github.com/golang/go/issues/14853#issuecomment-260170423
// log.Println("Warning: couldn't stat stdin, you are probably on Windows. Be sure to pipe something into this command, eg: 'echo \"hello\" | fn run'")
} else {
if (stat.Mode() & os.ModeCharDevice) == 0 {
// log.Println("data is being piped to stdin")
cmd.Stdin = os.Stdin
} else {
// log.Println("stdin is from a terminal")
cmd.Stdin = strings.NewReader("")
}
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = stdin
cmd.Stdout = stdout
cmd.Stderr = stderr
cmd.Env = env
return cmd.Run()
}