diff --git a/pkg/gator/reader/filereader.go b/pkg/gator/reader/filereader.go index 867bd211e52..ddc6808c1ff 100644 --- a/pkg/gator/reader/filereader.go +++ b/pkg/gator/reader/filereader.go @@ -148,7 +148,8 @@ func readStdin() ([]*unstructured.Unstructured, error) { return nil, fmt.Errorf("getting stdin info: %w", err) } - if stdinfo.Size() == 0 { + // check if data is being piped or redirected to stdin + if (stdinfo.Mode() & os.ModeCharDevice) != 0 { return nil, nil } diff --git a/test/gator/test/test.bats b/test/gator/test/test.bats index d03ae84736a..8299b50cc4e 100755 --- a/test/gator/test/test.bats +++ b/test/gator/test/test.bats @@ -46,6 +46,11 @@ match_yaml_msg () { # END OF HELPER FUNCTIONS #################################################################################################### +@test "gator test doesn't wait on stdin input" { + # this should fail with "no input data identified" + ! bin/gator test +} + @test "manifest with no violations piped to stdin returns 0 exit status" { bin/gator test < "$BATS_TEST_DIRNAME/fixtures/manifests/with-policies/no-violations.yaml" if [ "$?" -ne 0 ]; then @@ -54,10 +59,20 @@ match_yaml_msg () { fi } -@test "manifest with violations piped to stdin returns 1 exit status" { +@test "manifest with violations redirected to stdin returns 1 exit status" { ! bin/gator test < "$BATS_TEST_DIRNAME/fixtures/manifests/with-policies/with-violations.yaml" } +@test "manifest with violations piped to stdin returns 1 exit status" { + # first test that we fail the command + ! cat "$BATS_TEST_DIRNAME/fixtures/manifests/with-policies/with-violations.yaml" | bin/gator test + + output=$(! cat "$BATS_TEST_DIRNAME/fixtures/manifests/with-policies/with-violations.yaml" | bin/gator test) + + # now test that the failure reason is a violation + match_substring "${output[*]}" "Container in your has no " +} + @test "manifest with no violations included as flag returns 0 exit status" { bin/gator test --filename="$BATS_TEST_DIRNAME/fixtures/manifests/with-policies/no-violations.yaml" if [ "$?" -ne 0 ]; then