github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/tests/suites/hooks/reboot.sh (about)

     1  run_start_hook_fires_after_reboot() {
     2  	echo
     3  
     4  	model_name="test-start-hook-fires-after-reboot"
     5  	file="${TEST_DIR}/${model_name}.log"
     6  
     7  	ensure "${model_name}" "${file}"
     8  
     9  	# the log messages the test looks for do not appear if root
    10  	# log level is WARNING.
    11  	juju model-config -m "${model_name}" logging-config="<root>=INFO;unit=DEBUG"
    12  
    13  	juju deploy jameinel-ubuntu-lite --revision 9 --channel stable
    14  	wait_for "ubuntu-lite" "$(idle_condition "ubuntu-lite")"
    15  
    16  	# Ensure that the implicit start hook after reboot detection does not
    17  	# fire for the initial charm deployment
    18  	echo "[+] ensuring that implicit start hook does not fire after initial deployment"
    19  	logs=$(juju debug-log --include-module juju.worker.uniter --replay --no-tail | grep -n "reboot detected" || true)
    20  	echo "$logs" | sed 's/^/    | /g'
    21  	if [ -n "$logs" ]; then
    22  		# shellcheck disable=SC2046
    23  		echo $(red "Uniter incorrectly assumed a reboot occurred after initial charm deployment")
    24  		exit 1
    25  	fi
    26  
    27  	# Restart the agent and ensure that the implicit start hook still
    28  	# does not fire. In juju 2.9+, we use a unified agent so we need to restart
    29  	# the machine agent.
    30  	echo "[+] ensuring that implicit start hook does not fire after restarting the (unified) unit agent"
    31  	juju ssh ubuntu-lite/0 'sudo service jujud-machine-0 restart'
    32  	echo
    33  	wait_for "ubuntu-lite" "$(charm_rev "ubuntu-lite" 9)"
    34  	logs=$(juju debug-log --include-module juju.worker.uniter --replay --no-tail | grep -n "reboot detected" || true)
    35  	echo "$logs" | sed 's/^/    | /g'
    36  	if [ -n "$logs" ]; then
    37  		# shellcheck disable=SC2046
    38  		echo $(red "Uniter incorrectly assumed a reboot occurred after restarting the agent")
    39  		exit 1
    40  	fi
    41  	sleep 1
    42  	wait_for "ubuntu-lite" "$(idle_condition "ubuntu-lite")"
    43  
    44  	# Ensure that the implicit start hook does not fire after upgrading the unit
    45  	juju refresh ubuntu-lite --revision 10
    46  	echo
    47  	sleep 1
    48  	wait_for "ubuntu-lite" "$(charm_rev "ubuntu-lite" 10)"
    49  	logs=$(juju debug-log --include-module juju.worker.uniter --replay --no-tail | grep -n "reboot detected" || true)
    50  	echo "$logs" | sed 's/^/    | /g'
    51  	if [ -n "$logs" ]; then
    52  		# shellcheck disable=SC2046
    53  		echo $(red "Uniter incorrectly assumed a reboot occurred after restarting the agent")
    54  		exit 1
    55  	fi
    56  
    57  	sleep 1
    58  	wait_for "ubuntu-lite" "$(idle_condition "ubuntu-lite")"
    59  
    60  	# Trigger a reboot and verify that the implicit start hook fires
    61  	echo "[+] ensuring that implicit start hook fires after a machine reboot"
    62  	juju ssh ubuntu-lite/0 'sudo reboot now' || true
    63  	sleep 1
    64  	wait_for "ubuntu-lite" "$(idle_condition "ubuntu-lite")"
    65  	echo
    66  	logs=$(juju debug-log --include-module juju.worker.uniter --replay --no-tail | grep -n "reboot detected" || true)
    67  	echo "$logs" | sed 's/^/    | /g'
    68  	if [ -z "$logs" ]; then
    69  		# shellcheck disable=SC2046
    70  		echo $(red "Uniter did not fire start hook after the machine rebooted")
    71  		exit 1
    72  	fi
    73  
    74  	destroy_model "${model_name}"
    75  }
    76  
    77  run_reboot_monitor_state_cleanup() {
    78  	echo
    79  
    80  	model_name="test-reboot-monitor-state-cleanup"
    81  	file="${TEST_DIR}/${model_name}.log"
    82  
    83  	ensure "${model_name}" "${file}"
    84  
    85  	juju deploy juju-qa-test --base ubuntu@22.04
    86  	juju deploy juju-qa-dummy-subordinate
    87  	juju integrate juju-qa-test dummy-subordinate
    88  	wait_for "juju-qa-test" "$(idle_condition "juju-qa-test" 1)"
    89  	wait_for "dummy-subordinate" "$(idle_subordinate_condition "dummy-subordinate" "juju-qa-test")"
    90  
    91  	# Check that the reboot flag files have been created for both the charm and
    92  	# the subordinate. Note: juju ssh adds whitespace which we need to trim
    93  	# with a bit of awk magic to ensure that our comparisons work correctly
    94  	echo "[+] Verifying that reboot monitor state files are in place"
    95  	num_files=$(juju ssh juju-qa-test/0 'ls -1 /var/run/juju/reboot-monitor/ | wc -l' 2>/dev/null | tr -d "[:space:]")
    96  	echo "   | number of monitor state files: ${num_files}"
    97  	if [ "$num_files" != "2" ]; then
    98  		# shellcheck disable=SC2046
    99  		echo $(red "Expected 2 reboot monitor state files to be created; got ${num_files}")
   100  		exit 1
   101  	fi
   102  
   103  	# Remove subordinate and ensure that the state file for its monitor got purged
   104  	echo "[+] Verifying that reboot monitor state files are removed once a subordinate gets removed"
   105  	juju remove-relation juju-qa-test dummy-subordinate
   106  	wait_for "juju-qa-test" "$(idle_condition "juju-qa-test" 1)"
   107  
   108  	wait_for_subordinate_count "juju-qa-test"
   109  	num_files=$(juju ssh juju-qa-test/0 'ls -1 /var/run/juju/reboot-monitor/ | wc -l' 2>/dev/null | tr -d "[:space:]")
   110  	echo "   | number of monitor state files: ${num_files}"
   111  	if [ "$num_files" != "1" ]; then
   112  		# shellcheck disable=SC2046
   113  		echo $(red "Expected one remaining reboot monitor state file after subordinate removal; got ${num_files}")
   114  		exit 1
   115  	fi
   116  
   117  	destroy_model "${model_name}"
   118  }
   119  
   120  test_start_hook_fires_after_reboot() {
   121  	if [ "$(skip 'test_start_hook_fires_after_reboot')" ]; then
   122  		echo "==> TEST SKIPPED: start hook fires after reboot"
   123  		return
   124  	fi
   125  
   126  	(
   127  		set_verbosity
   128  
   129  		cd .. || exit
   130  
   131  		run "run_start_hook_fires_after_reboot"
   132  		run "run_reboot_monitor_state_cleanup"
   133  	)
   134  }