/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to mandos-ctl

  • Committer: Teddy Hogeborn
  • Date: 2019-03-12 21:11:32 UTC
  • Revision ID: teddy@recompile.se-20190312211132-q6mxkrybruj3irbn
mandos-ctl: Refactor

* mandos-ctl (Test_check_option_syntax): Reorder methods into logical
                                         order.
  (Test_command_from_options): Rename to "Test_commands_from_options",
                               and reorder methods to match order in
                               commands_from_options().

Show diffs side-by-side

added added

removed removed

Lines of Context:
840
840
 
841
841
 
842
842
class Test_check_option_syntax(unittest.TestCase):
 
843
    def setUp(self):
 
844
        self.parser = argparse.ArgumentParser()
 
845
        add_command_line_options(self.parser)
 
846
 
 
847
    def test_actions_requires_client_or_all(self):
 
848
        for action, value in self.actions.items():
 
849
            options = self.parser.parse_args()
 
850
            setattr(options, action, value)
 
851
            with self.assertParseError():
 
852
                self.check_option_syntax(options)
 
853
 
843
854
    # This mostly corresponds to the definition from has_actions() in
844
855
    # check_option_syntax()
845
856
    actions = {
866
877
        "deny": True,
867
878
    }
868
879
 
869
 
    def setUp(self):
870
 
        self.parser = argparse.ArgumentParser()
871
 
        add_command_line_options(self.parser)
872
 
 
873
880
    @contextlib.contextmanager
874
881
    def assertParseError(self):
875
882
        with self.assertRaises(SystemExit) as e:
897
904
    def check_option_syntax(self, options):
898
905
        check_option_syntax(self.parser, options)
899
906
 
900
 
    def test_actions_requires_client_or_all(self):
901
 
        for action, value in self.actions.items():
902
 
            options = self.parser.parse_args()
903
 
            setattr(options, action, value)
904
 
            with self.assertParseError():
905
 
                self.check_option_syntax(options)
906
 
 
907
907
    def test_actions_conflicts_with_verbose(self):
908
908
        for action, value in self.actions.items():
909
909
            options = self.parser.parse_args()
971
971
                self.check_option_syntax(options)
972
972
 
973
973
 
974
 
class Test_command_from_options(unittest.TestCase):
 
974
class Test_commands_from_options(unittest.TestCase):
975
975
    def setUp(self):
976
976
        self.parser = argparse.ArgumentParser()
977
977
        add_command_line_options(self.parser)
 
978
 
 
979
    def test_is_enabled(self):
 
980
        self.assert_command_from_args(["--is-enabled", "foo"],
 
981
                                      IsEnabledCmd)
 
982
 
978
983
    def assert_command_from_args(self, args, command_cls,
979
984
                                 **cmd_attrs):
980
985
        """Assert that parsing ARGS should result in an instance of
987
992
        self.assertIsInstance(command, command_cls)
988
993
        for key, value in cmd_attrs.items():
989
994
            self.assertEqual(getattr(command, key), value)
990
 
    def test_print_table(self):
991
 
        self.assert_command_from_args([], PrintTableCmd,
992
 
                                      verbose=False)
993
 
 
994
 
    def test_print_table_verbose(self):
995
 
        self.assert_command_from_args(["--verbose"], PrintTableCmd,
996
 
                                      verbose=True)
997
 
 
998
 
    def test_print_table_verbose_short(self):
999
 
        self.assert_command_from_args(["-v"], PrintTableCmd,
1000
 
                                      verbose=True)
 
995
 
 
996
    def test_is_enabled_short(self):
 
997
        self.assert_command_from_args(["-V", "foo"], IsEnabledCmd)
 
998
 
 
999
    def test_approve(self):
 
1000
        self.assert_command_from_args(["--approve", "foo"],
 
1001
                                      ApproveCmd)
 
1002
 
 
1003
    def test_approve_short(self):
 
1004
        self.assert_command_from_args(["-A", "foo"], ApproveCmd)
 
1005
 
 
1006
    def test_deny(self):
 
1007
        self.assert_command_from_args(["--deny", "foo"], DenyCmd)
 
1008
 
 
1009
    def test_deny_short(self):
 
1010
        self.assert_command_from_args(["-D", "foo"], DenyCmd)
 
1011
 
 
1012
    def test_remove(self):
 
1013
        self.assert_command_from_args(["--remove", "foo"],
 
1014
                                      RemoveCmd)
 
1015
 
 
1016
    def test_deny_before_remove(self):
 
1017
        options = self.parser.parse_args(["--deny", "--remove",
 
1018
                                          "foo"])
 
1019
        check_option_syntax(self.parser, options)
 
1020
        commands = commands_from_options(options)
 
1021
        self.assertEqual(len(commands), 2)
 
1022
        self.assertIsInstance(commands[0], DenyCmd)
 
1023
        self.assertIsInstance(commands[1], RemoveCmd)
 
1024
 
 
1025
    def test_deny_before_remove_reversed(self):
 
1026
        options = self.parser.parse_args(["--remove", "--deny",
 
1027
                                          "--all"])
 
1028
        check_option_syntax(self.parser, options)
 
1029
        commands = commands_from_options(options)
 
1030
        self.assertEqual(len(commands), 2)
 
1031
        self.assertIsInstance(commands[0], DenyCmd)
 
1032
        self.assertIsInstance(commands[1], RemoveCmd)
 
1033
 
 
1034
    def test_remove_short(self):
 
1035
        self.assert_command_from_args(["-r", "foo"], RemoveCmd)
 
1036
 
 
1037
    def test_dump_json(self):
 
1038
        self.assert_command_from_args(["--dump-json"], DumpJSONCmd)
1001
1039
 
1002
1040
    def test_enable(self):
1003
1041
        self.assert_command_from_args(["--enable", "foo"], EnableCmd)
1027
1065
        self.assert_command_from_args(["--stop-checker", "foo"],
1028
1066
                                      StopCheckerCmd)
1029
1067
 
1030
 
    def test_remove(self):
1031
 
        self.assert_command_from_args(["--remove", "foo"],
1032
 
                                      RemoveCmd)
 
1068
    def test_approve_by_default(self):
 
1069
        self.assert_command_from_args(["--approve-by-default", "foo"],
 
1070
                                      ApproveByDefaultCmd)
1033
1071
 
1034
 
    def test_remove_short(self):
1035
 
        self.assert_command_from_args(["-r", "foo"], RemoveCmd)
 
1072
    def test_deny_by_default(self):
 
1073
        self.assert_command_from_args(["--deny-by-default", "foo"],
 
1074
                                      DenyByDefaultCmd)
1036
1075
 
1037
1076
    def test_checker(self):
1038
1077
        self.assert_command_from_args(["--checker", ":", "foo"],
1046
1085
        self.assert_command_from_args(["-c", ":", "foo"],
1047
1086
                                      SetCheckerCmd, value_to_set=":")
1048
1087
 
 
1088
    def test_host(self):
 
1089
        self.assert_command_from_args(["--host", "foo.example.org",
 
1090
                                       "foo"], SetHostCmd,
 
1091
                                      value_to_set="foo.example.org")
 
1092
 
 
1093
    def test_host_short(self):
 
1094
        self.assert_command_from_args(["-H", "foo.example.org",
 
1095
                                       "foo"], SetHostCmd,
 
1096
                                      value_to_set="foo.example.org")
 
1097
 
 
1098
    def test_secret_devnull(self):
 
1099
        self.assert_command_from_args(["--secret", os.path.devnull,
 
1100
                                       "foo"], SetSecretCmd,
 
1101
                                      value_to_set=b"")
 
1102
 
 
1103
    def test_secret_tempfile(self):
 
1104
        with tempfile.NamedTemporaryFile(mode="r+b") as f:
 
1105
            value = b"secret\0xyzzy\nbar"
 
1106
            f.write(value)
 
1107
            f.seek(0)
 
1108
            self.assert_command_from_args(["--secret", f.name,
 
1109
                                           "foo"], SetSecretCmd,
 
1110
                                          value_to_set=value)
 
1111
 
 
1112
    def test_secret_devnull_short(self):
 
1113
        self.assert_command_from_args(["-s", os.path.devnull, "foo"],
 
1114
                                      SetSecretCmd, value_to_set=b"")
 
1115
 
 
1116
    def test_secret_tempfile_short(self):
 
1117
        with tempfile.NamedTemporaryFile(mode="r+b") as f:
 
1118
            value = b"secret\0xyzzy\nbar"
 
1119
            f.write(value)
 
1120
            f.seek(0)
 
1121
            self.assert_command_from_args(["-s", f.name, "foo"],
 
1122
                                          SetSecretCmd,
 
1123
                                          value_to_set=value)
 
1124
 
1049
1125
    def test_timeout(self):
1050
1126
        self.assert_command_from_args(["--timeout", "PT5M", "foo"],
1051
1127
                                      SetTimeoutCmd,
1072
1148
                                      SetIntervalCmd,
1073
1149
                                      value_to_set=120000)
1074
1150
 
1075
 
    def test_approve_by_default(self):
1076
 
        self.assert_command_from_args(["--approve-by-default", "foo"],
1077
 
                                      ApproveByDefaultCmd)
1078
 
 
1079
 
    def test_deny_by_default(self):
1080
 
        self.assert_command_from_args(["--deny-by-default", "foo"],
1081
 
                                      DenyByDefaultCmd)
1082
 
 
1083
1151
    def test_approval_delay(self):
1084
1152
        self.assert_command_from_args(["--approval-delay", "PT30S",
1085
1153
                                       "foo"], SetApprovalDelayCmd,
1090
1158
                                       "foo"], SetApprovalDurationCmd,
1091
1159
                                      value_to_set=1000)
1092
1160
 
1093
 
    def test_host(self):
1094
 
        self.assert_command_from_args(["--host", "foo.example.org",
1095
 
                                       "foo"], SetHostCmd,
1096
 
                                      value_to_set="foo.example.org")
1097
 
 
1098
 
    def test_host_short(self):
1099
 
        self.assert_command_from_args(["-H", "foo.example.org",
1100
 
                                       "foo"], SetHostCmd,
1101
 
                                      value_to_set="foo.example.org")
1102
 
 
1103
 
    def test_secret_devnull(self):
1104
 
        self.assert_command_from_args(["--secret", os.path.devnull,
1105
 
                                       "foo"], SetSecretCmd,
1106
 
                                      value_to_set=b"")
1107
 
 
1108
 
    def test_secret_tempfile(self):
1109
 
        with tempfile.NamedTemporaryFile(mode="r+b") as f:
1110
 
            value = b"secret\0xyzzy\nbar"
1111
 
            f.write(value)
1112
 
            f.seek(0)
1113
 
            self.assert_command_from_args(["--secret", f.name,
1114
 
                                           "foo"], SetSecretCmd,
1115
 
                                          value_to_set=value)
1116
 
 
1117
 
    def test_secret_devnull_short(self):
1118
 
        self.assert_command_from_args(["-s", os.path.devnull, "foo"],
1119
 
                                      SetSecretCmd, value_to_set=b"")
1120
 
 
1121
 
    def test_secret_tempfile_short(self):
1122
 
        with tempfile.NamedTemporaryFile(mode="r+b") as f:
1123
 
            value = b"secret\0xyzzy\nbar"
1124
 
            f.write(value)
1125
 
            f.seek(0)
1126
 
            self.assert_command_from_args(["-s", f.name, "foo"],
1127
 
                                          SetSecretCmd,
1128
 
                                          value_to_set=value)
1129
 
 
1130
 
    def test_approve(self):
1131
 
        self.assert_command_from_args(["--approve", "foo"],
1132
 
                                      ApproveCmd)
1133
 
 
1134
 
    def test_approve_short(self):
1135
 
        self.assert_command_from_args(["-A", "foo"], ApproveCmd)
1136
 
 
1137
 
    def test_deny(self):
1138
 
        self.assert_command_from_args(["--deny", "foo"], DenyCmd)
1139
 
 
1140
 
    def test_deny_short(self):
1141
 
        self.assert_command_from_args(["-D", "foo"], DenyCmd)
1142
 
 
1143
 
    def test_dump_json(self):
1144
 
        self.assert_command_from_args(["--dump-json"], DumpJSONCmd)
1145
 
 
1146
 
    def test_is_enabled(self):
1147
 
        self.assert_command_from_args(["--is-enabled", "foo"],
1148
 
                                      IsEnabledCmd)
1149
 
 
1150
 
    def test_is_enabled_short(self):
1151
 
        self.assert_command_from_args(["-V", "foo"], IsEnabledCmd)
1152
 
 
1153
 
    def test_deny_before_remove(self):
1154
 
        options = self.parser.parse_args(["--deny", "--remove",
1155
 
                                          "foo"])
1156
 
        check_option_syntax(self.parser, options)
1157
 
        commands = commands_from_options(options)
1158
 
        self.assertEqual(len(commands), 2)
1159
 
        self.assertIsInstance(commands[0], DenyCmd)
1160
 
        self.assertIsInstance(commands[1], RemoveCmd)
1161
 
 
1162
 
    def test_deny_before_remove_reversed(self):
1163
 
        options = self.parser.parse_args(["--remove", "--deny",
1164
 
                                          "--all"])
1165
 
        check_option_syntax(self.parser, options)
1166
 
        commands = commands_from_options(options)
1167
 
        self.assertEqual(len(commands), 2)
1168
 
        self.assertIsInstance(commands[0], DenyCmd)
1169
 
        self.assertIsInstance(commands[1], RemoveCmd)
 
1161
    def test_print_table(self):
 
1162
        self.assert_command_from_args([], PrintTableCmd,
 
1163
                                      verbose=False)
 
1164
 
 
1165
    def test_print_table_verbose(self):
 
1166
        self.assert_command_from_args(["--verbose"], PrintTableCmd,
 
1167
                                      verbose=True)
 
1168
 
 
1169
    def test_print_table_verbose_short(self):
 
1170
        self.assert_command_from_args(["-v"], PrintTableCmd,
 
1171
                                      verbose=True)
1170
1172
 
1171
1173
 
1172
1174
class TestCmd(unittest.TestCase):