;;======================================================================
;; Copyright 2006-2012, Matthew Welland.
;;
;; This file is part of Megatest.
;;
;; Megatest is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; Megatest is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with Megatest. If not, see <http://www.gnu.org/licenses/>.
;;======================================================================
(declare (unit genexample))
(declare (uses mtargs))
(declare (uses debugprint))
;; (declare (uses common))
(declare (uses commonmod))
(declare (uses configfmod))
(declare (uses rmtmod))
(declare (uses testsmod))
(declare (uses dbfile))
(declare (uses tasksmod))
(use posix regex matchable)
(module genexample
*
(import scheme
chicken
data-structures
extras
srfi-1
srfi-13
srfi-69
posix
regex
matchable
(prefix mtargs args:)
commonmod
configfmod
testsmod
rmtmod
debugprint
tasksmod
dbfile)
;; (include "db_records.scm")
(define genexample:example-logpro
#<<EOF
;; You should have at least one expect:required. This ensures that your process ran
;; comment out the line below and replace "put pattern here" with a pattern that will
;; always be seen in your log file if the step runs successfully.
;;
;; (expect:required in "LogFileBody" > 0 "Put description here" #/put pattern here/)
;;
;; You may need ignores to suppress false error or warning hits from the later expects
;; NOTE: Order is important here!
(expect:ignore in "LogFileBody" < 99 "Ignore the word error in comments" #/^\/\/.*error/)
(expect:warning in "LogFileBody" = 0 "Any warning" #/warn/)
(expect:error in "LogFileBody" = 0 "Any error" (list #/ERROR/ #/error/)) ;; but disallow any other errors
EOF
)
(define genexample:example-script
#<<EOF
#!/usr/bin/env bash
# Run your step here
EOF
)
(define (genexample:mk-megatest.config)
(let ((keystr #f)
(keys #f)
(lntree #f)
(path #f)
(firstd #f))
(print "Note: don't worry too much about typos in this process, you will be able to edit
the generated files before starting your first runs")
;; create the needed area
(print "==================\nWhere can I create your Megatest regresssion/continuous build area? Note, your \n"
"tests will not necessarily be run in this area, disk space needs are modest. Current directory is:\n\n"
(current-directory) "\n")
(display "Enter your megatest directory: ")
(set! path (read-line))
(if (not (directory? path))
(begin
(print "The path " path " does not exist or is not a directory. Attempting to create it now")
(create-directory path #t)))
;; First check that the directory is empty!
(if (and (common:file-exists? path)
(not (null? (glob (conc path "/*")))))
(begin
(print "WARNING: directory " path " is not empty, are you sure you want to continue?")
(display "Enter y/n: ")
(if (equal? "y" (read-line))
(print "Using directory " path " for your Megatest area.")
(begin
(print "INFO: Creation of megatest files in " path " aborted")
(exit 1)))))
;; first prompt user for fields
;;
(print
"==================
Next you must specify fields or keys for your megatest area. These
will be used to organise your runs. One field should probably be
\"RELEASE\". Other examples of useful fields might be \"PLATFORM\",
\"TARGET_OS\" or if you are in the semiconductor business perhaps
things like \"TECHNOLOGY_NODE\", \"DESIGN_KIT\" or \"METAL_STACK\".
The all caps is a convention because the variables you choose will be
available to your tests as environment variables. You can edit these
values later but it is generally a good idea to settle on them
early.
Your runs will be stored in directories specified by your
keys. Example, if you have keys OSFAMILY/VARIANT/OSVER/RELEASE you may
get a test \"build\" in a directory like this:
linux/ubuntu/11.04/rev_1.2/build
Please enter your keys now, separated by spaces or slashes. Only alpha-numeric characters,
upper case recommended. Example: COMPILER_VER/RELEASE_NAME/QUAL_LEVEL
")
(set! keys (let loop ((keystr ""))
(if (equal? keystr "q")
(begin
(print "Quiting ...")
(exit))
(let ((keylst (apply append
(map string-split (string-split keystr "/")))))
(if (or (null? keylst)
(not (null? (filter string-null? keylst))))
(begin
(display "Enter keys separated by spaces or slashes: ")
(loop (read-line)))
keylst)))))
(print "You have choosen " (string-intersperse keys ", ") " for your keys.")
;; Now get the link tree location and a first disk
(print
"==================
Now you need an initial place to store your runs. These are called \"disks\" and you
can add more at any time. To get going provide a writeable directory name.
")
(display "Enter your test runs directory: ")
(set! firstd (read-line))
(if (not (directory? firstd))
(begin
(print "WARNING: you have specified a path " firstd " that does not exist. Attempting to create it...\n")
(create-directory firstd #t)))
(print
"==================
Megatest uses a tree of symlinks to provide a uniform structure for finding all the tests
you run over time. Please provide a path where we can create this link tree.
")
(display "Enter link tree directory: ")
(set! lntree (read-line))
(if (not (directory? lntree))
(begin
(print "WARNING: you have specified a path " lntree "that does not exist. Attempting to create it...\n")
(create-directory lntree #t)))
(with-output-to-file (conc path "/megatest.config")
(lambda ()
(print "# This area uses Megatest. Learn more at http://www.kiatoa.com/fossils/megatest.")
(print "#\n")
(print "[fields]")
(map (lambda (k)(print k " TEXT")) keys)
(print "")
(print "[setup]")
(print "# Adjust max_concurrent_jobs to limit how much you load your machines")
(print "max_concurrent_jobs 50\n")
(print "# This is your link path. Avoid moving it once set.")
(print "linktree " lntree) ;; (common:real-path lntree))
(print "\n# Job tools are more advanced ways to control how your jobs are launched")
(print "[jobtools]\nuseshell yes\nlauncher nbfake\nmaxload 1.5\n")
(print "# You can override environment variables for all your tests here")
(print "[env-override]\nEXAMPLE_VAR example value\n")
(print "# As you run more tests you may need to add additional disks, the names are arbitrary but must be unique")
(print "[disks]\ndisk0 " firstd))) ;; (common:real-path firstd))))
(print
"==================
I'm now creating a runconfigs.config file for you with a default section.
You can use this file to set variables for your tests based on the \"target\" (the combination
of keys).
")
(with-output-to-file (conc path "/runconfigs.config")
(lambda ()
(print "# The variables in the default category will be seen in all runs\n[default]\nALLTESTS see this variable\n")
(print "# Your variables here are grouped by targets [" (string-intersperse keys "/") "]")
(let ((example-target (string-intersperse (map (lambda (k)(conc k "_val")) keys) "/")))
(print "[" example-target "]")
(print "ANOTHERVAR only defined if target is " example-target))
(print "\n# It can be handy to include a file based on the users unix username.\n"
"# To prevent cluttering up the top level directory we'll put this file\n# in a directory called \"configs\".")
(print "[include #{getenv USER}.config]")
))
(create-directory (conc path "/configs") #t)
(with-output-to-file (conc path "/configs/" (current-user-name) ".config")
(lambda ()
(print "# Override settings in ../runconfigs.config for user " (current-user-name) " here.")))
;; Now create a test and logpro file
(print
"==================
You now have the basic common files for your megatest setup. Next run
\"megatest -create-test <testname>\" to create a test.
Thank you for using Megatest.
You can edit your config files and create tests in the " path " directory
")))
;;======================================================================
;; create skeleton files for a test
;;======================================================================
(define (genexample:mk-megatest-test testname)
;; Gather needed data
(let ((waiton #f)
(priority #f)
(description #f)
(steps '())
(scripts '())
(items '())
(rel-path #f))
(cond
((common:file-exists? "megatest.config") (set! rel-path "./"))
((common:file-exists? "../megatest.config") (set! rel-path "../"))
((common:file-exists? "../../megatest.config") (set! rel-path "../../"))
((common:file-exists? "../../../megatest.config")(set! rel-path "../../../"))) ;; good enough dang it.
;; Don't gather data or continue if a) megatest.config can't be found or b) testconfig already exists
(if (not rel-path)
(begin
(print "ERROR: I could not find megatest.config, please run -create-test in the top dir of your megatest area")
(exit 1)))
(if (common:file-exists? (conc rel-path "tests/" testname "/testconfig"))
(begin
(print "WARNING: You already have a testconfig in " rel-path "tests/" testname ", do you want to clobber your files?")
(display "Enter y/n: ")
(if (not (equal? "y" (read-line)))
(begin
(print "INFO: user abort of creation of test " testname)
(exit 1)))))
(print "We are going to generate a skeleton set of files for your test " testname "\n"
" *** Note: do not worry too much about typos, you can edit the files created when you are done.")
(print "\n==================\nPlease describe this test. The description will be visible in various dialogs and reports")
(display "Enter one line description for this test: ")
(set! description (read-line))
(print "\n\n==================\nDoes this test, " testname ", require any other test be run prior to launch?")
(display (conc "Enter space delimited list of tests which " testname " must wait for (default is no waiton): "))
(set! waiton (read-line))
(print "\n\n==================\nDo you wish to prioritize the running of this test over other tests? If so")
(print "enter a number greater than zero here")
(display "Enter a priority of 0 (default) or higher: ")
(set! priority (read-line))
;; Get the steps
(print "\n==================\nNow to enter the one or more steps that make up your test, note; you can add more later")
(print "Hint; use .sh extension on the script names and we'll create a placeholder script."
(let ((stepname #f)
(scriptname #f))
(let loop ((done #f))
(display "Enter the name for this step (blank to stop): ")
(set! stepname (read-line))
(if (not (equal? stepname ""))
(begin
(display "Enter the script or progam to run: ")
(set! scriptname (read-line))
(set! steps (append steps (list (list stepname scriptname))))))
(if (not (equal? stepname ""))
(begin
(print "Added step " stepname " to list of steps.\n")
(loop #f)))))
;; Get the items
(print "\n\n==================\nNext we need to get the variables and values you wish to iterate this test over (blank for none)")
(let ((varname #f)
(values #f))
(let loop ((done #f))
(display "Enter the variable name: ")
(set! varname (read-line))
(if (not (equal? varname ""))
(begin
(display (conc "Enter the space separated list of values for " varname ": "))
(set! values (read-line))
(set! items (append items (list (conc varname " " values))))))
(if (not (equal? varname ""))
(loop #f))))
;; Now create the test
(if (not rel-path)
(begin
(print "ERROR: You must run this command in a megatest area under where the megatest.config file exists.")
(exit 1))
(let ((testdir (conc rel-path "tests/" testname)))
(create-directory testdir #t)
(with-output-to-file (conc testdir "/testconfig")
(lambda ()
(print "# Add additional steps here. Format is \"stepname script\"\n[ezsteps]")
(map (lambda (stp)(print (string-intersperse stp " "))) steps)
(print "")
(print "# Test requirements are specified here\n[requirements]")
(print (if (string-null? waiton) "# " "") "waiton " waiton)
(print "priority " (if (string-null? priority) 0 priority) "\n")
(print "# Iteration for your test is controlled by the items section\n[items]")
(map print items)
(print "")
(print "# Alternatively you could use a [itemstable] section")
(print "# [itemstable]")
(print "# ITEMVAR1 a b c")
(print "# ITEMVAR2 d e f")
(print "#\n# would result in items: a/d b/e c/f\n#\n")
(print "# Logpro rules for each step can be captured here in the testconfig")
(print "# note: The ;; after the stepname and the leading whitespace are required")
(print "#\n[logpro]\n")
(for-each (lambda (step)
(let ((stepname (car step))
(scriptname (cadr step)))
(print stepname " ;; rules for checking output from running step " step " with command " scriptname)
(print genexample:example-logpro "\n")))
steps)
(print "# test_meta is a section for storing additional data on your test\n[test_meta]")
(print "author " (get-environment-variable "USER"))
(print "owner " (get-environment-variable "USER"))
(print "description " description)
(print "tags tagone,tagtwo")
(print "reviewed never")))
;; Now create shell scripts (if extension is .sh) and logpro files
(for-each (lambda (stp)
(let ((stepname (car stp))
(script (cadr stp)))
(if (string-match ".*\\.sh$" script)
(begin
(with-output-to-file (conc testdir "/" script)
(lambda ()
(print genexample:example-script)))
(system (conc "chmod ug+r,a+x " (conc testdir "/" script)))))))
steps))))))
;; easier to work backwards than change the upstream code
;;
(define (hrs-min-sec->seconds str)
(let* ((parts (string-split str))
(res 0))
(for-each
(lambda (part)
(set! res
(+ res
(match (string-match "(\\d+)([a-z])" part)
((_ val units)(* (string->number val)(case (string->symbol units)
((s) 1)
((m) 60)
((h) 3600))))
(else 0)))))
parts)
res))
;; generate a skeleton Megatest area from a current area with runs
;;
;; specify target, runname etc to use specific runs for the template
;;
(define (genexample:extract-skeleton-area dest-path)
(let* ((target (args:get-arg "-target"))
(runname (args:get-arg "-runname"))
(obtuse (make-hash-table))
(obtusef (args:get-arg "-obfuscate"))
(letters (string-split-fields "\\S" "abcdefghijklmnopqrstuvwxyz"))
(maxletter (- (length letters) 1))
(lastlet 0)
(lastnum 1)
(obfuscate (lambda (instr)
(or (hash-table-ref/default obtuse instr #f)
(if obtusef
(let* ((letter (list-ref letters lastlet))
(val (conc letter lastnum)))
(if (>= lastlet maxletter)
(begin
(set! lastlet 0)
(set! lastnum (+ lastnum 1)))
(set! lastlet (+ lastlet 1)))
(hash-table-set! obtuse instr val)
val)
instr)))))
(if (not (and target runname))
(debug:print 0 *default-log-port* "WARNING: For best results please specifiy -target and -runname for a good run to use as a template."))
(if (not (and (file-exists? "megatest.config")
(file-exists? "megatest.db")))
(begin
(debug:print 0 *default-log-port* "ERROR: this command must be run at the top level of a megatest area where runs have been completed")
(exit)))
;; first create the dest path and needed subdirectories
(if (not (file-exists? dest-path))
(begin
(create-directory dest-path)
(create-directory (conc dest-path "/tests")))
(if (file-exists? (conc dest-path "/megatest.config"))
(begin
(debug:print 0 *default-log-port* "ERROR: destination path already has megatest.config, stopping now.")
(exit))))
;; dump the config files from this area to the dest area
(if (args:get-arg "-obfuscate")
(debug:print 0 *default-log-port* "WARNING: obfuscation is NOT done on megatest.config and runconfigs.config. Please edit those files to remove any sensitive information!"))
(system (conc "megatest -show-config > " dest-path "/megatest.config"))
(system (conc "megatest -show-runconfig > " dest-path "/runconfigs.config"))
;; create stepsinfo and items refdbs, some stuff has to be done due to refdb not initing area
;;
;; sheet row col value
;; stepsinfo testname itempath stepname steptime
;; miscinfo "itemsinfo" testname itempath "x"
;;
(for-each
(lambda (rdbname)
(if (not (file-exists? (conc dest-path "/" rdbname)))
(begin
(create-directory (conc dest-path "/" rdbname "/sxml") #t)
(with-output-to-file (conc dest-path "/" rdbname "/sheet-names.cfg")
(lambda ()(print))))))
'("stepsinfo" "miscinfo"))
(let* ((runs (rmt:simple-get-runs (or runname "%") #f #f (or target "%") #f))
(tests (make-hash-table)) ;; just tests
(fullt (make-hash-table)) ;; all test/items
(testreg (make-hash-table)) ;; for the testconfigs
(stepsrdb (conc dest-path "/stepsinfo"))
(miscrdb (conc dest-path "/miscinfo")))
(if (> (length runs) 1)
(debug:print-info 0 *default-log-port* "More than one run matches, first found data will be used."))
;; get all testnames
(for-each
(lambda (run-id)
(let* ((tests-data (rmt:get-tests-for-run run-id "%" '() '() #f #f #f #f #f #f #f #f)))
(for-each
(lambda (testdat)
(let* ((test-id (db:test-get-id testdat))
(testname (db:test-get-testname testdat))
(item-path (db:test-get-item-path testdat))
(tlevel (db:test-get-is-toplevel testdat))
(tfullname (db:test-get-fullname testdat))
;; now get steps info
(test-steps (tests:get-compressed-steps run-id test-id))
(testconfig (tests:get-testconfig testname item-path testreg #f)))
(if (not (hash-table-exists? fullt tfullname))
;; do the work for this test if not previously done
(let* ((new-test-dir (conc dest-path "/tests/" (obfuscate testname)))
(tconfigf (conc new-test-dir "/testconfig")))
(print "Analyzing and extracting info for " tfullname " as " (obfuscate testname))
(print " toplevel: " (if tlevel "yes" "no"))
(hash-table-set! fullt tfullname #t) ;; track that this one has been seen
(if (not (directory-exists? new-test-dir))
(create-directory new-test-dir #t))
;; create the testconfig IIF we are a toplevel or an item AND the testconfig has not been previously created
(if (and (or (not tlevel)
(not (equal? item-path "")))
(not (file-exists? tconfigf)))
(with-output-to-file tconfigf
(lambda ()
;; first the ezsteps
(print "[ezsteps]")
(for-each
(lambda (teststep)
(let* ((step-name (vector-ref teststep 0)))
(print (obfuscate step-name)
" sleep $(refdb lookup #{getenv MT_RUN_AREA_HOME}/stepsinfo "
(obfuscate testname) " $MT_ITEMPATH "
(obfuscate step-name) ")")))
test-steps)
;; now the requirements section
(if testconfig
(begin
(print "\n[requirements]")
(for-each
(lambda (entry)
(let* ((key (car entry))
(val (cadr entry)))
(case (string->symbol key)
((waiton) (print "waiton " (obfuscate val)))
(else (print key " " val)))))
(configf:get-section testconfig "requirements")))
#;(print "WARNING: No testconfig data for " testname ", " item-path))
(print "\n[items]")
(print "THE_ITEM [system refdb getrow #{getenv MT_RUN_AREA_HOME}/miscinfo itemsinfo " (obfuscate testname)" | awk '{print $1}']")
)))
;; fill the stepsrdb
(for-each
(lambda (teststep)
(let* ((step-name (vector-ref teststep 0))
(step-duration (hrs-min-sec->seconds (vector-ref teststep 4))))
(system (conc "refdb set " stepsrdb " " (obfuscate testname)
" '" (if (equal? item-path "")
"no-item-path"
(obfuscate item-path))
"' " (obfuscate step-name) " " step-duration))))
test-steps)
;; miscinfo "itemsinfo" testname itempath "x"
(if (not (equal? item-path ""))
(system (conc "refdb set " miscrdb " itemsinfo " (obfuscate testname) " " (obfuscate item-path) " x")))
))))
tests-data)))
(map (lambda (runrec)(simple-run-id runrec)) runs)))
))
)