runtime: Optimize func noNeedForOutput and add test cases

Optimize func noNeedForOutput and add test cases for this func.

Fixes: #2747

Signed-off-by: bin <bin@hyper.sh>
This commit is contained in:
bin 2021-09-28 16:58:44 +08:00
parent 05995632c3
commit 18bff58487
2 changed files with 40 additions and 9 deletions

View File

@ -112,13 +112,5 @@ func getAddress(ctx context.Context, bundlePath, address, id string) (string, er
}
func noNeedForOutput(detach bool, tty bool) bool {
if !detach {
return false
}
if !tty {
return false
}
return true
return detach && tty
}

View File

@ -16,6 +16,9 @@ import (
"path"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/opencontainers/runtime-spec/specs-go"
@ -326,3 +329,39 @@ func writeOCIConfigFile(spec specs.Spec, configPath string) error {
return ioutil.WriteFile(configPath, bytes, testFileMode)
}
func TestNoNeedForOutput(t *testing.T) {
assert := assert.New(t)
testCases := []struct {
detach bool
tty bool
result bool
}{
{
detach: true,
tty: true,
result: true,
},
{
detach: false,
tty: true,
result: false,
},
{
detach: true,
tty: false,
result: false,
},
{
detach: false,
tty: false,
result: false,
},
}
for i := range testCases {
result := noNeedForOutput(testCases[i].detach, testCases[i].tty)
assert.Equal(testCases[i].result, result)
}
}