mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
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:
committed by
Seif Lotfy سيف لطفي
parent
49cc0f6533
commit
28f713ed11
32
fn/run.go
32
fn/run.go
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user