diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c69819 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__/ +.vagrant/ +.idea/ diff --git a/README.md b/README.md index fdcb4b3..c232a56 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ -# QA +# QA - Тесты RPM-пакетов + +См. описание [здесь](https://git.inferitos.ru/internal/documentation/wiki/%D0%A2%D0%B5%D1%81%D1%82%D1%8B-RPM-%D0%BF%D0%B0%D0%BA%D0%B5%D1%82%D0%BE%D0%B2) -Тесты MSVSphere ОС \ No newline at end of file diff --git a/library/assertions.bash b/library/assertions.bash new file mode 100755 index 0000000..479fb65 --- /dev/null +++ b/library/assertions.bash @@ -0,0 +1,125 @@ +#!/bin/bash + +flunk() { + { if [ "$#" -eq 0 ]; then cat - + else echo -e "$@" + fi + } + return 1 +} + +assert_success() { + if [[ 0 -ne "$status" ]]; then + flunk "command failed with exit status ${status}\\noutput: ${output}" + elif [ "$#" -gt 0 ]; then + assert_output "$1" + fi +} + +assert_failure() { + if [[ 0 -eq "$status" ]]; then + flunk "expected failed exit status" + elif [ "$#" -gt 0 ]; then + assert_output "$1" + fi +} + +assert_equal() { + if [[ "$1" != "$2" ]]; then + { echo "expected: $1" + echo "actual: $2" + } | flunk + fi +} + +assert_different() { + if [[ "$1" == "$2" ]]; then + { + echo "expected: $1 != $2" + } | flunk + fi +} + +assert_match() { + out="${2:-${output}}" + if [ ! $(echo "${out}" | grep -- "${1}") ]; then + { echo "expected match: $1" + echo "actual: $out" + } | flunk + fi +} + +assert_not_match() { + out="${2:-${output}}" + if [ $(echo "${out}" | grep -- "${1}") ]; then + { echo "unexpected match: $1" + echo "source: $out" + } | flunk + fi +} + +assert_output() { + local expected + if [ $# -eq 0 ]; then expected="$(cat -)" + else expected="$1" + fi + assert_equal "$expected" "$output" +} + +assert_line() { + if [ "$1" -ge 0 ] 2>/dev/null; then + assert_equal "$2" "${lines[$1]}" + else + local line + for line in "${lines[@]}"; do + if [ "$line" = "$1" ]; then return 0; fi + done + flunk "expected line \`$1' in \n $output" + fi +} + +refute_line() { + if [ "$1" -ge 0 ] 2>/dev/null; then + local num_lines="${#lines[@]}" + if [ "$1" -lt "$num_lines" ]; then + flunk "output has $num_lines lines" + fi + else + local line + for line in "${lines[@]}"; do + if [ "$line" = "$1" ]; then + flunk "expected to not find line \`$line'" + fi + done + fi +} + +assert_include() { + eval 'local values=("${'$1'[@]}")' + + local element + for element in "${values[@]}"; do + [[ "$element" == "$2" ]] && return 0 + done + + flunk "failed: array ${values[*]} not include $2}" +} + +assert() { + if ! "$@"; then + flunk "failed: $@" + fi +} + +assert_running() { + ps aux | grep $1 >> /dev/null + if [ $? -ne 0 ]; then + flunk "process with id $1 is not running" + fi +} + +assert_not_running() { + if [ -d /proc/$1 ]; then + flunk "process with id $1 is running" + fi +} diff --git a/library/sh_lib.sh b/library/sh_lib.sh new file mode 100644 index 0000000..09ac408 --- /dev/null +++ b/library/sh_lib.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +set -uo pipefail + + +function check_test_status { + local exit_code=$? + if [ ${1} -ne 0 ]; then + echo "test [FAILED]: $2 Code: $1" + else + echo "test [PASSED]: $2" + fi + return ${exit_code} +} + +function eq_is_success { + local exit_code=$? + local check=$1 + if [[ ${exit_code} -ne ${2} ]]; then + let check+=1 + fi + echo ${check} + return ${exit_code} +} + +function not_eq_is_success { + local exit_code=$? + local check=$1 + if [[ ${exit_code} -eq ${2} ]]; then + let check+=1 + fi + echo ${check} + return ${exit_code} +} diff --git a/openqa/p_mariadb/01-check_service.sh b/openqa/p_mariadb/01-check_service.sh new file mode 100755 index 0000000..34176ad --- /dev/null +++ b/openqa/p_mariadb/01-check_service.sh @@ -0,0 +1,11 @@ +#!/bin/bash -x + +source ../../library/sh_lib.sh + +check=0 + +/bin/systemctl status --no-pager mariadb | /bin/grep -E 'running;|enabled;' +check=$(eq_is_success ${check} 0) + +check_test_status ${check} "$0" +exit ${check} diff --git a/openqa/p_mariadb/_init.sh b/openqa/p_mariadb/_init.sh new file mode 100755 index 0000000..b8d80c5 --- /dev/null +++ b/openqa/p_mariadb/_init.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +systemctl enable mariadb +systemctl restart mariadb + +exit 0 diff --git a/openqa/p_postgres/01-check_service.sh b/openqa/p_postgres/01-check_service.sh new file mode 100755 index 0000000..210839a --- /dev/null +++ b/openqa/p_postgres/01-check_service.sh @@ -0,0 +1,11 @@ +#!/bin/bash -x + +source ../../library/sh_lib.sh + +check=0 + +/bin/systemctl status --no-pager postgresql | /bin/grep -E 'running;|enabled;' +check=$(eq_is_success ${check} 0) + +check_test_status ${check} "$0" +exit ${check} diff --git a/openqa/p_postgres/_init.sh b/openqa/p_postgres/_init.sh new file mode 100755 index 0000000..6b1ab0d --- /dev/null +++ b/openqa/p_postgres/_init.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +source ../../library/sh_lib.sh + +check=0 + +postgresql-setup --initdb +check=$(eq_is_success ${check} 0) + +systemctl enable postgresql +systemctl restart postgresql + +check_test_status ${check} "$0" +exit ${check} diff --git a/rpm_tests/p_gcc/001-prepare_environment.sh b/rpm_tests/p_gcc/001-prepare_environment.sh new file mode 100755 index 0000000..ca89a1b --- /dev/null +++ b/rpm_tests/p_gcc/001-prepare_environment.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +echo "Подготовка окружения для тестирования пакета ${TEST_PACKAGE_NAME}" + +dnf -y install man man-pages util-linux + +exit 0 diff --git a/rpm_tests/p_gcc/01-bugzilla-links.sh b/rpm_tests/p_gcc/01-bugzilla-links.sh new file mode 100755 index 0000000..c4c7528 --- /dev/null +++ b/rpm_tests/p_gcc/01-bugzilla-links.sh @@ -0,0 +1,27 @@ +#!/bin/bash -x + +echo "Тест наличия bugzilla ссылок" + +source ../../library/sh_lib.sh + +check=0 + +test_gcc_v(){ + echo "INFO: Testing bugzilla URL in gcc -v" + gcc -v 2>&1 | grep "\-\-with\-bugurl=https://bugs.msvsphere.ru/" + check=$(eq_is_success ${check} 0) +} + +test_gcc_man_page(){ + echo "INFO: Testing bugzilla URL in man gcc" + /bin/man -P /bin/cat gcc | /bin/grep https://bugs.msvsphere.ru/ + check=$(eq_is_success ${check} 0) +} + +############ +# Run tests +test_gcc_v +test_gcc_man_page + +check_test_status ${check} "$0" +exit ${check} diff --git a/rpm_tests/p_gcc/02-compile-test.sh b/rpm_tests/p_gcc/02-compile-test.sh new file mode 100755 index 0000000..9dc7ef0 --- /dev/null +++ b/rpm_tests/p_gcc/02-compile-test.sh @@ -0,0 +1,16 @@ +#!/bin/bash -x + +echo "Тест компилятора" + +source ../../library/sh_lib.sh + +check=0 + +gcc -o simple_test files/simple_test.c +check=$(eq_is_success ${check} 0) + +./simple_test | grep -E '^Hello, world!$' +check=$(eq_is_success ${check} 0) + +check_test_status ${check} "$0" +exit ${check} diff --git a/rpm_tests/p_gcc/files/simple_test.c b/rpm_tests/p_gcc/files/simple_test.c new file mode 100644 index 0000000..cb26f85 --- /dev/null +++ b/rpm_tests/p_gcc/files/simple_test.c @@ -0,0 +1,10 @@ +// simple-test.c + +#include + +int main ( void ) +{ + printf ("Hello, world!"); + + return 0; +} // main \ No newline at end of file diff --git a/rpm_tests/p_grub2-efi-x64-cdboot/001-prepare_environment.sh b/rpm_tests/p_grub2-efi-x64-cdboot/001-prepare_environment.sh new file mode 120000 index 0000000..8d5a025 --- /dev/null +++ b/rpm_tests/p_grub2-efi-x64-cdboot/001-prepare_environment.sh @@ -0,0 +1 @@ +../p_grub2-efi-x64/001-prepare_environment.sh \ No newline at end of file diff --git a/rpm_tests/p_grub2-efi-x64-cdboot/01-files-check.sh b/rpm_tests/p_grub2-efi-x64-cdboot/01-files-check.sh new file mode 120000 index 0000000..1d9fb55 --- /dev/null +++ b/rpm_tests/p_grub2-efi-x64-cdboot/01-files-check.sh @@ -0,0 +1 @@ +../p_grub2-efi-x64/01-files-check.sh \ No newline at end of file diff --git a/rpm_tests/p_grub2-efi-x64-cdboot/02-pesigcheck.sh b/rpm_tests/p_grub2-efi-x64-cdboot/02-pesigcheck.sh new file mode 120000 index 0000000..4008d0b --- /dev/null +++ b/rpm_tests/p_grub2-efi-x64-cdboot/02-pesigcheck.sh @@ -0,0 +1 @@ +../p_grub2-efi-x64/02-pesigcheck.sh \ No newline at end of file diff --git a/rpm_tests/p_grub2-efi-x64/001-prepare_environment.sh b/rpm_tests/p_grub2-efi-x64/001-prepare_environment.sh new file mode 100755 index 0000000..8c9734d --- /dev/null +++ b/rpm_tests/p_grub2-efi-x64/001-prepare_environment.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +echo "Подготовка окружения для тестирования пакета ${TEST_PACKAGE_NAME}" + +# Установка вспомогательных пакетов +dnf install -y sphere-sb-certs pesign + +exit 0 diff --git a/rpm_tests/p_grub2-efi-x64/01-files-check.sh b/rpm_tests/p_grub2-efi-x64/01-files-check.sh new file mode 100755 index 0000000..14280de --- /dev/null +++ b/rpm_tests/p_grub2-efi-x64/01-files-check.sh @@ -0,0 +1,26 @@ +#!/bin/bash -x + +echo "Тест наличия файлов пакета ${TEST_PACKAGE_NAME}" + +source ../../library/sh_lib.sh + +if [[ "${TEST_PACKAGE_NAME}" != "grub2-efi-x64" ]] && [[ "${TEST_PACKAGE_NAME}" != "grub2-efi-x64-cdboot" ]]; then + echo "ERROR: Неверное имя пакета - '${TEST_PACKAGE_NAME}'. Этот тест предназначен для пакетов grub2-efi-x64 и grub2-efi-x64-cdboot" + exit 1 +fi + +if [[ "${TEST_PACKAGE_NAME}" == "grub2-efi-x64" ]]; then + _FILE1='/boot/efi/EFI/msvsphere/grubx64.efi' +else + _FILE1='/boot/efi/EFI/msvsphere/gcdx64.efi' +fi + +check=0 + +if [[ ! -f "${_FILE1}" ]]; then + echo "ERROR: File ${_FILE1} absent!" + let check+=1 +fi + +check_test_status ${check} "$0" +exit ${check} diff --git a/rpm_tests/p_grub2-efi-x64/02-pesigcheck.sh b/rpm_tests/p_grub2-efi-x64/02-pesigcheck.sh new file mode 100755 index 0000000..19497d0 --- /dev/null +++ b/rpm_tests/p_grub2-efi-x64/02-pesigcheck.sh @@ -0,0 +1,24 @@ +#!/bin/bash -x + +echo "Тест pesigcheck файлов пакета ${TEST_PACKAGE_NAME}" + +source ../../library/sh_lib.sh + +if [[ "${TEST_PACKAGE_NAME}" != "grub2-efi-x64" ]] && [[ "${TEST_PACKAGE_NAME}" != "grub2-efi-x64-cdboot" ]]; then + echo "ERROR: Неверное имя пакета - '${TEST_PACKAGE_NAME}'. Этот тест предназначен для пакетов grub2-efi-x64 и grub2-efi-x64-cdboot" + exit 1 +fi + +if [[ "${TEST_PACKAGE_NAME}" == "grub2-efi-x64" ]]; then + _FILE1='/boot/efi/EFI/msvsphere/grubx64.efi' +else + _FILE1='/boot/efi/EFI/msvsphere/gcdx64.efi' +fi + +check=0 + +/bin/pesigcheck -n 0 -c /etc/pki/sb-certs/secureboot-grub2-x86_64.cer -i "${_FILE1}" | /bin/grep "\"${_FILE1}\" is valid." +check=$(eq_is_success ${check} 0) + +check_test_status ${check} "$0" +exit ${check} diff --git a/rpm_tests/p_httpd-core/001-prepare_environment.sh b/rpm_tests/p_httpd-core/001-prepare_environment.sh new file mode 100755 index 0000000..3135f23 --- /dev/null +++ b/rpm_tests/p_httpd-core/001-prepare_environment.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +echo "Подготовка окружения для тестирования пакета ${TEST_PACKAGE_NAME}" + +exit 0 diff --git a/rpm_tests/p_httpd-core/01-httpd-running.bats b/rpm_tests/p_httpd-core/01-httpd-running.bats new file mode 100755 index 0000000..88b1d52 --- /dev/null +++ b/rpm_tests/p_httpd-core/01-httpd-running.bats @@ -0,0 +1,15 @@ +#!/usr/bin/bats + +load ../../library/assertions; + +PACKAGE=httpd-core + +setup() { + if [[ -z $(/bin/rpm -qa | /bin/grep ${PACKAGE}) ]]; then + skip "${PACKAGE} is not installed, tests skipped" + fi +} + +@test "dummy-test" { + skip "Skipped, this test only demo" +} diff --git a/rpm_tests/p_httpd/001-prepare_environment.sh b/rpm_tests/p_httpd/001-prepare_environment.sh new file mode 100755 index 0000000..3135f23 --- /dev/null +++ b/rpm_tests/p_httpd/001-prepare_environment.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +echo "Подготовка окружения для тестирования пакета ${TEST_PACKAGE_NAME}" + +exit 0 diff --git a/rpm_tests/p_httpd/01-httpd-running.bats b/rpm_tests/p_httpd/01-httpd-running.bats new file mode 100755 index 0000000..95633c8 --- /dev/null +++ b/rpm_tests/p_httpd/01-httpd-running.bats @@ -0,0 +1,16 @@ +#!/usr/bin/bats + +load ../../library/assertions; + +PACKAGE=httpd + +setup() { + if [[ -z $(/bin/rpm -qa | /bin/grep ${PACKAGE}) ]]; then + skip "${PACKAGE} is not installed, tests skipped" + fi +} + +@test "is httpd service present" { + /bin/systemctl status ${PACKAGE} | /bin/grep -E 'running;|disabled;' + assert_success; +} diff --git a/rpm_tests/p_httpd/02-group-presence.sh b/rpm_tests/p_httpd/02-group-presence.sh new file mode 100755 index 0000000..503b3dc --- /dev/null +++ b/rpm_tests/p_httpd/02-group-presence.sh @@ -0,0 +1,13 @@ +#!/bin/bash -x + +# Test apache group present + +source ../../library/sh_lib.sh + +check=0 + +cat /etc/group | grep apache +check=$(eq_is_success ${check} 0) + +check_test_status ${check} "$0" +exit ${check}