Add stable/2402 branch and remove stable/2306 branch to docker executor image scripts
[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 --upgrade pip \
76        && pip3 install wheel \
77        && pip3 install jenkins-job-builder==$jjb_version
78
79     alias jjsb='jenkins-jobs --conf $JENKINS_INI'
80     function jjsb-test() {
81         if [ -z "$(which jenkins-jobs 2>&1)" ] ; then
82             echo "jenkins-jobs not found!  Run jjb-sandbox-env to activate."
83             return
84         fi
85         local grep_job_names=""
86         if [ "$1" = "-n" ] ; then
87             grep_job_names="true"
88             shift
89         fi
90         if [ -z "$1" ] ; then
91             echo "Usage: $FUNCNAME <jenkins-job-name>"
92             return
93         fi
94         if [ -z "$grep_job_names" ]; then
95             jenkins-jobs --conf $JENKINS_INI test $WS_ROOT/jjb $@
96         else
97             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'
98         fi
99     }
100     function jjsb-update() {
101         if [ -z "$(which jenkins-jobs 2>&1)" ] ; then
102             echo "jenkins-jobs not found!  Run jjb-sandbox-env to activate."
103             return
104         fi
105         local grep_num_jobs=""
106         if [ "$1" = "-n" ] ; then
107             grep_num_jobs="true"
108             shift
109         fi
110         if [ -z "$1" ] ; then
111             echo "Usage: $FUNCNAME <jenkins-job-name>"
112             return
113         fi
114         if [ -z "grep_num_jobs" ] ; then
115             jenkins-jobs --conf $JENKINS_INI update $WS_ROOT/jjb $@
116         else
117             jenkins-jobs --conf $JENKINS_INI update $WS_ROOT/jjb $@ 2>&1 | grep -e'Number of jobs'
118         fi
119     }
120
121     jenkins-jobs --version
122 }
123
124 # Get the refspec for the specified project branch at HEAD
125 #
126 # Arguments:
127 #   $1 - branch
128 #   $2 - project (Optional: defaults to 'vpp')
129 get_gerrit_refspec() {
130     local branch=${1:-"master"}
131     local project=${2:-"vpp"}
132     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)"
133
134     if [ -z "$query" ] ; then
135         echo "ERROR: Invalid argument(s): branch ($1) project ($2)"
136         echo "Usage: $0 <branch> <project>"
137     else
138         echo "$query"
139     fi
140 }