2017-08-15 20:37:45 +05:30
|
|
|
(library
|
|
|
|
(functional-tests)
|
|
|
|
|
|
|
|
(export
|
|
|
|
slurp-file
|
|
|
|
|
|
|
|
scenario
|
|
|
|
scenarios
|
|
|
|
add-scenario
|
|
|
|
list-scenarios
|
|
|
|
describe-scenarios
|
|
|
|
define-scenario
|
|
|
|
fail
|
|
|
|
run-scenario
|
2017-08-17 20:54:10 +05:30
|
|
|
run-scenarios
|
|
|
|
|
|
|
|
tools-version
|
|
|
|
define-tool
|
|
|
|
|
|
|
|
assert-equal
|
|
|
|
assert-eof
|
2017-09-15 19:52:04 +05:30
|
|
|
assert-starts-with
|
2017-09-28 19:06:01 +05:30
|
|
|
assert-matches
|
2017-10-13 18:40:44 +05:30
|
|
|
assert-superblock-untouched
|
|
|
|
assert-member?
|
|
|
|
assert-raises-thunk
|
|
|
|
assert-raises
|
|
|
|
assert-every)
|
2017-08-15 20:37:45 +05:30
|
|
|
|
|
|
|
(import
|
|
|
|
(chezscheme)
|
2017-09-28 19:06:01 +05:30
|
|
|
(bcache block-manager)
|
2017-08-15 20:37:45 +05:30
|
|
|
(fmt fmt)
|
2017-08-17 15:53:43 +05:30
|
|
|
(list-utils)
|
2017-08-25 14:16:56 +05:30
|
|
|
(logging)
|
|
|
|
(process)
|
2017-09-15 19:52:04 +05:30
|
|
|
(regex)
|
2017-08-23 15:18:33 +05:30
|
|
|
(temp-file)
|
2017-08-21 14:40:15 +05:30
|
|
|
(utils)
|
2017-10-13 18:40:44 +05:30
|
|
|
(only (srfi s1 lists) every)
|
2017-08-25 14:16:56 +05:30
|
|
|
(srfi s8 receive))
|
2017-08-15 20:37:45 +05:30
|
|
|
|
|
|
|
;;;--------------------------------------------------------------------
|
|
|
|
|
|
|
|
(define (vector-sort-by cmp key-fn v)
|
|
|
|
(define (compare x y)
|
|
|
|
(cmp (key-fn x) (key-fn y)))
|
|
|
|
|
|
|
|
(vector-sort compare v))
|
|
|
|
|
|
|
|
;;;--------------------------------------------------------------------
|
|
|
|
|
|
|
|
(define-record-type scenario (fields desc thunk))
|
|
|
|
|
2017-08-17 15:53:43 +05:30
|
|
|
(define scenarios
|
|
|
|
(make-hashtable equal-hash equal?))
|
|
|
|
|
|
|
|
(define (add-scenario syms s)
|
|
|
|
(hashtable-set! scenarios syms s))
|
|
|
|
|
|
|
|
(define (fmt-keys prev-keys keys)
|
|
|
|
(define (fmt-keys% n keys)
|
|
|
|
(if (null? keys)
|
|
|
|
fmt-null
|
|
|
|
(cat (space-to n) (dsp (car keys)) (if (null? (cdr keys)) fmt-null nl)
|
|
|
|
(fmt-keys% (+ n 2) (cdr keys)))))
|
|
|
|
|
|
|
|
(let loop ((n 0)
|
|
|
|
(keys keys)
|
|
|
|
(prev-keys prev-keys))
|
|
|
|
(if (and (not (null? keys))
|
|
|
|
(not (null? prev-keys))
|
|
|
|
(eq? (car keys) (car prev-keys)))
|
|
|
|
(loop (+ n 2) (cdr keys) (cdr prev-keys))
|
|
|
|
(begin
|
|
|
|
(if (zero? n)
|
|
|
|
(cat nl (fmt-keys% n keys))
|
|
|
|
(fmt-keys% n keys))))))
|
2017-08-15 20:37:45 +05:30
|
|
|
|
|
|
|
(define (list-scenarios)
|
2017-08-17 15:53:43 +05:30
|
|
|
(define (fmt-keys ks)
|
|
|
|
(fmt #f (dsp ks)))
|
|
|
|
|
2017-08-15 20:37:45 +05:30
|
|
|
(vector->list
|
2017-08-17 15:53:43 +05:30
|
|
|
(vector-sort-by string<? fmt-keys (hashtable-keys scenarios))))
|
2017-08-15 20:37:45 +05:30
|
|
|
|
2017-08-17 15:53:43 +05:30
|
|
|
(define (fmt-scenarios keys fn)
|
2017-08-21 14:40:15 +05:30
|
|
|
(define (flush)
|
|
|
|
(flush-output-port (current-output-port)))
|
|
|
|
|
2017-08-17 15:53:43 +05:30
|
|
|
(define (describe prev-keys keys)
|
2017-08-15 20:37:45 +05:30
|
|
|
(fmt #t
|
2017-08-17 15:53:43 +05:30
|
|
|
(cat (fmt-keys prev-keys keys)
|
2017-08-21 14:40:15 +05:30
|
|
|
(dsp #\space)
|
2017-12-15 16:59:39 +05:30
|
|
|
(pad-char #\. (space-to 60))
|
2017-08-21 14:40:15 +05:30
|
|
|
(dsp #\space)))
|
|
|
|
(flush)
|
|
|
|
(fmt #t (cat (fn keys) nl))
|
|
|
|
(flush))
|
2017-08-17 15:53:43 +05:30
|
|
|
|
2017-10-05 19:35:22 +05:30
|
|
|
(unless (null? keys)
|
|
|
|
(for-each describe (cons '() (reverse (cdr (reverse keys)))) keys)))
|
2017-08-15 20:37:45 +05:30
|
|
|
|
2017-08-17 15:53:43 +05:30
|
|
|
(define (describe-scenarios keys)
|
|
|
|
(fmt-scenarios keys
|
|
|
|
(lambda (keys)
|
|
|
|
(scenario-desc
|
|
|
|
(hashtable-ref scenarios keys #f)))))
|
2017-08-15 20:37:45 +05:30
|
|
|
|
2017-08-24 18:33:07 +05:30
|
|
|
(define (test-dir cwd keys)
|
|
|
|
(apply string-append cwd "/"
|
|
|
|
(intersperse "/" (map symbol->string keys))))
|
|
|
|
|
2017-12-15 15:51:59 +05:30
|
|
|
(define (log-exceptions thunk)
|
|
|
|
(with-exception-handler
|
|
|
|
(lambda (x)
|
|
|
|
(let-values (((txt-port get) (open-string-output-port)))
|
|
|
|
(display-condition x txt-port)
|
|
|
|
(log-error (get)))
|
|
|
|
(raise x))
|
|
|
|
thunk))
|
|
|
|
|
2017-08-15 20:37:45 +05:30
|
|
|
(define-syntax define-scenario
|
2017-08-24 18:33:07 +05:30
|
|
|
(lambda (x)
|
|
|
|
(syntax-case x ()
|
2017-08-25 14:16:56 +05:30
|
|
|
((k keys desc b1 b2 ...)
|
|
|
|
#'(add-scenario 'keys
|
|
|
|
(make-scenario desc
|
|
|
|
(lambda ()
|
|
|
|
(with-dir (test-dir "." 'keys)
|
|
|
|
(with-log-port (open-file-output-port
|
|
|
|
(string-append (working-directory) "/log.txt")
|
|
|
|
(file-options no-fail)
|
|
|
|
(buffer-mode line)
|
|
|
|
(native-transcoder))
|
2017-12-15 15:51:59 +05:30
|
|
|
(log-exceptions (lambda () b1 b2 ...)))))))))))
|
2017-08-15 20:37:45 +05:30
|
|
|
|
|
|
|
(define (fail msg)
|
|
|
|
(raise (condition
|
|
|
|
(make-error)
|
|
|
|
(make-message-condition msg))))
|
|
|
|
|
2017-08-17 15:53:43 +05:30
|
|
|
(define (run-scenario syms)
|
|
|
|
(let ((s (hashtable-ref scenarios syms #f)))
|
|
|
|
(display syms)
|
2017-08-15 20:37:45 +05:30
|
|
|
(display " ... ")
|
|
|
|
((scenario-thunk s))
|
|
|
|
(display "pass")
|
|
|
|
(newline)))
|
|
|
|
|
2017-08-21 14:40:15 +05:30
|
|
|
;; Returns #f if an error was raised, otherwise #t (the values from thunk are
|
|
|
|
;; discarded).
|
|
|
|
(define (try thunk)
|
|
|
|
(call/cc
|
|
|
|
(lambda (k)
|
|
|
|
(with-exception-handler
|
|
|
|
(lambda (x)
|
|
|
|
(if (error? x)
|
|
|
|
(k #f)
|
|
|
|
(raise x)))
|
2017-12-15 15:51:59 +05:30
|
|
|
(lambda ()
|
|
|
|
(thunk)
|
|
|
|
#t)))))
|
2017-08-21 14:40:15 +05:30
|
|
|
|
2017-08-21 14:48:10 +05:30
|
|
|
;; Returns #t if all tests pass.
|
2017-08-15 20:37:45 +05:30
|
|
|
(define (run-scenarios ss)
|
2017-08-21 14:40:15 +05:30
|
|
|
(let ((pass 0)
|
|
|
|
(fail 0)
|
|
|
|
(fail-keys '()))
|
|
|
|
|
|
|
|
(fmt-scenarios ss
|
|
|
|
(lambda (keys)
|
|
|
|
(let ((s (hashtable-ref scenarios keys #f)))
|
|
|
|
(if (try (scenario-thunk s))
|
|
|
|
(begin (inc! pass)
|
|
|
|
(dsp "pass"))
|
|
|
|
(begin (inc! fail)
|
|
|
|
(set! fail-keys (cons keys fail-keys))
|
2017-08-22 14:17:00 +05:30
|
|
|
(dsp "fail"))))))
|
2017-09-15 17:18:34 +05:30
|
|
|
(unless (or (zero? fail) (zero? pass))
|
2017-08-23 14:38:28 +05:30
|
|
|
(fmt #t nl (dsp "There were failures:") nl)
|
|
|
|
(fmt-scenarios fail-keys
|
|
|
|
(lambda (_)
|
|
|
|
(dsp "fail"))))
|
2017-08-21 14:40:15 +05:30
|
|
|
(fmt #t (cat nl
|
|
|
|
(num pass)
|
|
|
|
(dsp "/")
|
|
|
|
(num (+ pass fail))
|
|
|
|
(dsp " tests passed")
|
|
|
|
(if (zero? fail)
|
|
|
|
(dsp #\.)
|
|
|
|
(cat (dsp ", ")
|
|
|
|
(num fail)
|
|
|
|
(dsp " failures.")))
|
2017-08-21 14:48:10 +05:30
|
|
|
nl))
|
|
|
|
(zero? fail)))
|
2017-08-17 20:54:10 +05:30
|
|
|
|
|
|
|
;;-----------------------------------------------
|
|
|
|
|
2017-10-10 16:14:05 +05:30
|
|
|
(define tools-version
|
|
|
|
(chomp
|
|
|
|
(with-input-from-file "../VERSION"
|
|
|
|
(lambda ()
|
|
|
|
(get-line (current-input-port))))))
|
2017-08-17 20:54:10 +05:30
|
|
|
|
2017-10-10 21:21:31 +05:30
|
|
|
;;-----------------------------------------------
|
|
|
|
;; A 'tool' is a function that builds up a command line. This can then be
|
|
|
|
;; passed to the functions in the (process) library.
|
|
|
|
(define (tool-path sym)
|
2017-08-17 20:54:10 +05:30
|
|
|
(define (to-underscore c)
|
|
|
|
(if (eq? #\- c) #\_ c))
|
|
|
|
|
2017-10-10 16:07:32 +05:30
|
|
|
(string-append "../bin/"
|
|
|
|
(list->string
|
|
|
|
(map to-underscore
|
|
|
|
(string->list
|
|
|
|
(symbol->string sym))))))
|
2017-08-17 20:54:10 +05:30
|
|
|
|
|
|
|
(define-syntax define-tool
|
|
|
|
(syntax-rules ()
|
2017-10-10 21:21:31 +05:30
|
|
|
((_ sym) (define sym
|
|
|
|
(let ((path (tool-path 'sym)))
|
|
|
|
(lambda args
|
|
|
|
(build-command-line (cons path args))))))))
|
2017-08-17 20:54:10 +05:30
|
|
|
|
|
|
|
(define (assert-equal str1 str2)
|
|
|
|
(unless (equal? str1 str2)
|
|
|
|
(fail (fmt #f (dsp "values differ: ")
|
|
|
|
(wrt str1)
|
|
|
|
(dsp ", ")
|
|
|
|
(wrt str2)))))
|
|
|
|
|
|
|
|
(define (assert-eof obj)
|
|
|
|
(unless (eof-object? obj)
|
|
|
|
(fail (fmt #f (dsp "object is not an #!eof: ") (dsp obj)))))
|
|
|
|
|
|
|
|
(define (starts-with prefix str)
|
|
|
|
(and (>= (string-length str) (string-length prefix))
|
|
|
|
(equal? (substring str 0 (string-length prefix))
|
|
|
|
prefix)))
|
|
|
|
|
|
|
|
(define (assert-starts-with prefix str)
|
|
|
|
(unless (starts-with prefix str)
|
|
|
|
(fail (fmt #f (dsp "string should begin with: ")
|
|
|
|
(wrt prefix)
|
|
|
|
(dsp ", ")
|
|
|
|
(wrt str)))))
|
|
|
|
|
2017-09-15 19:52:04 +05:30
|
|
|
(define (assert-matches pattern str)
|
|
|
|
(unless ((regex pattern) str)
|
|
|
|
(fail (fmt #f "string should match: " pattern ", " str))))
|
2017-08-17 20:54:10 +05:30
|
|
|
|
2017-09-28 19:06:01 +05:30
|
|
|
(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")))))
|
|
|
|
|
2017-10-13 18:40:44 +05:30
|
|
|
(define (assert-member? x xs)
|
|
|
|
(unless (member x xs)
|
|
|
|
(fail (fmt #f "expected " (wrt x) "to be a member of " (wrt xs)))))
|
|
|
|
|
|
|
|
(define (assert-raises-thunk thunk)
|
|
|
|
(call/cc
|
|
|
|
(lambda (k)
|
|
|
|
(with-exception-handler
|
|
|
|
(lambda (x)
|
|
|
|
(if (error? x)
|
|
|
|
(k #f)
|
|
|
|
(raise x)))
|
|
|
|
thunk)
|
|
|
|
(fail "expected an exception to be raised"))))
|
|
|
|
|
|
|
|
(define-syntax assert-raises
|
|
|
|
(syntax-rules ()
|
|
|
|
((_ b1 b2 ...)
|
|
|
|
(assert-raises-thunk
|
|
|
|
(lambda ()
|
|
|
|
b1 b2 ...)))))
|
|
|
|
|
|
|
|
(define (assert-every pred . args)
|
|
|
|
(unless (apply every pred args)
|
|
|
|
(fail "assert-every failed")))
|
2017-08-17 20:54:10 +05:30
|
|
|
)
|
2017-08-15 20:37:45 +05:30
|
|
|
|