From 4ee8fd331146ff715f857a92d6b7be961610d484 Mon Sep 17 00:00:00 2001 From: Susant Sahani Date: Mon, 2 Jul 2018 17:16:30 +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: ``` :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :: Test :: Test :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :: [ 17:15:59 ] :: [ LOG ] :: Starting libconfig tests ... :: [ 17:15:59 ] :: [ LOG ] :: Starting libconfig tests ... :: [ 17:15:59 ] :: [ BEGIN ] :: Running '/usr/bin/test-libconfig' [==========] Running 2 test(s). [ RUN ] test_config_write [ OK ] test_config_write [ RUN ] test_config_read [ OK ] test_config_read [==========] 2 test(s) run. [ PASSED ] 2 test(s). :: [ 17:15:59 ] :: [ PASS ] :: Command '/usr/bin/test-libconfig' (Expected 0, got 0) :: [ 17:15:59 ] :: [ PASS ] :: Command '/usr/bin/test-libconfig' (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/auth.cfg | 11 +++ tests/sanity-tests/runtest.sh | 35 ++++++++ tests/sanity-tests/test-libconfig.c | 131 ++++++++++++++++++++++++++++ tests/tests.yml | 13 +++ 5 files changed, 236 insertions(+) create mode 100644 tests/sanity-tests/Makefile create mode 100644 tests/sanity-tests/auth.cfg create mode 100755 tests/sanity-tests/runtest.sh create mode 100644 tests/sanity-tests/test-libconfig.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..cb54b1e --- /dev/null +++ b/tests/sanity-tests/Makefile @@ -0,0 +1,46 @@ +# SPDX-License-Identifier: LGPL-2.1+ +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# +# Makefile of /CoreOS/libconfig +# Description: Test if libconfig working ok +# Author: Susant Sahani +# +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +export TEST=/CoreOS/libconfig +export TESTVERSION=1.0 + +OBJS = test-libconfig.c +CFLAG = -Wall -g3 +CC = gcc +LIBS = -lconfig -lcmocka + +test-libconfig:${OBJ} + ${CC} ${CFLAGS} ${INCLUDES} -o $@ ${OBJS} ${LIBS} + +run: test-libconfig + ./runtest.sh +clean: + -rm -f *~ test-libconfig + +.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 libconfig works ok" >> $(METADATA) + @echo "Type: Sanity" >> $(METADATA) + @echo "TestTime: 5m" >> $(METADATA) + @echo "RunFor: libconfig" >> $(METADATA) + @echo "Requires: libconfig libconfig-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/auth.cfg b/tests/sanity-tests/auth.cfg new file mode 100644 index 0000000..8c56b42 --- /dev/null +++ b/tests/sanity-tests/auth.cfg @@ -0,0 +1,11 @@ +# authenticator + +name = "JP"; +enabled = false; +length = 186; + +ldap = { + host = "ldap.example.com"; + base = "ou=usr,o=example.com"; /* adapt this */ + retries = [10, 15, 20, 60]; // Use more than 2 +}; diff --git a/tests/sanity-tests/runtest.sh b/tests/sanity-tests/runtest.sh new file mode 100755 index 0000000..59d0eaa --- /dev/null +++ b/tests/sanity-tests/runtest.sh @@ -0,0 +1,35 @@ +#!/bin/bash +# SPDX-License-Identifier: LGPL-2.1+ +# ~~~ +# runtest.sh of libconfig +# Description: Tests for libconfig +# +# Author: Susant Sahani +# Copyright (c) 2018 Red Hat, Inc. +# ~~~ + +# Include Beaker environment +. /usr/share/beakerlib/beakerlib.sh || exit 1 + +PACKAGE="libconfig" + +rlJournalStart + rlPhaseStartSetup + rlAssertRpm $PACKAGE + rlRun "cp test-libconfig /usr/bin/" + rlRun "cp auth.cfg /var/run/" + rlPhaseEnd + + rlPhaseStartTest + rlLog "Starting libconfig tests ..." + rlRun "/usr/bin/test-libconfig" + rlPhaseEnd + + rlPhaseStartCleanup + rlRun "rm /usr/bin/test-libconfig /var/run/auth.cfg" + rlLog "libconfig tests done" + rlPhaseEnd +rlJournalPrintText +rlJournalEnd + +rlGetTestState diff --git a/tests/sanity-tests/test-libconfig.c b/tests/sanity-tests/test-libconfig.c new file mode 100644 index 0000000..5df2afd --- /dev/null +++ b/tests/sanity-tests/test-libconfig.c @@ -0,0 +1,131 @@ +/* +# SPDX-License-Identifier: LGPL-2.1+ +# ~~~ +# Description: libconfig +# +# Author: Susant Sahani +# Copyright (c) 2018 Red Hat, Inc. +# ~~~ +*/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define TEST_WRITE_FILE_NAME "/var/run/example.cfg" +#define TEST_READ_FILE_NAME "/var/run/auth.cfg" + +static void test_config_read(void **state) { + const char *base = NULL, *host = NULL, *name = NULL; + const config_setting_t *retries; + int aretries[] = {10, 15, 20, 60}; + int count, n, enabled; + config_t cfg, *cf; + + cf = &cfg; + config_init(cf); + + assert_non_null(config_read_file(cf, TEST_READ_FILE_NAME)); + + assert_true(config_lookup_string(cf, "name", &name) >=0); + assert_string_equal(name, "JP"); + + assert_true(config_lookup_bool(cf, "enabled", &enabled) >=0); + assert_int_equal(enabled, 0); + + assert_true((config_lookup_string(cf, "ldap.host", &host)) >=0); + assert_string_equal(host, "ldap.example.com"); + + assert_true((config_lookup_string(cf, "ldap.base", &base)) >=0); + assert_string_equal(base , "ou=usr,o=example.com"); + + assert_non_null((retries = config_lookup(cf, "ldap.retries"))); + count = config_setting_length(retries); + + assert_int_equal(count, 4); + for (n = 0; n < count; n++) + assert_int_equal(aretries[n], config_setting_get_int_elem(retries, n)); + + config_destroy(cf); +} + +static void test_config_write(void **state) { + config_setting_t *root, *setting, *group, *array; + const char *street, *city, *st, *str; + config_t cfg, *cf; + int i, zip; + + config_init(&cfg); + root = config_root_setting(&cfg); + + assert_non_null((group = config_setting_add(root, "address", CONFIG_TYPE_GROUP))); + + assert_non_null((setting = config_setting_add(group, "street", CONFIG_TYPE_STRING))); + assert_true(config_setting_set_string(setting, "1 Woz Way") >= 0); + + assert_non_null((setting = config_setting_add(group, "city", CONFIG_TYPE_STRING))); + assert_true(config_setting_set_string(setting, "San Jose") >= 0); + + assert_non_null((setting = config_setting_add(group, "state", CONFIG_TYPE_STRING))); + assert_true(config_setting_set_string(setting, "CA") >= 0); + + assert_non_null((setting = config_setting_add(group, "zip", CONFIG_TYPE_INT))); + assert_true(config_setting_set_int(setting, 95110) >= 0); + + assert_non_null((array = config_setting_add(root, "numbers", CONFIG_TYPE_ARRAY))); + + for(i = 0; i < 10; ++i) { + assert_non_null((setting = config_setting_add(array, NULL, CONFIG_TYPE_INT))); + assert_true(config_setting_set_int(setting, 10 * i) >=0); + } + + assert_non_null((config_write_file(&cfg, TEST_WRITE_FILE_NAME))); + config_destroy(&cfg); + + /* Read Back and test */ + config_init(&cfg); + + assert_non_null((config_read_file(&cfg, TEST_WRITE_FILE_NAME))); + assert_non_null((setting = config_lookup(&cfg, "address"))); + + assert_true(config_setting_lookup_string(setting, "street", &street) >= 0); + assert_string_equal(street, "1 Woz Way"); + + assert_true(config_setting_lookup_string(setting, "city", &city) >=0); + assert_string_equal(city, "San Jose"); + + assert_true(config_setting_lookup_string(setting, "state", &st) >=0); + assert_string_equal(st, "CA"); + + assert_true(config_setting_lookup_int(setting, "zip", &zip) >=0); + assert_int_equal(zip, 95110); + + array = config_lookup(&cfg, "numbers"); + if(array) { + int count, n; + + count = config_setting_length(array); + for (n = 0; n < count; n++) { + assert_int_equal(n * 10, config_setting_get_int_elem(array, n)); + } + } + config_destroy(&cfg); + + unlink(TEST_WRITE_FILE_NAME); +} + +int main(int argc, char *argv[]) { + const struct CMUnitTest libconfig_tests[] = { + cmocka_unit_test(test_config_write), + cmocka_unit_test(test_config_read), + }; + + return cmocka_run_group_tests(libconfig_tests, NULL, NULL); +} diff --git a/tests/tests.yml b/tests/tests.yml new file mode 100644 index 0000000..0fde44c --- /dev/null +++ b/tests/tests.yml @@ -0,0 +1,13 @@ +- hosts: localhost + roles: + - role: standard-test-beakerlib + tags: + - classic + tests: + - sanity-tests + required_packages: + - libconfig + - libconfig-devel + - libcmocka + - libcmocka-devel + - gcc