summaryrefslogtreecommitdiffstats
path: root/share/dtf/lib/tests
blob: b4a1a4cfe4620e0fc67416527253b7b6c061c054 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#! /bin/sh

# Library for dtf tests.

# dtf_skip [REASON]
# -----------------
dtf_skip ()
{
    test -n "$1" && echo >&2 "SKIP: $1"
    exit 77
}

dtf_fail ()
{
    test -n "$1" && echo >&2 "$1"
    exit 1
}


__dtf_method_not_implemented ()
{
    dtf_fail "'$1' method is not implemented"
}


# dtf_pkg_installed PKGNAME
# -------------------------
dtf_pkg_installed ()
{
# {% if config.os.id in ["fedora", "rhel", "centos"] %}
    rpm -q "$1" &>/dev/null
# {% else %}
    __dtf_method_not_implemented "dtf_pkg_installed"
# {% endif %}
}


__dtf_assert_nargs ()
{
    _d_nargs=$1 ; shift
    _d_method=$1 ; shift

    test $# -ge "$_d_nargs" && return

    if test "$_d_nargs" -eq 1; then
      dtf_fail "method '$_d_method' requires at least 1 argument"
    else
      dtf_fail "method '$_d_method' requires at least $_d_nargs arguments"
    fi
}


dtf_is_root ()
{
    _d_uid=`id -u`
    test x"$_d_uid" = x0
}


dtf_assert ()
{
    __dtf_assert_nargs 1 'dtf_pkg_install' ${1+"$@"}

    case $1 in
      root|ROOT)
        dtf_is_root || dtf_fail "root accout required"
        ;;
      *)
        dtf_fail "dtf_assert '$1' not implemented"
        ;;
    esac
}


dtf_pkg_install ()
{
    __dtf_assert_nargs 1 'dtf_pkg_install' ${1+"$@"}

    _d_pkg_inst=$1 ; shift

    dtf_assert root

    # {{ "\n    " + commands.pkginstaller.install(['"$_d_pkg_inst"'], {'docs': True}) }}

    test $? -eq 0 || dtf_fail "can't install '$_d_pkg_inst' package"
}


__dtf_prereq_pkg ()
{
    __dtf_assert_nargs 2 'dtf_prereq pkg' ${1+"$@"}

    _d_pkg=$1 ; shift

    dtf_pkg_installed "$_d_pkg" && return 0

    dtf_pkg_install "$_d_pkg"
}


# dtf_prereq REQUIREMENT [ARGS..]
# -------------------------------
# Make sure the requirement is met or fail.
dtf_prereq ()
{
    __dtf_assert_nargs 1 dtf_prereq ${1+"$@"}

    _d_requirement=$1 ; shift

    case $_d_requirement in
      pkg)
        __dtf_prereq_pkg "$@"
        ;;

      *)
        dtf_fail "dtf_prereq: action '$_d_requirement' not implemented"
        ;;
    esac
}