From 931be7aaf4d7d0611fe7c60506dd366d4b6b13f4 Mon Sep 17 00:00:00 2001 From: Susant Sahani Date: Mon, 25 Jun 2018 18:05:04 +0530 Subject: [PATCH] Adds tests according to the CI justification Adds tests according to the CI wiki specifically the standard test interface in the spec. The playbook includes Tier1 level test cases that have been tested in the following contexts and is passing reliably: Classic. Test logs are stored in the artifacts directory. The following steps are used to execute the tests using the standard test interface: Test enveronment Make sure you have installed packages from the spec ``` ansible-2.4.1.0-2.fc28.noarch python2-dnf-2.7.5-1.fc28.noarch libselinux-python-2.7-2.fc28.x86_64 standard-test-roles-2.5-1.fc28.noarch ``` Run tests for Classic Snip of the example test run for Classic tests: ``` :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :: Duration: 0s :: Duration: 0s :: Assertions: 2 good, 0 bad :: Assertions: 2 good, 0 bad :: RESULT: PASS :: RESULT: PASS :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :: Test :: Test :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :: [ 15:49:17 ] :: [ LOG ] :: Starting libcap tests ... :: [ 15:49:17 ] :: [ LOG ] :: Starting libcap tests ... :: [ 15:49:17 ] :: [ BEGIN ] :: Running '/usr/bin/test-libcap' [==========] Running 1 test(s). [ RUN ] test_drop_cap_net_raw [ OK ] test_drop_cap_net_raw [==========] 1 test(s) run. [ PASSED ] 1 test(s). :: [ 15:49:17 ] :: [ PASS ] :: Command '/usr/bin/test-libcap' (Expected 0, got 0) :: [ 15:49:17 ] :: [ PASS ] :: Command '/usr/bin/test-libcap' (Expected 0, got 0) :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :: Duration: 0s :: Duration: 0s :: Assertions: 1 good, 0 bad :: Assertions: 1 good, 0 bad :: RESULT: PASS :: RESULT: PASS ``` --- tests/sanity-tests/Makefile | 46 ++++++++++++++++++++++++++++ tests/sanity-tests/runtest.sh | 34 +++++++++++++++++++++ tests/sanity-tests/test-libcap.c | 52 ++++++++++++++++++++++++++++++++ tests/tests.yml | 13 ++++++++ 4 files changed, 145 insertions(+) create mode 100644 tests/sanity-tests/Makefile create mode 100755 tests/sanity-tests/runtest.sh create mode 100644 tests/sanity-tests/test-libcap.c create mode 100644 tests/tests.yml diff --git a/tests/sanity-tests/Makefile b/tests/sanity-tests/Makefile new file mode 100644 index 0000000..9e75815 --- /dev/null +++ b/tests/sanity-tests/Makefile @@ -0,0 +1,46 @@ +# SPDX-License-Identifier: LGPL-2.1+ +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# +# Makefile of /CoreOS/libcap +# Description: Test if libcap working ok +# Author: Susant Sahani +# +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +export TEST=/CoreOS/libcap +export TESTVERSION=1.0 + +OBJS = test-libcap.c +CFLAG = -Wall -g3 +CC = gcc +LIBS = -lcap -lcmocka + +test-libcap:${OBJ} + ${CC} ${CFLAGS} ${INCLUDES} -o $@ ${OBJS} ${LIBS} + +run: test-libcap + ./runtest.sh +clean: + -rm -f *~ test-libcap + +.c.o: + ${CC} ${CFLAGS} ${INCLUDES} -c $< + +CC = gcc + +include /usr/share/rhts/lib/rhts-make.include +$(METADATA): Makefile + @echo "Owner: Susant Sahani" > $(METADATA) + @echo "Name: $(TEST)" >> $(METADATA) + @echo "TestVersion: $(TESTVERSION)" >> $(METADATA) + @echo "Path: $(TEST_DIR)" >> $(METADATA) + @echo "Description: Test libcap works ok" >> $(METADATA) + @echo "Type: Sanity" >> $(METADATA) + @echo "TestTime: 5m" >> $(METADATA) + @echo "RunFor: libcap" >> $(METADATA) + @echo "Requires: libcap libcap-devel" >> $(METADATA) + @echo "Priority: Normal" >> $(METADATA) + @echo "License: GPLv2" >> $(METADATA) + @echo "Confidential: no" >> $(METADATA) + @echo "Destructive: no" >> $(METADATA) + @echo "Releases: -Fedora 29" >> $(METADATA) + rhts-lint $(METADATA) diff --git a/tests/sanity-tests/runtest.sh b/tests/sanity-tests/runtest.sh new file mode 100755 index 0000000..17d83e3 --- /dev/null +++ b/tests/sanity-tests/runtest.sh @@ -0,0 +1,34 @@ +#!/bin/bash +# SPDX-License-Identifier: LGPL-2.1+ +# ~~~ +# runtest.sh of libcap +# Description: Tests for libcap +# +# Author: Susant Sahani +# Copyright (c) 2018 Red Hat, Inc. +# ~~~ + +# Include Beaker environment +. /usr/share/beakerlib/beakerlib.sh || exit 1 + +PACKAGE="libcap" + +rlJournalStart + rlPhaseStartSetup + rlAssertRpm $PACKAGE + rlRun "cp test-libcap /usr/bin/" + rlPhaseEnd + + rlPhaseStartTest + rlLog "Starting libcap tests ..." + rlRun "/usr/bin/test-libcap" + rlPhaseEnd + + rlPhaseStartCleanup + rlRun "rm /usr/bin/test-libcap" + rlLog "libcap tests done" + rlPhaseEnd +rlJournalPrintText +rlJournalEnd + +rlGetTestState diff --git a/tests/sanity-tests/test-libcap.c b/tests/sanity-tests/test-libcap.c new file mode 100644 index 0000000..ca4101d --- /dev/null +++ b/tests/sanity-tests/test-libcap.c @@ -0,0 +1,52 @@ +/* +# SPDX-License-Identifier: LGPL-2.1+ +# ~~~ +# Description: libcap tests +# +# Author: Susant Sahani +# Copyright (c) 2018 Red Hat, Inc. +# ~~~ +*/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void drop_cap(cap_value_t capflag) { + cap_t d; + + d = cap_get_proc(); + assert_non_null(d); + + assert_return_code(cap_set_flag(d, CAP_EFFECTIVE, 1, &capflag, CAP_CLEAR), 0); + assert_return_code(cap_set_flag(d, CAP_PERMITTED, 1, &capflag, CAP_CLEAR), 0); + assert_return_code(cap_set_proc(d), 0); +} + +void test_drop_cap_net_raw(void **state) { + int s; + + assert_true((s = socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) >= 0); + close(s); + + drop_cap(CAP_NET_RAW); + + assert_false((s = socket(PF_INET, SOCK_RAW, IPPROTO_UDP)) >= 0); +} + +int main(int argc, char *argv[]) { + const struct CMUnitTest libcap_tests[] = { + cmocka_unit_test(test_drop_cap_net_raw), + }; + + return cmocka_run_group_tests(libcap_tests, NULL, NULL); +} diff --git a/tests/tests.yml b/tests/tests.yml new file mode 100644 index 0000000..08a5513 --- /dev/null +++ b/tests/tests.yml @@ -0,0 +1,13 @@ +- hosts: localhost + roles: + - role: standard-test-beakerlib + tags: + - classic + tests: + - sanity-tests + required_packages: + - libcap + - libcap-devel + - libcmocka + - libcmocka-devel + - gcc