~didrocks/ubuntuone-client/use_result_var

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#
# Author: Guillermo Gonzalez <guillermo.gonzalez@canonical.com>
#
# Copyright 2009 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, 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 along
# with this program.  If not, see <http://www.gnu.org/licenses/>.
"""tests for the syncdaemon config module """
from __future__ import with_statement

import logging
import os
from ConfigParser import ConfigParser
from xdg.BaseDirectory import (
    xdg_data_home,
    xdg_cache_home,
)

from ubuntuone.syncdaemon import config
from contrib.testing.testcase import (
    BaseTwistedTestCase,
    environ,
)


class TestConfigBasic(BaseTwistedTestCase):
    """Basic _Config object tests"""

    def setUp(self):
        BaseTwistedTestCase.setUp(self)
        self.test_root = self.mktemp()

    def assertThrottlingSection(self, expected, current, on, read, write):
        """Assert for equality two ConfigParser and against the on, read and
        write args
        """
        self.assertEquals(expected.getboolean(config.THROTTLING, 'on'), on)
        self.assertEquals(expected.getint(config.THROTTLING, 'read_limit'),
                          read)
        self.assertEquals(expected.getint(config.THROTTLING, 'write_limit'),
                          write)
        self.assertEquals(expected.getboolean(config.THROTTLING, 'on'),
                          current.get_throttling())
        self.assertEquals(expected.getint(config.THROTTLING, 'read_limit'),
                          current.get_throttling_read_limit())
        self.assertEquals(expected.getint(config.THROTTLING, 'write_limit'),
                          current.get_throttling_write_limit())

    def test_load_empty(self):
        """test loading the a non-existent config file"""
        conf_file = os.path.join(self.test_root, 'test_missing_config.conf')
        # create the config object with an empty config file
        conf = config._Config(conf_file)
        self.assertEquals(False, conf.get_throttling())
        self.assertEquals(2097152, conf.get_throttling_read_limit())
        self.assertEquals(2097152, conf.get_throttling_write_limit())

    def test_load_basic(self):
        """test loading the config file containing only the throttling values"""
        conf_file = os.path.join(self.test_root, 'test_load_config.conf')
        # write some throttling values to the config file
        with open(conf_file, 'w') as fp:
            fp.write('[bandwidth_throttling]\n')
            fp.write('on = True\n')
            fp.write('read_limit = 1000\n')
            fp.write('write_limit = 200\n')
        conf = config._Config(conf_file)
        self.assertEquals(True, conf.get_throttling())
        self.assertEquals(1000, conf.get_throttling_read_limit())
        self.assertEquals(200, conf.get_throttling_write_limit())

    def test_load_extra_data(self):
        """test loading the a config file with other sections too"""
        conf_file = os.path.join(self.test_root, 'test_load_extra_config.conf')
        # write some throttling values to the config file
        with open(conf_file, 'w') as fp:
            fp.write('[__main__]\n')
            fp.write('log_level = INFO\n')
            fp.write('disable_ssl_verify = True\n')
            fp.write('\n')
            fp.write('[bandwidth_throttling]\n')
            fp.write('on = True\n')
            fp.write('read_limit = 1000\n')
            fp.write('write_limit = 200\n')
        conf = config._Config(conf_file)
        self.assertEquals(True, conf.get_throttling())
        self.assertEquals(1000, conf.get_throttling_read_limit())
        self.assertEquals(200, conf.get_throttling_write_limit())

    def test_write_new(self):
        """test writing the throttling section to a new config file"""
        conf_file = os.path.join(self.test_root, 'test_write_new_config.conf')
        self.assertFalse(os.path.exists(conf_file))
        conf = config._Config(conf_file)
        conf.set_throttling(True)
        conf.set_throttling_read_limit(1000)
        conf.set_throttling_write_limit(100)
        conf.save()
        # load the config in a barebone ConfigParser and check
        conf_1 = ConfigParser()
        conf_1.read(conf_file)
        self.assertThrottlingSection(conf_1, conf, True, 1000, 100)

    def test_write_existing(self):
        """test writing the throttling section to a existing config file"""
        conf_file = os.path.join(self.test_root,
                                 'test_write_existing_config.conf')
        # write some throttling values to the config file
        with open(conf_file, 'w') as fp:
            fp.write('[bandwidth_throttling]\n')
            fp.write('on = False\n')
            fp.write('read_limit = 1000\n')
            fp.write('write_limit = 100\n')
        self.assertTrue(os.path.exists(conf_file))
        conf = config._Config(conf_file)
        conf.set_throttling(True)
        conf.set_throttling_read_limit(2000)
        conf.set_throttling_write_limit(200)
        conf.save()
        # load the config in a barebone ConfigParser and check
        conf_1 = ConfigParser()
        conf_1.read(conf_file)
        self.assertThrottlingSection(conf_1, conf, True, 2000, 200)

    def test_write_extra(self):
        """test writing the throttling section back to the config file,
        including extra sections
        """
        conf_file = os.path.join(self.test_root, 'test_write_extra_config.conf')
        # write some throttling values to the config file
        with open(conf_file, 'w') as fp:
            fp.write('[__main__]\n')
            fp.write('log_level = INFO\n')
            fp.write('disable_ssl_verify = True\n')
            fp.write('\n')
            fp.write('[bandwidth_throttling]\n')
            fp.write('on = False\n')
            fp.write('read_limit = 2000\n')
            fp.write('write_limit = 200\n')
        self.assertTrue(os.path.exists(conf_file))
        conf = config._Config(conf_file)
        conf.set_throttling(True)
        conf.set_throttling_read_limit(3000)
        conf.set_throttling_write_limit(300)
        conf.save()
        # load the config in a barebone ConfigParser and check
        conf_1 = ConfigParser()
        conf_1.read(conf_file)
        self.assertThrottlingSection(conf_1, conf, True, 3000, 300)
        self.assertEquals(conf_1.get('__main__', 'log_level'),
                          conf.get('__main__', 'log_level'))
        self.assertEquals(conf_1.getboolean('__main__', 'disable_ssl_verify'),
                          conf.getboolean('__main__', 'disable_ssl_verify'))

    def test_write_existing_partial(self):
        """test writing a partially updated throttling section
        to a existing config file
        """
        conf_file = os.path.join(self.test_root,
                                 'test_write_existing_config.conf')
        # write some throttling values to the config file
        with open(conf_file, 'w') as fp:
            fp.write('[bandwidth_throttling]\n')
            fp.write('on = True\n')
            fp.write('read_limit = 1000\n')
            fp.write('write_limit = 100\n')
        self.assertTrue(os.path.exists(conf_file))
        conf = config._Config(conf_file)
        conf.set_throttling(False)
        conf.save()
        # load the config in a barebone ConfigParser and check
        conf_1 = ConfigParser()
        conf_1.read(conf_file)
        self.assertThrottlingSection(conf_1, conf, False, 1000, 100)

    def test_load_negative_limits(self):
        """test loading the config file with negative read/write limits"""
        conf_file = os.path.join(self.test_root, 'test_load_config.conf')
        # write some throttling values to the config file
        with open(conf_file, 'w') as fp:
            fp.write('[bandwidth_throttling]\n')
            fp.write('on = True\n')
            fp.write('read_limit = -1\n')
            fp.write('write_limit = -1\n')
        conf = config._Config(conf_file)
        self.assertEquals(True, conf.get_throttling())
        self.assertEquals(None, conf.get_throttling_read_limit())
        self.assertEquals(None, conf.get_throttling_write_limit())


    def test_load_partial_config(self):
        """test loading a partial config file and fallback to defaults"""
        conf_file = os.path.join(self.test_root, 'test_load_config.conf')
        # write some throttling values to the config file
        with open(conf_file, 'w') as fp:
            fp.write('[bandwidth_throttling]\n')
            fp.write('on = True\n')
            fp.write('read_limit = 1\n')
        conf = config._Config(conf_file)
        self.assertEquals(True, conf.get_throttling())
        self.assertEquals(1, conf.get_throttling_read_limit())
        self.assertEquals(2097152, conf.get_throttling_write_limit())

    def test_override(self):
        """test loading the config file containing only the throttling values"""
        conf_file = os.path.join(self.test_root, 'test_load_config.conf')
        # write some throttling values to the config file
        with open(conf_file, 'w') as fp:
            fp.write('[bandwidth_throttling]\n')
            fp.write('on = True\n')
            fp.write('read_limit = 1000\n')
            fp.write('write_limit = 200\n')
        conf = config._Config(conf_file)
        conf_orig = config._Config(conf_file)
        overridden_opts = [('bandwidth_throttling', 'on', False)]
        conf.override_options(overridden_opts)
        self.assertEquals(False, conf.get_throttling())
        self.assertFalse(conf.get_throttling() == conf_orig.get_throttling())
        self.assertEquals(1000, conf.get_throttling_read_limit())
        self.assertEquals(200, conf.get_throttling_write_limit())
        conf.save()
        # load the config in a barebone ConfigParser and check
        conf_1 = ConfigParser()
        conf_1.read(conf_file)
        self.assertThrottlingSection(conf_1, conf_orig, True, 1000, 200)

    def test_load_udf_autosubscribe(self):
        """Test load/set/override of udf_autosubscribe config value."""
        conf_file = os.path.join(self.test_root,
                                 'test_udf_autosubscribe_config.conf')
        # write some throttling values to the config file
        with open(conf_file, 'w') as fp:
            fp.write('[__main__]\n')
            fp.write('log_level = INFO\n')
            fp.write('disable_ssl_verify = True\n')
            fp.write('udf_autosubscribe = True\n')
            fp.write('\n')
            fp.write('[bandwidth_throttling]\n')
            fp.write('on = True\n')
            fp.write('read_limit = 1000\n')
            fp.write('write_limit = 200\n')

        # keep a original around
        conf_orig = config._Config(conf_file)

        # load the config
        conf = config._Config(conf_file)
        self.assertTrue(conf.get_udf_autosubscribe())
        # change it to False
        conf.set_udf_autosubscribe(False)
        self.assertFalse(conf.get_udf_autosubscribe())
        # save, load and check
        conf.save()
        conf_1 = config._Config(conf_file)
        self.assertFalse(conf_1.get_udf_autosubscribe())
        # change it to True
        conf.set_udf_autosubscribe(True)
        self.assertTrue(conf.get_udf_autosubscribe())
        # save, load and check
        conf.save()
        conf_1 = config._Config(conf_file)
        self.assertTrue(conf_1.get_udf_autosubscribe())

        # load the config, check the override of the value
        conf = config._Config(conf_file)
        self.assertTrue(conf.get_udf_autosubscribe())
        overridden_opts = [('__main__', 'udf_autosubscribe', False)]
        conf.override_options(overridden_opts)
        self.assertFalse(conf.get_udf_autosubscribe())
        self.assertNotEqual(conf.get_udf_autosubscribe(),
                            conf_orig.get_udf_autosubscribe())
        conf.save()
        conf_1 = config._Config(conf_file)
        self.assertEquals(True, conf_1.get_udf_autosubscribe())

    def test_load_share_autosubscribe(self):
        """Test load/set/override of share_autosubscribe config value."""
        conf_file = os.path.join(self.test_root,
                                 'test_share_autosubscribe_config.conf')
        # write some throttling values to the config file
        with open(conf_file, 'w') as fp:
            fp.write('[__main__]\n')
            fp.write('share_autosubscribe = True\n')

        # keep a original around
        conf_orig = config._Config(conf_file)

        # load the config
        conf = config._Config(conf_file)
        self.assertTrue(conf.get_share_autosubscribe())
        # change it to False
        conf.set_share_autosubscribe(False)
        self.assertFalse(conf.get_share_autosubscribe())
        # save, load and check
        conf.save()
        conf_1 = config._Config(conf_file)
        self.assertFalse(conf_1.get_share_autosubscribe())
        # change it to True
        conf.set_share_autosubscribe(True)
        self.assertTrue(conf.get_share_autosubscribe())
        # save, load and check
        conf.save()
        conf_1 = config._Config(conf_file)
        self.assertTrue(conf_1.get_share_autosubscribe())

        # load the config, check the override of the value
        conf = config._Config(conf_file)
        self.assertTrue(conf.get_share_autosubscribe())
        overridden_opts = [('__main__', 'share_autosubscribe', False)]
        conf.override_options(overridden_opts)
        self.assertFalse(conf.get_share_autosubscribe())
        self.assertNotEqual(conf.get_share_autosubscribe(),
                            conf_orig.get_share_autosubscribe())
        conf.save()
        conf_1 = config._Config(conf_file)
        self.assertEquals(True, conf_1.get_share_autosubscribe())

    def test_load_autoconnect(self):
        """Test load/set/override of autoconnect config value."""
        conf_file = os.path.join(self.test_root,
                                 'test_autoconnect_config.conf')
        # ensure that autoconnect is True
        with open(conf_file, 'w') as fp:
            fp.write('[__main__]\n')
            fp.write('autoconnect = True\n')

        # keep a original around
        conf_orig = config._Config(conf_file)

        # assert default is correct
        self.assertTrue(conf_orig.get_autoconnect(),
                        'autoconnect is True by default.')

        # load the config
        conf = config._Config(conf_file)
        self.assertTrue(conf.get_autoconnect())

        # change it to False
        conf.set_autoconnect(False)
        self.assertFalse(conf.get_autoconnect())

        # save, load and check
        conf.save()
        conf_1 = config._Config(conf_file)
        self.assertFalse(conf_1.get_autoconnect())
        # change it to True
        conf.set_autoconnect(True)
        self.assertTrue(conf.get_autoconnect())
        # save, load and check
        conf.save()
        conf_1 = config._Config(conf_file)
        self.assertTrue(conf_1.get_autoconnect())

        # load the config, check the override of the value
        conf = config._Config(conf_file)
        self.assertTrue(conf.get_autoconnect())
        overridden_opts = [('__main__', 'autoconnect', False)]
        conf.override_options(overridden_opts)
        self.assertFalse(conf.get_autoconnect())
        self.assertNotEqual(conf.get_autoconnect(),
                            conf_orig.get_autoconnect())
        conf.save()
        conf_1 = config._Config(conf_file)
        self.assertEquals(True, conf_1.get_autoconnect())


class ConfigglueParsersTests(BaseTwistedTestCase):
    """Tests for our custom configglue parsers"""

    def test_throttling_limit_parser(self):
        """Test throttling_limit_parser"""
        good_value = '20480'
        unset_value = '-1'
        bad_value = 'hola'
        invalid_value = None
        zero_value = '0'
        parser = config.throttling_limit_parser
        self.assertEquals(20480, parser(good_value))
        self.assertEquals(None, parser(unset_value))
        self.assertRaises(ValueError, parser, bad_value)
        self.assertRaises(TypeError, parser, invalid_value)
        self.assertEquals(None, parser(zero_value))

    def test_log_level_parser(self):
        """Test log_level_parser"""
        good_value = 'INFO'
        bad_value = 'hola'
        invalid_value = None
        parser = config.log_level_parser
        self.assertEqual(logging.INFO, parser(good_value))
        self.assertEqual(logging.DEBUG, parser(bad_value))
        self.assertEqual(logging.DEBUG, parser(invalid_value))

    def test_home_dir_parser(self):
        """Test home_dir_parser"""
        good_value = '~/hola'
        bad_value = 'hola'
        invalid_value = None
        parser = config.home_dir_parser
        with environ('HOME', '/home/fake'):
            self.assertEquals('/home/fake/hola', parser(good_value))
        self.assertEquals('hola', parser(bad_value))
        self.assertRaises(AttributeError, parser, invalid_value)

    def test_xdg_cache_dir_parser(self):
        """Test xdg_cache_dir_parser"""
        good_value = 'hola'
        bad_value = '/hola'
        invalid_value = None
        parser = config.xdg_cache_dir_parser
        self.assertEquals(os.path.join(xdg_cache_home, 'hola'),
                          parser(good_value))
        self.assertEquals('/hola', parser(bad_value))
        self.assertRaises(AttributeError, parser, invalid_value)

    def test_xdg_data_dir_parser(self):
        """Test xdg_data_dir_parser"""
        good_value = 'hola'
        bad_value = '/hola'
        invalid_value = None
        parser = config.xdg_data_dir_parser
        self.assertEquals(os.path.join(xdg_data_home, 'hola'),
                          parser(good_value))
        self.assertEquals('/hola', parser(bad_value))
        self.assertRaises(AttributeError, parser, invalid_value)


class SyncDaemonConfigParserTests(BaseTwistedTestCase):
    """Tests for SyncDaemonConfigParser"""

    def setUp(self):
        BaseTwistedTestCase.setUp(self)
        self.test_root = self.mktemp()
        self.default_config = os.path.join(os.environ['ROOTDIR'], 'data',
                                           'syncdaemon.conf')
        self.logging_config = os.path.join(os.environ['ROOTDIR'], 'data',
                                           'logging.conf')
        self.cp = config.SyncDaemonConfigParser()
        self.cp.readfp(file(self.default_config))
        self.cp.readfp(file(self.logging_config))

    def test_log_level_old_config(self):
        """Test log_level upgrade hook."""
        conf_file = os.path.join(self.test_root, 'test_old_config.conf')
        # write some throttling values to the config file
        with open(conf_file, 'w') as fp:
            fp.write('[__main__]\n')
            fp.write('log_level = DEBUG\n')
        self.assertTrue(os.path.exists(conf_file))
        self.cp.read([conf_file])
        self.cp.parse_all()
        self.assertEquals(self.cp.get('logging', 'level').value, 10)

    def test_log_level_new_config(self):
        """Test log_level upgrade hook with new config"""
        conf_file = os.path.join(self.test_root, 'test_new_config.conf')
        # write some throttling values to the config file
        with open(conf_file, 'w') as fp:
            fp.write('[logging]\n')
            fp.write('level = DEBUG\n')
        self.assertTrue(os.path.exists(conf_file))
        self.cp.read([conf_file])
        self.cp.parse_all()
        self.assertEquals(self.cp.get('logging', 'level').value, 10)

    def test_log_level_old_and_new_config(self):
        """Test log_level upgrade hook with a mixed config."""
        conf_file = os.path.join(self.test_root,
                                 'test_old_and_new_config.conf')
        # write some throttling values to the config file (not default ones)
        with open(conf_file, 'w') as fp:
            fp.write('[__main__]\n')
            fp.write('log_level = NOTE\n')
            fp.write('[logging]\n')
            fp.write('level = ERROR\n')
        self.assertTrue(os.path.exists(conf_file))
        self.cp.read([conf_file])
        self.cp.parse_all()
        self.assertEquals(self.cp.get('logging', 'level').value, logging.ERROR)

    def test_old_default_config(self):
        """Test log_level upgrade hook with an old default config"""
        self.cp.read(config.get_config_files()[0])
        # fake an old config
        value = self.cp.get('logging', 'level.default')
        help = self.cp.get('logging', 'level.help')
        parser = self.cp.get('logging', 'level.parser')
        self.cp.set('__main__', 'log_level.default', value)
        self.cp.set('__main__', 'log_level.help', help)
        self.cp.set('__main__', 'log_level.parser', parser)
        self.cp.remove_option('logging', 'level.default')
        self.cp.remove_option('logging', 'level.help')
        self.cp.remove_option('logging', 'level.parser')
        # parse it
        self.cp.parse_all()
        new_value = self.cp.get('logging', 'level')
        self.assertEquals(new_value.value, new_value.parser(value))

    def test_add_upgrade_hook(self):
        """Test add_upgrade_hook method"""
        self.cp.add_upgrade_hook('foo', 'bar', lambda x: None)
        self.assertIn(('foo', 'bar'), self.cp.upgrade_hooks)
        # try to add the same upgrade_hook
        self.assertRaises(ValueError, self.cp.add_upgrade_hook, 'foo', 'bar',
                          lambda x: None)

    def test_ignore_one(self):
        """Test ignore files config, one regex."""
        conf_file = os.path.join(self.test_root, 'test_new_config.conf')
        with open(conf_file, 'w') as fp:
            fp.write('[__main__]\n')
            fp.write('ignore = .*\\.pyc\n')  # all .pyc files
        self.assertTrue(os.path.exists(conf_file))
        self.cp.read([conf_file])
        self.cp.parse_all()
        self.assertEquals(self.cp.get('__main__', 'ignore').value,
                          [r'.*\.pyc'])

    def test_ignore_two(self):
        """Test ignore files config, two regexes."""
        conf_file = os.path.join(self.test_root, 'test_new_config.conf')
        with open(conf_file, 'w') as fp:
            fp.write('[__main__]\n')
            fp.write('ignore = .*\\.pyc\n')  # all .pyc files
            fp.write('         .*\\.sw[opnx]\n')  # all gvim temp files
        self.assertTrue(os.path.exists(conf_file))
        self.cp.read([conf_file])
        self.cp.parse_all()
        self.assertEquals(self.cp.get('__main__', 'ignore').value,
                          ['.*\\.pyc', '.*\\.sw[opnx]'])