Ticket #68: test_multi_introducers.4.py

File test_multi_introducers.4.py, 3.8 KB (added by writefaruq, at 2010-08-04T14:54:39Z)

Merged all tests

Line 
1#!/usr/bin/python
2import unittest, os
3from mock import Mock, patch
4
5from allmydata.util.fileutil import write, remove
6from allmydata.client import Client, MULTI_INTRODUCERS_CFG
7from allmydata.scripts.create_node import write_node_config
8from allmydata.web.root import Root
9
10INTRODUCERS_CFG_FURLS=['furl1', 'furl2']
11
12def cfg_setup():       
13    # setup tahoe.cfg and basedir/introducers
14    # create a custom tahoe.cfg
15    c = open(os.path.join("tahoe.cfg"), "w")
16    config = {}
17    write_node_config(c, config)
18    fake_furl = "furl1"
19    c.write("[client]\n")
20    c.write("introducer.furl = %s\n" % fake_furl)       
21    c.close()
22   
23    # create a basedir/introducers
24    write(MULTI_INTRODUCERS_CFG, '\n'.join(INTRODUCERS_CFG_FURLS))
25   
26def cfg_cleanup():
27    # clean-up all cfg files
28    remove("tahoe.cfg") 
29    remove(MULTI_INTRODUCERS_CFG)
30
31class TestRoot(unittest.TestCase):
32   
33    def setUp(self):       
34        cfg_setup()
35       
36    def tearDown(self):
37        cfg_cleanup()
38           
39    @patch('allmydata.web.root.Root')
40    def test_introducer_furls(self, MockRoot):       
41        """Ensure that a client's 'welcome page can fetch all introducer FURLs
42         loaded by the Client"""
43       
44        # mock setup
45        mockctx = Mock()
46        mockdata = Mock() 
47       
48        # get the Client and furl count
49        myclient = Client() 
50        furls = myclient.introducer_furls
51        furl_count = len(furls)   
52
53        # Pass mock value to Root
54        myroot = Root(myclient)
55       
56        # make the call
57        s = myroot.data_introducers(mockctx, mockdata)
58       
59        #assertions: compare return value with preset value
60        self.failUnlessEqual(furl_count, len(s))         
61   
62
63
64class TestClient(unittest.TestCase):
65    def setUp(self):       
66        cfg_setup()
67       
68    def tearDown(self):
69        cfg_cleanup()
70   
71    def test_introducer_count(self):
72        """ Ensure that the Client creates same number of introducer clients
73        as found in "basedir/introducers" config file. """
74        write(MULTI_INTRODUCERS_CFG, '\n'.join(INTRODUCERS_CFG_FURLS))
75       
76        # get a client and count of introducer_clients
77        myclient = Client()
78        ic_count = len(myclient.introducer_clients)
79       
80        # assertions
81        self.failUnlessEqual(ic_count, 2)
82   
83    def test_read_introducer_furl_from_tahoecfg(self):
84        """ Ensure that the Client reads the introducer.furl config item from
85        the tahoe.cfg file. """
86        # create a custom tahoe.cfg
87        c = open(os.path.join("tahoe.cfg"), "w")
88        config = {}
89        write_node_config(c, config)
90        fake_furl = "furl1"
91        c.write("[client]\n")
92        c.write("introducer.furl = %s\n" % fake_furl)       
93        c.close()
94
95        # get a client and first introducer_furl
96        myclient = Client()       
97        tahoe_cfg_furl = myclient.introducer_furls[0]
98       
99        # assertions
100        self.failUnlessEqual(fake_furl, tahoe_cfg_furl)
101       
102    def test_warning(self):
103        """ Ensure that the Client warns user if the the introducer.furl config item from the tahoe.cfg file is copied to "introducers" cfg file """
104        # prepare tahoe.cfg
105        c = open(os.path.join("tahoe.cfg"), "w")
106        config = {}
107        write_node_config(c, config)
108        fake_furl = "furl0"
109        c.write("[client]\n")
110        c.write("introducer.furl = %s\n" % fake_furl)       
111        c.close()
112       
113        # prepare "basedir/introducers"
114        write(MULTI_INTRODUCERS_CFG, '\n'.join(INTRODUCERS_CFG_FURLS))       
115
116        # get a client
117        myclient = Client()       
118       
119        # assertions: we expect a warning as tahoe_cfg furl is different     
120        self.failUnlessEqual(True, myclient.warn_flag)
121   
122
123if __name__ == "__main__":
124    unittest.main()