Test running of jobs.
authorRichard W.M. Jones <rjones@redhat.com>
Tue, 21 Feb 2012 10:45:06 +0000 (10:45 +0000)
committerRichard W.M. Jones <rjones@redhat.com>
Tue, 21 Feb 2012 13:51:30 +0000 (13:51 +0000)
.gitignore
Makefile.am
configure.ac
tests/jobs/Makefile.am [new file with mode: 0644]
tests/jobs/t100_counter.ml [new file with mode: 0644]
tests/jobs/t100_counter.ml.expected [new file with mode: 0644]
tests/jobs/t101_updown.ml [new file with mode: 0644]
tests/jobs/t101_updown.ml.expected [new file with mode: 0644]
tests/jobs/test_run.sh [new file with mode: 0755]

index 6c9f2b6..3510f62 100644 (file)
@@ -35,6 +35,7 @@ Makefile
 /ltmain.sh
 /missing
 /stamp-h1
+/tests/jobs/test_output
 /tests/parsing/test_load
 /tools/libdir.ml
 /tools/whenjobs
index d0a3cfb..43933f2 100644 (file)
@@ -19,6 +19,11 @@ ACLOCAL_AMFLAGS = -I m4
 
 EXTRA_DIST = COPYING README whenjobs.spec whenjobs.spec.in
 
-SUBDIRS = lib daemon tools tests/parsing
+SUBDIRS = \
+       lib \
+       daemon \
+       tools \
+       tests/parsing \
+       tests/jobs
 
 CLEANFILES = *~
index 40483be..4bd2e41 100644 (file)
@@ -106,6 +106,7 @@ AC_CONFIG_FILES([Makefile
                  daemon/Makefile
                  lib/config.ml
                  lib/Makefile
+                 tests/jobs/Makefile
                  tests/parsing/Makefile
                  tools/Makefile
                  whenjobs.spec])
diff --git a/tests/jobs/Makefile.am b/tests/jobs/Makefile.am
new file mode 100644 (file)
index 0000000..f6d0580
--- /dev/null
@@ -0,0 +1,61 @@
+# whenjobs
+# Copyright (C) 2012 Red Hat Inc.
+#
+# This program 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 2 of the License, or
+# (at your option) any later version.
+#
+# This program 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 this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+EXTRA_DIST = test_run.sh $(TESTS)
+
+TESTS_ENVIRONMENT = ./test_run.sh
+TESTS = t100_counter.ml t101_updown.ml
+
+OCAMLPACKAGES = -package unix,num,camlp4.lib,calendar,rpc -I ../../lib
+
+OCAMLCFLAGS = -g -warn-error CDEFLMPSUVYZX $(OCAMLPACKAGES)
+OCAMLOPTFLAGS = $(OCAMLCFLAGS)
+
+PP = -pp '$(CAMLP4O) ../../lib/pa_when.cmo'
+
+# Rules for all OCaml files.
+%.cmi: %.mli ../../lib/pa_when.cmo
+       $(OCAMLFIND) ocamlc $(OCAMLCFLAGS) $(PP) -c $< -o $@
+%.cmo: %.ml ../../lib/pa_when.cmo
+       $(OCAMLFIND) ocamlc $(OCAMLCFLAGS) $(PP) -c $< -o $@
+#%.cmx: %.ml ../../lib/pa_when.cmo
+#      $(OCAMLFIND) ocamlopt $(OCAMLOPTFLAGS) $(PP) -c $< -o $@
+
+SUFFIXES = .cmo .cmi .cmx .ml .mli .mll .mly
+
+# OCaml dependencies.
+depend: .depend
+
+.depend: $(SOURCES)
+       rm -f $@ $@-t
+       $(OCAMLFIND) ocamldep $(PP) $^ | \
+         $(SED) -e 's/ *$$//' | \
+         $(SED) -e :a -e '/ *\\$$/N; s/ *\\\n */ /; ta' | \
+         LANG=C sort > $@-t
+       mv $@-t $@
+
+-include .depend
+
+# Useful for debugging: print the generated AST for the tests.
+print:
+       @for f in $(check_SCRIPTS); do \
+         b=`basename $$f .cmo`; \
+         echo "Test: $$b.ml"; \
+         camlp4o ../../lib/pa_when.cmo -printer pr_o.cmo $$b.ml; \
+       done
+
+CLEANFILES = *.cmi *.cmo *.cmx *.cma *.cmxa *~
diff --git a/tests/jobs/t100_counter.ml b/tests/jobs/t100_counter.ml
new file mode 100644 (file)
index 0000000..d25f6c7
--- /dev/null
@@ -0,0 +1,31 @@
+(* whenjobs
+ * Copyright (C) 2012 Red Hat Inc.
+ *
+ * This program 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 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+(* Test count to 3, then fire a whenjob. *)
+
+every 2 seconds :
+<<
+  echo $JOBNAME $counter >\> $HOME/test_output
+  whenjobs --set counter $(($counter+1)) --type int
+>>
+
+when counter = 3 :
+<<
+  echo $JOBNAME $counter >\> $HOME/test_output
+  whenjobs --daemon-stop
+>>
diff --git a/tests/jobs/t100_counter.ml.expected b/tests/jobs/t100_counter.ml.expected
new file mode 100644 (file)
index 0000000..48c981e
--- /dev/null
@@ -0,0 +1,4 @@
+job$1
+job$1 1
+job$1 2
+job$2 3
diff --git a/tests/jobs/t101_updown.ml b/tests/jobs/t101_updown.ml
new file mode 100644 (file)
index 0000000..75695ce
--- /dev/null
@@ -0,0 +1,43 @@
+(* whenjobs
+ * Copyright (C) 2012 Red Hat Inc.
+ *
+ * This program 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 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+(* Reproduce a bug found during development where the everyjob would
+ * start running twice per period.
+ *)
+
+every second :
+<<
+  echo $JOBNAME >\> $HOME/test_output
+  whenjobs --set counter $(($counter+1)) --type int
+>>
+
+when counter < 5 && counter mod 2 == 0 :
+<<
+  echo $JOBNAME >\> $HOME/test_output
+>>
+
+when counter < 5 && counter mod 2 == 1 :
+<<
+  echo $JOBNAME >\> $HOME/test_output
+>>
+
+when counter == 5 :
+<<
+  echo $JOBNAME >\> $HOME/test_output
+  whenjobs --daemon-stop
+>>
diff --git a/tests/jobs/t101_updown.ml.expected b/tests/jobs/t101_updown.ml.expected
new file mode 100644 (file)
index 0000000..ce4c74e
--- /dev/null
@@ -0,0 +1,10 @@
+job$1
+job$3
+job$1
+job$2
+job$1
+job$3
+job$1
+job$2
+job$1
+job$4
diff --git a/tests/jobs/test_run.sh b/tests/jobs/test_run.sh
new file mode 100755 (executable)
index 0000000..13cd844
--- /dev/null
@@ -0,0 +1,73 @@
+#!/bin/bash -
+# Copyright (C) 2012 Red Hat Inc.
+#
+# This program 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 2 of the License, or
+# (at your option) any later version.
+#
+# This program 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 this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+# Run the test script using the real daemon and tools.
+
+unset CDPATH
+set -e
+
+testdir=$(pwd)
+libdir=$(cd ../../lib; pwd)
+toolsdir=$(cd ../../tools; pwd)
+daemondir=$(cd ../../daemon; pwd)
+
+HOME="$testdir"
+export HOME
+
+rm -rf "$testdir/.whenjobs"
+mkdir "$testdir/.whenjobs"
+rm -f "$testdir/test_output"
+
+PATH="$toolsdir:$daemondir:$PATH"
+export PATH
+
+# Kill the daemon and clean up when the script exits.
+function cleanup {
+    kill `cat "$testdir/.whenjobs/daemon_pid"`
+    rm -rf "$testdir/.whenjobs"
+}
+trap cleanup INT TERM QUIT EXIT
+
+# Run the daemon.
+whenjobsd -d
+
+# Copy the job into the directory.
+cp "$1" "$testdir/.whenjobs/jobs.ml"
+
+# Upload it to the daemon.
+whenjobs --upload --lib "$libdir"
+
+# Now wait until the test completes.  Note we cannot use 'wait'
+# because the daemon is not a direct child of the script.
+while [ -e /proc/`cat "$testdir/.whenjobs/daemon_pid"` ]; do
+    sleep 1
+done
+
+trap - INT TERM QUIT EXIT
+rm -rf "$testdir/.whenjobs"
+
+# Check the test output matches the expected output.
+if ! cmp -s "$testdir/test_output" "$1.expected"; then
+    echo "$0: $1: test output did not match expected output"
+    echo "test output was:"
+    echo "-----"
+    cat "$testdir/test_output"
+    echo "-----"
+    exit 1
+fi
+
+rm -f "$testdir/test_output"