Commits

Yann Malet  committed d79a785

Implement the is_valid_config method to validate a cherokee config file

  • Participants
  • Parent commits 3c3bc57

Comments (0)

Files changed (2)

File cherokee_admin_api/__init__.py

 import os
 import shutil
+import shlex
+import subprocess
 
 from cherokee_admin_api.settings import (VERSION,
                                          configuration_dirs)
 def get_version():
     raise NotImplementedError
 
-def validate_config_file(config_file):
-    raise NotImplementedError
+def is_valid_config(config_file):
+    args = shlex.split("cherokee -C %s -t" %config_file)
+    validation_result = subprocess.call(args)
+    proc = subprocess.Popen(args,
+                            stdin=subprocess.PIPE,
+                            stderr=subprocess.PIPE,
+                            stdout=subprocess.PIPE)
+    
+    EXPECTED_STDERR = 'Test on %s: OK\n' %config_file
+
+    if  proc.stderr.read() == EXPECTED_STDERR:
+        return True
+    else:
+        return False
 
 def generate_config(config_file,
                     template="cherokee.conf.sample"):

File tests/__init__.py

 from nose.plugins.skip import Skip, SkipTest
 
 from cherokee_admin_api import (get_version,
-                                validate_config_file,
+                                is_valid_config,
                                 generate_config)
 
 from cherokee_admin_api.admin import Admin
         cfg = Config(file=config_file)
         self.assertEqual(cfg["config"].get_val('version'),VERSION)   
             
+  
+  
+class TestIsValidConfig(TestCase):
+    def setUp(self):
+        """
+        Duplicate the template of the cherokee.conf before starting the test.
+        """
+        shutil.copyfile(CHEROKEE_CONF, TMP_CHEROKEE_CONF)
+        self.config_file = TMP_CHEROKEE_CONF
+        
+    def tearDown(self):
+        _remove_tmp_cherokee_conf(TMP_CHEROKEE_CONF)
+        
+    def test_is_valid_config(self):
+        self.assertEqual(is_valid_config(config_file=self.config_file),True)
         
 class TestAdmin(TestCase):
     def setUp(self):
         except NotImplementedError:
             raise SkipTest
         
-    def test_validate_config_file(self):
-        try:    
-            validate_config_file(config_file="the-cherokee.conf")
-        except NotImplementedError:
-            raise SkipTest
+