summaryrefslogtreecommitdiffstats
path: root/utils/test/cli_installer_tests.py
blob: a0fc95c339e736ba502ce86712e077a253c1d32c (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
# TODO: Temporarily disabled due to importing old code into openshift-ansible
# repo. We will work on these over time.
# pylint: disable=bad-continuation,missing-docstring,no-self-use,invalid-name

import copy
import os
import ConfigParser

import ooinstall.cli_installer as cli

from test.fixture import OOCliFixture, SAMPLE_CONFIG, build_input, read_yaml
from mock import patch


MOCK_FACTS = {
    '10.0.0.1': {
        'common': {
            'ip': '10.0.0.1',
            'public_ip': '10.0.0.1',
            'hostname': 'master-private.example.com',
            'public_hostname': 'master.example.com'
        }
    },
    '10.0.0.2': {
        'common': {
            'ip': '10.0.0.2',
            'public_ip': '10.0.0.2',
            'hostname': 'node1-private.example.com',
            'public_hostname': 'node1.example.com'
        }
    },
    '10.0.0.3': {
        'common': {
            'ip': '10.0.0.3',
            'public_ip': '10.0.0.3',
            'hostname': 'node2-private.example.com',
            'public_hostname': 'node2.example.com'
        }
    },
}

MOCK_FACTS_QUICKHA = {
    '10.0.0.1': {
        'common': {
            'ip': '10.0.0.1',
            'public_ip': '10.0.0.1',
            'hostname': 'master-private.example.com',
            'public_hostname': 'master.example.com'
        }
    },
    '10.0.0.2': {
        'common': {
            'ip': '10.0.0.2',
            'public_ip': '10.0.0.2',
            'hostname': 'node1-private.example.com',
            'public_hostname': 'node1.example.com'
        }
    },
    '10.0.0.3': {
        'common': {
            'ip': '10.0.0.3',
            'public_ip': '10.0.0.3',
            'hostname': 'node2-private.example.com',
            'public_hostname': 'node2.example.com'
        }
    },
    '10.0.0.4': {
        'common': {
            'ip': '10.0.0.4',
            'public_ip': '10.0.0.4',
            'hostname': 'proxy-private.example.com',
            'public_hostname': 'proxy.example.com'
        }
    },
}

# Missing connect_to on some hosts:
BAD_CONFIG = """
variant: %s
ansible_ssh_user: root
hosts:
  - connect_to: 10.0.0.1
    ip: 10.0.0.1
    hostname: master-private.example.com
    public_ip: 24.222.0.1
    public_hostname: master.example.com
    master: true
    node: true
  - ip: 10.0.0.2
    hostname: node1-private.example.com
    public_ip: 24.222.0.2
    public_hostname: node1.example.com
    node: true
  - connect_to: 10.0.0.3
    ip: 10.0.0.3
    hostname: node2-private.example.com
    public_ip: 24.222.0.3
    public_hostname: node2.example.com
    node: true
"""

QUICKHA_CONFIG = """
variant: %s
ansible_ssh_user: root
hosts:
  - connect_to: 10.0.0.1
    ip: 10.0.0.1
    hostname: master-private.example.com
    public_ip: 24.222.0.1
    public_hostname: master.example.com
    master: true
    node: true
  - connect_to: 10.0.0.2
    ip: 10.0.0.2
    hostname: node1-private.example.com
    public_ip: 24.222.0.2
    public_hostname: node1.example.com
    master: true
    node: true
  - connect_to: 10.0.0.3
    ip: 10.0.0.3
    hostname: node2-private.example.com
    public_ip: 24.222.0.3
    public_hostname: node2.example.com
    node: true
  - connect_to: 10.0.0.4
    ip: 10.0.0.4
    hostname: proxy-private.example.com
    public_ip: 24.222.0.4
    public_hostname: proxy.example.com
    master_lb: true
"""

QUICKHA_CONFIG_REUSED_LB = """
variant: %s
ansible_ssh_user: root
hosts:
  - connect_to: 10.0.0.1
    ip: 10.0.0.1
    hostname: master-private.example.com
    public_ip: 24.222.0.1
    public_hostname: master.example.com
    master: true
    node: true
  - connect_to: 10.0.0.2
    ip: 10.0.0.2
    hostname: node1-private.example.com
    public_ip: 24.222.0.2
    public_hostname: node1.example.com
    master: true
    node: true
    master_lb: true
  - connect_to: 10.0.0.3
    ip: 10.0.0.3
    hostname: node2-private.example.com
    public_ip: 24.222.0.3
    public_hostname: node2.example.com
    node: true
"""

QUICKHA_CONFIG_NO_LB = """
variant: %s
ansible_ssh_user: root
hosts:
  - connect_to: 10.0.0.1
    ip: 10.0.0.1
    hostname: master-private.example.com
    public_ip: 24.222.0.1
    public_hostname: master.example.com
    master: true
    node: true
  - connect_to: 10.0.0.2
    ip: 10.0.0.2
    hostname: node1-private.example.com
    public_ip: 24.222.0.2
    public_hostname: node1.example.com
    master: true
    node: true
  - connect_to: 10.0.0.3
    ip: 10.0.0.3
    hostname: node2-private.example.com
    public_ip: 24.222.0.3
    public_hostname: node2.example.com
    node: true
"""

class UnattendedCliTests(OOCliFixture):

    def setUp(self):
        OOCliFixture.setUp(self)
        self.cli_args.append("-u")

    # unattended with config file and all installed hosts (without --force)
    @patch('ooinstall.openshift_ansible.run_main_playbook')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_get_hosts_to_run_on1(self, load_facts_mock, run_playbook_mock):
        mock_facts = copy.deepcopy(MOCK_FACTS)
        mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
        mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
        mock_facts['10.0.0.3']['common']['version'] = "3.0.0"

        load_facts_mock.return_value = (mock_facts, 0)
        run_playbook_mock.return_value = 0

        config_file = self.write_config(os.path.join(self.work_dir,
            'ooinstall.conf'), SAMPLE_CONFIG % 'openshift-enterprise')

        self.cli_args.extend(["-c", config_file, "install"])
        result = self.runner.invoke(cli.cli, self.cli_args)

        if result.exception is None or result.exit_code != 1:
            print "Exit code: %s" % result.exit_code
            self.fail("Unexpected CLI return")

    # unattended with config file and all installed hosts (with --force)
    @patch('ooinstall.openshift_ansible.run_main_playbook')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_get_hosts_to_run_on2(self, load_facts_mock, run_playbook_mock):
        mock_facts = copy.deepcopy(MOCK_FACTS)
        mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
        mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
        mock_facts['10.0.0.3']['common']['version'] = "3.0.0"
        self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock, run_playbook_mock,
                                         cli_input=None,
                                         exp_hosts_len=3,
                                         exp_hosts_to_run_on_len=3,
                                         force=True)

    # unattended with config file and no installed hosts (without --force)
    @patch('ooinstall.openshift_ansible.run_main_playbook')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_get_hosts_to_run_on3(self, load_facts_mock, run_playbook_mock):
        load_facts_mock.return_value = (MOCK_FACTS, 0)
        run_playbook_mock.return_value = 0
        self._verify_get_hosts_to_run_on(MOCK_FACTS, load_facts_mock, run_playbook_mock,
                                         cli_input=None,
                                         exp_hosts_len=3,
                                         exp_hosts_to_run_on_len=3,
                                         force=False)

    # unattended with config file and no installed hosts (with --force)
    @patch('ooinstall.openshift_ansible.run_main_playbook')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_get_hosts_to_run_on4(self, load_facts_mock, run_playbook_mock):
        load_facts_mock.return_value = (MOCK_FACTS, 0)
        run_playbook_mock.return_value = 0
        self._verify_get_hosts_to_run_on(MOCK_FACTS, load_facts_mock, run_playbook_mock,
                                         cli_input=None,
                                         exp_hosts_len=3,
                                         exp_hosts_to_run_on_len=3,
                                         force=True)

    # unattended with config file and some installed some uninstalled hosts (without --force)
    @patch('ooinstall.openshift_ansible.run_main_playbook')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_get_hosts_to_run_on5(self, load_facts_mock, run_playbook_mock):
        mock_facts = copy.deepcopy(MOCK_FACTS)
        mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
        mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
        self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock, run_playbook_mock,
                                         cli_input=None,
                                         exp_hosts_len=3,
                                         exp_hosts_to_run_on_len=2,
                                         force=False)

    # unattended with config file and some installed some uninstalled hosts (with --force)
    @patch('ooinstall.openshift_ansible.run_main_playbook')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_get_hosts_to_run_on6(self, load_facts_mock, run_playbook_mock):
        mock_facts = copy.deepcopy(MOCK_FACTS)
        mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
        mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
        self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock, run_playbook_mock,
                                         cli_input=None,
                                         exp_hosts_len=3,
                                         exp_hosts_to_run_on_len=3,
                                         force=True)

    @patch('ooinstall.openshift_ansible.run_main_playbook')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_cfg_full_run(self, load_facts_mock, run_playbook_mock):
        load_facts_mock.return_value = (MOCK_FACTS, 0)
        run_playbook_mock.return_value = 0

        config_file = self.write_config(os.path.join(self.work_dir,
            'ooinstall.conf'), SAMPLE_CONFIG % 'openshift-enterprise')

        self.cli_args.extend(["-c", config_file, "install"])
        result = self.runner.invoke(cli.cli, self.cli_args)
        self.assert_result(result, 0)

        load_facts_args = load_facts_mock.call_args[0]
        self.assertEquals(os.path.join(self.work_dir, ".ansible/hosts"),
            load_facts_args[0])
        self.assertEquals(os.path.join(self.work_dir,
            "playbooks/byo/openshift_facts.yml"), load_facts_args[1])
        env_vars = load_facts_args[2]
        self.assertEquals(os.path.join(self.work_dir,
            '.ansible/callback_facts.yaml'),
            env_vars['OO_INSTALL_CALLBACK_FACTS_YAML'])
        self.assertEqual('/tmp/ansible.log', env_vars['ANSIBLE_LOG_PATH'])
        # If user running test has rpm installed, this might be set to default:
        self.assertTrue('ANSIBLE_CONFIG' not in env_vars or
            env_vars['ANSIBLE_CONFIG'] == cli.DEFAULT_ANSIBLE_CONFIG)

        # Make sure we ran on the expected masters and nodes:
        hosts = run_playbook_mock.call_args[0][0]
        hosts_to_run_on = run_playbook_mock.call_args[0][1]
        self.assertEquals(3, len(hosts))
        self.assertEquals(3, len(hosts_to_run_on))

    @patch('ooinstall.openshift_ansible.run_main_playbook')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_inventory_write(self, load_facts_mock, run_playbook_mock):

        # Add an ssh user so we can verify it makes it to the inventory file:
        merged_config = "%s\n%s" % (SAMPLE_CONFIG % 'openshift-enterprise',
            "ansible_ssh_user: bob")
        load_facts_mock.return_value = (MOCK_FACTS, 0)
        run_playbook_mock.return_value = 0

        config_file = self.write_config(os.path.join(self.work_dir,
            'ooinstall.conf'), merged_config)

        self.cli_args.extend(["-c", config_file, "install"])
        result = self.runner.invoke(cli.cli, self.cli_args)
        self.assert_result(result, 0)

        # Check the inventory file looks as we would expect:
        inventory = ConfigParser.ConfigParser(allow_no_value=True)
        inventory.read(os.path.join(self.work_dir, '.ansible/hosts'))
        self.assertEquals('bob',
            inventory.get('OSEv3:vars', 'ansible_ssh_user'))
        self.assertEquals('openshift-enterprise',
            inventory.get('OSEv3:vars', 'deployment_type'))

        # Check the masters:
        self.assertEquals(1, len(inventory.items('masters')))
        self.assertEquals(3, len(inventory.items('nodes')))

        for item in inventory.items('masters'):
            # ansible host lines do NOT parse nicely:
            master_line = item[0]
            if item[1] is not None:
                master_line = "%s=%s" % (master_line, item[1])
            self.assertTrue('openshift_ip' in master_line)
            self.assertTrue('openshift_public_ip' in master_line)
            self.assertTrue('openshift_hostname' in master_line)
            self.assertTrue('openshift_public_hostname' in master_line)

    @patch('ooinstall.openshift_ansible.run_main_playbook')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_variant_version_latest_assumed(self, load_facts_mock,
        run_playbook_mock):
        load_facts_mock.return_value = (MOCK_FACTS, 0)
        run_playbook_mock.return_value = 0

        config_file = self.write_config(os.path.join(self.work_dir,
            'ooinstall.conf'), SAMPLE_CONFIG % 'openshift-enterprise')

        self.cli_args.extend(["-c", config_file, "install"])
        result = self.runner.invoke(cli.cli, self.cli_args)
        self.assert_result(result, 0)

        written_config = read_yaml(config_file)

        self.assertEquals('openshift-enterprise', written_config['variant'])
        # We didn't specify a version so the latest should have been assumed,
        # and written to disk:
        self.assertEquals('3.1', written_config['variant_version'])

        # Make sure the correct value was passed to ansible:
        inventory = ConfigParser.ConfigParser(allow_no_value=True)
        inventory.read(os.path.join(self.work_dir, '.ansible/hosts'))
        self.assertEquals('openshift-enterprise',
            inventory.get('OSEv3:vars', 'deployment_type'))

    @patch('ooinstall.openshift_ansible.run_main_playbook')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_variant_version_preserved(self, load_facts_mock,
        run_playbook_mock):
        load_facts_mock.return_value = (MOCK_FACTS, 0)
        run_playbook_mock.return_value = 0

        config = SAMPLE_CONFIG % 'openshift-enterprise'
        config = '%s\n%s' % (config, 'variant_version: 3.0')
        config_file = self.write_config(os.path.join(self.work_dir,
            'ooinstall.conf'), config)

        self.cli_args.extend(["-c", config_file, "install"])
        result = self.runner.invoke(cli.cli, self.cli_args)
        self.assert_result(result, 0)

        written_config = read_yaml(config_file)

        self.assertEquals('openshift-enterprise', written_config['variant'])
        # Make sure our older version was preserved:
        # and written to disk:
        self.assertEquals('3.0', written_config['variant_version'])

        inventory = ConfigParser.ConfigParser(allow_no_value=True)
        inventory.read(os.path.join(self.work_dir, '.ansible/hosts'))
        self.assertEquals('enterprise',
            inventory.get('OSEv3:vars', 'deployment_type'))

    @patch('ooinstall.openshift_ansible.run_ansible')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_no_ansible_config_specified(self, load_facts_mock, run_ansible_mock):
        load_facts_mock.return_value = (MOCK_FACTS, 0)
        run_ansible_mock.return_value = 0

        config = SAMPLE_CONFIG % 'openshift-enterprise'

        self._ansible_config_test(load_facts_mock, run_ansible_mock,
            config, None, None)

    @patch('ooinstall.openshift_ansible.run_ansible')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_ansible_config_specified_cli(self, load_facts_mock, run_ansible_mock):
        load_facts_mock.return_value = (MOCK_FACTS, 0)
        run_ansible_mock.return_value = 0

        config = SAMPLE_CONFIG % 'openshift-enterprise'
        ansible_config = os.path.join(self.work_dir, 'ansible.cfg')

        self._ansible_config_test(load_facts_mock, run_ansible_mock,
            config, ansible_config, ansible_config)

    @patch('ooinstall.openshift_ansible.run_ansible')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_ansible_config_specified_in_installer_config(self,
        load_facts_mock, run_ansible_mock):

        load_facts_mock.return_value = (MOCK_FACTS, 0)
        run_ansible_mock.return_value = 0

        ansible_config = os.path.join(self.work_dir, 'ansible.cfg')
        config = SAMPLE_CONFIG % 'openshift-enterprise'
        config = "%s\nansible_config: %s" % (config, ansible_config)
        self._ansible_config_test(load_facts_mock, run_ansible_mock,
            config, None, ansible_config)

    #pylint: disable=too-many-arguments
    # This method allows for drastically simpler tests to write, and the args
    # are all useful.
    def _ansible_config_test(self, load_facts_mock, run_ansible_mock,
        installer_config, ansible_config_cli=None, expected_result=None):
        """
        Utility method for testing the ways you can specify the ansible config.
        """

        load_facts_mock.return_value = (MOCK_FACTS, 0)
        run_ansible_mock.return_value = 0

        config_file = self.write_config(os.path.join(self.work_dir,
            'ooinstall.conf'), installer_config)

        self.cli_args.extend(["-c", config_file])
        if ansible_config_cli:
            self.cli_args.extend(["--ansible-config", ansible_config_cli])
        self.cli_args.append("install")
        result = self.runner.invoke(cli.cli, self.cli_args)
        self.assert_result(result, 0)

        # Test the env vars for facts playbook:
        facts_env_vars = load_facts_mock.call_args[0][2]
        if expected_result:
            self.assertEquals(expected_result, facts_env_vars['ANSIBLE_CONFIG'])
        else:
            # If user running test has rpm installed, this might be set to default:
            self.assertTrue('ANSIBLE_CONFIG' not in facts_env_vars or
                facts_env_vars['ANSIBLE_CONFIG'] == cli.DEFAULT_ANSIBLE_CONFIG)

        # Test the env vars for main playbook:
        env_vars = run_ansible_mock.call_args[0][2]
        if expected_result:
            self.assertEquals(expected_result, env_vars['ANSIBLE_CONFIG'])
        else:
            # If user running test has rpm installed, this might be set to default:
            self.assertTrue('ANSIBLE_CONFIG' not in env_vars or
                env_vars['ANSIBLE_CONFIG'] == cli.DEFAULT_ANSIBLE_CONFIG)

    # unattended with bad config file and no installed hosts (without --force)
    @patch('ooinstall.openshift_ansible.run_main_playbook')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_bad_config(self, load_facts_mock, run_playbook_mock):
        load_facts_mock.return_value = (MOCK_FACTS, 0)
        run_playbook_mock.return_value = 0

        config_file = self.write_config(os.path.join(self.work_dir,
            'ooinstall.conf'), BAD_CONFIG % 'openshift-enterprise')

        self.cli_args.extend(["-c", config_file, "install"])
        result = self.runner.invoke(cli.cli, self.cli_args)

        self.assertEquals(1, result.exit_code)
        self.assertTrue("You must specify either an ip or hostname"
            in result.output)

    #unattended with two masters, one node, and haproxy
    @patch('ooinstall.openshift_ansible.run_main_playbook')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_quick_ha_full_run(self, load_facts_mock, run_playbook_mock):
        load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
        run_playbook_mock.return_value = 0

        config_file = self.write_config(os.path.join(self.work_dir,
            'ooinstall.conf'), QUICKHA_CONFIG % 'openshift-enterprise')

        self.cli_args.extend(["-c", config_file, "install"])
        result = self.runner.invoke(cli.cli, self.cli_args)
        self.assert_result(result, 0)

        # Make sure we ran on the expected masters and nodes:
        hosts = run_playbook_mock.call_args[0][0]
        hosts_to_run_on = run_playbook_mock.call_args[0][1]
        self.assertEquals(4, len(hosts))
        self.assertEquals(4, len(hosts_to_run_on))

    #unattended with two masters, one node, but no load balancer specified:
    @patch('ooinstall.openshift_ansible.run_main_playbook')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_quick_ha_no_lb(self, load_facts_mock, run_playbook_mock):
        load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
        run_playbook_mock.return_value = 0

        config_file = self.write_config(os.path.join(self.work_dir,
            'ooinstall.conf'), QUICKHA_CONFIG_NO_LB % 'openshift-enterprise')

        self.cli_args.extend(["-c", config_file, "install"])
        result = self.runner.invoke(cli.cli, self.cli_args)

        # We consider this a valid outcome but lets make sure the warning
        # was displayed:
        self.assert_result(result, 0)
        self.assertTrue('No master load balancer specified in config' in result.output)

        # Make sure we ran on the expected masters and nodes:
        hosts = run_playbook_mock.call_args[0][0]
        hosts_to_run_on = run_playbook_mock.call_args[0][1]
        self.assertEquals(3, len(hosts))
        self.assertEquals(3, len(hosts_to_run_on))

    #unattended with two masters, one node, and one of the masters reused as load balancer:
    @patch('ooinstall.openshift_ansible.run_main_playbook')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_quick_ha_reused_lb(self, load_facts_mock, run_playbook_mock):
        load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
        run_playbook_mock.return_value = 0

        config_file = self.write_config(os.path.join(self.work_dir,
            'ooinstall.conf'), QUICKHA_CONFIG_REUSED_LB % 'openshift-enterprise')

        self.cli_args.extend(["-c", config_file, "install"])
        result = self.runner.invoke(cli.cli, self.cli_args)

        # This is not a valid configuration:
        self.assert_result(result, 0)


class AttendedCliTests(OOCliFixture):

    def setUp(self):
        OOCliFixture.setUp(self)
        # Doesn't exist but keeps us from reading the local users config:
        self.config_file = os.path.join(self.work_dir, 'config.yml')
        self.cli_args.extend(["-c", self.config_file])

    @patch('ooinstall.openshift_ansible.run_main_playbook')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_full_run(self, load_facts_mock, run_playbook_mock):
        load_facts_mock.return_value = (MOCK_FACTS, 0)
        run_playbook_mock.return_value = 0

        cli_input = build_input(hosts=[
            ('10.0.0.1', True),
            ('10.0.0.2', False),
            ('10.0.0.3', False)],
                                      ssh_user='root',
                                      variant_num=1,
                                      confirm_facts='y')
        self.cli_args.append("install")
        result = self.runner.invoke(cli.cli, self.cli_args,
            input=cli_input)
        self.assert_result(result, 0)

        self._verify_load_facts(load_facts_mock)
        self._verify_run_playbook(run_playbook_mock, 3, 3)

        written_config = read_yaml(self.config_file)
        self._verify_config_hosts(written_config, 3)

        inventory = ConfigParser.ConfigParser(allow_no_value=True)
        inventory.read(os.path.join(self.work_dir, '.ansible/hosts'))
        self.assertEquals('False',
            inventory.get('nodes', '10.0.0.1  openshift_schedulable'))
        self.assertEquals(None,
            inventory.get('nodes', '10.0.0.2'))
        self.assertEquals(None,
            inventory.get('nodes', '10.0.0.3'))

    # interactive with config file and some installed some uninstalled hosts
    @patch('ooinstall.openshift_ansible.run_main_playbook')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_add_nodes(self, load_facts_mock, run_playbook_mock):

        # Modify the mock facts to return a version indicating OpenShift
        # is already installed on our master, and the first node.
        mock_facts = copy.deepcopy(MOCK_FACTS)
        mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
        mock_facts['10.0.0.2']['common']['version'] = "3.0.0"

        load_facts_mock.return_value = (mock_facts, 0)
        run_playbook_mock.return_value = 0

        cli_input = build_input(hosts=[
            ('10.0.0.1', True),
            ('10.0.0.2', False),
            ],
                                      add_nodes=[('10.0.0.3', False)],
                                      ssh_user='root',
                                      variant_num=1,
                                      confirm_facts='y')
        self.cli_args.append("install")
        result = self.runner.invoke(cli.cli,
                                    self.cli_args,
                                    input=cli_input)
        self.assert_result(result, 0)

        self._verify_load_facts(load_facts_mock)
        self._verify_run_playbook(run_playbook_mock, 3, 2)

        written_config = read_yaml(self.config_file)
        self._verify_config_hosts(written_config, 3)

    @patch('ooinstall.openshift_ansible.run_main_playbook')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_fresh_install_with_config(self, load_facts_mock, run_playbook_mock):
        load_facts_mock.return_value = (MOCK_FACTS, 0)
        run_playbook_mock.return_value = 0

        config_file = self.write_config(os.path.join(self.work_dir,
                                                     'ooinstall.conf'),
                                        SAMPLE_CONFIG % 'openshift-enterprise')
        cli_input = build_input(confirm_facts='y')
        self.cli_args.extend(["-c", config_file])
        self.cli_args.append("install")
        result = self.runner.invoke(cli.cli,
                                    self.cli_args,
                                    input=cli_input)
        self.assert_result(result, 0)

        self._verify_load_facts(load_facts_mock)
        self._verify_run_playbook(run_playbook_mock, 3, 3)

        written_config = read_yaml(config_file)
        self._verify_config_hosts(written_config, 3)

    #interactive with config file and all installed hosts
    @patch('ooinstall.openshift_ansible.run_main_playbook')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_get_hosts_to_run_on(self, load_facts_mock, run_playbook_mock):
        mock_facts = copy.deepcopy(MOCK_FACTS)
        mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
        mock_facts['10.0.0.2']['common']['version'] = "3.0.0"

        cli_input = build_input(hosts=[
            ('10.0.0.1', True),
            ],
                                      add_nodes=[('10.0.0.2', False)],
                                      ssh_user='root',
                                      variant_num=1,
                                      scheduleable_masters_ok=True,
                                      confirm_facts='y')

        self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock,
                                         run_playbook_mock,
                                         cli_input,
                                         exp_hosts_len=2,
                                         exp_hosts_to_run_on_len=2,
                                         force=False)

    #interactive multimaster: one more node than master
    @patch('ooinstall.openshift_ansible.run_main_playbook')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_ha_dedicated_node(self, load_facts_mock, run_playbook_mock):
        load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
        run_playbook_mock.return_value = 0

        cli_input = build_input(hosts=[
            ('10.0.0.1', True),
            ('10.0.0.2', True),
            ('10.0.0.3', False),
            ('10.0.0.4', True)],
                                      ssh_user='root',
                                      variant_num=1,
                                      confirm_facts='y',
                                      master_lb=('10.0.0.5', False))
        self.cli_args.append("install")
        result = self.runner.invoke(cli.cli, self.cli_args,
            input=cli_input)
        self.assert_result(result, 0)

        self._verify_load_facts(load_facts_mock)
        self._verify_run_playbook(run_playbook_mock, 5, 5)

        written_config = read_yaml(self.config_file)
        self._verify_config_hosts(written_config, 5)

        inventory = ConfigParser.ConfigParser(allow_no_value=True)
        inventory.read(os.path.join(self.work_dir, '.ansible/hosts'))
        self.assertEquals('False',
            inventory.get('nodes', '10.0.0.1  openshift_schedulable'))
        self.assertEquals('False',
            inventory.get('nodes', '10.0.0.2  openshift_schedulable'))
        self.assertEquals(None,
            inventory.get('nodes', '10.0.0.3'))
        self.assertEquals('False',
            inventory.get('nodes', '10.0.0.4  openshift_schedulable'))

        self.assertTrue(inventory.has_section('etcd'))
        self.assertEquals(3, len(inventory.items('etcd')))

    #interactive multimaster: identical masters and nodes
    @patch('ooinstall.openshift_ansible.run_main_playbook')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_ha_no_dedicated_nodes(self, load_facts_mock, run_playbook_mock):
        load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
        run_playbook_mock.return_value = 0

        cli_input = build_input(hosts=[
            ('10.0.0.1', True),
            ('10.0.0.2', True),
            ('10.0.0.3', True)],
                                      ssh_user='root',
                                      variant_num=1,
                                      confirm_facts='y',
                                      master_lb=('10.0.0.5', False))
        self.cli_args.append("install")
        result = self.runner.invoke(cli.cli, self.cli_args,
            input=cli_input)
        self.assert_result(result, 0)

        self._verify_load_facts(load_facts_mock)
        self._verify_run_playbook(run_playbook_mock, 4, 4)

        written_config = read_yaml(self.config_file)
        self._verify_config_hosts(written_config, 4)

        inventory = ConfigParser.ConfigParser(allow_no_value=True)
        inventory.read(os.path.join(self.work_dir, '.ansible/hosts'))
        self.assertEquals('True',
            inventory.get('nodes', '10.0.0.1  openshift_schedulable'))
        self.assertEquals('True',
            inventory.get('nodes', '10.0.0.2  openshift_schedulable'))
        self.assertEquals('True',
            inventory.get('nodes', '10.0.0.3  openshift_schedulable'))

    #interactive multimaster: attempting to use a master as the load balancer should fail:
    @patch('ooinstall.openshift_ansible.run_main_playbook')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_ha_reuse_master_as_lb(self, load_facts_mock, run_playbook_mock):
        load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
        run_playbook_mock.return_value = 0

        cli_input = build_input(hosts=[
                                      ('10.0.0.1', True),
                                      ('10.0.0.2', True),
                                      ('10.0.0.3', False),
                                      ('10.0.0.4', True)],
                                      ssh_user='root',
                                      variant_num=1,
                                      confirm_facts='y',
                                      master_lb=(['10.0.0.2', '10.0.0.5'], False))
        self.cli_args.append("install")
        result = self.runner.invoke(cli.cli, self.cli_args,
            input=cli_input)
        self.assert_result(result, 0)

    #interactive all-in-one
    @patch('ooinstall.openshift_ansible.run_main_playbook')
    @patch('ooinstall.openshift_ansible.load_system_facts')
    def test_all_in_one(self, load_facts_mock, run_playbook_mock):
        load_facts_mock.return_value = (MOCK_FACTS, 0)
        run_playbook_mock.return_value = 0

        cli_input = build_input(hosts=[
            ('10.0.0.1', True)],
                                      ssh_user='root',
                                      variant_num=1,
                                      confirm_facts='y')
        self.cli_args.append("install")
        result = self.runner.invoke(cli.cli, self.cli_args,
            input=cli_input)
        self.assert_result(result, 0)

        self._verify_load_facts(load_facts_mock)
        self._verify_run_playbook(run_playbook_mock, 1, 1)

        written_config = read_yaml(self.config_file)
        self._verify_config_hosts(written_config, 1)

        inventory = ConfigParser.ConfigParser(allow_no_value=True)
        inventory.read(os.path.join(self.work_dir, '.ansible/hosts'))
        self.assertEquals('True',
            inventory.get('nodes', '10.0.0.1  openshift_schedulable'))

# TODO: test with config file, attended add node
# TODO: test with config file, attended new node already in config file
# TODO: test with config file, attended new node already in config file, plus manually added nodes
# TODO: test with config file, attended reject facts