1#!/bin/bash
2
3# This file contains bash functions which may be of use to our Jenkins jobs.
4
5
6function process_git {
7  # Do not echo all commands to terminal.
8  set +x
9  local git_dir_path="${1}" ; shift || :
10  local post_clone_command="${1}" ; shift || :
11
12  # Do git processing for this Jenkins job which includes:
13  # - Recognizing existing git repo if appropriate.
14  # - Cloning git repo.
15  # - Running caller's post_clone_command.
16
17  # Description of argument(s):
18  # git_dir_path                    The location of the git dir path (e.g.
19  #                                 "/home/johndoe/git/").  If the
20  #                                 git_dir_path already exists and it was
21  #                                 specified explicitly by the user, this
22  #                                 function will neither clone the git repo
23  #                                 to this path nor run the
24  #                                 post_clone_commands, i.e. the indicated
25  #                                 git repo will be used as-is.
26  # post_clone_command              Any valid bash command to be run after git
27  #                                 clone of openbmc-test-automation. Note
28  #                                 that this is intended primarily for Open
29  #                                 BMC Test code developers who may wish to
30  #                                 cherry pick code changes for testing.
31
32  if [ -d "${git_dir_path}" -a "${git_dir_path}" != "${WORKSPACE}" ] ; then
33    echo "\"${git_dir_path}\" already exists and is not the standard" \
34         "location of \"${WORKSPACE}\" so no git processing is required."
35    return
36  fi
37
38  # Echo all subsequent commands to terminal.
39  set -x
40  mkdir -p "${git_dir_path}" || return 1
41  cd "${git_dir_path}" || return 1
42
43  echo "Remove old git repo files."
44  rm -Rf ./openbmc-build-scripts
45  rm -Rf ./openbmc-test-automation
46
47  git clone https://gerrit.openbmc-project.xyz/openbmc/openbmc-build-scripts\
48    || return 1
49  git clone https://github.com/openbmc/openbmc-test-automation.git || return 1
50
51  if [ ! -z "${post_clone_command}" ] ; then
52    cd openbmc-test-automation || return 1
53    echo "Run the caller's post clone command."
54    eval "${post_clone_command}" || return 1
55  fi
56
57}
58
59
60function process_docker {
61  # Do not echo all commands to terminal.
62  set +x
63  local git_dir_path="${1}" ; shift || :
64
65  # Source the docker script to prepare our environment for calling docker.
66
67  # Description of argument(s):
68  # git_dir_path                    The location of the git dir path (e.g.
69  #                                 "/home/johndoe/git/") to be used.
70
71  # Set global DOCKER_IMG_NAME.
72  DOCKER_IMG_NAME="openbmc/obmc-docker-image"
73
74  echo "Build the docker image required to execute the robot tests."
75  # Echo all subsequent commands to terminal.
76  set -x
77  cd "${git_dir_path}openbmc-build-scripts" || return 1
78  . "./scripts/build-qemu-robot-docker.sh" || return 1
79
80  cd "${git_dir_path}" || return 1
81
82}
83
84
85if ! test "${stock_robot_program_parms+defined}" ; then
86  readonly stock_robot_program_parms="openbmc_host openbmc_username"\
87" openbmc_password os_host os_username os_password quiet debug test_mode"
88  readonly master_robot_gen_parms="console consolecolors outputdir output log"\
89" report loglevel include"
90fi
91function run_docker_robot {
92  # Do not echo all commands to terminal.
93  set +x
94  local robot_file_path="${1}" ; shift || :
95  local robot_pgm_parms="${1-}" ; shift || :
96  local robot_gen_parms="${1:-${master_robot_gen_parms}}" ; shift || :
97
98  # Compose a robot command string and run it in a docker environment.
99
100  # Description of argument(s):
101  # robot_file_path                 The file path of the robot file (with or
102  #                                 without .robot suffix).  This file path is
103  #                                 relative to the base git repository.
104  # robot_pgm_parms                 A space-separated list of parms which are
105  #                                 to be passed to the robot program via the
106  #                                 -v robot parameter.  These parms will be
107  #                                 processed in addition to the
108  #                                 stock_robot_program_parms listed above.
109  # robot_gen_parms                 A space-separated list of general robot
110  #                                 parameters understood by the robot program
111  #                                 (e.g. consolecolors, etc).
112
113  # Strip, then re-append ".robot" so that the user can pass with or without
114  # the .robot suffix.
115  robot_file_path="${robot_file_path%.robot}.robot"
116
117  # Determine the robot_file_name form the robot_file_path.
118  local robot_file_name="${robot_file_path%.robot}"
119  robot_file_name="${robot_file_name##*/}"
120  local robot_short_file_name="${robot_file_name%.robot}"
121
122  # Set default values for robot_gen_parms.
123  local dft_console=dotted
124  local dft_consolecolors=off
125  local dft_outputdir=/status_dir
126  local dft_output=${robot_short_file_name}.output.xml
127  local dft_log=${robot_short_file_name}.log.html
128  local dft_report=${robot_short_file_name}.report.html
129  local dft_loglevel='TRACE'
130  local dft_include=''
131
132  local cmd_buf
133  # Loop through robot general parms setting any that have no value to a
134  # default value (defined above).
135  for parm_name in ${robot_gen_parms} ; do
136    # If the parm already has a value, continue to next loop iteration.
137    [ ! -z "${!parm_name-}" ] && continue || :
138    cmd_buf="${parm_name}=\"\${dft_${parm_name}}\""
139    eval ${cmd_buf}
140  done
141
142  local robot_cmd_buf='robot'
143  # Process our stock robot program parms along with the caller's
144  # robot_pgm_parms to add to the robot_cmd_buf.
145  local parm_name
146  local escape_quote_char="'\''"
147  for parm_name in ${stock_robot_program_parms} ${robot_pgm_parms} ; do
148    [ -z "${parm_name-}" ] && continue
149    robot_cmd_buf="${robot_cmd_buf} -v ${parm_name}:${!parm_name-}"
150  done
151
152  # Process the robot general parms to add to the robot_cmd_buf.
153  for parm_name in ${robot_gen_parms} ; do
154    robot_cmd_buf="${robot_cmd_buf} --${parm_name}=${!parm_name-}"
155  done
156
157  # Finally append the robot file path to the robot_cmd_buf.
158  additional_parms=" ${additional_parms-}"
159  robot_cmd_buf="${robot_cmd_buf}${additional_parms} ${HOME}/openbmc-test"
160  robot_cmd_buf="${robot_cmd_buf}-automation/${robot_file_path}"
161
162  # Run the docker container to execute the code update.
163  # The test results will be put in ${WORKSPACE}.
164
165  cmd_buf="docker run --user=root --env=HOME=${HOME} --env=PYTHONPATH=${HOME}"
166  cmd_buf="${cmd_buf}/openbmc-test-automation/lib --workdir=${HOME} --volume"
167  cmd_buf="${cmd_buf}=${git_dir_path}:${HOME} --volume=${WORKSPACE}:/status_"
168  cmd_buf="${cmd_buf}dir --tty ${DOCKER_IMG_NAME} python -m ${robot_cmd_buf}"
169  # Echo all subsequent commands to terminal.
170  set -x
171  eval "${cmd_buf}"
172
173}
174