Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: piping input in gator #2589

Merged
merged 7 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so why is this breaking piping? from my understanding and offline convos, data is being actively piped so the buffer may not have data yet so we bail too early.

// 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