~cr3/launchpad-results/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Copyright 2010 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

__metaclass__ = type

__all__ = [
    "TestBackendMixin",
    ]

import new
import sys

from unittest import (
    defaultTestLoader,
    TestCase,
    )

from lpx.disk import backend
from lpx.disk.backend import (
    Backend,
    create_backend,
    register_scheme,
    )
from lpx.disk.connection import Connection
from lpx.disk.uri import URI

from lpx.disk.testing.layers import FunctionalLayer


class TestCreateBackend(TestCase):

    module_name = "lpx.disk.backends.backend_module"

    def setUp(self):
        super(TestCreateBackend, self).setUp()
        self.backend_module = new.module("backend_module")
        self.uri = None

        def create_from_uri(uri):
            self.uri = uri
            return "RESULT"

        self.backend_module.create_from_uri = create_from_uri
        sys.modules[self.module_name] = self.backend_module

    def tearDown(self):
        del sys.modules[self.module_name]
        super(TestCreateBackend, self).tearDown()

    def test_str(self):
        create_backend("backend-module:/path")
        self.assertTrue(self.uri)
        self.assertEquals(self.uri.scheme, "backend-module")
        self.assertEquals(self.uri.path, "/path")

    def test_unicode(self):
        create_backend(u"backend-module:/path")
        self.assertTrue(self.uri)
        self.assertEquals(self.uri.scheme, "backend-module")
        self.assertEquals(self.uri.path, "/path")

    def test_uri(self):
        uri = URI("backend-module:/path")
        create_backend(uri)
        self.assertTrue(self.uri is uri)


class TestRegisterScheme(TestCase):

    uri = None

    def tearDown(self):
        if "factory" in backend._backend_schemes:
            del backend._backend_schemes["factory"]

        super(TestRegisterScheme, self).tearDown()

    def test_register(self):

        def factory(uri):
            self.uri = uri
            return "FACTORY RESULT"

        register_scheme("factory", factory)
        self.assertEqual(backend._backend_schemes["factory"], factory)
        # Check that we can create backends that use this scheme ...
        result = create_backend("factory:/path")
        self.assertEqual(result, "FACTORY RESULT")
        self.assertTrue(self.uri)
        self.assertEqual(self.uri.scheme, "factory")
        self.assertEqual(self.uri.path, "/path")


class TestBackendMixin:

    layer = FunctionalLayer

    def getBackend(self):
        raise NotImplementedError

    def test_init(self):
        backend = self.getBackend()
        self.assertTrue(isinstance(backend, Backend))

    def test_connect(self):
        backend = self.getBackend()
        connection = backend.connect()
        self.assertTrue(isinstance(connection, Connection))


def test_suite():
    return defaultTestLoader.loadTestsFromName(__name__)