[Kimchi-devel] [PATCH] [WoK 5/9] /config/plugins API: test changes

dhbarboza82 at gmail.com dhbarboza82 at gmail.com
Fri Jan 20 12:15:40 UTC 2017


From: Daniel Henrique Barboza <danielhb at linux.vnet.ibm.com>

This patch added new tests in test_api.py and test_utils.py
for the new /config/plugins API.

Signed-off-by: Daniel Henrique Barboza <danielhb at linux.vnet.ibm.com>
---
 tests/test_api.py   | 43 ++++++++++++++++++++++++++++++++++++++++++-
 tests/test_utils.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 92 insertions(+), 2 deletions(-)

diff --git a/tests/test_api.py b/tests/test_api.py
index 23c263d..1358151 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -1,7 +1,7 @@
 #
 # Project Wok
 #
-# Copyright IBM Corp, 2016
+# Copyright IBM Corp, 2016-2017
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -25,6 +25,8 @@ import utils
 from functools import partial
 
 from wok.asynctask import AsyncTask
+from wok.utils import set_plugin_state
+from wok.rollbackcontext import RollbackContext
 
 test_server = None
 model = None
@@ -53,6 +55,45 @@ class APITests(unittest.TestCase):
                 "server_root"]
         self.assertEquals(sorted(keys), sorted(conf.keys()))
 
+    def test_config_plugins(self):
+        resp = self.request('/config/plugins')
+        self.assertEquals(200, resp.status)
+
+        plugins = json.loads(resp.read())
+        if len(plugins) == 0:
+            return
+
+        plugin = plugins[0]
+        plugin_state = plugin.get('enabled')
+        plugin_name = plugin.get('name')
+
+        with RollbackContext() as rollback:
+            rollback.prependDefer(set_plugin_state, plugin_name,
+                                  plugin_state)
+
+            resp = self.request('/config/plugins/%s' % plugin_name)
+            self.assertEquals(200, resp.status)
+
+            resp = self.request('/config/plugins/%s/enable' % plugin_name,
+                                '{}', 'POST')
+            self.assertEquals(200, resp.status)
+
+            resp = self.request('/config/plugins')
+            self.assertEquals(200, resp.status)
+            plugins = json.loads(resp.read())
+            plugin = plugins[0]
+            self.assertEqual(plugin.get('enabled'), True)
+
+            resp = self.request('/config/plugins/%s/disable' % plugin_name,
+                                '{}', 'POST')
+            self.assertEquals(200, resp.status)
+
+            resp = self.request('/config/plugins')
+            self.assertEquals(200, resp.status)
+            plugins = json.loads(resp.read())
+            plugin = plugins[0]
+            self.assertEqual(plugin.get('enabled'), False)
+
     def test_user_log(self):
         # Login and logout to make sure there there are entries in user log
         hdrs = {'AUTHORIZATION': '',
diff --git a/tests/test_utils.py b/tests/test_utils.py
index e7fd264..c0e1009 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -19,10 +19,14 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 
+import mock
+import os
+import tempfile
 import unittest
 
 from wok.exception import InvalidParameter
-from wok.utils import convert_data_size
+from wok.rollbackcontext import RollbackContext
+from wok.utils import convert_data_size, set_plugin_state
 
 
 class UtilsTests(unittest.TestCase):
@@ -69,3 +73,48 @@ class UtilsTests(unittest.TestCase):
 
         for d in success_data:
             self.assertEquals(d['got'], d['want'])
+
+    def _get_fake_config_file_content(self, enable=True):
+        return """\
+[wok]
+# Enable plugin on Wok server (values: True|False)
+enable = %s
+
+[fakeplugin]
+# Yet another comment on this config file
+very_interesting_option = True
+""" % str(enable)
+
+    def _create_fake_config_file(self):
+        _, tmp_file_name = tempfile.mkstemp(suffix='.conf')
+
+        config_contents = self._get_fake_config_file_content()
+        with open(tmp_file_name, 'w') as f:
+            f.writelines(config_contents)
+
+        return tmp_file_name
+
+    @mock.patch('wok.utils.get_plugin_config_file')
+    def test_set_plugin_state(self, mock_config_file):
+        with RollbackContext() as rollback:
+
+            config_file_name = self._create_fake_config_file()
+            rollback.prependDefer(os.remove, config_file_name)
+
+            mock_config_file.return_value = config_file_name
+
+            set_plugin_state('pluginA', False)
+            with open(config_file_name, 'r') as f:
+                updated_conf = f.read()
+                self.assertEqual(
+                    updated_conf,
+                    self._get_fake_config_file_content(enable=False)
+                )
+
+            set_plugin_state('pluginA', True)
+            with open(config_file_name, 'r') as f:
+                updated_conf = f.read()
+                self.assertEqual(
+                    updated_conf,
+                    self._get_fake_config_file_content(enable=True)
+                )
-- 
2.7.4



More information about the Kimchi-devel mailing list