kmod/tests/libkmod/test-libkmod.c
Susant Sahani f85d022c8b CI: Add libkmod to tests.
1. Fix sanity tests.

2. Add libkmod to test
```
gcc   -o test-libkmod test-libkmod.c -lkmod -lcmocka
./runtest.sh

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   TEST PROTOCOL
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

    Package       : kmod-devel
    Installed     : kmod-devel-25-2.fc28.x86_64
    beakerlib RPM : beakerlib-1.17-13.fc28.noarch
    Test version  : 1.0
    Test started  : 2018-06-21 20:07:27 IST
    Test finished :
    Test duration :
    Test name     : /CoreOS/libkmod
    Distro        : Fedora release 28 (Twenty Eight)
    Hostname      : Zeus
    Architecture  : x86_64
    CPUs          : 8 x Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
    RAM size      : 31542 MB
    HDD size      : 452.94 GB

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   Setup
::   Setup
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: [ 20:07:27 ] :: [   PASS   ] :: File /usr/lib/modules/4.16.15-300.fc28.x86_64/kernel/net/ipv4/ipip.ko.xz should exist
:: [ 20:07:27 ] :: [  BEGIN   ] :: Running 'cp test-libkmod /usr/bin/'
:: [ 20:07:27 ] :: [   PASS   ] :: Command 'cp test-libkmod /usr/bin/' (Expected 0, got 0)
:: [ 20:07:27 ] :: [   PASS   ] :: Command 'cp test-libkmod /usr/bin/' (Expected 0, got 0)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   Duration: 0s
::   Duration: 0s
::   Assertions: 3 good, 0 bad
::   Assertions: 3 good, 0 bad
::   RESULT: PASS
::   RESULT: PASS

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   Test
::   Test
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: [ 20:07:27 ] :: [   LOG    ] :: Starting libkmod tests ...
:: [ 20:07:27 ] :: [   LOG    ] :: Starting libkmod tests ...
:: [ 20:07:27 ] :: [  BEGIN   ] :: Running '/usr/bin/test-libkmod'
[==========] Running 2 test(s).
[ RUN      ] test_load_module
[       OK ] test_load_module
[ RUN      ] test_remove_module
[       OK ] test_remove_module
[==========] 2 test(s) run.
[  PASSED  ] 2 test(s).
:: [ 20:07:27 ] :: [   PASS   ] :: Command '/usr/bin/test-libkmod' (Expected 0, got 0)
:: [ 20:07:27 ] :: [   PASS   ] :: Command '/usr/bin/test-libkmod' (Expected 0, got 0)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   Duration: 0s
::   Duration: 0s
::   Assertions: 1 good, 0 bad

```

SANITY:
```
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   kmod_load_insmod
::   kmod_load_insmod
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: [ 20:07:26 ] :: [  BEGIN   ] :: Running 'insmod /usr/lib/modules/4.16.15-300.fc28.x86_64///kernel/net/ipv4/udp_tunnel.ko.xz'
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'insmod /usr/lib/modules/4.16.15-300.fc28.x86_64///kernel/net/ipv4/udp_tunnel.ko.xz' (Expected 0, got 0)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'insmod /usr/lib/modules/4.16.15-300.fc28.x86_64///kernel/net/ipv4/udp_tunnel.ko.xz' (Expected 0, got 0)
:: [ 20:07:26 ] :: [  BEGIN   ] :: Running 'lsmod | grep udp_tunnel'
udp_tunnel             16384  0
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'lsmod | grep udp_tunnel' (Expected 0, got 0)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'lsmod | grep udp_tunnel' (Expected 0, got 0)
:: [ 20:07:26 ] :: [  BEGIN   ] :: Running 'rmmod udp_tunnel'
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'rmmod udp_tunnel' (Expected 0, got 0)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'rmmod udp_tunnel' (Expected 0, got 0)
:: [ 20:07:26 ] :: [  BEGIN   ] :: Running 'lsmod | grep udp_tunnel'
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'lsmod | grep udp_tunnel' (Expected 1-255, got 1)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'lsmod | grep udp_tunnel' (Expected 1-255, got 1)
:: [ 20:07:26 ] :: [  BEGIN   ] :: Running 'insmod /usr/lib/modules/4.16.15-300.fc28.x86_64///kernel/net/wireless/lib80211.ko.xz'
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'insmod /usr/lib/modules/4.16.15-300.fc28.x86_64///kernel/net/wireless/lib80211.ko.xz' (Expected 0, got 0)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'insmod /usr/lib/modules/4.16.15-300.fc28.x86_64///kernel/net/wireless/lib80211.ko.xz' (Expected 0, got 0)
:: [ 20:07:26 ] :: [  BEGIN   ] :: Running 'lsmod | grep lib80211'
lib80211               16384  0
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'lsmod | grep lib80211' (Expected 0, got 0)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'lsmod | grep lib80211' (Expected 0, got 0)
:: [ 20:07:26 ] :: [  BEGIN   ] :: Running 'rmmod lib80211'
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'rmmod lib80211' (Expected 0, got 0)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'rmmod lib80211' (Expected 0, got 0)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'rmmod lib80211' (Expected 0, got 0)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'rmmod lib80211' (Expected 0, got 0)
:: [ 20:07:26 ] :: [  BEGIN   ] :: Running 'lsmod | grep lib80211'
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'lsmod | grep lib80211' (Expected 1-255, got 1)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'lsmod | grep lib80211' (Expected 1-255, got 1)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   Duration: 0s
::   Duration: 0s
::   Assertions: 8 good, 0 bad
::   Assertions: 8 good, 0 bad
::   RESULT: PASS
::   RESULT: PASS

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   kmod_load_modprobe
::   kmod_load_modprobe
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: [ 20:07:26 ] :: [  BEGIN   ] :: Running 'modprobe tun'
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'modprobe tun' (Expected 0, got 0)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'modprobe tun' (Expected 0, got 0)
:: [ 20:07:26 ] :: [  BEGIN   ] :: Running 'modprobe -r tun'
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'modprobe -r tun' (Expected 0, got 0)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'modprobe -r tun' (Expected 0, got 0)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   Duration: 0s
::   Duration: 0s
::   Assertions: 2 good, 0 bad
::   Assertions: 2 good, 0 bad
::   RESULT: PASS
::   RESULT: PASS

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   kmod_modinfo
::   kmod_modinfo
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: [ 20:07:26 ] :: [  BEGIN   ] :: Running 'modinfo -l udp_tunnel'
GPL
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'modinfo -l udp_tunnel' (Expected 0, got 0)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'modinfo -l udp_tunnel' (Expected 0, got 0)
:: [ 20:07:26 ] :: [  BEGIN   ] :: Running 'modinfo -l lib80211'
GPL
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'modinfo -l lib80211' (Expected 0, got 0)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'modinfo -l lib80211' (Expected 0, got 0)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   Duration: 0s
::   Duration: 0s
::   Assertions: 2 good, 0 bad
::   Assertions: 2 good, 0 bad
::   RESULT: PASS
::   RESULT: PASS

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   Duration: 0s
::   Duration: 0s
::   Assertions: 12 good, 0 bad
::   Assertions: 12 good, 0 bad
::   RESULT: PASS
::   RESULT: PASS

gre                    16384  0
uwb                    90112  0

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   Modprobe with wrong parameter
::   Modprobe with wrong parameter
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: [ 20:07:26 ] :: [  BEGIN   ] :: Running 'modprobe atm BADPARAM=this_should_fail'
:: [ 20:07:26 ] :: [   LOG    ] :: Output of 'modprobe atm BADPARAM=this_should_fail':
:: [ 20:07:26 ] :: [   LOG    ] :: Output of 'modprobe atm BADPARAM=this_should_fail':
:: [ 20:07:26 ] :: [   LOG    ] :: --------------- OUTPUT START ---------------
:: [ 20:07:26 ] :: [   LOG    ] :: --------------- OUTPUT START ---------------
:: [ 20:07:26 ] :: [   LOG    ] :: ---------------  OUTPUT END  ---------------
:: [ 20:07:26 ] :: [   LOG    ] :: ---------------  OUTPUT END  ---------------
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'modprobe atm BADPARAM=this_should_fail' (Expected 0-1, got 0)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'modprobe atm BADPARAM=this_should_fail' (Expected 0-1, got 0)
:: [ 20:07:26 ] :: [  BEGIN   ] :: Running 'dmesg | grep -i 'Unknown parameter' | grep 'BADPARAM' | grep 'atm''
[17881.492103] atm: unknown parameter 'BADPARAM' ignored
:: [ 20:07:26 ] :: [   LOG    ] :: Output of 'dmesg | grep -i 'Unknown parameter' | grep 'BADPARAM' | grep 'atm'':
:: [ 20:07:26 ] :: [   LOG    ] :: Output of 'dmesg | grep -i 'Unknown parameter' | grep 'BADPARAM' | grep 'atm'':
:: [ 20:07:26 ] :: [   LOG    ] :: --------------- OUTPUT START ---------------
:: [ 20:07:26 ] :: [   LOG    ] :: --------------- OUTPUT START ---------------
:: [ 20:07:26 ] :: [   LOG    ] :: [17881.492103] atm: unknown parameter 'BADPARAM' ignored
:: [ 20:07:26 ] :: [   LOG    ] :: [17881.492103] atm: unknown parameter 'BADPARAM' ignored
:: [ 20:07:26 ] :: [   LOG    ] :: ---------------  OUTPUT END  ---------------
:: [ 20:07:26 ] :: [   LOG    ] :: ---------------  OUTPUT END  ---------------
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'dmesg | grep -i 'Unknown parameter' | grep 'BADPARAM' | grep 'atm'' (Expected 0, got 0)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'dmesg | grep -i 'Unknown parameter' | grep 'BADPARAM' | grep 'atm'' (Expected 0, got 0)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   Duration: 0s
::   Duration: 0s
::   Assertions: 2 good, 0 bad
::   Assertions: 2 good, 0 bad
::   RESULT: PASS

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   Test
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   kmod_load_insmod
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: [ 20:07:26 ] :: [   PASS   ] :: Command 'insmod /usr/lib/modules/4.16.15-300.fc28.x86_64///kernel/net/ipv4/udp_tunnel.ko.xz' (Expected 0, got 0)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'lsmod | grep udp_tunnel' (Expected 0, got 0)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'rmmod udp_tunnel' (Expected 0, got 0)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'lsmod | grep udp_tunnel' (Expected 1-255, got 1)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'insmod /usr/lib/modules/4.16.15-300.fc28.x86_64///kernel/net/wireless/lib80211.ko.xz' (Expected 0, got 0)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'lsmod | grep lib80211' (Expected 0, got 0)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'rmmod lib80211' (Expected 0, got 0)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'lsmod | grep lib80211' (Expected 1-255, got 1)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   Duration: 0s
::   Assertions: 8 good, 0 bad
::   RESULT: PASS

:: [ 20:07:26 ] :: [   PASS   ] :: Command 'modprobe tun' (Expected 0, got 0)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'modprobe -r tun' (Expected 0, got 0)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   Duration: 0s
::   Assertions: 2 good, 0 bad
::   RESULT: PASS

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   kmod_modinfo
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: [ 20:07:26 ] :: [   PASS   ] :: Command 'modinfo -l udp_tunnel' (Expected 0, got 0)
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'modinfo -l lib80211' (Expected 0, got 0)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   Duration: 0s
::   Assertions: 2 good, 0 bad
::   RESULT: PASS

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   Duration: 0s
::   Assertions: 12 good, 0 bad
::   RESULT: PASS

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   Modprobe with wrong parameter
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: [ 20:07:26 ] :: [   LOG    ] :: Output of 'modprobe atm BADPARAM=this_should_fail':
:: [ 20:07:26 ] :: [   LOG    ] :: --------------- OUTPUT START ---------------
:: [ 20:07:26 ] :: [   LOG    ] :: ---------------  OUTPUT END  ---------------
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'modprobe atm BADPARAM=this_should_fail' (Expected 0-1, got 0)
:: [ 20:07:26 ] :: [   LOG    ] :: Output of 'dmesg | grep -i 'Unknown parameter' | grep 'BADPARAM' | grep 'atm'':
:: [ 20:07:26 ] :: [   LOG    ] :: --------------- OUTPUT START ---------------
:: [ 20:07:26 ] :: [   LOG    ] :: [17881.492103] atm: unknown parameter 'BADPARAM' ignored
:: [ 20:07:26 ] :: [   LOG    ] :: ---------------  OUTPUT END  ---------------
:: [ 20:07:26 ] :: [   PASS   ] :: Command 'dmesg | grep -i 'Unknown parameter' | grep 'BADPARAM' | grep 'atm'' (Expected 0, got 0)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
```
2018-06-21 20:30:24 +05:30

80 lines
2.0 KiB
C

/* SPDX-License-Identifier: LGPL-2.1+
# ~~~
# Description: Tests libkmod
#
# Author: Susant Sahani <susant@redhat.com>
# Copyright (c) 2018 Red Hat, Inc.
# ~~~
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libkmod.h>
#include <setjmp.h>
#include <inttypes.h>
#include <cmocka.h>
static int load_module(void **state) {
struct kmod_list *list = NULL;
struct kmod_ctx *ctx = NULL;
struct kmod_list *l;
int r;
assert_non_null((ctx = kmod_new(NULL, NULL)));
assert_return_code(kmod_module_new_from_lookup(ctx, "ipip", &list), 0);
kmod_list_foreach(l, list) {
struct kmod_module *mod = NULL;
mod = kmod_module_get_module(l);
assert_non_null(mod);
assert_return_code(kmod_module_probe_insert_module(mod, 0, NULL, NULL, NULL, NULL), 0);
}
free(ctx);
return 0;
}
static int remove_module(void **state) {
struct kmod_list *list = NULL;
struct kmod_ctx *ctx = NULL;
struct kmod_list *l;
int r;
assert_non_null((ctx = kmod_new(NULL, NULL)));
assert_return_code(kmod_module_new_from_lookup(ctx, "ipip", &list), 0);
kmod_list_foreach(l, list) {
struct kmod_module *mod = NULL;
mod = kmod_module_get_module(l);
assert_non_null(mod);
assert_return_code(kmod_module_remove_module(mod, 0), 0);
}
free(ctx);
return 0;
}
void test_load_module(void **state) {
load_module(NULL);
}
void test_remove_module(void **state) {
remove_module(NULL);
}
int main(int argc, char *argv[]) {
const struct CMUnitTest libmod_tests[] = {
cmocka_unit_test_teardown(test_load_module, remove_module),
cmocka_unit_test_setup(test_remove_module, load_module),
};
return cmocka_run_group_tests(libmod_tests, NULL, NULL);
}