069c57b88ab21d1a55e22e8d7667a70df4adf558
[deb_dpdk.git] / debian / tests / test-initscripts
1 #!/bin/sh
2 set -e
3
4 basedir=$(dirname "$0")
5 . "${basedir}"/check-dpdk-supported-arch.sh
6
7 # Overall that could require up to 1.2G for hugepages in the test environment
8 EXPECT2MHP=10
9 # Some page sizes like e.g. 1G might not be available in all test environments
10 # The test still configures 1 page of 1G size.
11 # One of two things will happen, depending on the test environment:
12 # - has 1G huge page size => they will tried to be allocated (usually env is
13 #   too small, but we want to see it fail gracefully for that)
14 #   We will not check for the 1G alloc, as we know it often fails in small adt's
15 # - has no 1G huge page size (HW feature) => we check if it fails gracefully
16 EXPECT1GHP=1
17
18 DPDK_CONF="/etc/dpdk/dpdk.conf"
19 DPDK_INTERF="/etc/dpdk/interfaces"
20
21 checkhp() {
22     MMDIR="/sys/kernel/mm/hugepages/${1}"
23     EXPECTHP="${2}"
24     if [ -d "$MMDIR" -a -r "$MMDIR/nr_hugepages" ]; then
25         hpcount=$(cat "$MMDIR/nr_hugepages")
26         if [ "${hpcount}" -ne "${EXPECTHP}" ]; then
27             echo "Hugepages (${hpcount}) not as expected (${EXPECTHP})"
28             exit 1
29         else
30             echo "Hugepages ok (${hpcount})"
31         fi
32     fi
33 }
34
35 checkstatus() {
36     MARK=${1}
37     EXPMPCOUNT=${2}
38     PRE=${3}
39     POST=${4}
40     EXPECTEDSTATUS=${5}
41     echo "Status after ${MARK}"
42     echo "Status of the Service"
43     ${PRE} status "${POST}" || true
44
45     GOTSTATUS=$(${PRE} status "${POST}" | awk '/^ *Active: / { print $2 }')
46     if [ "${GOTSTATUS}" != "${EXPECTEDSTATUS}" ]; then
47         echo "Service status (${GOTSTATUS}) not as expected (${EXPECTEDSTATUS})"
48         exit 1
49     else
50         echo "Service status (${GOTSTATUS}) as expected"
51     fi
52
53     echo "Status of hugetlbfs mount points"
54     # this section is ok to create bad RCs when no mounts are available
55     set +e
56     grep hugetlbfs < /proc/mounts
57     htlbfscount=$(grep -c hugetlbfs < /proc/mounts)
58     set -e
59
60     # we have to reduce the expected mountpoint count in case some sizes are
61     # not supported by the current kernel/environment
62     if [ ${EXPMPCOUNT} -gt 0 ]; then
63         if [ ! -d /sys/kernel/mm/hugepages/hugepages-2048kB ]; then
64             EXPMPCOUNT=$((EXPMPCOUNT-1))
65         fi
66         if [ ! -d /sys/kernel/mm/hugepages/hugepages-1048576kB ]; then
67             EXPMPCOUNT=$((EXPMPCOUNT-1))
68         fi
69     fi
70
71     if [ "${htlbfscount}" -eq "${EXPMPCOUNT}" ]; then
72         echo "MP Count (${htlbfscount}) as expected (${EXPMPCOUNT})"
73     else
74         echo "MP Count (${htlbfscount}) not as expected (${EXPMPCOUNT})"
75         exit 1
76     fi
77
78     # check if setting HP worked
79     if [ "${EXPMPCOUNT}" -ne "0" ]; then
80         checkhp "hugepages-2048kB" "${EXPECT2MHP}"
81         # We do not check 1G alloc, as it is known to be often not available
82     fi
83 }
84
85 resetservice() {
86     # help a bit with memory fragmentation regarding huge page allocation
87     sync
88     echo 3 > /proc/sys/vm/drop_caches
89
90     # stopping and resetting Service
91     systemctl stop dpdk.service || /bin/true
92     systemctl reset-failed dpdk.service || /bin/true
93
94     echo "Unmounting all potential hugetlbfs mounts"
95     awk '/hugetlbfs/ {print $2}' /proc/mounts | while read hugetlbmount; do
96         umount -v "$hugetlbmount"
97     done
98 }
99
100 checkinitstyle() {
101     # We want to verify that
102     # - initially our environment has no hugetlbfs mount
103     # - a system without hugetlbfs mount gets it mounted
104     # - a restart of the service does neither drop nor duplicate the mount
105     PRE=${1}
106     POST=${2}
107     TYPE=${3}
108     printf "\n\n### Checking Type %s ###\n" "${TYPE}"
109     resetservice
110     checkstatus "${TYPE}-BEGIN" 0 "${PRE}" "${POST}" "inactive"
111     echo "### Starting Service ###"
112     ${PRE} start "${POST}"
113     checkstatus "${TYPE}-START" 2 "${PRE}" "${POST}" "active"
114     echo "### Restarting Service ###"
115     ${PRE} restart "${POST}"
116     checkstatus "${TYPE}-RESTART" 2 "${PRE}" "${POST}" "active"
117 }
118
119 echo "NR_2M_PAGES=$EXPECT2MHP" >> ${DPDK_CONF}
120 echo "NR_1G_PAGES=$EXPECT1GHP" >> ${DPDK_CONF}
121
122 # We can't rely on any real device for DPDK tests in adt-* environments. But
123 # we can expect all kind of broken configuration not to break it (would be
124 # detected via set -e).
125 # So add all kind of known-to-be-broken definitions and expect it not to fail.
126 cat <<EOF > ${DPDK_INTERF}
127 # wrong bus
128 pTi 0000:04:00.0 uio-pci-generic
129 # not enough parms
130 0000:04:00.0 uio-pci-generic
131 # empty line
132
133 # non existing device
134 pci 1234:56:78.9 uio-pci-generic
135 EOF
136
137 # some had issues in the past caused by different init systems, so we test all
138 # Direct Calls
139 checkinitstyle "/etc/init.d/dpdk" "" "Direct"
140 # System V style init
141 checkinitstyle "service dpdk" "" "SysV"
142 # SystemD style init
143 checkinitstyle "systemctl" "dpdk.service" "SystemD"