Awight has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/343120 )

Change subject: Config validation
......................................................................

Config validation

Change-Id: Id9783d59eb69c82a1552b6ef330dbed830cefe86
---
M README.md
M job_wrapper.py
A tests/data/missing_fields.yaml
A tests/data/schedule_assign.yaml
A tests/data/schedule_atsign.yaml
A tests/data/schedule_good.yaml
A tests/data/schedule_invalid.yaml
A tests/test_validation.py
8 files changed, 71 insertions(+), 0 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/process-control 
refs/changes/20/343120/1

diff --git a/README.md b/README.md
index 54583c8..8bce9c0 100644
--- a/README.md
+++ b/README.md
@@ -21,3 +21,7 @@
 # Optional filename for the job output.  All output will be concatenated into 
this file, with a header for each job.
 stdout_destination: "/tmp/jobnuts.log"
 ```
+
+TODO:
+* Syslog actions, at least when tweezing new crontabs.
+* Log invocations.
diff --git a/job_wrapper.py b/job_wrapper.py
index eb5d5cf..9ae8512 100644
--- a/job_wrapper.py
+++ b/job_wrapper.py
@@ -8,12 +8,15 @@
 
 import lock
 
+# TODO: Global config.
 DEFAULT_TIMEOUT = 600
 
 
 class JobWrapper(object):
     def __init__(self, config_path=None):
         self.config = yaml.safe_load(open(config_path, "r"))
+        self.validate_config()
+
         self.name = self.config["name"]
         self.start_time = datetime.datetime.utcnow().isoformat()
 
@@ -75,3 +78,16 @@
         print(header, file=out)
 
         out.write(stdout_data.decode("utf-8"))
+
+    def validate_config(self):
+        assert "name" in self.config
+        assert "command" in self.config
+        if "schedule" in self.config:
+            # No tricky assignments.
+            assert "=" not in self.config["schedule"]
+            # Legal cron, but I don't want to deal with it.
+            assert "@" not in self.config["schedule"]
+
+            # Be sure the schedule is valid.
+            terms = self.config["schedule"].split()
+            assert len(terms) == 5
diff --git a/tests/data/missing_fields.yaml b/tests/data/missing_fields.yaml
new file mode 100644
index 0000000..f52d584
--- /dev/null
+++ b/tests/data/missing_fields.yaml
@@ -0,0 +1 @@
+command: /bin/true
diff --git a/tests/data/schedule_assign.yaml b/tests/data/schedule_assign.yaml
new file mode 100644
index 0000000..4a881a5
--- /dev/null
+++ b/tests/data/schedule_assign.yaml
@@ -0,0 +1,3 @@
+name: Bad assignment job
+command: /bin/true
+schedule: USER=root a b c d
diff --git a/tests/data/schedule_atsign.yaml b/tests/data/schedule_atsign.yaml
new file mode 100644
index 0000000..c55c51c
--- /dev/null
+++ b/tests/data/schedule_atsign.yaml
@@ -0,0 +1,3 @@
+name: Bad schedule job
+command: /bin/true
+schedule: "@daily root a b c"
diff --git a/tests/data/schedule_good.yaml b/tests/data/schedule_good.yaml
new file mode 100644
index 0000000..5f4476e
--- /dev/null
+++ b/tests/data/schedule_good.yaml
@@ -0,0 +1,3 @@
+name: Valid cron
+command: /bin/true
+schedule: "*/5 * * * *"
diff --git a/tests/data/schedule_invalid.yaml b/tests/data/schedule_invalid.yaml
new file mode 100644
index 0000000..9bfbe2a
--- /dev/null
+++ b/tests/data/schedule_invalid.yaml
@@ -0,0 +1,3 @@
+name: Invalid cron
+command: /bin/true
+schedule: "*/5 * * *"
diff --git a/tests/test_validation.py b/tests/test_validation.py
new file mode 100644
index 0000000..19bd635
--- /dev/null
+++ b/tests/test_validation.py
@@ -0,0 +1,38 @@
+import nose
+import os
+
+import job_wrapper
+
+
+data_dir = os.path.dirname(__file__) + "/data"
+
+
+def load_job(filename):
+    path = data_dir + "/" + filename
+    job = job_wrapper.JobWrapper(config_path=path)
+    return job
+
+
+@nose.tools.raises(AssertionError)
+def test_missing_fields():
+    job = load_job("missing_fields.yaml")
+
+
+@nose.tools.raises(AssertionError)
+def test_schedule_atsign():
+    job = load_job("schedule_atsign.yaml")
+
+
+@nose.tools.raises(AssertionError)
+def test_schedule_assign():
+    job = load_job("schedule_assign.yaml")
+
+
+@nose.tools.raises(AssertionError)
+def test_schedule_invalid():
+    job = load_job("schedule_invalid.yaml")
+
+
+def test_schedule_good():
+    job = load_job("schedule_good.yaml")
+    assert job.config["schedule"]

-- 
To view, visit https://gerrit.wikimedia.org/r/343120
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id9783d59eb69c82a1552b6ef330dbed830cefe86
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/process-control
Gerrit-Branch: master
Gerrit-Owner: Awight <awi...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to