From 1cdfea174dc6c4657b306e91c4f2aeb9531c2205 Mon Sep 17 00:00:00 2001 From: Michael Hudson-Doyle Date: Fri, 15 Jun 2018 11:43:06 +1200 Subject: [PATCH] add some validation of curtin config generated from answers --- scripts/runtests.sh | 9 ++++-- scripts/validate-yaml.py | 62 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 scripts/validate-yaml.py diff --git a/scripts/runtests.sh b/scripts/runtests.sh index c1a3555c..2bf3eb8d 100755 --- a/scripts/runtests.sh +++ b/scripts/runtests.sh @@ -1,4 +1,9 @@ #!/bin/bash +set -eux python3 -m unittest discover -# The --foreground is important to avoid subiquity getting SIGTTOU-ed. -timeout --foreground 60 sh -c 'LANG=C.UTF-8 PYTHONPATH=. python3 -m subiquity.cmd.tui --answers examples/answers.yaml --dry-run --machine-config examples/mwhudson.json' +export SUBIQUITY_REPLAY_TIMESCALE=10 +for answers in examples/answers*.yaml; do + # The --foreground is important to avoid subiquity getting SIGTTOU-ed. + timeout --foreground 60 sh -c "LANG=C.UTF-8 python3 -m subiquity.cmd.tui --answers $answers --dry-run --machine-config examples/mwhudson.json" + python3 scripts/validate-yaml.py .subiquity/subiquity-curtin-install.conf +done diff --git a/scripts/validate-yaml.py b/scripts/validate-yaml.py new file mode 100644 index 00000000..26f404be --- /dev/null +++ b/scripts/validate-yaml.py @@ -0,0 +1,62 @@ +#!/usr/bin/python3 +import sys + +import yaml + + +class StorageChecker: + + def __init__(self): + self.actions = {} + self.unmounted_swap_ids = set() + + def _check_partition(self, action): + assert 'device' in action + assert action['device'] in self.actions + assert 'ptable' in self.actions[action['device']] + + def _check_format(self, action): + assert 'volume' in action + assert action['volume'] in self.actions + if action['fstype'] == 'swap': + self.unmounted_swap_ids.add(action['id']) + + def _check_mount(self, action): + assert 'device' in action + assert action['device'] in self.actions + if not action.get('path'): + assert self.actions[action['device']]['fstype'] == "swap" + self.unmounted_swap_ids.remove(action['device']) + + def check(self, action): + assert 'type' in action + assert 'id' in action + m = getattr(self, '_check_{type}'.format(**action), None) + if m: + m(action) + self.actions[action['id']] = action + + def final_checks(self): + if len(self.unmounted_swap_ids) > 0: + raise Exception( + "some swap formats had no mounts: {}".format( + self.unmounted_swap_ids)) + + +config = yaml.safe_load(open(sys.argv[1])) + + +def main(): + storage_checker = StorageChecker() + + for action in config['storage']['config']: + try: + storage_checker.check(action) + except Exception: + print('checking {} failed'.format(action)) + raise + + storage_checker.final_checks() + + +main()