~cr3/launchpad-results/trunk

« back to all changes in this revision

Viewing changes to lib/lpx/disk/tests/test_backend.py

  • Committer: Marc Tardif
  • Date: 2011-04-23 21:55:09 UTC
  • Revision ID: marc.tardif@canonical.com-20110423215509-lr5lve64vl0fin7b
Added disk package, attachment modules and corresponding infrastructure.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
__metaclass__ = type
 
5
 
 
6
__all__ = [
 
7
    "TestBackendMixin",
 
8
    ]
 
9
 
 
10
import new
 
11
import sys
 
12
 
 
13
from unittest import (
 
14
    defaultTestLoader,
 
15
    TestCase,
 
16
    )
 
17
 
 
18
from lpx.disk import backend
 
19
from lpx.disk.backend import (
 
20
    Backend,
 
21
    create_backend,
 
22
    register_scheme,
 
23
    )
 
24
from lpx.disk.connection import Connection
 
25
from lpx.disk.uri import URI
 
26
 
 
27
from lpx.disk.testing.layers import FunctionalLayer
 
28
 
 
29
 
 
30
class TestCreateBackend(TestCase):
 
31
 
 
32
    module_name = "lpx.disk.backends.backend_module"
 
33
 
 
34
    def setUp(self):
 
35
        super(TestCreateBackend, self).setUp()
 
36
        self.backend_module = new.module("backend_module")
 
37
        self.uri = None
 
38
 
 
39
        def create_from_uri(uri):
 
40
            self.uri = uri
 
41
            return "RESULT"
 
42
 
 
43
        self.backend_module.create_from_uri = create_from_uri
 
44
        sys.modules[self.module_name] = self.backend_module
 
45
 
 
46
    def tearDown(self):
 
47
        del sys.modules[self.module_name]
 
48
        super(TestCreateBackend, self).tearDown()
 
49
 
 
50
    def test_str(self):
 
51
        create_backend("backend-module:/path")
 
52
        self.assertTrue(self.uri)
 
53
        self.assertEquals(self.uri.scheme, "backend-module")
 
54
        self.assertEquals(self.uri.path, "/path")
 
55
 
 
56
    def test_unicode(self):
 
57
        create_backend(u"backend-module:/path")
 
58
        self.assertTrue(self.uri)
 
59
        self.assertEquals(self.uri.scheme, "backend-module")
 
60
        self.assertEquals(self.uri.path, "/path")
 
61
 
 
62
    def test_uri(self):
 
63
        uri = URI("backend-module:/path")
 
64
        create_backend(uri)
 
65
        self.assertTrue(self.uri is uri)
 
66
 
 
67
 
 
68
class TestRegisterScheme(TestCase):
 
69
 
 
70
    uri = None
 
71
 
 
72
    def tearDown(self):
 
73
        if "factory" in backend._backend_schemes:
 
74
            del backend._backend_schemes["factory"]
 
75
 
 
76
        super(TestRegisterScheme, self).tearDown()
 
77
 
 
78
    def test_register(self):
 
79
 
 
80
        def factory(uri):
 
81
            self.uri = uri
 
82
            return "FACTORY RESULT"
 
83
 
 
84
        register_scheme("factory", factory)
 
85
        self.assertEqual(backend._backend_schemes["factory"], factory)
 
86
        # Check that we can create backends that use this scheme ...
 
87
        result = create_backend("factory:/path")
 
88
        self.assertEqual(result, "FACTORY RESULT")
 
89
        self.assertTrue(self.uri)
 
90
        self.assertEqual(self.uri.scheme, "factory")
 
91
        self.assertEqual(self.uri.path, "/path")
 
92
 
 
93
 
 
94
class TestBackendMixin:
 
95
 
 
96
    layer = FunctionalLayer
 
97
 
 
98
    def getBackend(self):
 
99
        raise NotImplementedError
 
100
 
 
101
    def test_init(self):
 
102
        backend = self.getBackend()
 
103
        self.assertTrue(isinstance(backend, Backend))
 
104
 
 
105
    def test_connect(self):
 
106
        backend = self.getBackend()
 
107
        connection = backend.connect()
 
108
        self.assertTrue(isinstance(connection, Connection))
 
109
 
 
110
 
 
111
def test_suite():
 
112
    return defaultTestLoader.loadTestsFromName(__name__)