summaryrefslogtreecommitdiffstats
path: root/share/dtf/lib/tests
blob: 3989211ca9257c14e44263da41c1a8e2428ad035 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# Library for dtf tests.
# Copyright (C) 2015 Red Hat, Inc.
# Written by Pavel Raiskup.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


# Public API is prefixed by ^dtf_ prefix.


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


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

    echo >& "running dtf_fail hooks $dtf_fail_hooks"
    for _i_hook in $dtf_fail_hooks
    do
      $_i_hook
    done

    exit 1
}


dtf_debug ()
{
    for _d_i in $1
    do
        case " $DTF_DBG_KEYWORDS " in
            *\ $_d_i\ *)
                shift
                echo >&4 " ~ $*"
                return 0
            ;;
        esac
    done
}


dtf_assert_right_distro ()
{
    _d_distro='{{ config.os.id }}'
    _d_distroversion='{{ config.os.version }}'
    _d_expected=$_d_distro-$_d_distroversion

    case $_d_expected in
      fedora-*|rhel-7*)
        test -f /etc/os-release || dtf_fail "/etc/os-release not found!"
        _d_real=$(. /etc/os-release ; echo "$ID-$VERSION_ID" ; )
        test "$_d_real" = "$_d_expected" \
            || dtf_fail "/etc/os-release is wong: $_d_real vs $_d_expected"
        ;;
      *)
        dtf_fail "dtf_assert_right_distro () is not implemented here"
        ;;
    esac
}


__dtf_debug ()
{
    dtf_debug dtf_lib "$*"
}

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


# dtf_pkg_installed PKGNAME
# -------------------------
dtf_pkg_installed ()
{
    __dtf_debug "checking whether '$1' is 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

    _d_tr="$# given ($*)"
    if test "$_d_nargs" -eq 1; then
      dtf_fail "method '$_d_method' requires at least 1 argument, $_d_tr"
    else
      dtf_fail "method '$_d_method' requires at least $_d_nargs arguments, $_d_tr"
    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"
        ;;
      cmd)
        shift
        __dtf_assert_cmd "$@"
        ;;
      *)
        dtf_fail "dtf_assert '$1' not implemented"
        ;;
    esac
}


dtf_pkg ()
{
    __dtf_assert_nargs 2 'dtf_pkg' ${1+"$@"}
    _d_action=$1 ; shift
    _d_pkg=$1 ; shift

    dtf_assert root

    case $_d_action in
      remove)
        # {{ "\n    " + commands.pkginstaller.remove(['"$_d_pkg"'], {'docs': True}) }}
        ;;
      install)
        # {{ "\n    " + commands.pkginstaller.install(['"$_d_pkg"'], {'docs': True}) }}
        ;;
      *)
        dtf_fail "dtf_pkg: unimplemented action '$_d_action'"
        ;;
    esac
}


dtf_pkg_install ()
{
    __dtf_assert_nargs 1 'dtf_pkg_install' ${1+"$@"}
    dtf_pkg install "$1"
    test $? -eq 0 || dtf_fail "can't install '$1' package"
}


dtf_pkg_remove ()
{
    __dtf_assert_nargs 1 'dtf_pkg_remove' ${1+"$@"}
    dtf_pkg remove "$1"
    test $? -eq 0 || dtf_fail "can't remove '$1' package"
}


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

    _d_subaction=$1 ; shift
    _d_pkg=$1 ; shift

    __dtf_debug "making sure $_d_pkg is $_d_subaction"

    case $_d_subaction in
      installed)
        dtf_pkg_installed "$_d_pkg" && return 0
        dtf_pkg_install "$_d_pkg"
        ;;
      missing)
        dtf_pkg_installed "$_d_pkg" || return 0
        dtf_pkg_remove "$_d_pkg"
        ;;
      *)
        dtf_fail "dtf_prereq pkg: not implemented action $_d_subaction"
        ;;
    esac
}


__dtf_cmd ()
{
    __dtf_debug "running $1"

    _d_cmd=$1 ; shift
    eval "$_d_cmd"
    __dtf_cmd_result_status=$?
}


__dtf_assert_cmd ()
{
    _d_a_cmd=$1         ; shift
    _d_ok_statuses=$1   ; shift
    __dtf_cmd "$_d_a_cmd"

    case " $_d_ok_statuses " in
      *"$__dtf_cmd_result_status "*)
        ;;
      *)
        _d_f_msg="cmd '$_d_a_cmd' ended with '$__dtf_cmd_result_status' while "
        _d_f_msg="$_d_f_msg it should end with one of { $_d_ok_statuses }"
        dtf_fail "$_d_f_msg"
        ;;
    esac
}


# 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
}