From 916323de9d18e7616c58ad33c9e79ad212de1b4a Mon Sep 17 00:00:00 2001
From: Rafael Monnerat <rafael@nexedi.com>
Date: Wed, 17 Oct 2012 23:26:08 -0300
Subject: [PATCH] Create promise.cfg when during unit tests

 Create promise configuration on-the-fly with the configurations reusing
 existing API to get cloudooo, memcached and kumofs informations from
 environment.

 This approach allow us use erp5_promise alarms at any unit test to
 configure cloudooo, memcached and kumofs, instead implement ad-hoc
 configurations on tests afterSetup methods.
---
 product/ERP5Type/tests/ERP5TypeTestCase.py | 32 ++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/product/ERP5Type/tests/ERP5TypeTestCase.py b/product/ERP5Type/tests/ERP5TypeTestCase.py
index d6e265426a..46cfed10d6 100644
--- a/product/ERP5Type/tests/ERP5TypeTestCase.py
+++ b/product/ERP5Type/tests/ERP5TypeTestCase.py
@@ -19,6 +19,7 @@ import sys
 import time
 import traceback
 import urllib
+import ConfigParser
 from cStringIO import StringIO
 from cPickle import dumps
 from glob import glob
@@ -263,6 +264,27 @@ def _getPersistentMemcachedServerDict():
   port = os.environ.get('persistent_memcached_server_port', '12121')
   return dict(hostname=hostname, port=port)
 
+def _createTestPromiseConfigurationFile(promise_path):
+  kumofs_url = "memcached://%(hostname)s:%(port)s/" % \
+                             _getVolatileMemcachedServerDict()
+  memcached_url = "memcached://%(hostname)s:%(port)s/" % \
+                             _getPersistentMemcachedServerDict()
+  cloudooo_url = "cloudooo://%(hostname)s:%(port)s/" % \
+                             _getConversionServerDict()
+
+  promise_config = ConfigParser.RawConfigParser()
+  promise_config.add_section('external_service')
+  promise_config.set('external_service', 'cloudooo_url', cloudooo_url)
+  promise_config.set('external_service', 'memcached_url',memcached_url)
+  promise_config.set('external_service', 'kumofs_url', kumofs_url)
+
+  if os.environ.get('TEST_CA_PATH') is not None:
+    promise_config.add_section('portal_certificate_authority')
+    promise_config.set('portal_certificate_authority', 'certificate_authority_path',
+                                           os.environ['TEST_CA_PATH'])
+
+  promise_config.write(open(promise_path, 'w'))
+
 def profile_if_environ(environment_var_name):
     if int(os.environ.get(environment_var_name, 0)):
       def decorator(self, method):
@@ -928,6 +950,15 @@ class ERP5TypeCommandLineTestCase(ERP5TypeTestCaseMixin):
       """
       return ()
 
+    def loadPromise(self):
+      """ Create promise configuration file and load it into configuration
+      """
+      promise_path = os.path.join(instancehome, "promise.cfg")
+      ZopeTestCase._print('Adding Promise at %s...\n' % promise_path)
+      _createTestPromiseConfigurationFile(promise_path)
+      config.product_config["/%s" % self.getPortalName()] = \
+         {"promise_path": promise_path}
+
     def _updateConnectionStrings(self):
       """Update connection strings with values passed by the testRunner
       """
@@ -1088,6 +1119,7 @@ class ERP5TypeCommandLineTestCase(ERP5TypeTestCaseMixin):
               self._setUpDummyMailHost()
               self.serverhost, self.serverport = self.startZServer(verbose=True)
               self._registerNode(distributing=1, processing=1)
+              self.loadPromise()
 
             self._updateConnectionStrings()
             self._recreateCatalog()
-- 
2.30.9