From 11575952ece456be4e07c5f24ead3cf8783ddcba Mon Sep 17 00:00:00 2001 From: jobol Date: Wed, 14 Mar 2018 13:58:25 +0100 Subject: service-ids-check: Add check uid/gid of services This new test allows to check whether service run with expected uid and gid. This check doesn't check that the service runs because there is an other check for that. It currently only checks weston service to run as display/display but it can be easily extended for checking other services. Bug-AGL: SPEC-546 Signed-off-by: jobol --- common/scripts/service-ids-check.sh | 78 ++++++++++++++++++++++++++ test-suites/short-smoke/service-ids-check.yaml | 15 +++++ 2 files changed, 93 insertions(+) create mode 100755 common/scripts/service-ids-check.sh create mode 100644 test-suites/short-smoke/service-ids-check.yaml diff --git a/common/scripts/service-ids-check.sh b/common/scripts/service-ids-check.sh new file mode 100755 index 0000000..f1fbef1 --- /dev/null +++ b/common/scripts/service-ids-check.sh @@ -0,0 +1,78 @@ +#!/bin/bash +#----------------------------------------------------- +# This scripts allows to check the ids (user and/or group) +# of running services. +# It doesn't check that the service runs because this is +# the object of other tests. +# If a process doesn't run it emits an unknown status for +# the service, but not a fail. +# When there are multiple instance of a service, it checks +# that at least one instance matches expectation. +# Author: jose.bollo@iot.bzh +#----------------------------------------------------- + +# out of LAVA run +if ! type lava-test-case 2>/dev/null; then + lava-test-case() { echo "lava-test-case $*"; } +fi +if ! type lava-test-set 2>/dev/null; then + lava-test-set() { echo "lava-test-set $*"; } +fi + +# extraction of numeric ids for user and group +get-id() { + local id="$1" file="$2" + if grep -q "^${id}:" "$file"; then + grep "^${id}:" "$file" | cut -d: -f3 + else + echo -n "$id" + fi +} +get-uid() { get-id "$1" /etc/passwd; } +get-gid() { get-id "$1" /etc/group; } + +# extraction of numeric effective ids of processes +get-p-id() { + local pid="$1" field="$2" + grep "$field" "/proc/$pid/status" | cut -f3 +} +get-p-uid() { get-p-id "$1" "Uid:"; } +get-p-gid() { get-p-id "$1" "Gid:"; } + +# check the process status +check-p-ids() { + local pid="$1" uid="$2" gid="$3" + local u=$(get-p-uid "$pid") + local g=$(get-p-gid "$pid") + [[ -z "$uid" || "$uid" -eq "$u" ]] && [[ -z "$gid" || "$gid" -eq "$g" ]] +} +check-user-group() { + local service="$1" user="$2" group="$3" + local name="${service}-${user}-${group}" + local pid uid=$(get-uid "$user") gid=$(get-gid "$group") + + if ! pgrep -c "^$service\$" > /dev/null 2>&1; then + lava-test-case "$name" --result unknown + return 0 + fi + + for pid in $(pgrep "^$service\$"); do + if check-p-ids "$pid" "$uid" "$gid"; then + lava-test-case "$name" --result pass + return 0 + fi + done + lava-test-case "$name" --result fail + return 1 +} +check-user() { check-user-group "$1" "$2" ""; } +check-group() { check-user-group "$1" "" "$2"; } + +# the test effective +lava-test-set start check-services-user-group + +check-user-group weston display display + +lava-test-set stop check-services-user-group + +exit 0 diff --git a/test-suites/short-smoke/service-ids-check.yaml b/test-suites/short-smoke/service-ids-check.yaml new file mode 100644 index 0000000..e491fec --- /dev/null +++ b/test-suites/short-smoke/service-ids-check.yaml @@ -0,0 +1,15 @@ +metadata: + name: service-ids-check + format: "Lava-Test-Shell Test Definition 1.0" + description: "Check the ids of services" + os: + - openembedded + scope: + - functional + maintainer: + - jose.bollo@iot.bzh + +run: + steps: + - "cd common/scripts" + - "./service-ids-check.sh" -- cgit 1.2.3-korg