Merge "Import jjb test bash functions from vpp repo."
[ci-management.git] / extras / bash / sandbox_test_functions.sh
1 # Copyright (c) 2021 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 # bash function to set up jenkins sandbox environment
15 #
16 # See LF Sandbox documentation:
17 #   https://docs.releng.linuxfoundation.org/en/latest/jenkins-sandbox.html
18 #
19 # Prerequisites:
20 #   1. Create jenkins sandbox token and add it to your local jenkins.ini file
21 #      Either specify the location of the init file in $JENKINS_INI or
22 #      JENKINS_INI will be initialized to either
23 #         ~/.config/jenkins_jobs/jenkins.ini
24 #         $WS_ROOT/jenkins.ini
25 #   2. Clone ci-management workspace from gerrit.fd.io
26 #   3. export WS_ROOT=<local ci-management workspace>
27 jjb-sandbox-env()
28 {
29     local jjb_version=${JJB_VERSION:-"3.5.0"}
30
31     if [ -z "$WS_ROOT" ] ; then
32         echo "ERROR: WS_ROOT is not set!"
33         return
34     elif [ ! -d "$WS_ROOT/jjb" ] ; then
35         echo "ERROR: WS_ROOT is not set to a ci-management workspace:"
36         echo "       '$WS_ROOT'"
37         return
38     fi
39
40     if [ -n "$(declare -f deactivate)" ]; then
41         echo "Deactivating Python Virtualenv!"
42         deactivate
43     fi
44
45     if [ -z "$JENKINS_INI" ] ; then
46         local user_jenkins_ini="/home/$USER/.config/jenkins_jobs/jenkins.ini"
47         if [ -f "$user_jenkins_ini" ] ; then
48             export JENKINS_INI=$user_jenkins_ini
49         elif [ -f "$WS_ROOT/jenkins.ini" ] ; then
50             export JENKINS_INI="$WS_ROOT/jenkins.ini"
51         else
52             echo "ERROR: Unable to find 'jenkins.ini'!"
53             return
54         fi
55         echo "Exporting JENKINS_INI=$JENKINS_INI"
56     elif [ ! -f "$JENKINS_INI" ] ; then
57         echo "ERROR: file specified in JENKINS_INI ($JENKINS_INI) not found!"
58         return
59     fi
60
61     if [ -n "$(declare -f deactivate)" ]; then
62         echo "Deactivating Python Virtualenv!"
63         deactivate
64     fi
65     cd $WS_ROOT
66     git submodule update --init --recursive
67
68     local VENV_DIR=$WS_ROOT/venv
69     rm -rf $VENV_DIR \
70        && python3 -m venv $VENV_DIR \
71        && source $VENV_DIR/bin/activate \
72        && pip3 install wheel \
73        && pip3 install jenkins-job-builder==$jjb_version
74
75     alias jjsb='jenkins-jobs --conf $JENKINS_INI'
76     function jjsb-test() {
77         if [ -z "$(which jenkins-jobs 2>&1)" ] ; then
78             echo "jenkins-jobs not found!  Run jjb-sandbox-env to activate."
79             return
80         fi
81         if [ -z "$1" ] ; then
82             echo "Usage: $FUNCNAME <jenkins-job-name>"
83             return
84         fi
85         which jenkins-jobs \
86             && jenkins-jobs --conf $JENKINS_INI test $WS_ROOT/jjb $@
87     }
88     function jjsb-update() {
89         if [ -z "$(which jenkins-jobs 2>&1)" ] ; then
90             echo "jenkins-jobs not found!  Run jjb-sandbox-env to activate."
91             return
92         fi
93         if [ -z "$1" ] ; then
94             echo "Usage: $FUNCNAME <jenkins-job-name>"
95             return
96         fi
97         which jenkins-jobs \
98             && jenkins-jobs --conf $JENKINS_INI update $WS_ROOT/jjb $@
99     }
100     jenkins-jobs --version
101 }
102
103 # Get the refspec for the specified project branch at HEAD
104 #
105 # Arguments:
106 #   $1 - branch
107 #   $2 - project (Optional: defaults to 'vpp')
108 get_gerrit_refspec() {
109     local branch=${1:-"master"}
110     local project=${2:-"vpp"}
111     local query="$(ssh -p 29418 gerrit.fd.io gerrit query status:merged project:$project branch:$branch limit:1 --format=JSON --current-patch-set | tr ',' '\n' | grep refs | cut -d'"' -f4)"
112
113     if [ -z "$query" ] ; then
114         echo "ERROR: Invalid project ($1) or branch ($2)"
115     else
116         echo "$query"
117     fi
118 }