grype/test/cli/subprocess_test.go
Alex Goodman afc9de6058
Fix hang when running as a subprocess (#484)
* use named pipe bit on stdin as indicator for piped input

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* ensure stdin is ignored when the CLI hints are present

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* add CLI test to cover subprocess integration behavior

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* added test case for java regression

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* remove extra line in makefile

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
2021-10-29 14:51:58 +00:00

61 lines
1.6 KiB
Go

package cli
import (
"fmt"
"path"
"strings"
"testing"
"time"
"github.com/anchore/stereoscope/pkg/imagetest"
)
func TestSubprocessStdin(t *testing.T) {
binDir := path.Dir(getGrypeSnapshotLocation(t, "linux"))
tests := []struct {
name string
args []string
env map[string]string
assertions []traitAssertion
}{
{
// regression
name: "ensure can be used by node subprocess (without hanging)",
args: []string{"-v", fmt.Sprintf("%s:%s:ro", binDir, "/app/bin"), imagetest.LoadFixtureImageIntoDocker(t, "image-node-subprocess"), "node", "/app.js"},
env: map[string]string{
"GRYPE_CHECK_FOR_APP_UPDATE": "false",
},
assertions: []traitAssertion{
assertSucceedingReturnCode,
},
},
{
// regression: https://github.com/anchore/grype/issues/267
name: "ensure can be used by java subprocess (without hanging)",
args: []string{"-v", fmt.Sprintf("%s:%s:ro", binDir, "/app/bin"), imagetest.LoadFixtureImageIntoDocker(t, "image-java-subprocess"), "java", "/app.java"},
env: map[string]string{
"GRYPE_CHECK_FOR_APP_UPDATE": "false",
},
assertions: []traitAssertion{
assertSucceedingReturnCode,
},
},
}
for _, test := range tests {
testFn := func(t *testing.T) {
cmd := getDockerRunCommand(t, test.args...)
stdout, stderr := runCommand(cmd, test.env)
for _, traitAssertionFn := range test.assertions {
traitAssertionFn(t, stdout, stderr, cmd.ProcessState.ExitCode())
}
if t.Failed() {
t.Log("STDOUT:\n", stdout)
t.Log("STDERR:\n", stderr)
t.Log("COMMAND:", strings.Join(cmd.Args, " "))
}
}
testWithTimeout(t, test.name, 60*time.Second, testFn)
}
}