From 0f902ee74bf6775260dc1af22ac34816558d0306 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Mon, 20 Jan 2014 23:39:25 +0400 Subject: [PATCH] System test, first sketch. --- system/README.rst | 1 + system/lib.py | 81 +++++++++++++++++++++++++++++++ system/run.py | 39 +++++++++++++++ system/t01_version/__init__.py | 9 ++++ system/t01_version/gold | 1 + system/t02_config/__init__.py | 15 ++++++ system/t02_config/gold | 8 +++ system/t03_bad_config/__init__.py | 18 +++++++ system/t03_bad_config/gold | 1 + 9 files changed, 173 insertions(+) create mode 100644 system/README.rst create mode 100644 system/lib.py create mode 100755 system/run.py create mode 100644 system/t01_version/__init__.py create mode 100644 system/t01_version/gold create mode 100644 system/t02_config/__init__.py create mode 100644 system/t02_config/gold create mode 100644 system/t03_bad_config/__init__.py create mode 100644 system/t03_bad_config/gold diff --git a/system/README.rst b/system/README.rst new file mode 100644 index 00000000..ec23ec65 --- /dev/null +++ b/system/README.rst @@ -0,0 +1 @@ +System test for aptly \ No newline at end of file diff --git a/system/lib.py b/system/lib.py new file mode 100644 index 00000000..66b90a73 --- /dev/null +++ b/system/lib.py @@ -0,0 +1,81 @@ +""" +Test library. +""" + +import difflib +import inspect +import subprocess +import os +import shutil +import string + +class BaseTest(object): + """ + Base class for all tests. + """ + + expectedCode = 0 + + def test(self): + self.prepare() + self.run() + self.check() + + def prepare_remove_all(self): + if os.path.exists(os.path.join(os.environ["HOME"], ".aptly")): + shutil.rmtree(os.path.join(os.environ["HOME"], ".aptly")) + if os.path.exists(os.path.join(os.environ["HOME"], ".aptly.conf")): + os.remove(os.path.join(os.environ["HOME"], ".aptly.conf")) + + def prepare_default_config(self): + f = open(os.path.join(os.environ["HOME"], ".aptly.conf"), "w") + f.write(config_file) + f.close() + + def run(self): + try: + proc = subprocess.Popen(self.runCmd.split(" "), stderr=subprocess.STDOUT, stdout=subprocess.PIPE) + self.output, _ = proc.communicate() + if proc.returncode != self.expectedCode: + raise Exception("exit code %d != %d" % (proc.returncode, self.expectedCode)) + except Exception, e: + raise Exception("Running command %s failed: %s" % (self.runCmd, str(e))) + + def gold_processor(self, gold): + return gold + + def expand_environ(self, gold): + return string.Template(gold).substitute(os.environ) + + def get_gold(self): + gold = os.path.join(os.path.dirname(inspect.getsourcefile(self.__class__)), "gold") + return self.gold_processor(open(gold, "r").read()) + + def check_output(self): + self.verify_match(self.get_gold(), self.output) + + def verify_match(self, a, b): + if a != b: + diff = "".join(difflib.unified_diff([l + "\n" for l in a.split("\n")], [l + "\n" for l in b.split("\n")])) + + raise Exception("content doesn't match:\n" + diff + "\n") + + def check_file(self): + self.verify_match(self.get_gold(), open(self.checkedFile, "r").read()) + + check = check_output + + def prepare(self): + self.prepare_remove_all() + self.prepare_default_config() + +config_file = """ +{ + "rootDir": "%s/.aptly", + "downloadConcurrency": 4, + "architectures": [], + "dependencyFollowSuggests": false, + "dependencyFollowRecommends": false, + "dependencyFollowAllVariants": false +} +""" % (os.environ["HOME"]) diff --git a/system/run.py b/system/run.py new file mode 100755 index 00000000..cae119f9 --- /dev/null +++ b/system/run.py @@ -0,0 +1,39 @@ +#!/usr/bin/python + +import glob +import importlib +import sys + + +def run(): + """ + Run system test. + """ + tests = glob.glob("t*_*") + fails = [] + + for test in tests: + sys.stdout.write("%s..." % (test, )) + + testModule = importlib.import_module(test) + t = testModule.Test() + + try: + t.test() + except BaseException, e: + fails.append((test, t, e, testModule)) + sys.stdout.write("FAIL\n") + else: + sys.stdout.write("OK\n") + + if len(fails) > 0: + print "\nFAILURES (%d):" % (len(fails), ) + + for (test, t, e, testModule) in fails: + print "%s: %s" % (test, testModule.__doc__.strip()) + print "ERROR: %s" % (e, ) + print "=" * 60 + + +if __name__ == "__main__": + run() diff --git a/system/t01_version/__init__.py b/system/t01_version/__init__.py new file mode 100644 index 00000000..358f0496 --- /dev/null +++ b/system/t01_version/__init__.py @@ -0,0 +1,9 @@ +""" +Test aptly version. +""" + +from lib import BaseTest + + +class Test(BaseTest): + runCmd = "aptly version" diff --git a/system/t01_version/gold b/system/t01_version/gold new file mode 100644 index 00000000..420c88fe --- /dev/null +++ b/system/t01_version/gold @@ -0,0 +1 @@ +aptly version: 0.2 diff --git a/system/t02_config/__init__.py b/system/t02_config/__init__.py new file mode 100644 index 00000000..7a6ae465 --- /dev/null +++ b/system/t02_config/__init__.py @@ -0,0 +1,15 @@ +""" +Test config file generation. +""" + +import os +from lib import BaseTest + + +class Test(BaseTest): + runCmd = "aptly" + checkedFile = os.path.join(os.environ["HOME"], ".aptly.conf") + + check = BaseTest.check_file + gold_processor = BaseTest.expand_environ + prepare = BaseTest.prepare_remove_all diff --git a/system/t02_config/gold b/system/t02_config/gold new file mode 100644 index 00000000..07c2522d --- /dev/null +++ b/system/t02_config/gold @@ -0,0 +1,8 @@ +{ + "rootDir": "${HOME}/.aptly", + "downloadConcurrency": 4, + "architectures": [], + "dependencyFollowSuggests": false, + "dependencyFollowRecommends": false, + "dependencyFollowAllVariants": false +} \ No newline at end of file diff --git a/system/t03_bad_config/__init__.py b/system/t03_bad_config/__init__.py new file mode 100644 index 00000000..2dde4233 --- /dev/null +++ b/system/t03_bad_config/__init__.py @@ -0,0 +1,18 @@ +""" +Start with bad config. +""" + +import os +from lib import BaseTest + + +class Test(BaseTest): + runCmd = "aptly" + expectedCode = 1 + + def prepare(self): + self.prepare_remove_all() + + f = open(os.path.join(os.environ["HOME"], ".aptly.conf"), "w") + f.write("{some crap") + f.close() diff --git a/system/t03_bad_config/gold b/system/t03_bad_config/gold new file mode 100644 index 00000000..9925cc0d --- /dev/null +++ b/system/t03_bad_config/gold @@ -0,0 +1 @@ +ERROR: error loading config file /Users/smira/.aptly.conf: invalid character 's' looking for beginning of object key string