unittest_bindctl.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # Copyright (C) 2009 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. import unittest
  16. import cmdparse
  17. import bindcmd
  18. from moduleinfo import *
  19. from exception import *
  20. try:
  21. from collections import OrderedDict
  22. except ImportError:
  23. from mycollections import OrderedDict
  24. class TestCmdLex(unittest.TestCase):
  25. def my_assert_raise(self, exception_type, cmd_line):
  26. self.assertRaises(exception_type, cmdparse.BindCmdParse, cmd_line)
  27. def testCommandWithoutParameter(self):
  28. cmd = cmdparse.BindCmdParse("zone add")
  29. assert cmd.module == "zone"
  30. assert cmd.command == "add"
  31. self.assertEqual(len(cmd.params), 0)
  32. def testCommandWithParameters(self):
  33. lines = {"zone add zone_name = cnnic.cn, file = cnnic.cn.file master=1.1.1.1",
  34. "zone add zone_name = \"cnnic.cn\", file ='cnnic.cn.file' master=1.1.1.1 ",
  35. "zone add zone_name = 'cnnic.cn\", file ='cnnic.cn.file' master=1.1.1.1, " }
  36. for cmd_line in lines:
  37. cmd = cmdparse.BindCmdParse(cmd_line)
  38. assert cmd.module == "zone"
  39. assert cmd.command == "add"
  40. assert cmd.params["zone_name"] == "cnnic.cn"
  41. assert cmd.params["file"] == "cnnic.cn.file"
  42. assert cmd.params["master"] == '1.1.1.1'
  43. def testCommandWithListParam(self):
  44. cmd = cmdparse.BindCmdParse("zone set zone_name='cnnic.cn', master='1.1.1.1, 2.2.2.2'")
  45. assert cmd.params["master"] == '1.1.1.1, 2.2.2.2'
  46. def testCommandWithHelpParam(self):
  47. cmd = cmdparse.BindCmdParse("zone add help")
  48. assert cmd.params["help"] == "help"
  49. cmd = cmdparse.BindCmdParse("zone add help *&)&)*&&$#$^%")
  50. assert cmd.params["help"] == "help"
  51. self.assertEqual(len(cmd.params), 1)
  52. def testCmdModuleNameFormatError(self):
  53. self.my_assert_raise(CmdModuleNameFormatError, "zone=good")
  54. self.my_assert_raise(CmdModuleNameFormatError, "zo/ne")
  55. self.my_assert_raise(CmdModuleNameFormatError, "")
  56. self.my_assert_raise(CmdModuleNameFormatError, "=zone")
  57. self.my_assert_raise(CmdModuleNameFormatError, "zone,")
  58. def testCmdMissCommandNameFormatError(self):
  59. self.my_assert_raise(CmdMissCommandNameFormatError, "zone")
  60. self.my_assert_raise(CmdMissCommandNameFormatError, "zone ")
  61. self.my_assert_raise(CmdMissCommandNameFormatError, "help ")
  62. def testCmdCommandNameFormatError(self):
  63. self.my_assert_raise(CmdCommandNameFormatError, "zone =d")
  64. self.my_assert_raise(CmdCommandNameFormatError, "zone z=d")
  65. self.my_assert_raise(CmdCommandNameFormatError, "zone z-d ")
  66. self.my_assert_raise(CmdCommandNameFormatError, "zone zdd/")
  67. self.my_assert_raise(CmdCommandNameFormatError, "zone zdd/ \"")
  68. def testCmdParamFormatError(self):
  69. self.my_assert_raise(CmdParamFormatError, "zone load load")
  70. self.my_assert_raise(CmdParamFormatError, "zone load load=")
  71. self.my_assert_raise(CmdParamFormatError, "zone load load==dd")
  72. self.my_assert_raise(CmdParamFormatError, "zone load , zone_name=dd zone_file=d" )
  73. self.my_assert_raise(CmdParamFormatError, "zone load zone_name=dd zone_file" )
  74. self.my_assert_raise(CmdParamFormatError, "zone zdd \"")
  75. class TestCmdSyntax(unittest.TestCase):
  76. def _create_bindcmd(self):
  77. """Create one bindcmd"""
  78. tool = bindcmd.BindCmdInterpreter()
  79. zone_file_param = ParamInfo(name = "zone_file")
  80. load_cmd = CommandInfo(name = "load")
  81. load_cmd.add_param(zone_file_param)
  82. param_master = ParamInfo(name = "master", optional = True)
  83. param_allow_update = ParamInfo(name = "allow_update", optional = True)
  84. set_cmd = CommandInfo(name = "set")
  85. set_cmd.add_param(param_master)
  86. set_cmd.add_param(param_allow_update)
  87. reload_all_cmd = CommandInfo(name = "reload_all", need_inst_param = False)
  88. zone_module = ModuleInfo(name = "zone", inst_name = "zone_name")
  89. zone_module.add_command(load_cmd)
  90. zone_module.add_command(set_cmd)
  91. zone_module.add_command(reload_all_cmd)
  92. tool.add_module_info(zone_module)
  93. return tool
  94. def setUp(self):
  95. self.bindcmd = self._create_bindcmd()
  96. def no_assert_raise(self, cmd_line):
  97. cmd = cmdparse.BindCmdParse(cmd_line)
  98. self.bindcmd.validate_cmd(cmd)
  99. def my_assert_raise(self, exception_type, cmd_line):
  100. cmd = cmdparse.BindCmdParse(cmd_line)
  101. self.assertRaises(exception_type, self.bindcmd.validate_cmd, cmd)
  102. def testValidateSuccess(self):
  103. self.no_assert_raise("zone load zone_file='cn' zone_name='cn'")
  104. self.no_assert_raise("zone load zone_file='cn', zone_name='cn', ")
  105. self.no_assert_raise("zone help ")
  106. self.no_assert_raise("zone load help ")
  107. self.no_assert_raise("zone help help='dd' ")
  108. self.no_assert_raise("zone set allow_update='1.1.1.1' zone_name='cn'")
  109. self.no_assert_raise("zone set zone_name='cn'")
  110. self.no_assert_raise("zone reload_all")
  111. def testCmdUnknownModuleSyntaxError(self):
  112. self.my_assert_raise(CmdUnknownModuleSyntaxError, "zoned d")
  113. self.my_assert_raise(CmdUnknownModuleSyntaxError, "dd dd ")
  114. def testCmdUnknownCmdSyntaxError(self):
  115. self.my_assert_raise(CmdUnknownCmdSyntaxError, "zone dd")
  116. def testCmdMissParamSyntaxError(self):
  117. self.my_assert_raise(CmdMissParamSyntaxError, "zone load zone_file='cn'")
  118. self.my_assert_raise(CmdMissParamSyntaxError, "zone load zone_name='cn'")
  119. self.my_assert_raise(CmdMissParamSyntaxError, "zone set allow_update='1.1.1.1'")
  120. self.my_assert_raise(CmdMissParamSyntaxError, "zone set ")
  121. def testCmdUnknownParamSyntaxError(self):
  122. self.my_assert_raise(CmdUnknownParamSyntaxError, "zone load zone_d='cn'")
  123. self.my_assert_raise(CmdUnknownParamSyntaxError, "zone reload_all zone_name = 'cn'")
  124. class TestNameSequence(unittest.TestCase):
  125. """
  126. Test if the module/command/parameters is saved in the order creation
  127. """
  128. def _create_bindcmd(self):
  129. """Create one bindcmd"""
  130. self._cmd = CommandInfo(name = "load")
  131. self.module = ModuleInfo(name = "zone")
  132. self.tool = bindcmd.BindCmdInterpreter()
  133. for random_str in self.random_names:
  134. self._cmd.add_param(ParamInfo(name = random_str))
  135. self.module.add_command(CommandInfo(name = random_str))
  136. self.tool.add_module_info(ModuleInfo(name = random_str))
  137. def setUp(self):
  138. self.random_names = ['1erdfeDDWsd', '3fe', '2009erd', 'Fe231', 'tere142', 'rei8WD']
  139. self._create_bindcmd()
  140. def testSequence(self):
  141. param_names = self._cmd.get_param_names()
  142. cmd_names = self.module.get_command_names()
  143. module_names = self.tool.get_module_names()
  144. i = 0
  145. while i < len(self.random_names):
  146. assert self.random_names[i] == param_names[i+1]
  147. assert self.random_names[i] == cmd_names[i+1]
  148. assert self.random_names[i] == module_names[i+1]
  149. i = i + 1
  150. if __name__== "__main__":
  151. unittest.main()