Change unittest2 back into unittest.
[htsworkflow.git] / test / test_copier.py
1 from unittest import TestCase, skipIf
2
3 from StringIO import StringIO
4 from htsworkflow.automation.solexa import is_runfolder
5
6 try:
7     from htsworkflow.automation import copier
8     BENDERJAB_UNAVAILABLE = False
9 except ImportError as e:
10     BENDERJAB_UNAVAILABLE = True
11
12 @skipIf(BENDERJAB_UNAVAILABLE, "Can't test copier daemon without a working benderjab")
13 class testCopier(TestCase):
14     def test_empty_config(self):
15         cfg = StringIO("""[fake]
16 something: unrelated
17 """)
18         bot = copier.CopierBot('fake', configfile=cfg)
19         self.failUnlessRaises(RuntimeError, bot.read_config)
20
21     def test_full_config(self):
22         cfg = StringIO("""[copier]
23 jid: copier@example.fake
24 password: badpassword
25 authorized_users: user1@example.fake user2@example.fake
26 rsync_password_file: ~/.sequencer
27 rsync_sources: rsync://localhost/tmp/sequencer_source rsync://user@server:1234/other_sequencer
28 rsync_destination: /tmp/sequencer_destination
29 notify_users: user3@example.fake
30 # who to run to
31 #runner:
32 """)
33         c = copier.CopierBot("copier", configfile=cfg)
34         c.read_config()
35         c._init_rsync()
36         self.failUnlessEqual(c.jid, 'copier@example.fake')
37         self.failUnlessEqual(c.cfg['password'], 'badpassword')
38         self.failUnlessEqual(len(c.authorized_users), 2)
39         self.failUnlessEqual(c.authorized_users[0], 'user1@example.fake')
40         self.failUnlessEqual(c.authorized_users[1], 'user2@example.fake')
41         self.failUnlessEqual(c.rsync.source_base_list[0],
42                              'rsync://localhost/tmp/sequencer_source/')
43         self.failUnlessEqual(c.rsync.dest_base, '/tmp/sequencer_destination')
44         self.failUnlessEqual(len(c.notify_users), 1)
45         self.failUnlessEqual(c.notify_users[0], 'user3@example.fake')
46         self.failUnlessEqual(c.validate_url('rsync://other/tmp'), None)
47         self.failUnlessEqual(c.validate_url('http://localhost/tmp'), None)
48         # In the rsync process the URL gets a trailing '/' added to it
49         # But in the bot config its still slash-less.
50         # It is debatable when to add the trailing slash.
51         self.failUnlessEqual(
52           c.validate_url('rsync://localhost/tmp/sequencer_source'),
53           'rsync://localhost/tmp/sequencer_source')
54         self.failUnlessEqual(
55           c.validate_url('rsync://localhost/tmp/sequencer_source/'),
56           'rsync://localhost/tmp/sequencer_source/')
57         self.failUnlessEqual(
58           c.validate_url('rsync://localhost/tmp/sequencer_source/bleem'),
59           'rsync://localhost/tmp/sequencer_source/bleem')
60         self.failUnlessEqual(
61           c.validate_url('rsync://user@server:1234/other_sequencer'),
62           'rsync://user@server:1234/other_sequencer')
63
64
65     def test_dirlist_filter(self):
66        """
67        test our dir listing parser
68        """
69        # everyone should have a root dir, and since we're not
70        # currently writing files... it should all be good
71        r = copier.rsync('/', '/', '/')
72
73        listing = [
74          'drwxrwxr-x           0 2007/12/29 12:34:56 071229_USI-EAS229_001_FC1234\n',
75          '-rwxrw-r--      123268 2007/12/29 17:39:31 2038EAAXX.rtf\n',
76          '-rwxrw-r--           6 2007/12/29 15:10:29 New Text Document.txt\n',
77        ]
78
79        result = r.list_filter(listing)
80        self.failUnlessEqual(len(result), 1)
81        self.failUnlessEqual(result[0][-1], '4')
82
83
84 def suite():
85     from unittest import TestSuite, defaultTestLoader
86     suite = TestSuite()
87     suite.addTests(defaultTestLoader.loadTestsFromTestCase(testCopier))
88     return suite
89
90
91 if __name__ == "__main__":
92     from unittest import main
93     main(defaultTest="suite")