This adds installer.py, that holds functionality for registering and
gettings installer classes for different virtualization technologies.

A unittest that checks common use cases is also included.

Signed-off-by: Cleber Rosa <[email protected]>
---
 client/virt/installer.py          |  102 +++++++++++++++++++++++++++++++++++++
 client/virt/installer_unittest.py |   63 +++++++++++++++++++++++
 2 files changed, 165 insertions(+), 0 deletions(-)
 create mode 100644 client/virt/installer.py
 create mode 100644 client/virt/installer_unittest.py

diff --git a/client/virt/installer.py b/client/virt/installer.py
new file mode 100644
index 0000000..838b7e8
--- /dev/null
+++ b/client/virt/installer.py
@@ -0,0 +1,102 @@
+'''
+Installer classes are responsible for building and installing virtualization
+specific software components. This is the main entry point for tests that
+wish to install virtualization software components.
+
+The most common use case is to simply call make_installer() inside your tests.
+'''
+
+from autotest_lib.client.common_lib import error
+
+__all__ = ['InstallerRegistry', 'INSTALLER_REGISTRY', 'make_installer']
+
+class InstallerRegistry(dict):
+    '''
+    Holds information on known installer classes
+
+    This class is used to create a single instance, named INSTALLER_REGISTRY,
+    that will hold all information on known installer types.
+
+    For registering a new installer class, use the register() method. If the
+    virt type is not set explicitly, it will be set to 'base'. Example:
+
+    >>> INSTALLER_REGISTRY.register('yum', base_installer.YumInstaller)
+
+    If you want to register a virt specific installer class, set the virt
+    (third) param:
+
+    >>> INSTALLER_REGISTRY.register('yum', kvm_installer.YumInstaller, 'kvm')
+
+    For getting a installer class, use the get_installer() method. This method
+    has a fallback option 'get_default_virt' that will return a generic virt
+    installer if set to true.
+    '''
+
+    DEFAULT_VIRT_NAME = 'base'
+
+    def __init__(self, **kwargs):
+        dict.__init__(self, **kwargs)
+        self[self.DEFAULT_VIRT_NAME] = {}
+
+
+    def register(self, mode, klass, virt=None):
+        '''
+        Register a class as responsible for installing virt software components
+
+        If virt is not set, it will assume a default of 'base'.
+        '''
+        if virt is None:
+            virt = self.DEFAULT_VIRT_NAME
+        elif not self.has_key(virt):
+            self[virt] = {}
+
+        self[virt][mode] = klass
+
+
+    def get_installer(self, mode, virt=None, get_default_virt=False):
+        '''
+        Gets a installer class that should be able to install the virt software
+
+        Always try to use classes that are specific to the virtualization
+        technology that is being tested. If you have confidence that the
+        installation is rather trivial and does not require custom steps, you
+        may be able to get away with a base class (by setting get_default_virt
+        to True).
+        '''
+        if virt is None:
+            virt = self.DEFAULT_VIRT_NAME
+        if not self.has_key(virt):
+            # return a base installer so the test could and give it a try?
+            if get_default_virt:
+                return self[self.DEFAULT_VIRT_NAME].get(mode)
+        else:
+            return self[virt].get(mode)
+
+
+#
+# InstallerRegistry unique instance
+#
+INSTALLER_REGISTRY = InstallerRegistry()
+
+
+def make_installer(params, test=None):
+    '''
+    Installer factory: returns a new installer for the chosen mode and vm type
+
+    This is the main entry point for acquiring an installer. Tests, such as
+    the build test, should use this function.
+
+    Param priority evaluation order is 'install_mode', then 'mode'. For virt
+    type, 'vm_type' is consulted.
+
+    @param params: dictionary with parameters generated from cartersian config
+    @param test: the test instance
+    '''
+    mode = params.get("install_mode", params.get("mode", None))
+    virt = params.get("vm_type", None)
+    klass = INSTALLER_REGISTRY.get_installer(mode, virt)
+
+    if klass is None:
+        raise error.TestError('Invalid or unsupported install mode: %s' % mode)
+    else:
+        return klass(test, params)
diff --git a/client/virt/installer_unittest.py 
b/client/virt/installer_unittest.py
new file mode 100644
index 0000000..8e907fe
--- /dev/null
+++ b/client/virt/installer_unittest.py
@@ -0,0 +1,63 @@
+#!/usr/bin/python
+
+import unittest
+from autotest_lib.client.virt import installer
+from autotest_lib.client.common_lib import cartesian_config
+
+class installer_test(unittest.TestCase):
+
+    def setUp(self):
+        self.registry = installer.InstallerRegistry()
+
+
+    def test_register_get_installer(self):
+        install_mode = 'custom_install_mode'
+        virt_type = 'custom_virt_type'
+
+        class CustomVirtInstaller:
+            pass
+
+        self.registry.register(install_mode, CustomVirtInstaller, virt_type)
+        klass = self.registry.get_installer(install_mode, virt_type)
+        self.assertIs(klass, CustomVirtInstaller)
+
+
+    def test_register_get_installer_default(self):
+        install_mode = 'base_install_mode'
+
+        class BaseVirtInstaller:
+            pass
+
+        self.registry.register(install_mode, BaseVirtInstaller)
+        klass = self.registry.get_installer(install_mode,
+                                            get_default_virt=True)
+        self.assertIs(klass, BaseVirtInstaller)
+
+        klass = self.registry.get_installer(install_mode,
+                                            virt=None,
+                                            get_default_virt=True)
+        self.assertIs(klass, BaseVirtInstaller)
+
+
+    def test_make_installer(self):
+        config = """install_mode = test_install_mode
+vm_type = test"""
+
+        class Installer:
+            def __init__(self, test, params):
+                pass
+
+        installer.INSTALLER_REGISTRY.register('test_install_mode',
+                                              Installer,
+                                              'test')
+
+        config_parser = cartesian_config.Parser()
+        config_parser.parse_string(config)
+        params = config_parser.get_dicts().next()
+
+        instance = installer.make_installer(params)
+        self.assertIsInstance(instance, Installer)
+
+
+if __name__ == '__main__':
+    unittest.main()
-- 
1.7.4.4

_______________________________________________
Autotest mailing list
[email protected]
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest

Reply via email to