Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # This file contains bash functions which may be of use to our Jenkins jobs. |
| 4 | |
| 5 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 6 | function 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 || : |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 11 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 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. |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 16 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 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. |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 31 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 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 |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 37 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 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 |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 42 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 43 | echo "Remove old git repo files." |
| 44 | rm -Rf ./openbmc-build-scripts |
| 45 | rm -Rf ./openbmc-test-automation |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 46 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 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 |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 50 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 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 |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 56 | |
| 57 | } |
| 58 | |
| 59 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 60 | function process_docker() { |
| 61 | # Do not echo all commands to terminal. |
| 62 | set +x |
| 63 | local git_dir_path="${1}" ; shift || : |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 64 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 65 | # Source the docker script to prepare our environment for calling docker. |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 66 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 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. |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 70 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 71 | # Set global DOCKER_IMG_NAME. |
| 72 | DOCKER_IMG_NAME="openbmc/obmc-docker-image" |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 73 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 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 |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 79 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 80 | cd "${git_dir_path}" || return 1 |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 81 | |
| 82 | } |
| 83 | |
| 84 | |
| 85 | if ! test "${stock_robot_program_parms+defined}" ; then |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 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" |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 90 | fi |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 91 | function 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 || : |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 97 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 98 | # Compose a robot command string and run it in a docker environment. |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 99 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 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). |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 112 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 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" |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 116 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 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}" |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 121 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 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='' |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 131 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 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 |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 141 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 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 |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 151 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 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 |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 156 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 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}" |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 161 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 162 | # Run the docker container to execute the code update. |
| 163 | # The test results will be put in ${WORKSPACE}. |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 164 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 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}" |
Michael Walsh | 665afe7 | 2017-10-04 14:49:39 -0500 | [diff] [blame] | 172 | |
| 173 | } |