Skip to content

Commit

Permalink
fix: piping input in gator (#2589)
Browse files Browse the repository at this point in the history
Co-authored-by: Sertaç Özercan <852750+sozercan@users.noreply.github.com>
  • Loading branch information
acpana and sozercan authored Feb 22, 2023
1 parent 4ed4663 commit ab0c959
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pkg/gator/reader/filereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
17 changes: 16 additions & 1 deletion test/gator/test/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <tomcat> in your <Pod> <test-pod1> has no <readinessProbe>"
}

@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
Expand Down

0 comments on commit ab0c959

Please sign in to comment.