From 6328691bb926422655c9e26ea2aaf3787351f716 Mon Sep 17 00:00:00 2001 From: Alex Pana <8968914+acpana@users.noreply.github.com> Date: Thu, 16 Feb 2023 22:27:26 +0000 Subject: [PATCH 1/4] wip fix Signed-off-by: Alex Pana <8968914+acpana@users.noreply.github.com> --- pkg/gator/reader/filereader.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pkg/gator/reader/filereader.go b/pkg/gator/reader/filereader.go index 867bd211e52..af4760400bb 100644 --- a/pkg/gator/reader/filereader.go +++ b/pkg/gator/reader/filereader.go @@ -143,15 +143,11 @@ func verifyFile(filename string) error { } func readStdin() ([]*unstructured.Unstructured, error) { - stdinfo, err := os.Stdin.Stat() + _, err := os.Stdin.Stat() if err != nil { return nil, fmt.Errorf("getting stdin info: %w", err) } - if stdinfo.Size() == 0 { - return nil, nil - } - us, err := ReadK8sResources(os.Stdin) if err != nil { return nil, fmt.Errorf("reading: %w", err) From d3b445c850dcfddb4f5f3cac2b009f2d00ecc3c4 Mon Sep 17 00:00:00 2001 From: Alex Pana <8968914+acpana@users.noreply.github.com> Date: Fri, 17 Feb 2023 01:39:07 +0000 Subject: [PATCH 2/4] add test for piping Signed-off-by: Alex Pana <8968914+acpana@users.noreply.github.com> --- test/gator/test/test.bats | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/gator/test/test.bats b/test/gator/test/test.bats index d03ae84736a..eac180f9911 100755 --- a/test/gator/test/test.bats +++ b/test/gator/test/test.bats @@ -54,10 +54,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 From 2d2d0189512294ff5b6d6776f70fc293c905575f Mon Sep 17 00:00:00 2001 From: Alex Pana <8968914+acpana@users.noreply.github.com> Date: Tue, 21 Feb 2023 18:15:54 +0000 Subject: [PATCH 3/4] dont block on stdin input Signed-off-by: Alex Pana <8968914+acpana@users.noreply.github.com> --- pkg/gator/reader/filereader.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pkg/gator/reader/filereader.go b/pkg/gator/reader/filereader.go index af4760400bb..ddc6808c1ff 100644 --- a/pkg/gator/reader/filereader.go +++ b/pkg/gator/reader/filereader.go @@ -143,11 +143,16 @@ func verifyFile(filename string) error { } func readStdin() ([]*unstructured.Unstructured, error) { - _, err := os.Stdin.Stat() + stdinfo, err := os.Stdin.Stat() if err != nil { return nil, fmt.Errorf("getting stdin info: %w", err) } + // check if data is being piped or redirected to stdin + if (stdinfo.Mode() & os.ModeCharDevice) != 0 { + return nil, nil + } + us, err := ReadK8sResources(os.Stdin) if err != nil { return nil, fmt.Errorf("reading: %w", err) From fc77db529d46d2a193cd6b4586729321b795b852 Mon Sep 17 00:00:00 2001 From: Alex Pana <8968914+acpana@users.noreply.github.com> Date: Tue, 21 Feb 2023 18:23:32 +0000 Subject: [PATCH 4/4] test stdin hang up Signed-off-by: Alex Pana <8968914+acpana@users.noreply.github.com> --- test/gator/test/test.bats | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/gator/test/test.bats b/test/gator/test/test.bats index eac180f9911..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