summaryrefslogtreecommitdiffstats
path: root/run-tests-in-vagrant.sh
blob: a3f2ac7c72d1b52a8f67e41ac372b91f6dd822fa (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/bin/bash

###############################################################################
# TODO: Support other OSes.                                                   #
###############################################################################

ORIGIN_DIR=$PWD
autostart="no"
destroy_after_test="no"
os="fedora"
destroy_now="no"
run_tests_args=""
redirect=">/dev/null 2>&1"
ssh="no"
custom_cflags=""


pushd () {
    command pushd "$@" >/dev/null
}

popd () {
    command popd "$@" >/dev/null
}

usage() {
    echo "Usage: $0 [...]"
    echo ''
    echo 'The options that this script accepts are:'
    echo ''
    echo '-a, --autostart        configure the testVM to autostart on boot'
    echo '--destroy-now          cleanup the testVM'
    echo '--destroy-after-test   cleanup once the tests finishes'
    echo '-h, --help             show this help text'
    echo '--os=<flavor>          select the OS for the testVM (fedora, centos6)'
    echo '--ssh                  ssh into the testVM'
    echo '--verbose              show what commands in the testVM are executed'
    echo ''
}

function parse_args () {
    args=`getopt \
              --options ah \
              --long autostart,os:,destroy-now,destroy-after-test,verbose,ssh,help \
              -n 'run-tests-in-vagrant.sh' \
              --  "$@"`
    eval set -- "$args"
    while true; do
        case "$1" in
            -a|--autostart) autostart="yes"; shift ;;
            --destroy-after-test) destroy_after_test="yes"; shift ;;
            --destroy-now)  destroy_now="yes"; shift ;;
            -h|--help) usage ; exit 0 ;;
            --ssh)  sshvm="yes"; shift ;;
            --os)
                case "$2" in
                    "") shift 2 ;;
                     *) os="$2" ; shift 2 ;;
                esac ;;
            --verbose)  redirect=""; shift ;;
            --) shift ; break ;;
            *) echo "Internal error!" ; exit 1;;
        esac
    done
    run_tests_args="$@"
}

function force_location()
{
    current_dir=$(dirname $0);

    if [ ! -f ${current_dir}/tests/vagrant/vagrant-template-fedora/Vagrantfile ]; then
        echo "Aborting."
        echo "The tests/vagrant subdirectory seems to be missing."
        echo "Please correct the problem and try again."
        exit 1
    fi
}

function vagrant_check()
{
    vagrant -v >/dev/null  2>&1;

    if [ $? -ne 0 ]; then
        echo "Aborting"
        echo "Vagrant not found. Please install Vagrant and try again."
        echo "On Fedora, run "dnf install vagrant vagrant-libvirt" "
        exit 1
    fi
}

function ansible_check()
{
    ansible --version  >/dev/null  2>&1 ;

    if [ $? -ne 0 ]; then
        echo "Aborting"
        echo "Ansible not found. Please install Ansible and try again."
        echo "On Fedora, run "dnf install ansible" "
        exit 1
    fi
}

function set_branchname_from_git_branch()
{
    BRANCHNAME=`git rev-parse --abbrev-ref HEAD`
    if [ $? -ne 0 ]; then
        echo "Could not get branch name from git, will exit"
        exit 1
    fi
}


function destroy_vm()
{
    local retval=0

    echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!CAUTION!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    echo "This will destroy VM and delete tests/vagrant/${BRANCHNAME} dir"
    echo
    while true; do
        read -p "Do you want to continue?" yn
        case $yn in
            [Yy]* ) break;;
            * ) echo "Did not get an yes, exiting."; exit 1 ;;
        esac
    done
    if [ -d "tests/vagrant/${BRANCHNAME}" ]; then
        pushd "tests/vagrant/${BRANCHNAME}"
        eval vagrant destroy $redirect
        popd
        rm -rf "tests/vagrant/${BRANCHNAME}"
    else
        echo "Could not find vagrant dir for corresponding git branch, exiting"
        retval=1
    fi

    return ${retval}
}


function create_vagrant_dir()
{
    mkdir -p tests/vagrant/$BRANCHNAME
    if [ -d "tests/vagrant/vagrant-template-${os}" ]; then
        echo "Copying tests/vagrant/vagrant-template-${os} dir to tests/vagrant/${BRANCHNAME} ...."
        cp -R tests/vagrant/vagrant-template-${os}/* tests/vagrant/$BRANCHNAME
    else
        echo "Could not find template files for requested os $os, exiting"
        exit 1
    fi
}


function start_vm()
{
    echo "Doing vagrant up...."
    pushd "tests/vagrant/${BRANCHNAME}"
    eval vagrant up $redirect
    if [ $? -eq 0 ]
    then
            popd
    else
            echo "Vagrant up failed, exiting....";
            popd
            exit 1
    fi
}

function set_vm_attributes()
{
    if [ "x$autostart" == "xyes" ] ; then
        virsh autostart ${BRANCHNAME}_vagrant-testVM
    fi
}

function copy_source_code()
{
    echo "Copying source code from host machine to VM...."
    pushd "tests/vagrant/${BRANCHNAME}"
    vagrant ssh-config > ssh_config
    rsync -az -e "ssh -F ssh_config" --rsync-path="sudo rsync" "$ORIGIN_DIR/." vagrant-testVM:/home/vagrant/glusterfs
    if [ $? -eq 0 ]
    then
            popd
    else
            echo "Copy failed, exiting...."
            popd
            exit 1
    fi
}

function compile_gluster()
{
    echo "Source compile and install Gluster...."
    pushd "tests/vagrant/${BRANCHNAME}"
    vagrant ssh -c "cd /home/vagrant/glusterfs ; sudo make clean $redirect" -- -t
    vagrant ssh -c "cd /home/vagrant/glusterfs ; sudo ./autogen.sh $redirect" -- -t
    if [ $? -ne 0 ]
    then
            echo "autogen failed, exiting...."
            popd
            exit 1
    fi

    # GCC on fedora complains about uninitialized variables and
    # GCC on centos6 does not under don't warn on uninitialized variables flag.
    if [ "x$os" == "fedora" ] ; then
            custom_cflags="CFLAGS='-g -O0 -Werror -Wall -Wno-error=cpp -Wno-error=maybe-uninitialized'"
    else
            custom_cflags="CFLAGS='-g -O0 -Werror -Wall'"
    fi


    custom_cflags=
    vagrant ssh -c "cd /home/vagrant/glusterfs ; \
            sudo \
            $custom_cflags \
            ./configure \
            --prefix=/usr \
            --exec-prefix=/usr \
            --bindir=/usr/bin \
            --sbindir=/usr/sbin \
            --sysconfdir=/etc \
            --datadir=/usr/share \
            --includedir=/usr/include \
            --libdir=/usr/lib64 \
            --libexecdir=/usr/libexec \
            --localstatedir=/var \
            --sharedstatedir=/var/lib \
            --mandir=/usr/share/man \
            --infodir=/usr/share/info \
            --libdir=/usr/lib64 \
            --enable-gnfs \
            --enable-debug $redirect" -- -t
    if [ $? -ne 0 ]
    then
            echo "configure failed, exiting...."
            popd
            exit 1
    fi
    # Test for missing dependencies based on the BuildRequires in the
    # glusterfs.spec. If anything is missing, install them (and only then, dnf
    # cache is a large download).
    vagrant ssh -c "cd /home/vagrant/glusterfs; ( sudo dnf -C -y builddep --spec glusterfs.spec || sudo dnf -y builddep --spec glusterfs.spec ) $redirect" -- -t
    vagrant ssh -c "cd /home/vagrant/glusterfs; sudo make -j install $redirect" -- -t
    if [ $? -ne 0 ]
    then
            echo "make failed, exiting...."
            popd
            exit 1
    fi
    popd
}

function run_tests()
{
    local retval=0

    pushd "tests/vagrant/${BRANCHNAME}"
    vagrant ssh -c "cd /home/vagrant/glusterfs; sudo ./run-tests.sh $run_tests_args" -- -t
    retval=$?
    popd

    return ${retval}
}

function ssh_into_vm_using_exec()
{
    pushd "tests/vagrant/${BRANCHNAME}"
    exec vagrant ssh
    popd
}

echo
parse_args "$@"

# Check environment for dependencies
force_location
vagrant_check
ansible_check

# We have one vm per git branch, query git branch
set_branchname_from_git_branch

if [ "x$destroy_now" == "xyes" ] ; then
    destroy_vm
    exit $?
fi

if [ "x$sshvm" == "xyes" ] ; then
    ssh_into_vm_using_exec
fi


create_vagrant_dir
start_vm
set_vm_attributes



copy_source_code
compile_gluster
run_tests
RET=$?

if [ "x$destroy_after_test" == "xyes" ] ; then
    destroy_vm
fi

exit ${RET}