1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
|
#!/usr/bin/env bash
include './tests/unit/utils.sh'
include './src/plugins/kernel_install/install.sh'
include './src/lib/kwlib.sh'
include './src/lib/kwio.sh'
declare -r TEST_ROOT_PATH="$PWD"
function oneTimeSetUp()
{
# Setup a fake path to make easier to mock some scenarios
TARGET_PATH="$SHUNIT_TMPDIR"
REMOTE_KW_DEPLOY="${PWD}/tests/unit/samples"
INSTALLED_KERNELS_PATH="${REMOTE_KW_DEPLOY}/INSTALLED_KERNELS"
}
function oneTimeTearDown()
{
rm -f "$INSTALLED_KERNELS_PATH"
# shellcheck disable=SC2115
[[ -d ${TARGET_PATH} ]] && rm --recursive --force "${TARGET_PATH}/*"
}
function setUp()
{
mk_fake_boot "$SHUNIT_TMPDIR"
# Creating fake installed kernels
touch "$INSTALLED_KERNELS_PATH"
# Replace KW_DEPLOY_TMP_FILE
test_tmp_file="${SHUNIT_TMPDIR}/tmp/kw"
REMOTE_KW_DEPLOY="${SHUNIT_TMPDIR}/opt/kw"
KW_DEPLOY_TMP_FILE="$test_tmp_file"
mkdir --parents "$test_tmp_file"
# Mock variables
KW_PLUGINS_DIR="${PWD}/src/plugins"
REMOTE_KW_DEPLOY="${KW_PLUGINS_DIR}/kernel_install"
}
function tearDown()
{
rm --recursive --force "$SHUNIT_TMPDIR"
mkdir --parents "$SHUNIT_TMPDIR"
}
function test_install_modules_force_uncompress_failure_path()
{
local module_target='5.9.0-rc5-NEW-VRR-TRACK+.tar'
local lib_modules_path_bkp="$LIB_MODULES_PATH"
local output
output="$(
function uncompress_kw_package()
{
return 2
}
install_modules "$module_target" 'TEST_MODE'
)"
assert_equals_helper 'We did not find required files' "$LINENO" 2 "$?"
}
function test_install_modules()
{
local module_target='5.9.0-rc5-NEW-VRR-TRACK+.tar'
local lib_modules_path_bkp="$LIB_MODULES_PATH"
local output
local cmd
cd "$test_tmp_file" || {
fail "(${LINENO}) It was not possible to move to temporary directory"
return
}
# Test preparation
mk_fake_tar_file_to_deploy "$PWD"
# Note: LIB_MODULES_PATH is defined in the utils.sh as /lib/modules
LIB_MODULES_PATH="${KW_DEPLOY_TMP_FILE}${LIB_MODULES_PATH}"
mkdir --parents "$LIB_MODULES_PATH"
install_modules 'remote'
assertTrue "(${LINENO})" '[[ -f "${LIB_MODULES_PATH}/something_1" ]]'
assertTrue "(${LINENO})" '[[ -f "${LIB_MODULES_PATH}/something_2" ]]'
cd "$TEST_ROOT_PATH" || {
fail "(${LINENO}) It was not possible to move to temporary directory"
return
}
# Restore original LIB_MODULES_PATH value
LIB_MODULES_PATH="$lib_modules_path_bkp"
}
function test_install_kernel_remote_force_uncompress_failures()
{
local target='remote'
local reboot='1'
local output=''
output="$(
function uncompress_kw_package()
{
return 2
}
install_kernel 'debian' "$reboot" "$target" '' 'TEST_MODE'
)"
ret="$?"
assert_equals_helper 'Test invalid name' "$LINENO" 2 "$ret"
}
function helper_create_kw_pkg_info_file()
{
local base_path="$1"
local name="$2"
touch "${base_path}/kw_pkg/kw.pkg.info"
{
printf 'kernel_name=%s\n' "$name"
printf 'kernel_binary_image_file=bzImage\n'
printf 'architecture=x86_64\n'
printf 'previous_kernel_backup=yes\n'
} > "${base_path}/kw_pkg/kw.pkg.info"
}
function test_validates_install_kernel_force_failure_due_to_missing_name()
{
local name='test'
local path_prefix=''
local output=''
cd "$SHUNIT_TMPDIR" || {
fail "(${LINENO}) It was not possible to move to temporary directory"
return
}
# Test preparation
mk_fake_tar_file_to_deploy "$PWD" "$KW_DEPLOY_TMP_FILE" "$name"
mkdir --parents "${KW_DEPLOY_TMP_FILE}/kw_pkg"
helper_create_kw_pkg_info_file "$KW_DEPLOY_TMP_FILE" ''
touch "${PWD}/boot/vmlinuz-${name}"
output="$(
function update_bootloader()
{
printf '%s\n' 'update_bootloader_mock'
return 0
}
function migrate_old_kernel_list()
{
return 0
}
function reboot_machine()
{
printf '%s\n' 'reboot'
return 0
}
install_kernel 'debian' '1' 'remote' '' 'TEST_MODE'
)"
assert_equals_helper 'No kerne name' "$LINENO" 22 "$?"
cd "$TEST_ROOT_PATH" || {
fail "(${LINENO}) It was not possible to move to temporary directory"
return
}
}
function test_validates_install_kernel_force_bootloader_failure()
{
local name='test'
local path_prefix=''
local output=''
local ret
cd "$SHUNIT_TMPDIR" || {
fail "(${LINENO}) It was not possible to move to temporary directory"
return
}
# Test preparation
mk_fake_tar_file_to_deploy "$PWD" "$KW_DEPLOY_TMP_FILE" "$name"
mkdir --parents "${KW_DEPLOY_TMP_FILE}/kw_pkg"
helper_create_kw_pkg_info_file "$KW_DEPLOY_TMP_FILE" 'test'
touch "${PWD}/boot/vmlinuz-${name}"
output="$(
function update_bootloader()
{
printf '%s\n' 'update_bootloader_mock'
return 22
}
function migrate_old_kernel_list()
{
return 0
}
function reboot_machine()
{
printf '%s\n' 'reboot'
return 0
}
install_kernel 'debian' '1' 'remote' '' 'TEST_MODE'
)"
assert_equals_helper 'No kerne name' "$LINENO" 22 "$?"
cd "$TEST_ROOT_PATH" || {
fail "(${LINENO}) It was not possible to move to temporary directory"
return
}
}
function test_validates_install_kernel_to_remote_target()
{
local name='test'
local path_prefix=''
local output=''
cd "$SHUNIT_TMPDIR" || {
fail "(${LINENO}) It was not possible to move to temporary directory"
return
}
# Check standard remote kernel installation
declare -a cmd_sequence=(
"rm --recursive --force ${KW_DEPLOY_TMP_FILE}/kw_pkg"
"tar --touch --auto-compress --extract --file='${KW_DEPLOY_TMP_FILE}/${name}.kw.tar' --directory='${SHUNIT_TMPDIR}/tmp/kw' --no-same-owner"
"rsync --archive ${SHUNIT_TMPDIR}/tmp/kw/kw_pkg/modules/lib/modules/* /lib/modules"
"cp ${PWD}/boot/vmlinuz-${name} ${PWD}/boot/vmlinuz-${name}.old"
"cp ${SHUNIT_TMPDIR}/tmp/kw/kw_pkg/config-test /boot/"
"cp ${SHUNIT_TMPDIR}/tmp/kw/kw_pkg/bzImage /boot/"
'update_bootloader_mock'
"grep --fixed-strings --line-regexp --quiet ${name} ${INSTALLED_KERNELS_PATH}"
'reboot'
)
# Test preparation
mk_fake_tar_file_to_deploy "$PWD" "$KW_DEPLOY_TMP_FILE" "$name"
mkdir --parents "${KW_DEPLOY_TMP_FILE}/kw_pkg"
helper_create_kw_pkg_info_file "$KW_DEPLOY_TMP_FILE" "$name"
touch "${PWD}/boot/vmlinuz-${name}"
output="$(
function update_bootloader()
{
printf '%s\n' 'update_bootloader_mock'
return 0
}
function migrate_old_kernel_list()
{
return 0
}
function reboot_machine()
{
printf '%s\n' 'reboot'
return 0
}
install_kernel 'debian' '1' 'remote' '' 0 'TEST_MODE'
)"
compare_command_sequence '' "$LINENO" 'cmd_sequence' "$output"
cd "$TEST_ROOT_PATH" || {
fail "(${LINENO}) It was not possible to move to temporary directory"
return
}
}
function test_install_kernel_local()
{
local name='test'
local target='local'
local flag='TEST_MODE'
local sudo_cmd='sudo --preserve-env'
local path_prefix=''
local output
cd "$SHUNIT_TMPDIR" || {
fail "(${LINENO}) It was not possible to move to temporary directory"
return
}
# Test preparation
mk_fake_tar_file_to_deploy "$PWD" "$KW_DEPLOY_TMP_FILE"
mkdir --parents "${KW_DEPLOY_TMP_FILE}/kw_pkg"
helper_create_kw_pkg_info_file "$KW_DEPLOY_TMP_FILE" "$name"
# Check standard remote kernel installation
declare -a cmd_sequence=(
"rm --recursive --force ${KW_DEPLOY_TMP_FILE}/kw_pkg"
"tar --touch --auto-compress --extract --file='${KW_DEPLOY_TMP_FILE}/${name}.kw.tar' --directory='${SHUNIT_TMPDIR}/tmp/kw' --no-same-owner"
"sudo --preserve-env rsync --archive ${SHUNIT_TMPDIR}/tmp/kw/kw_pkg/modules/lib/modules/* /lib/modules"
"sudo --preserve-env cp ${KW_DEPLOY_TMP_FILE}/kw_pkg/config-test /boot/"
"sudo --preserve-env cp ${KW_DEPLOY_TMP_FILE}/kw_pkg/bzImage /boot/"
'update_bootloader_mock'
"sudo --preserve-env grep --fixed-strings --line-regexp --quiet ${name} ${INSTALLED_KERNELS_PATH}"
'sudo --preserve-env reboot'
)
output="$(
function update_bootloader()
{
printf '%s\n' 'update_bootloader_mock'
return 0
}
function migrate_old_kernel_list()
{
return 0
}
function reboot_machine()
{
printf '%s\n' 'sudo --preserve-env reboot'
return 0
}
install_kernel 'debian' '1' 'local' '' 1 'TEST_MODE'
)"
compare_command_sequence '' "$LINENO" 'cmd_sequence' "$output"
cd "$TEST_ROOT_PATH" || {
fail "(${LINENO}) It was not possible to move to temporary directory"
return
}
}
function test_uncompress_kw_package()
{
cd "$SHUNIT_TMPDIR" || {
fail "(${LINENO}) It was not possible to move to temporary directory"
return
}
# Test preparation
mk_fake_tar_file_to_deploy "$PWD" "$KW_DEPLOY_TMP_FILE"
uncompress_kw_package
assertTrue "(${LINENO}): Expected kw_pkg" '[[ -d "${KW_DEPLOY_TMP_FILE}/kw_pkg" ]]'
cd "$TEST_ROOT_PATH" || {
fail "(${LINENO}) It was not possible to move to temporary directory"
return
}
}
function test_uncompress_kw_package_check_invalid_path()
{
uncompress_kw_package '/somethig/xpto/abc/kw.pkg.tar'
assert_equals_helper 'Invalid path' "(${LINENO})" 2 "$?"
}
function test_parse_kw_package_metadata()
{
# Prepare fake kw.pkg.info
mkdir --parents "${KW_DEPLOY_TMP_FILE}/kw_pkg"
touch "${KW_DEPLOY_TMP_FILE}/kw_pkg/kw.pkg.info"
printf 'kernel_name=test\n' > "${KW_DEPLOY_TMP_FILE}/kw_pkg/kw.pkg.info"
printf 'kernel_binary_image_file=vmlinuz-test\n' >> "${KW_DEPLOY_TMP_FILE}/kw_pkg/kw.pkg.info"
printf 'architecture=x86_64\n' >> "${KW_DEPLOY_TMP_FILE}/kw_pkg/kw.pkg.info"
parse_kw_package_metadata ''
name=${kw_package_metadata['kernel_name']}
arch=${kw_package_metadata['architecture']}
kernel_image_name=${kw_package_metadata['kernel_binary_image_file']}
assert_equals_helper 'Wrong kernel name' "(${LINENO})" 'test' "${kw_package_metadata['kernel_name']}"
assert_equals_helper 'Wrong architecture' "(${LINENO})" 'x86_64' "${kw_package_metadata['architecture']}"
assert_equals_helper 'Wrong binary image name' "(${LINENO})" 'vmlinuz-test' "${kw_package_metadata['kernel_binary_image_file']}"
}
function test_parse_kw_package_metadata_invalid_path()
{
parse_kw_package_metadata '/an/invalid/folder'
assert_equals_helper 'Expected an error with invalid path' "(${LINENO})" 22 "$?"
}
function test_parse_kw_package_metadata_no_pkg_info()
{
parse_kw_package_metadata ''
assert_equals_helper 'Expected an error due to the lack of info file' "(${LINENO})" 22 "$?"
}
invoke_shunit
|