From f018e6ecf705e8c498437e15ec3b09bd84b2e3c4 Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Thu, 28 Sep 2017 14:36:01 +0100 Subject: [PATCH] [functional-tests] Check *_restore doesn't touch the metadata if the xml doesn't exists, or isn't xml. --- functional-tests/cache-functional-tests.scm | 8 ++++++++ functional-tests/era-functional-tests.scm | 14 ++++++++++++++ functional-tests/functional-tests.scm | 19 ++++++++++++++++++- functional-tests/temp-file.scm | 5 ++--- functional-tests/thin-functional-tests.scm | 9 +++++++++ 5 files changed, 51 insertions(+), 4 deletions(-) diff --git a/functional-tests/cache-functional-tests.scm b/functional-tests/cache-functional-tests.scm index 4084c11..a6136f5 100644 --- a/functional-tests/cache-functional-tests.scm +++ b/functional-tests/cache-functional-tests.scm @@ -167,8 +167,16 @@ "the input file can't be found" (with-empty-metadata (md) (receive (_ stderr) (run-fail "cache_restore -i no-such-file -o" md) + (assert-superblock-untouched md) (assert-starts-with "Couldn't stat file" stderr)))) + (define-scenario (cache-restore garbage-input-file) + "the input file is just zeroes" + (with-empty-metadata (md) + (with-temp-file-sized ((xml "cache.xml" 4096)) + (receive (_ stderr) (run-fail "cache_restore -i " xml "-o" md) + (assert-superblock-untouched md))))) + (define-scenario (cache-restore missing-output-file) "the output file can't be found" (with-cache-xml (xml) diff --git a/functional-tests/era-functional-tests.scm b/functional-tests/era-functional-tests.scm index bf124ad..efb874a 100644 --- a/functional-tests/era-functional-tests.scm +++ b/functional-tests/era-functional-tests.scm @@ -136,6 +136,20 @@ (receive (_ stderr) (run-fail "era_restore" "-o" md) (assert-starts-with "No input file provided." stderr)))) + (define-scenario (era-restore missing-input-file) + "the input file can't be found" + (with-empty-metadata (md) + (receive (_ stderr) (run-fail "era_restore -i no-such-file -o" md) + (assert-superblock-untouched md) + (assert-starts-with "Couldn't stat file" stderr)))) + + (define-scenario (era-restore garbage-input-file) + "the input file is just zeroes" + (with-empty-metadata (md) + (with-temp-file-sized ((xml "era.xml" 4096)) + (receive (_ stderr) (run-fail "era_restore -i " xml "-o" md) + (assert-superblock-untouched md))))) + (define-scenario (era-restore output-unspecified) "Fails if no metadata dev specified" (with-era-xml (xml) diff --git a/functional-tests/functional-tests.scm b/functional-tests/functional-tests.scm index 899f4f6..fcc6701 100644 --- a/functional-tests/functional-tests.scm +++ b/functional-tests/functional-tests.scm @@ -20,10 +20,12 @@ assert-equal assert-eof assert-starts-with - assert-matches) + assert-matches + assert-superblock-untouched) (import (chezscheme) + (bcache block-manager) (fmt fmt) (list-utils) (logging) @@ -221,5 +223,20 @@ (unless ((regex pattern) str) (fail (fmt #f "string should match: " pattern ", " str)))) + (define (all-zeroes? ptr count) + (let ((u8-ptr (make-ftype-pointer unsigned-8 ptr))) + (let loop ((i 0)) + (if (= count i) + #t + (if (zero? (ftype-ref unsigned-8 () u8-ptr i)) + (loop (+ i 1)) + #f))))) + + (define (assert-superblock-untouched md) + (with-bcache (cache md 1) + (with-block (b cache 0 (get-flags)) + (unless (all-zeroes? (block-data b) 4096) + (fail "superblock contains non-zero data"))))) + ) diff --git a/functional-tests/temp-file.scm b/functional-tests/temp-file.scm index bef5254..6687f6e 100644 --- a/functional-tests/temp-file.scm +++ b/functional-tests/temp-file.scm @@ -97,9 +97,8 @@ (define (with-temp-file-sized-thunk filename size fn) (with-temp-file-thunk filename (lambda (path) - (let ((cmd (fmt #f (dsp "fallocate -l ") (wrt size) (dsp " ") (dsp path)))) - (system cmd) - (fn path))))) + (system (fmt #f "fallocate -l " (wrt size) " " path)) + (fn path)))) (define-syntax with-temp-file-sized (syntax-rules () diff --git a/functional-tests/thin-functional-tests.scm b/functional-tests/thin-functional-tests.scm index eb96d51..b4b1f49 100644 --- a/functional-tests/thin-functional-tests.scm +++ b/functional-tests/thin-functional-tests.scm @@ -5,6 +5,7 @@ (import (chezscheme) + (bcache block-manager) (disk-units) (fmt fmt) (functional-tests) @@ -158,8 +159,16 @@ "the input file can't be found" (with-empty-metadata (md) (receive (_ stderr) (run-fail "thin_restore -i no-such-file -o" md) + (assert-superblock-untouched md) (assert-starts-with "Couldn't stat file" stderr)))) + (define-scenario (thin-restore garbage-input-file) + "the input file is just zeroes" + (with-empty-metadata (md) + (with-temp-file-sized ((xml "thin.xml" 4096)) + (receive (_ stderr) (run-fail "thin_restore -i " xml "-o" md) + (assert-superblock-untouched md))))) + (define-scenario (thin-restore missing-output-file) "the output file can't be found" (with-thin-xml (xml)