summaryrefslogtreecommitdiffstats
path: root/debian/tests/pgrep
blob: d39400e07222de8fc7a4866f15789a5b9538dfa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/sh

exec 2>&1

test_no_arguments() {
    ( pgrep >"${stdoutF}" 2>"${stderrF}" )
    rtrn=$?

    assertEquals "pgrep exit value" "2" "${rtrn}"
    grep -E '^pgrep: no matching criteria specified' "${stderrF}" > /dev/null
    assertTrue 'no matching line' $?
}

test_count() {
    ( pgrep -c "$fakeproc}" >"${stdoutF}" 2>"${stderrF}" )
    rtrn=$?

    assertEquals "pgrep -c exit value" "1" "${rtrn}"
    grep '^0$' "${stderrF}" > /dev/null
    assertFalse 'count 0 of missing process' $?
}

test_user_procs() {
    myUID=$(awk '$1 == "Uid:" { print $2}' "/proc/${myPID}/status" 2>/dev/null)
    if [ -z "${myUID:-}" ];then
       startSkipping
       myUID=0
    fi

    ( pgrep -u "${myUID}" >"${stdoutF}" 2>"${stderrF}" )
    rtrn=$?

    assertEquals "pgrep exit value" "0" "${rtrn}"

    grep "^${myPID}$" "${stderrF}" > /dev/null
    assertFalse "User ${myUID} doesn't match PID ${myPID}" $?
}

oneTimeSetUp() {
  # Define global variables for command output.
  stdoutF="${AUTOPKGTEST_TMP}/stdout"
  stderrF="${AUTOPKGTEST_TMP}/stderr"
  fakeproc='fakename-test'
  myPID=$$
}

setUp() {
  # Truncate the output files.
  cp /dev/null "${stdoutF}"
  cp /dev/null "${stderrF}"
}


# shellcheck disable=SC1091
. shunit2