github.com/crowdsecurity/crowdsec@v1.6.1/test/bats/00_wait_for.bats (about)

     1  #!/usr/bin/env bats
     2  # vim: ft=bats:list:ts=8:sts=4:sw=4:et:ai:si:
     3  
     4  set -u
     5  
     6  setup_file() {
     7      load "../lib/setup_file.sh"
     8  }
     9  
    10  setup() {
    11      load "../lib/setup.sh"
    12  }
    13  
    14  @test "run a command and capture its stdout" {
    15      run -0 wait-for seq 1 3
    16      assert_output - <<-EOT
    17  	1
    18  	2
    19  	3
    20  	EOT
    21  }
    22  
    23  @test "run a command and capture its stderr" {
    24      rune -0 wait-for sh -c 'seq 1 3 >&2'
    25      assert_stderr - <<-EOT
    26  	1
    27  	2
    28  	3
    29  	EOT
    30  }
    31  
    32  @test "run a command until a pattern is found in stdout" {
    33      run -0 wait-for --out "1[12]0" seq 1 200
    34      assert_line --index 0 "1"
    35      assert_line --index -1 "110"
    36      refute_line "111"
    37  }
    38  
    39  @test "run a command until a pattern is found in stderr" {
    40      rune -0 wait-for --err "10" sh -c 'seq 1 20 >&2'
    41      assert_stderr - <<-EOT
    42  	1
    43  	2
    44  	3
    45  	4
    46  	5
    47  	6
    48  	7
    49  	8
    50  	9
    51  	10
    52  	EOT
    53  }
    54  
    55  @test "run a command with timeout (no match)" {
    56      # when the process is terminated without a match, it returns
    57      # 256 - 15 (SIGTERM) = 241
    58      rune -241 wait-for --timeout 0.1 --out "10" sh -c 'echo 1; sleep 3; echo 2'
    59      assert_line 1
    60      # there may be more, but we don't care
    61  }
    62  
    63  @test "run a command with timeout (match)" {
    64      # when the process is terminated with a match, return code is 128
    65      rune -128 wait-for --timeout .4 --out "2" sh -c 'echo 1; sleep .1; echo 2; echo 3; echo 4; sleep 10'
    66      assert_output - <<-EOT
    67  	1
    68  	2
    69  	EOT
    70  }
    71