aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot-0.8.8-py2.7.egg/buildbot/test/unit/test_scripts_runner.py
blob: 0da2c7d0e3ff9447e0c0b6a64b20294cc15ce89f (plain)
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
# This file is part of Buildbot.  Buildbot 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, version 2.
#
# 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 along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members

from __future__ import with_statement

import os
import sys
import getpass
import mock
import cStringIO
from twisted.trial import unittest
from twisted.python import usage, runtime, log
from buildbot.scripts import base, runner
from buildbot.test.util import misc

class OptionsMixin(object):

    def setUpOptions(self):
        self.options_file = {}
        self.patch(base.SubcommandOptions, 'loadOptionsFile',
                lambda other_self : self.options_file)

    def assertOptions(self, opts, exp):
        got = dict([(k, opts[k]) for k in exp])
        if got != exp:
            msg = []
            for k in exp:
                if opts[k] != exp[k]:
                    msg.append(" %s: expected %r, got %r" %
                               (k, exp[k], opts[k]))
            self.fail("did not get expected options\n" + ("\n".join(msg)))


class TestUpgradeMasterOptions(OptionsMixin, unittest.TestCase):

    def setUp(self):
        self.setUpOptions()

    def parse(self, *args):
        self.opts = runner.UpgradeMasterOptions()
        self.opts.parseOptions(args)
        return self.opts

    def test_synopsis(self):
        opts = runner.UpgradeMasterOptions()
        self.assertIn('buildbot upgrade-master', opts.getSynopsis())

    def test_defaults(self):
        opts = self.parse()
        exp = dict(quiet=False, replace=False)
        self.assertOptions(opts, exp)

    def test_short(self):
        opts = self.parse('-q', '-r')
        exp = dict(quiet=True, replace=True)
        self.assertOptions(opts, exp)

    def test_long(self):
        opts = self.parse('--quiet', '--replace')
        exp = dict(quiet=True, replace=True)
        self.assertOptions(opts, exp)


class TestCreateMasterOptions(OptionsMixin, unittest.TestCase):

    def setUp(self):
        self.setUpOptions()

    def parse(self, *args):
        self.opts = runner.CreateMasterOptions()
        self.opts.parseOptions(args)
        return self.opts

    def defaults_and(self, **kwargs):
        defaults = dict(force=False, relocatable=False, config='master.cfg',
                db='sqlite:///state.sqlite', basedir=os.getcwd(), quiet=False,
                **{'no-logrotate':False, 'log-size':'10000000',
                   'log-count':'10'})
        unk_keys = set(kwargs.keys()) - set(defaults.keys())
        assert not unk_keys, "invalid keys %s" % (unk_keys,)
        opts = defaults.copy()
        opts.update(kwargs)
        return opts

    def test_synopsis(self):
        opts = runner.CreateMasterOptions()
        self.assertIn('buildbot create-master', opts.getSynopsis())

    def test_defaults(self):
        opts = self.parse()
        exp = self.defaults_and()
        self.assertOptions(opts, exp)

    def test_db_quiet(self):
        opts = self.parse('-q')
        exp = self.defaults_and(quiet=True)
        self.assertOptions(opts, exp)

    def test_db_quiet_long(self):
        opts = self.parse('--quiet')
        exp = self.defaults_and(quiet=True)
        self.assertOptions(opts, exp)

    def test_force(self):
        opts = self.parse('-f')
        exp = self.defaults_and(force=True)
        self.assertOptions(opts, exp)

    def test_force_long(self):
        opts = self.parse('--force')
        exp = self.defaults_and(force=True)
        self.assertOptions(opts, exp)

    def test_relocatable(self):
        opts = self.parse('-r')
        exp = self.defaults_and(relocatable=True)
        self.assertOptions(opts, exp)

    def test_relocatable_long(self):
        opts = self.parse('--relocatable')
        exp = self.defaults_and(relocatable=True)
        self.assertOptions(opts, exp)

    def test_no_logrotate(self):
        opts = self.parse('-n')
        exp = self.defaults_and(**{'no-logrotate' : True})
        self.assertOptions(opts, exp)

    def test_no_logrotate_long(self):
        opts = self.parse('--no-logrotate')
        exp = self.defaults_and(**{'no-logrotate' : True})
        self.assertOptions(opts, exp)

    def test_config(self):
        opts = self.parse('-cxyz')
        exp = self.defaults_and(config='xyz')
        self.assertOptions(opts, exp)

    def test_config_long(self):
        opts = self.parse('--config=xyz')
        exp = self.defaults_and(config='xyz')
        self.assertOptions(opts, exp)

    def test_log_size(self):
        opts = self.parse('-s124')
        exp = self.defaults_and(**{'log-size':'124'})
        self.assertOptions(opts, exp)

    def test_log_size_long(self):
        opts = self.parse('--log-size=124')
        exp = self.defaults_and(**{'log-size':'124'})
        self.assertOptions(opts, exp)

    def test_log_size_noninteger(self):
        self.assertRaises(usage.UsageError,
            lambda :self.parse('--log-size=1M'))

    def test_log_count(self):
        opts = self.parse('-l124')
        exp = self.defaults_and(**{'log-count':'124'})
        self.assertOptions(opts, exp)

    def test_log_count_long(self):
        opts = self.parse('--log-count=124')
        exp = self.defaults_and(**{'log-count':'124'})
        self.assertOptions(opts, exp)

    def test_log_count_noninteger(self):
        self.assertRaises(usage.UsageError,
            lambda :self.parse('--log-count=M'))

    def test_db_long(self):
        opts = self.parse('--db=foo://bar')
        exp = self.defaults_and(db='foo://bar')
        self.assertOptions(opts, exp)

    def test_db_basedir(self):
        path = r'c:\foo\bar' if runtime.platformType == "win32" else '/foo/bar'
        opts = self.parse('-f', path)
        exp = self.defaults_and(force=True, basedir=path)
        self.assertOptions(opts, exp)


class BaseTestSimpleOptions(OptionsMixin):
    # tests for options with just --quiet and a usage message

    commandName = None
    optionsClass = None

    def setUp(self):
        self.setUpOptions()

    def parse(self, *args):
        self.opts = self.optionsClass()
        self.opts.parseOptions(args)
        return self.opts

    def test_synopsis(self):
        opts = self.optionsClass()
        self.assertIn('buildbot %s' % self.commandName, opts.getSynopsis())

    def test_defaults(self):
        opts = self.parse()
        exp = dict(quiet=False)
        self.assertOptions(opts, exp)

    def test_quiet(self):
        opts = self.parse('--quiet')
        exp = dict(quiet=True)
        self.assertOptions(opts, exp)


class TestStopOptions(BaseTestSimpleOptions, unittest.TestCase):
    commandName = 'stop'
    optionsClass = runner.StopOptions


class TestResetartOptions(BaseTestSimpleOptions, unittest.TestCase):
    commandName = 'restart'
    optionsClass = runner.RestartOptions

    def test_nodaemon(self):
        opts = self.parse('--nodaemon')
        exp = dict(nodaemon=True)
        self.assertOptions(opts, exp)


class TestStartOptions(BaseTestSimpleOptions, unittest.TestCase):
    commandName = 'start'
    optionsClass = runner.StartOptions

    def test_nodaemon(self):
        opts = self.parse('--nodaemon')
        exp = dict(nodaemon=True)
        self.assertOptions(opts, exp)


class TestReconfigOptions(BaseTestSimpleOptions, unittest.TestCase):
    commandName = 'reconfig'
    optionsClass = runner.ReconfigOptions


class TestDebugClientOptions(OptionsMixin, unittest.TestCase):

    def setUp(self):
        self.setUpOptions()

    def parse(self, *args):
        self.opts = runner.DebugClientOptions()
        self.opts.parseOptions(args)
        return self.opts

    def test_synopsis(self):
        opts = runner.DebugClientOptions()
        self.assertIn('buildbot debugclient', opts.getSynopsis())

    def test_defaults(self):
        self.assertRaises(usage.UsageError,
                lambda : self.parse())

    def test_args_missing_passwd(self):
        self.assertRaises(usage.UsageError,
                lambda : self.parse('-m', 'mm'))

    def test_options_long(self):
        opts = self.parse('--master', 'mm:9989', '--passwd', 'pp')
        exp = dict(master='mm:9989', passwd='pp')
        self.assertOptions(opts, exp)

    def test_positional_master_passwd(self):
        opts = self.parse('foo:9989', 'pass')
        exp = dict(master='foo:9989', passwd='pass')
        self.assertOptions(opts, exp)

    def test_positional_master(self):
        opts = self.parse('-p', 'pass', 'foo:9989')
        exp = dict(master='foo:9989', passwd='pass')
        self.assertOptions(opts, exp)

    def test_args_master_passwd(self):
        opts = self.parse('foo:9989', 'pass')
        exp = dict(master='foo:9989', passwd='pass')
        self.assertOptions(opts, exp)

    def test_missing_both(self):
        self.assertRaises(usage.UsageError,
            lambda :self.parse())

    def test_missing_passwd(self):
        self.assertRaises(usage.UsageError,
            lambda :self.parse('master'))

    def test_missing_master(self):
        self.assertRaises(usage.UsageError,
            lambda :self.parse('-p', 'pass'))

    def test_invalid_master(self):
        self.assertRaises(usage.UsageError, self.parse,
                          "-m", "foo", "-p", "pass")

    def test_options_extra_positional(self):
        self.assertRaises(usage.UsageError,
                lambda : self.parse('mm', 'pp', '??'))

    def test_options_master(self):
        self.options_file['master'] = 'opt:9989'
        opts = self.parse('-p', 'pass')
        exp = dict(master='opt:9989', passwd='pass')
        self.assertOptions(opts, exp)

    def test_options_debugMaster(self):
        self.options_file['master'] = 'not seen'
        self.options_file['debugMaster'] = 'opt:9989'
        opts = self.parse('-p', 'pass')
        exp = dict(master='opt:9989', passwd='pass')
        self.assertOptions(opts, exp)


class TestBaseStatusClientOptions(OptionsMixin, unittest.TestCase):

    def setUp(self):
        self.setUpOptions()

    def parse(self, *args):
        self.opts = runner.BaseStatusClientOptions()
        self.opts.parseOptions(args)
        return self.opts

    def test_defaults(self):
        opts = self.parse('--master', 'm:20')
        exp = dict(master='m:20', username='statusClient', passwd='clientpw')
        self.assertOptions(opts, exp)

    def test_short(self):
        opts = self.parse('-m', 'm:20', '-u', 'u', '-p', 'p')
        exp = dict(master='m:20', username='u', passwd='p')
        self.assertOptions(opts, exp)

    def test_long(self):
        opts = self.parse('--master', 'm:20',
                          '--username', 'u', '--passwd', 'p')
        exp = dict(master='m:20', username='u', passwd='p')
        self.assertOptions(opts, exp)

    def test_positional_master(self):
        opts = self.parse('--username', 'u', '--passwd', 'p', 'm:20')
        exp = dict(master='m:20', username='u', passwd='p')
        self.assertOptions(opts, exp)

    def test_positional_extra(self):
        self.assertRaises(usage.UsageError,
            lambda : self.parse('--username', 'u', '--passwd', 'p', 'm', '2'))

    def test_missing_master(self):
        self.assertRaises(usage.UsageError,
            lambda : self.parse('--username', 'u', '--passwd', 'p'))

    def test_invalid_master(self):
        self.assertRaises(usage.UsageError, self.parse, "-m foo")

    def test_options_masterstatus(self):
        self.options_file['master'] = 'not_seen:2'
        self.options_file['masterstatus'] = 'opt:3'
        opts = self.parse('-p', 'pass', '-u', 'user')
        exp = dict(master='opt:3', username='user', passwd='pass')
        self.assertOptions(opts, exp)


class TestStatusLogOptions(unittest.TestCase):

    def test_synopsis(self):
        opts = runner.StatusLogOptions()
        self.assertIn('buildbot statuslog', opts.getSynopsis())


class TestStatusGuiOptions(unittest.TestCase):

    def test_synopsis(self):
        opts = runner.StatusGuiOptions()
        self.assertIn('buildbot statusgui', opts.getSynopsis())


class TestTryOptions(OptionsMixin, unittest.TestCase):

    def setUp(self):
        self.setUpOptions()

    def parse(self, *args):
        self.opts = runner.TryOptions()
        self.opts.parseOptions(args)
        return self.opts

    def defaults_and(self, **kwargs):
        defaults = dict(connect=None, host=None, jobdir=None, username=None,
                master=None, passwd=None, who=None, comment=None, diff=None,
                patchlevel=0, baserev=None, vc=None, branch=None,
                repository=None, topfile=None, topdir=None, wait=False,
                dryrun=False, quiet=False, builders=[], properties={},
                buildbotbin='buildbot')
        # dashes make python syntax hard..
        defaults['get-builder-names'] = False
        if 'get_builder_names' in kwargs:
            kwargs['get-builder-names'] = kwargs['get_builder_names']
            del kwargs['get_builder_names']
        assert set(kwargs.keys()) <= set(defaults.keys()), "invalid keys"
        opts = defaults.copy()
        opts.update(kwargs)
        return opts

    def test_synopsis(self):
        opts = runner.TryOptions()
        self.assertIn('buildbot try', opts.getSynopsis())

    def test_defaults(self):
        opts = self.parse()
        exp = self.defaults_and()
        self.assertOptions(opts, exp)

    def test_properties(self):
        opts = self.parse('--properties=a=b')
        exp = self.defaults_and(properties=dict(a='b'))
        self.assertOptions(opts, exp)

    def test_properties_multiple_opts(self):
        opts = self.parse('--properties=X=1', '--properties=Y=2')
        exp = self.defaults_and(properties=dict(X='1', Y='2'))
        self.assertOptions(opts, exp)

    def test_properties_equals(self):
        opts = self.parse('--properties=X=2+2=4')
        exp = self.defaults_and(properties=dict(X='2+2=4'))
        self.assertOptions(opts, exp)

    def test_properties_commas(self):
        opts = self.parse('--properties=a=b,c=d')
        exp = self.defaults_and(properties=dict(a='b', c='d'))
        self.assertOptions(opts, exp)

    def test_property(self):
        opts = self.parse('--property=a=b')
        exp = self.defaults_and(properties=dict(a='b'))
        self.assertOptions(opts, exp)

    def test_property_multiple_opts(self):
        opts = self.parse('--property=X=1', '--property=Y=2')
        exp = self.defaults_and(properties=dict(X='1', Y='2'))
        self.assertOptions(opts, exp)

    def test_property_equals(self):
        opts = self.parse('--property=X=2+2=4')
        exp = self.defaults_and(properties=dict(X='2+2=4'))
        self.assertOptions(opts, exp)

    def test_property_commas(self):
        opts = self.parse('--property=a=b,c=d')
        exp = self.defaults_and(properties=dict(a='b,c=d'))
        self.assertOptions(opts, exp)

    def test_property_and_properties(self):
        opts = self.parse('--property=X=1', '--properties=Y=2')
        exp = self.defaults_and(properties=dict(X='1', Y='2'))
        self.assertOptions(opts, exp)

    def test_properties_builders_multiple(self):
        opts = self.parse('--builder=aa', '--builder=bb')
        exp = self.defaults_and(builders=['aa', 'bb'])
        self.assertOptions(opts, exp)

    def test_options_short(self):
        opts = self.parse(
                *'-n -q -c pb -u me -m mr:7 -w you -C comm -p 2 -b bb'.split())
        exp = self.defaults_and(dryrun=True, quiet=True, connect='pb',
                username='me', master='mr:7', who='you', comment='comm',
                patchlevel=2, builders=['bb'])
        self.assertOptions(opts, exp)

    def test_options_long(self):
        opts = self.parse(
                *"""--wait --dryrun --get-builder-names --quiet --connect=pb
                --host=h --jobdir=j --username=u --master=m:1234 --passwd=p
                --who=w --comment=comm --diff=d --patchlevel=7 --baserev=br
                --vc=cvs --branch=br --repository=rep --builder=bl
                --properties=a=b --topfile=Makefile --topdir=.
                --buildbotbin=.virtualenvs/buildbot/bin/buildbot""".split())
        exp = self.defaults_and(wait=True, dryrun=True, get_builder_names=True,
                quiet=True, connect='pb', host='h', jobdir='j', username='u',
                master='m:1234', passwd='p', who='w', comment='comm', diff='d',
                patchlevel=7, baserev='br', vc='cvs', branch='br',
                repository='rep', builders=['bl'], properties=dict(a='b'),
                topfile='Makefile', topdir='.',
                buildbotbin='.virtualenvs/buildbot/bin/buildbot')
        self.assertOptions(opts, exp)

    def test_patchlevel_inval(self):
        self.assertRaises(ValueError, lambda:
                self.parse('-p', 'a'))

    def test_config_builders(self):
        self.options_file['try_builders'] = ['a', 'b']
        opts = self.parse()
        self.assertOptions(opts, dict(builders=['a', 'b']))

    def test_config_builders_override(self):
        self.options_file['try_builders'] = ['a', 'b']
        opts = self.parse('-b', 'd') # overrides a, b
        self.assertOptions(opts, dict(builders=['d']))

    def test_config_old_names(self):
        self.options_file['try_masterstatus'] = 'ms'
        self.options_file['try_dir'] = 'td'
        self.options_file['try_password'] = 'pw'
        opts = self.parse()
        self.assertOptions(opts, dict(master='ms', jobdir='td', passwd='pw'))

    def test_config_masterstatus(self):
        self.options_file['masterstatus'] = 'ms'
        opts = self.parse()
        self.assertOptions(opts, dict(master='ms'))

    def test_config_masterstatus_override(self):
        self.options_file['masterstatus'] = 'ms'
        opts = self.parse('-m', 'mm')
        self.assertOptions(opts, dict(master='mm'))

    def test_config_options(self):
        self.options_file.update(dict(try_connect='pb', try_vc='cvs',
            try_branch='br', try_repository='rep', try_topdir='.',
            try_topfile='Makefile', try_host='h', try_username='u',
            try_jobdir='j', try_password='p', try_master='m:8', try_who='w',
            try_comment='comm', try_quiet='y', try_wait='y',
            try_buildbotbin='.virtualenvs/buildbot/bin/buildbot'))
        opts = self.parse()
        exp = self.defaults_and(wait=True, quiet=True, connect='pb', host='h',
                jobdir='j', username='u', master='m:8', passwd='p', who='w',
                comment='comm', vc='cvs', branch='br', repository='rep',
                topfile='Makefile', topdir='.',
                buildbotbin='.virtualenvs/buildbot/bin/buildbot')
        self.assertOptions(opts, exp)

    def test_pb_withNoMaster(self):
        """
        When 'builbot try' is asked to connect via pb, but no master is
        specified, a usage error is raised.
        """
        self.assertRaises(usage.UsageError, self.parse, '--connect=pb')

    def test_pb_withInvalidMaster(self):
        """
        When 'buildbot try' is asked to conncect via pb, but an invalid
        master is specified, a usage error is raised.
        """
        self.assertRaises(usage.UsageError, self.parse,
                          '--connect=pb', '--master=foo')

class TestSendChangeOptions(OptionsMixin, unittest.TestCase):

    master_and_who = ['-m', 'm:1', '-W', 'w']

    def setUp(self):
        self.setUpOptions()
        self.getpass_response = 'typed-password'
        self.patch(getpass, 'getpass', lambda prompt : self.getpass_response)

    def parse(self, *args):
        self.opts = runner.SendChangeOptions()
        self.opts.parseOptions(args)
        return self.opts

    def test_synopsis(self):
        opts = runner.SendChangeOptions()
        self.assertIn('buildbot sendchange', opts.getSynopsis())

    def test_defaults(self):
        opts = self.parse('-m', 'm:1', '-W', 'me')
        exp = dict(master='m:1', auth=('change', 'changepw'), who='me',
                vc=None, repository='', project='', branch=None, category=None,
                revision=None, revision_file=None, property=None,
                comments='', logfile=None, when=None, revlink='',
                encoding='utf8', files=())
        self.assertOptions(opts, exp)

    def test_files(self):
        opts = self.parse(*self.master_and_who + ['a', 'b', 'c'])
        self.assertEqual(opts['files'], ('a', 'b', 'c'))

    def test_properties(self):
        opts = self.parse('--property', 'x:y', '--property', 'a:b',
                                *self.master_and_who)
        self.assertEqual(opts['properties'], dict(x="y", a="b"))

    def test_properties_with_colon(self):
        opts = self.parse('--property', 'x:http://foo', *self.master_and_who)
        self.assertEquals(opts['properties'], dict(x='http://foo'))

    def test_config_file(self):
        self.options_file['master'] = 'MMM:123'
        self.options_file['who'] = 'WWW'
        self.options_file['branch'] = 'BBB'
        self.options_file['category'] = 'CCC'
        self.options_file['vc'] = 'svn'
        opts = self.parse()
        exp = dict(master='MMM:123', who='WWW',
                branch='BBB', category='CCC', vc='svn')
        self.assertOptions(opts, exp)

    def test_short_args(self):
        opts = self.parse(*('-m m:1 -a a:b -W W -R r -P p -b b -s git ' +
            '-C c -r r -p pn:pv -c c -F f -w 123 -l l -e e').split())
        exp = dict(master='m:1', auth=('a','b'), who='W', repository='r',
                project='p', branch='b', category='c', revision='r', vc='git',
                properties=dict(pn='pv'), comments='c', logfile='f',
                when=123.0, revlink='l', encoding='e')
        self.assertOptions(opts, exp)

    def test_long_args(self):
        opts = self.parse(*('--master m:1 --auth a:b --who w --repository r ' +
            '--project p --branch b --category c --revision r --vc git ' +
            '--property pn:pv --comments c --logfile f ' +
            '--when 123 --revlink l --encoding e').split())
        exp = dict(master='m:1', auth=('a', 'b'), who='w', repository='r',
                project='p', branch='b', category='c', revision='r', vc='git',
                properties=dict(pn='pv'), comments='c', logfile='f',
                when=123.0, revlink='l', encoding='e')
        self.assertOptions(opts, exp)

    def test_revision_file(self):
        with open('revfile', 'wt') as f:
            f.write('my-rev')
        self.addCleanup(lambda : os.unlink('revfile'))
        opts = self.parse('--revision_file', 'revfile', *self.master_and_who)
        self.assertOptions(opts, dict(revision='my-rev'))

    def test_invalid_when(self):
        self.assertRaises(usage.UsageError,
            lambda : self.parse('--when=foo', *self.master_and_who))

    def test_comments_overrides_logfile(self):
        opts = self.parse('--logfile', 'logs', '--comments', 'foo',
                                *self.master_and_who)
        self.assertOptions(opts, dict(comments='foo'))

    def test_logfile(self):
        with open('comments', 'wt') as f:
            f.write('hi')
        self.addCleanup(lambda : os.unlink('comments'))
        opts = self.parse('--logfile', 'comments', *self.master_and_who)
        self.assertOptions(opts, dict(comments='hi'))

    def test_logfile_stdin(self):
        stdin = mock.Mock()
        stdin.read = lambda : 'hi'
        self.patch(sys, 'stdin', stdin)
        opts = self.parse('--logfile', '-', *self.master_and_who)
        self.assertOptions(opts, dict(comments='hi'))

    def test_auth_getpass(self):
        opts = self.parse('--auth=dustin', *self.master_and_who)
        self.assertOptions(opts, dict(auth=('dustin', 'typed-password')))

    def test_invalid_vcs(self):
        self.assertRaises(usage.UsageError,
            lambda : self.parse('--vc=foo', *self.master_and_who))

    def test_invalid_master(self):
        self.assertRaises(usage.UsageError,
                          self.parse,
                          "--who=test", "-m foo")


class TestTryServerOptions(OptionsMixin, unittest.TestCase):

    def setUp(self):
        self.setUpOptions()

    def parse(self, *args):
        self.opts = runner.TryServerOptions()
        self.opts.parseOptions(args)
        return self.opts

    def test_synopsis(self):
        opts = runner.TryServerOptions()
        self.assertIn('buildbot tryserver', opts.getSynopsis())

    def test_defaults(self):
        self.assertRaises(usage.UsageError,
                lambda : self.parse())

    def test_with_jobdir(self):
        opts = self.parse('--jobdir', 'xyz')
        exp = dict(jobdir='xyz')
        self.assertOptions(opts, exp)


class TestCheckConfigOptions(OptionsMixin, unittest.TestCase):

    def setUp(self):
        self.setUpOptions()

    def parse(self, *args):
        self.opts = runner.CheckConfigOptions()
        self.opts.parseOptions(args)
        return self.opts

    def test_synopsis(self):
        opts = runner.CheckConfigOptions()
        self.assertIn('buildbot checkconfig', opts.getSynopsis())

    def test_defaults(self):
        opts = self.parse()
        exp = dict(quiet=False, configFile='master.cfg')
        self.assertOptions(opts, exp)

    def test_configfile(self):
        opts = self.parse('foo.cfg')
        exp = dict(quiet=False, configFile='foo.cfg')
        self.assertOptions(opts, exp)

    def test_quiet(self):
        opts = self.parse('-q')
        exp = dict(quiet=True, configFile='master.cfg')
        self.assertOptions(opts, exp)


class TestUserOptions(OptionsMixin, unittest.TestCase):

    # mandatory arguments
    extra_args = [ '--master', 'a:1',
                   '--username', 'u', '--passwd', 'p' ]

    def setUp(self):
        self.setUpOptions()

    def parse(self, *args):
        self.opts = runner.UserOptions()
        self.opts.parseOptions(args)
        return self.opts

    def test_defaults(self):
        self.assertRaises(usage.UsageError,
                lambda : self.parse())

    def test_synopsis(self):
        opts = runner.UserOptions()
        self.assertIn('buildbot user', opts.getSynopsis())

    def test_master(self):
        opts = self.parse("--master", "abcd:1234",
                          '--op=get', '--ids=x', '--username=u', '--passwd=p')
        self.assertOptions(opts, dict(master="abcd:1234"))

    def test_ids(self):
        opts = self.parse("--ids", "id1,id2,id3",
                        '--op', 'get', *self.extra_args)
        self.assertEqual(opts['ids'], ['id1', 'id2', 'id3'])

    def test_info(self):
        opts = self.parse("--info", "git=Tyler Durden <tyler@mayhem.net>",
                          '--op', 'add', *self.extra_args)
        self.assertEqual(opts['info'],
                         [dict(git='Tyler Durden <tyler@mayhem.net>')])

    def test_info_only_id(self):
        opts = self.parse("--info", "tdurden",
                          '--op', 'update', *self.extra_args)
        self.assertEqual(opts['info'], [dict(identifier='tdurden')])

    def test_info_with_id(self):
        opts = self.parse("--info", "tdurden:svn=marla",
                          '--op', 'update', *self.extra_args)
        self.assertEqual(opts['info'], [dict(identifier='tdurden', svn='marla')])

    def test_info_multiple(self):
        opts = self.parse("--info", "git=Tyler Durden <tyler@mayhem.net>",
                          "--info", "git=Narrator <narrator@mayhem.net>",
                          '--op', 'add', *self.extra_args)
        self.assertEqual(opts['info'],
                         [dict(git='Tyler Durden <tyler@mayhem.net>'),
                          dict(git='Narrator <narrator@mayhem.net>')])

    def test_config_user_params(self):
        self.options_file['user_master'] = 'mm:99'
        self.options_file['user_username'] = 'un'
        self.options_file['user_passwd'] = 'pw'
        opts = self.parse('--op', 'get', '--ids', 'x')
        self.assertOptions(opts, dict(master='mm:99', username='un', passwd='pw'))

    def test_config_master(self):
        self.options_file['master'] = 'mm:99'
        opts = self.parse('--op', 'get', '--ids', 'x',
                          '--username=u', '--passwd=p')
        self.assertOptions(opts, dict(master='mm:99'))

    def test_config_master_override(self):
        self.options_file['master'] = 'not seen'
        self.options_file['user_master'] = 'mm:99'
        opts = self.parse('--op', 'get', '--ids', 'x',
                        '--username=u', '--passwd=p')
        self.assertOptions(opts, dict(master='mm:99'))

    def test_invalid_info(self):
        self.assertRaises(usage.UsageError,
                lambda : self.parse("--info", "foo=bar",
                          '--op', 'add', *self.extra_args))

    def test_no_master(self):
        self.assertRaises(usage.UsageError,
            lambda : self.parse('-op=foo'))

    def test_invalid_master(self):
        self.assertRaises(usage.UsageError, self.parse,'-m', 'foo')

    def test_no_operation(self):
        self.assertRaises(usage.UsageError, self.parse, '-m', 'a:1')

    def test_bad_operation(self):
        self.assertRaises(usage.UsageError,
                          self.parse,
                          '-m', 'a:1', '--op=mayhem')

    def test_no_username(self):
        self.assertRaises(usage.UsageError,
                          self.parse,
                          '-m', 'a:1', '--op=add')

    def test_no_password(self):
        self.assertRaises(usage.UsageError,
                          self.parse,
                          '--op=add', '-m', 'a:1', '-u', 'tdurden')

    def test_invalid_bb_username(self):
        self.assertRaises(usage.UsageError,
                          self.parse,
                          '--op=add', '--bb_username=tdurden',
                          *self.extra_args)

    def test_invalid_bb_password(self):
        self.assertRaises(usage.UsageError,
                          self.parse,
                          '--op=add', '--bb_password=marla',
                          *self.extra_args)

    def test_update_no_bb_username(self):
        self.assertRaises(usage.UsageError,
                          self.parse,
                          '--op=update', '--bb_password=marla',
                          *self.extra_args)

    def test_update_no_bb_password(self):
        self.assertRaises(usage.UsageError,
                          self.parse,
                          '--op=update', '--bb_username=tdurden',
                          *self.extra_args)

    def test_no_ids_info(self):
        self.assertRaises(usage.UsageError,
                          self.parse,
                          '--op=add', *self.extra_args)

    def test_ids_with_add(self):
        self.assertRaises(usage.UsageError,
                          self.parse,
                          '--op=add', '--ids=id1', *self.extra_args)

    def test_ids_with_update(self):
        self.assertRaises(usage.UsageError,
                          self.parse,
                          '--op=update', '--ids=id1', *self.extra_args)

    def test_no_ids_found_update(self):
        self.assertRaises(usage.UsageError,
                          self.parse,
                          "--op=update", "--info=svn=x", *self.extra_args)

    def test_id_with_add(self):
        self.assertRaises(usage.UsageError,
                          self.parse,
                          "--op=add", "--info=id:x", *self.extra_args)

    def test_info_with_remove(self):
        self.assertRaises(usage.UsageError,
                          self.parse,
                          '--op=remove', '--info=x=v', *self.extra_args)


    def test_info_with_get(self):
        self.assertRaises(usage.UsageError,
                          self.parse,
                          '--op=get', '--info=x=v', *self.extra_args)


class TestOptions(OptionsMixin, misc.StdoutAssertionsMixin, unittest.TestCase):

    def setUp(self):
        self.setUpOptions()
        self.setUpStdoutAssertions()

    def parse(self, *args):
        self.opts = runner.Options()
        self.opts.parseOptions(args)
        return self.opts

    def test_defaults(self):
        self.assertRaises(usage.UsageError,
            lambda : self.parse())

    def test_version(self):
        try:
            self.parse('--version')
        except SystemExit, e:
            self.assertEqual(e.args[0], 0)
        self.assertInStdout('Buildbot version:')

    def test_verbose(self):
        self.patch(log, 'startLogging', mock.Mock())
        self.assertRaises(usage.UsageError, self.parse, "--verbose")
        log.startLogging.assert_called_once_with(sys.stderr)


class TestRun(unittest.TestCase):

    class MySubCommand(usage.Options):
        subcommandFunction = 'buildbot.test.unit.test_scripts_runner.subcommandFunction'
        optFlags = [
            [ 'loud', 'l', 'be noisy' ]
        ]
        def postOptions(self):
            if self['loud']:
                raise usage.UsageError('THIS IS ME BEING LOUD')

    def setUp(self):
        # patch our subcommand in
        self.patch(runner.Options, 'subCommands',
            [ [ 'my', None, self.MySubCommand, 'my, my' ] ])

        # and patch in the callback for it
        global subcommandFunction
        subcommandFunction = mock.Mock(name='subcommandFunction',
                return_value=3)

    def test_run_good(self):
        self.patch(sys, 'argv', [ 'buildbot', 'my' ])
        try:
            runner.run()
        except SystemExit, e:
            self.assertEqual(e.args[0], 3)
        else:
            self.fail("didn't exit")

    def test_run_bad(self):
        self.patch(sys, 'argv', [ 'buildbot', 'my', '-l' ])
        stdout = cStringIO.StringIO()
        self.patch(sys, 'stdout', stdout)
        try:
            runner.run()
        except SystemExit, e:
            self.assertEqual(e.args[0], 1)
        else:
            self.fail("didn't exit")
        self.assertIn('THIS IS ME', stdout.getvalue())