~bkerensa/ubuntu/precise/landscape-client/fix-for-962974

« back to all changes in this revision

Viewing changes to landscape/tests/test_configuration.py

  • Committer: Bazaar Package Importer
  • Author(s): Free Ekanayaka
  • Date: 2010-02-10 18:50:53 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20100210185053-kqyzavz3rkpkl7nx
Tags: 1.4.4-0ubuntu0.10.04
* New upstream release (LP: #519200):
  - Add a message for creating package locks (LP: #514334)
  - Add support for auto-approved change-packages messages (LP: #517175)
  - Add support for installing server-generated debian packages (LP: #509752)
  - Add support for reporting Eucalyptus topology information (LP: #518501)
  - Fix timeout while inserting large free-space message (LP: #218388)
  - Fix wrong log path in motd (LP: #517454)
  - Fix race condition in process excecution (LP: #517453)

Show diffs side-by-side

added added

removed removed

Lines of Context:
455
455
                          "FooPlugin, ScriptExecution")
456
456
 
457
457
    def test_query_script_users_defined_on_command_line(self):
 
458
        """
 
459
        Confirm with the user for users specified for the ScriptPlugin.
 
460
        """
458
461
        self.config.include_manager_plugins = "FooPlugin"
459
462
        self.mocker.order()
460
463
        script_mock = self.mocker.patch(self.script)
462
465
        script_mock.prompt_yes_no("Enable script execution?", default=False)
463
466
        self.mocker.result(True)
464
467
        script_mock.show_help(ANY)
465
 
        raw_input_mock = self.mocker.replace(raw_input, passthrough=False)
466
 
        self.expect(raw_input_mock(ANY)).count(0)
 
468
        script_mock.prompt_get_input(
 
469
            "Script users [root, nobody, landscape]: ", False)
467
470
        self.mocker.replay()
468
471
 
469
472
        self.config.load_command_line(
472
475
        self.assertEquals(self.config.script_users,
473
476
                          "root, nobody, landscape")
474
477
 
 
478
    def test_query_script_users_defined_on_command_line_with_unknown_user(self):
 
479
        """
 
480
        If several users are provided on the command line, we verify the users
 
481
        and raise a ConfigurationError if any are unknown on this system.
 
482
        """
 
483
        pwnam_mock = self.mocker.replace("pwd.getpwnam")
 
484
        pwnam_mock("root")
 
485
        self.mocker.result(None)
 
486
        pwnam_mock("nobody")
 
487
        self.mocker.result(None)
 
488
        pwnam_mock("landscape")
 
489
        self.mocker.result(None)
 
490
        pwnam_mock("unknown")
 
491
        self.mocker.throw(KeyError())
 
492
        self.mocker.replay()
 
493
 
 
494
        self.config.load_command_line(
 
495
            ["--script-users", "root, nobody, landscape, unknown",
 
496
            "--include-manager-plugins", "ScriptPlugin"])
 
497
        self.assertRaises(ConfigurationError, self.script.query_script_plugin)
 
498
 
 
499
    def test_query_script_users_defined_on_command_line_with_all_user(self):
 
500
        """
 
501
        We shouldn't accept all as a synonym for ALL
 
502
        """
 
503
        self.config.load_command_line(
 
504
            ["--script-users", "all",
 
505
            "--include-manager-plugins", "ScriptPlugin"])
 
506
        self.assertRaises(ConfigurationError, self.script.query_script_plugin)
 
507
 
 
508
    def test_query_script_users_defined_on_command_line_with_ALL_user(self):
 
509
        """
 
510
        ALL is the special marker for all users.
 
511
        """
 
512
        self.config.load_command_line(
 
513
            ["--script-users", "ALL",
 
514
             "--include-manager-plugins", "ScriptPlugin"])
 
515
        self.script.query_script_plugin()
 
516
        self.assertEquals(self.config.script_users,
 
517
                          "ALL")
 
518
 
 
519
    def test_query_script_users_defined_on_command_line_with_ALL_and_extra_user(self):
 
520
        """
 
521
        If ALL and additional users are provided as the users on the command
 
522
        line, this should raise an appropriate ConfigurationError.
 
523
        """
 
524
        self.config.load_command_line(
 
525
            ["--script-users", "ALL, kevin",
 
526
            "--include-manager-plugins", "ScriptPlugin"])
 
527
        self.assertRaises(ConfigurationError, self.script.query_script_plugin)
 
528
 
 
529
    def test_invalid_user_entered_by_user(self):
 
530
        """
 
531
        If an invalid user is entered on the command line the user should be
 
532
        informed and prompted again.
 
533
        """
 
534
        help_snippet = "Landscape has a feature which enables administrators"
 
535
        self.mocker.order()
 
536
        script_mock = self.mocker.patch(self.script)
 
537
        script_mock.show_help(self.get_matcher(help_snippet))
 
538
        script_mock.prompt_yes_no("Enable script execution?", default=False)
 
539
        self.mocker.result(True)
 
540
        script_mock.show_help(
 
541
            self.get_matcher("By default, scripts are restricted"))
 
542
        script_mock.prompt_get_input("Script users: ", False)
 
543
        self.mocker.result(u"nonexistent")
 
544
        script_mock.show_help("Unknown system users: nonexistent")
 
545
        script_mock.prompt_get_input("Script users: ", False)
 
546
        self.mocker.result(u"root")
 
547
        self.mocker.replay()
 
548
        self.script.query_script_plugin()
 
549
        self.assertEquals(self.config.script_users,
 
550
                          "root")
 
551
 
 
552
    def test_tags_not_defined_on_command_line(self):
 
553
        """
 
554
        If tags are not provided, the user should be prompted for them.
 
555
        """
 
556
        self.mocker.order()
 
557
        script_mock = self.mocker.patch(self.script)
 
558
        script_mock.show_help("You may provide tags for this computer e.g. "
 
559
                              "server,hardy.")
 
560
        script_mock.prompt("tags", "Tags", False)
 
561
        self.mocker.replay()
 
562
        self.script.query_tags()
 
563
 
 
564
    def test_invalid_tags_entered_by_user(self):
 
565
        """
 
566
        If tags are not provided, the user should be prompted for them, and
 
567
        they should be valid tags, if not the user should be prompted for them
 
568
        again.
 
569
        """
 
570
        script_mock = self.mocker.patch(self.script)
 
571
        script_mock.show_help("You may provide tags for this computer e.g. "
 
572
                              "server,hardy.")
 
573
        script_mock.prompt_get_input("Tags: ", False)
 
574
        self.mocker.result(u"<script>alert();</script>")
 
575
        script_mock.show_help("Tag names may only contain alphanumeric "
 
576
                              "characters.")
 
577
        script_mock.prompt_get_input("Tags: ", False)
 
578
        self.mocker.result(u"london")
 
579
        self.mocker.replay()
 
580
        self.script.query_tags()
 
581
 
 
582
    def test_tags_defined_on_command_line(self):
 
583
        """
 
584
        Tags defined on the command line can be verified by the user.
 
585
        """
 
586
        raw_input_mock = self.mocker.replace(raw_input, passthrough=False)
 
587
        self.expect(raw_input_mock(ANY)).count(0)
 
588
        self.mocker.replay()
 
589
        self.config.load_command_line(["--tags", u"server,london"])
 
590
        self.script.query_tags()
 
591
        self.assertEquals(self.config.tags, u"server,london")
 
592
 
 
593
    def test_invalid_tags_defined_on_command_line_raises_error(self):
 
594
        raw_input_mock = self.mocker.replace(raw_input, passthrough=False)
 
595
        self.expect(raw_input_mock(ANY)).count(0)
 
596
        self.mocker.replay()
 
597
        self.config.load_command_line(["--tags", u"<script>alert();</script>"])
 
598
        self.assertRaises(ConfigurationError, self.script.query_tags)
 
599
 
475
600
    def test_show_header(self):
476
601
        help_snippet = "This script will"
477
602
        script_mock = self.mocker.patch(self.script)
488
613
        script_mock.query_registration_password()
489
614
        script_mock.query_proxies()
490
615
        script_mock.query_script_plugin()
 
616
        script_mock.query_tags()
491
617
        self.mocker.replay()
492
618
 
493
619
        self.script.run()
525
651
                                 "http_proxy = http://old.proxy\n"
526
652
                                 "https_proxy = https://old.proxy\n"
527
653
                                 "url = http://url\n"
528
 
                                 "include_manager_plugins = ScriptExecution"
 
654
                                 "include_manager_plugins = ScriptExecution\n"
 
655
                                 "tags = london, server"
529
656
                                 )
530
657
 
531
658
        raw_input = self.mocker.replace("__builtin__.raw_input",
541
668
        expect(raw_input(C("[http://old.proxy]"))).result("http://new.proxy")
542
669
        expect(raw_input(C("[https://old.proxy]"))).result("https://new.proxy")
543
670
        expect(raw_input(C("Enable script execution? [Y/n]"))).result("n")
 
671
        expect(raw_input(C("Tags [london, server]: "))).result(
 
672
            u"glasgow, laptop")
544
673
 
545
674
        # Negative assertion.  We don't want it called in any other way.
546
675
        expect(raw_input(ANY)).count(0)
550
679
        expect(print_text_mock(ANY)).count(0, None)
551
680
 
552
681
        self.mocker.replay()
553
 
 
554
682
        config = self.get_config(["--no-start", "--config", filename])
555
683
        setup(config)
556
684
        self.assertEquals(type(config), LandscapeSetupConfiguration)
564
692
        self.assertEquals(config.http_proxy, "http://new.proxy")
565
693
        self.assertEquals(config.https_proxy, "https://new.proxy")
566
694
        self.assertEquals(config.include_manager_plugins, "")
 
695
        self.assertEquals(config.tags, u"glasgow, laptop")
567
696
 
568
697
    def test_silent_setup(self):
569
698
        """
637
766
account_name = account
638
767
""")
639
768
 
 
769
    def test_silent_script_users_with_all_user(self):
 
770
        """
 
771
        In silent mode, we shouldn't accept invalid users, it should raise a
 
772
        configuration error.
 
773
        """
 
774
        sysvconfig_mock = self.mocker.patch(SysVConfig)
 
775
        sysvconfig_mock.set_start_on_boot(True)
 
776
        self.mocker.replay()
 
777
 
 
778
        config = self.get_config(
 
779
            ["--script-users", "all",
 
780
             "--include-manager-plugins", "ScriptPlugin",
 
781
             "-a", "account",
 
782
             "-t", "rex",
 
783
             "--silent"])
 
784
        self.assertRaises(ConfigurationError, setup, config)
 
785
 
640
786
    def test_silent_setup_with_ping_url(self):
641
787
        sysvconfig_mock = self.mocker.patch(SysVConfig)
642
788
        sysvconfig_mock.set_start_on_boot(True)
806
952
        sysvconfig_mock.set_start_on_boot(True)
807
953
        sysvconfig_mock.restart_landscape()
808
954
        self.mocker.throw(ProcessError)
809
 
        
 
955
 
810
956
        print_text_mock("Couldn't restart the Landscape client.", error=True)
811
957
        print_text_mock(CONTAINS("This machine will be registered"), error=True)
812
958
 
827
973
        sysvconfig_mock.set_start_on_boot(True)
828
974
        sysvconfig_mock.restart_landscape()
829
975
        self.mocker.throw(ProcessError)
830
 
        
 
976
 
831
977
        print_text_mock("Couldn't restart the Landscape client.", error=True)
832
978
        print_text_mock(CONTAINS("This machine will be registered"), error=True)
833
979
 
1169
1315
            self.get_config(["--config", config_filename, "--silent",
1170
1316
                             "--import", "https://config.url"])
1171
1317
        except ImportOptionError, error:
1172
 
            self.assertEquals(str(error), 
 
1318
            self.assertEquals(str(error),
1173
1319
                              "Couldn't download configuration from "
1174
1320
                              "https://config.url: Server "
1175
1321
                              "returned HTTP code 501")
1192
1338
            self.get_config(["--config", config_filename, "--silent",
1193
1339
                             "--import", "https://config.url"])
1194
1340
        except ImportOptionError, error:
1195
 
            self.assertEquals(str(error), 
 
1341
            self.assertEquals(str(error),
1196
1342
                              "Couldn't download configuration from "
1197
1343
                              "https://config.url: Error 60: pycurl message")
1198
1344
        else:
1261
1407
        self.addCleanup(os.remove, key_filename)
1262
1408
 
1263
1409
        print_text_mock = self.mocker.replace(print_text)
1264
 
        print_text_mock("Writing SSL public key to %s..." % key_filename)
 
1410
        print_text_mock("Writing SSL CA certificate to %s..." % key_filename)
1265
1411
 
1266
1412
        self.mocker.replay()
1267
1413