Cleanup config interfaces

    - Add "add_line" and "add_lines" to config classes
    - auto-load all configs in layout.sysconfigdir for all environment cases
    - auto-save all configs in environment case after setUpEnv()


Project: http://git-wip-us.apache.org/repos/asf/trafficserver-qa/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver-qa/commit/04c3ab6e
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver-qa/tree/04c3ab6e
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver-qa/diff/04c3ab6e

Branch: refs/heads/master
Commit: 04c3ab6ec8b61165b54c7d0a8da67593740f1a1c
Parents: 71fa4c1
Author: Thomas Jackson <[email protected]>
Authored: Tue Jan 6 13:30:23 2015 -0800
Committer: Thomas Jackson <[email protected]>
Committed: Tue Jan 6 13:30:23 2015 -0800

----------------------------------------------------------------------
 tsqa/configs.py    | 30 ++++++++++++++++++++++--------
 tsqa/test_cases.py | 14 +++++++++++---
 2 files changed, 33 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver-qa/blob/04c3ab6e/tsqa/configs.py
----------------------------------------------------------------------
diff --git a/tsqa/configs.py b/tsqa/configs.py
index 2a4cc3e..7a0fbe6 100644
--- a/tsqa/configs.py
+++ b/tsqa/configs.py
@@ -7,6 +7,10 @@ class Config(object):
         self.filename = filename
         self.load()
 
+    def add_lines(self, lines):
+        for line in lines:
+            self.add_line(line)
+
     def load(self):
         with open(self.filename, 'r') as fh:
             self.contents = fh.read()
@@ -18,6 +22,10 @@ class Config(object):
         with open(self.filename, 'w') as fh:
             fh.write(self.contents)
 
+    def add_line(self, line):
+        if not line.endswith('\n'):
+            line += '\n'
+        self.contents += line
 
 
 class RecordsConfig(Config, dict):
@@ -47,17 +55,23 @@ class RecordsConfig(Config, dict):
 
         self.load()
 
+    def _load_line(self, line):
+        line = line.strip()
+        # skip comments
+        if not line or line.startswith('#'):
+            return
+        top_kind, name, kind, val = line.split(' ', 3)
+        if top_kind not in self:
+            self[top_kind] = {}
+        self[top_kind][name] = self.kind_map[kind](val)
+
     def load(self):
         with open(self.filename, 'r') as fh:
             for line in fh:
-                line = line.strip()
-                # skip comments
-                if not line or line.startswith('#'):
-                    continue
-                top_kind, name, kind, val = line.split(' ', 3)
-                if top_kind not in self:
-                    self[top_kind] = {}
-                self[top_kind][name] = self.kind_map[kind](val)
+                self._load_line(line)
+
+    def add_line(self, line):
+        self._load_line(line)
 
     def write(self):
         with open(self.filename, 'w') as fh:

http://git-wip-us.apache.org/repos/asf/trafficserver-qa/blob/04c3ab6e/tsqa/test_cases.py
----------------------------------------------------------------------
diff --git a/tsqa/test_cases.py b/tsqa/test_cases.py
index 87885b0..9ee81f9 100644
--- a/tsqa/test_cases.py
+++ b/tsqa/test_cases.py
@@ -34,13 +34,21 @@ class EnvironmentCase(unittest.TestCase):
         cfg_dir = os.path.join(cls.environment.layout.prefix, 'etc', 
'trafficserver')
 
         # create a bunch of config objects that people can access/modify
-        cls.configs = {
-            'records.config': 
tsqa.configs.RecordsConfig(os.path.join(cls.environment.layout.sysconfdir, 
'records.config'))
-        }
+        # classes that override our default config naming
+        config_classes = {'records.config': tsqa.configs.RecordsConfig}
+        # create a mapping of config-name -> config-obj
+        cls.configs = {}
+        for name in os.listdir(cls.environment.layout.sysconfdir):
+            path = os.path.join(cls.environment.layout.sysconfdir, name)
+            if os.path.isfile(path):
+                cls.configs[name] = config_classes.get(name, 
tsqa.configs.Config)(path)
 
         # call env setup, so people can change configs etc
         cls.setUpEnv(cls.environment)
 
+        for _, cfg in cls.configs.iteritems():
+            cfg.write()
+
         # start ATS
         cls.environment.start()
 

Reply via email to