testIdentificationHandler.py 3.77 KB
Newer Older
1
#
2
# Copyright (C) 2009-2012  Nexedi SA
3 4 5 6 7 8 9 10 11 12 13 14
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 17 18

import unittest
from mock import Mock
19
from .. import NeoUnitTestBase
Olivier Cros's avatar
Olivier Cros committed
20
from neo.lib.protocol import NodeTypes, NotReadyError, \
21 22
        BrokenNodeDisallowedError
from neo.lib.pt import PartitionTable
23 24 25
from neo.storage.app import Application
from neo.storage.handlers.identification import IdentificationHandler

26
class StorageIdentificationHandlerTests(NeoUnitTestBase):
27 28

    def setUp(self):
29
        NeoUnitTestBase.setUp(self)
30 31 32 33 34 35 36
        config = self.getStorageConfiguration(master_number=1)
        self.app = Application(config)
        self.app.name = 'NEO'
        self.app.ready = True
        self.app.pt = PartitionTable(4, 1)
        self.identification = IdentificationHandler(self.app)

37
    def _tearDown(self, success):
38 39
        self.app.close()
        del self.app
40
        super(StorageIdentificationHandlerTests, self)._tearDown(success)
41

42 43 44 45 46 47 48 49
    def test_requestIdentification1(self):
        """ nodes are rejected during election or if unknown storage """
        self.app.ready = False
        self.assertRaises(
                NotReadyError,
                self.identification.requestIdentification,
                self.getFakeConnection(),
                NodeTypes.CLIENT,
50
                self.getClientUUID(),
51 52 53 54 55 56 57 58 59
                None,
                self.app.name,
        )
        self.app.ready = True
        self.assertRaises(
                NotReadyError,
                self.identification.requestIdentification,
                self.getFakeConnection(),
                NodeTypes.STORAGE,
60
                self.getStorageUUID(),
61 62 63 64 65 66
                None,
                self.app.name,
        )

    def test_requestIdentification3(self):
        """ broken nodes must be rejected """
67
        uuid = self.getClientUUID()
68 69 70 71 72 73 74 75 76 77 78 79 80 81
        conn = self.getFakeConnection(uuid=uuid)
        node = self.app.nm.createClient(uuid=uuid)
        node.setBroken()
        self.assertRaises(BrokenNodeDisallowedError,
                self.identification.requestIdentification,
                conn,
                NodeTypes.CLIENT,
                uuid,
                None,
                self.app.name,
        )

    def test_requestIdentification2(self):
        """ accepted client must be connected and running """
82
        uuid = self.getClientUUID()
83 84
        conn = self.getFakeConnection(uuid=uuid)
        node = self.app.nm.createClient(uuid=uuid)
85
        master = (self.local_ip, 3000)
86
        self.app.master_node = Mock({
87
          'getAddress': master,
88
        })
89 90 91 92 93 94 95
        self.identification.requestIdentification(conn, NodeTypes.CLIENT, uuid,
                None, self.app.name)
        self.assertTrue(node.isRunning())
        self.assertTrue(node.isConnected())
        self.assertEqual(node.getUUID(), uuid)
        self.assertTrue(node.getConnection() is conn)
        args = self.checkAcceptIdentification(conn, decode=True)
96
        node_type, address, _np, _nr, _uuid, _master, _master_list = args
97 98 99
        self.assertEqual(node_type, NodeTypes.STORAGE)
        self.assertEqual(address, None)
        self.assertEqual(_uuid, uuid)
100
        self.assertEqual(_master, master)
101
        # TODO: check _master_list ?
102 103 104

if __name__ == "__main__":
    unittest.main()