Skip to content

Commit

Permalink
Watchdog check
Browse files Browse the repository at this point in the history
  • Loading branch information
happz committed Nov 21, 2023
1 parent b4585a5 commit e908fd3
Show file tree
Hide file tree
Showing 9 changed files with 465 additions and 11 deletions.
33 changes: 33 additions & 0 deletions tests/test/check/data/main.fmf
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,36 @@
sudo bash -c "passwd --help &> /root/passwd.log; \
ls -alZ /root/passwd.log; \
rm -f /root/passwd.log" || /bin/true

/watchdog/ping:
test: |
set -x

export

# Collect a couple of successful reponse
sysctl net.ipv4.icmp_echo_ignore_all
# sysctl net.ipv4.icmp_echo_ignore_all=0
echo "test starts, will sleep for a while"
sleep 10

# Disable ICMP response
# sysctl net.ipv4.icmp_echo_ignore_all=1

# Now wait to be rebooted
sleep 60

echo "bye!"

duration: 30m

check:
- name: watchdog
interval: 5

ping: true
ping-packets: 1
ping-threshold: 3

ssh-ping: true
ssh-ping-threshold: 3
12 changes: 12 additions & 0 deletions tests/test/check/main.fmf
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,15 @@ tier: 2
environment:
PROVISION_METHODS: local virtual
tag+: [additional_coverage]

/watchdog:
test: ./test-watchdog.sh

environment:
PROVISION_METHODS: local virtual

adjust:
- when: how == full
environment:
PROVISION_METHODS: virtual
tag+: [additional_coverage]
42 changes: 42 additions & 0 deletions tests/test/check/test-watchdog.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

. /usr/share/beakerlib/beakerlib.sh || exit 1

function assert_check_result () {
rlAssertEquals "$1" "watchdog:$2" "$(yq -r ".[] | .check | .[] | select(.event == \"$3\") | \"\\(.name):\\(.result)\"" $results)"
}


rlJournalStart
rlPhaseStartSetup
rlRun "run=\$(mktemp -d)" 0 "Create run directory"

rlRun "results=$run/plan/execute/results.yaml"

rlRun "pushd data"
rlRun "set -o pipefail"
rlPhaseEnd

for method in ${PROVISION_METHODS:-local}; do
rlPhaseStartTest "Test guest watchdog ping with $method provisioning"
rlRun "watchdog_log=$run/plan/execute/data/guest/default-0/watchdog/ping-1/tmt-watchdog.txt"

rlRun "tmt run --id $run --scratch -a -vv provision -h $method test -n /watchdog"

rlRun "cat $results"
rlRun "cat $watchdog_log"

rlAssertExists "$watchdog_log"

assert_check_result "watchdog as an after-test should pass" "pass" "after-test"

# rlAssertGrep "<no matches>" "$avc_log"
rlPhaseEnd
done

rlPhaseStartCleanup
rlRun "popd"

# rlRun "rm -rf $run"
rlPhaseEnd
rlJournalEnd
20 changes: 12 additions & 8 deletions tmt/checks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dataclasses
import enum
from typing import TYPE_CHECKING, Any, Callable, Optional, TypedDict, cast
from typing import TYPE_CHECKING, Any, Callable, Generic, Optional, TypedDict, TypeVar, cast

import tmt.log
import tmt.steps.provision
Expand All @@ -12,15 +12,15 @@
SpecBasedContainer,
cached_property,
field,
)
)

if TYPE_CHECKING:
import tmt.base
from tmt.result import CheckResult
from tmt.steps.execute import TestInvocation


CheckPluginClass = type['CheckPlugin']
CheckPluginClass = type['CheckPlugin[Any]']

_CHECK_PLUGIN_REGISTRY: PluginRegistry[CheckPluginClass] = PluginRegistry()

Expand Down Expand Up @@ -154,10 +154,13 @@ def go(
raise tmt.utils.GeneralError(f"Unsupported test check event '{event}'.")


class CheckPlugin(tmt.utils._CommonBase):
CheckT = TypeVar('CheckT', bound='Check')


class CheckPlugin(tmt.utils._CommonBase, Generic[CheckT]):
""" Base class for plugins providing extra checks before, during and after tests """

_check_class: type[Check] = Check
_check_class: type[CheckT]

# Keep this method around, to correctly support Python's method resolution order.
def __init__(self, *args: Any, **kwargs: Any) -> None:
Expand All @@ -171,13 +174,14 @@ def delegate(
logger: tmt.log.Logger) -> Check:
""" Create a check data instance for the plugin """

return find_plugin(raw_data['name'])._check_class.from_spec(raw_data, logger)
return cast(CheckPlugin[CheckT], find_plugin(raw_data['name'])) \
._check_class.from_spec(raw_data, logger)

@classmethod
def before_test(
cls,
*,
check: Check,
check: CheckT,
invocation: 'TestInvocation',
environment: Optional[tmt.utils.EnvironmentType] = None,
logger: tmt.log.Logger) -> list['CheckResult']:
Expand All @@ -187,7 +191,7 @@ def before_test(
def after_test(
cls,
*,
check: Check,
check: CheckT,
invocation: 'TestInvocation',
environment: Optional[tmt.utils.EnvironmentType] = None,
logger: tmt.log.Logger) -> list['CheckResult']:
Expand Down
4 changes: 3 additions & 1 deletion tmt/checks/avc.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@


@provides_check('avc')
class AvcDenials(CheckPlugin):
class AvcDenials(CheckPlugin[Check]):
_check_class = Check

@classmethod
def _save_report(
cls,
Expand Down
4 changes: 3 additions & 1 deletion tmt/checks/dmesg.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@


@provides_check('dmesg')
class DmesgCheck(CheckPlugin):
class DmesgCheck(CheckPlugin[Check]):
_check_class = Check

@classmethod
def _fetch_dmesg(
cls,
Expand Down
Loading

0 comments on commit e908fd3

Please sign in to comment.