c4145675cd22b014ae688cf269a5c61a7487f2bf
[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 #   4. cd $WS_ROOT
28 #   5. git submodule update --init --recursive
29
30 jjb-sandbox-env()
31 {
32     local jjb_version=${JJB_VERSION:-"5.0.2"}
33
34     if [ -z "$WS_ROOT" ] ; then
35         echo "ERROR: WS_ROOT is not set!"
36         return
37     elif [ ! -d "$WS_ROOT/jjb" ] ; then
38         echo "ERROR: WS_ROOT is not set to a ci-management workspace:"
39         echo "       '$WS_ROOT'"
40         return
41     fi
42
43     if [ -n "$(declare -f deactivate)" ]; then
44         echo "Deactivating Python Virtualenv!"
45         deactivate
46     fi
47
48     if [ -z "$JENKINS_INI" ] ; then
49         local user_jenkins_ini="/home/$USER/.config/jenkins_jobs/jenkins.ini"
50         if [ -f "$user_jenkins_ini" ] ; then
51             export JENKINS_INI=$user_jenkins_ini
52         elif [ -f "$WS_ROOT/jenkins.ini" ] ; then
53             export JENKINS_INI="$WS_ROOT/jenkins.ini"
54         else
55             echo "ERROR: Unable to find 'jenkins.ini'!"
56             return
57         fi
58         echo "Exporting JENKINS_INI=$JENKINS_INI"
59     elif [ ! -f "$JENKINS_INI" ] ; then
60         echo "ERROR: file specified in JENKINS_INI ($JENKINS_INI) not found!"
61         return
62     fi
63
64     if [ -n "$(declare -f deactivate)" ]; then
65         echo "Deactivating Python Virtualenv!"
66         deactivate
67     fi
68     cd $WS_ROOT
69     git submodule update --init --recursive
70
71     local VENV_DIR=$WS_ROOT/venv
72     rm -rf $VENV_DIR \
73        && python3 -m venv $VENV_DIR \
74        && source $VENV_DIR/bin/activate \
75        && pip3 install wheel \
76        && pip3 install jenkins-job-builder==$jjb_version
77
78     alias jjsb='jenkins-jobs --conf $JENKINS_INI'
79     function jjsb-test() {
80         if [ -z "$(which jenkins-jobs 2>&1)" ] ; then
81             echo "jenkins-jobs not found!  Run jjb-sandbox-env to activate."
82             return
83         fi
84         local grep_job_names=""
85         if [ "$1" = "-n" ] ; then
86             grep_job_names="true"
87             shift
88         fi
89         if [ -z "$1" ] ; then
90             echo "Usage: $FUNCNAME <jenkins-job-name>"
91             return
92         fi
93         if [ -z "$grep_job_names" ]; then
94             jenkins-jobs --conf $JENKINS_INI test $WS_ROOT/jjb $@
95         else
96             jenkins-jobs --conf $JENKINS_INI test $WS_ROOT/jjb $@ 2>&1 | grep -e'Number of jobs' -e'Job name' | sed -e 's/INFO:jenkins_jobs.builder://g'
97         fi
98     }
99     function jjsb-update() {
100         if [ -z "$(which jenkins-jobs 2>&1)" ] ; then
101             echo "jenkins-jobs not found!  Run jjb-sandbox-env to activate."
102             return
103         fi
104         local grep_num_jobs=""
105         if [ "$1" = "-n" ] ; then
106             grep_num_jobs="true"
107             shift
108         fi
109         if [ -z "$1" ] ; then
110             echo "Usage: $FUNCNAME <jenkins-job-name>"
111             return
112         fi
113         if [ -z "grep_num_jobs" ] ; then
114             jenkins-jobs --conf $JENKINS_INI update $WS_ROOT/jjb $@
115         else
116             jenkins-jobs --conf $JENKINS_INI update $WS_ROOT/jjb $@ 2>&1 | grep -e'Number of jobs'
117         fi
118     }
119
120     jenkins-jobs --version
121 }
122
123 # Get the refspec for the specified project branch at HEAD
124 #
125 # Arguments:
126 #   $1 - branch
127 #   $2 - project (Optional: defaults to 'vpp')
128 get_gerrit_refspec() {
129     local branch=${1:-"master"}
130     local project=${2:-"vpp"}
131     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)"
132
133     if [ -z "$query" ] ; then
134         echo "ERROR: Invalid argument(s): branch ($1) project ($2)"
135         echo "Usage: $0 <branch> <project>"
136     else
137         echo "$query"
138     fi
139 }