/tests/uni_tests/test_dgm_uni.py

https://github.com/pv-bull/sequencer · Python · 300 lines · 243 code · 26 blank · 31 comment · 14 complexity · cdb35f4ac5a6c257386fc27b37ccc0f2 MD5 · raw file

  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. import ConfigParser
  4. import os, sys
  5. import codecs
  6. import logging
  7. from commons import MinimalInitTest, FullInitTest, Capturer, BASEDIR, \
  8. TESTFILE, SRCDIR
  9. from sequencer.commons import to_unicode
  10. from sequencer.dgm import cli as dgm_cli
  11. def fetch_and_check(rsfile, section, option, value):
  12. """
  13. Checks in the file 'rsfile' if the given option in the given section is
  14. equal to 'value'.
  15. """
  16. cp = ConfigParser.SafeConfigParser()
  17. try:
  18. with codecs.open(rsfile, 'r', encoding='utf-8') as f:
  19. cp.readfp(f)
  20. except IOError:
  21. return "OMGBBQ"
  22. if cp.has_section(section) and cp.has_option(section, option):
  23. return value == cp.get(section, option)
  24. else:
  25. return False
  26. def compare_to_ref(funcName, result):
  27. """
  28. For a unit test 'funcName', returns the expected value of a test result,
  29. stored in the file test_ref_values. The char '*' is used to preserve spaces
  30. at the end and at the beginning of a line.
  31. """
  32. cp = ConfigParser.SafeConfigParser()
  33. try:
  34. with codecs.open(SRCDIR+"/test_ref_values", 'r', encoding='utf-8') as f:
  35. cp.readfp(f)
  36. except IOError:
  37. return "OMGBBQ"
  38. if cp.has_section(funcName) and cp.has_option(funcName, result):
  39. val = cp.get(funcName, result).replace('\\n', '\n')
  40. val = val.replace('*', '')
  41. return val
  42. else:
  43. return "ALBATROS!"
  44. class DGMUnicodeTest(MinimalInitTest):
  45. def test_dbcreate(self):
  46. dgm_cli.dbcreate(self.db, self.config, [])
  47. assert os.path.exists(BASEDIR)
  48. class DGMUnicodeFullTest(FullInitTest):
  49. def test_dbdrop(self):
  50. os.remove(BASEDIR+"/config")
  51. dgm_cli.dbdrop(self.db, self.config, ["-E"])
  52. assert os.path.exists(BASEDIR) == False
  53. # required for tearDown
  54. os.mkdir(BASEDIR)
  55. def test_dbadd_create_ruleset(self):
  56. uniargs = [u"mmąöî666", u"mmąöî", u"fake@mmąöî", u"mmąöî", u"mmąöî", \
  57. u"mmąöî", u"mmąöî", u"mmąöî", u"mmąöî"]
  58. dgm_cli.dbadd(self.db, self.config, uniargs)
  59. filepath = os.path.join(BASEDIR, u"mmąöî666.rs")
  60. assert os.path.exists(filepath)
  61. with open(filepath) as f1:
  62. with open(TESTFILE) as f2:
  63. assert f1.read() == f2.read()
  64. def test_dbadd_nocreate(self):
  65. uniargs = [u"testmmąöî", u"mmąöîname", u"fake@mmąöîtype", \
  66. u"mmąöîfilter", u"mmąöîaction", u"mmąöîdeps", \
  67. u"mmąöîdepends", u"mmąöîcomments", u"mmąöîhelp"]
  68. dgm_cli.dbadd(self.db, self.config, uniargs)
  69. assert os.path.exists(TESTFILE)
  70. assert fetch_and_check(TESTFILE, u"mmąöîname", "types", u"fake@mmąöîtype")
  71. assert fetch_and_check(TESTFILE, u"mmąöîname", "filter", u"mmąöîfilter")
  72. assert fetch_and_check(TESTFILE, u"mmąöîname", "action", u"mmąöîaction")
  73. assert fetch_and_check(TESTFILE, u"mmąöîname", "depsfinder", u"mmąöîdeps")
  74. assert fetch_and_check(TESTFILE, u"mmąöîname", "dependson", u"mmąöîdepends")
  75. assert fetch_and_check(TESTFILE, u"mmąöîname", "comments", u"mmąöîcomments")
  76. assert fetch_and_check(TESTFILE, u"mmąöîname", "help", u"mmąöîhelp")
  77. def test_dbremove_rule(self):
  78. with open(TESTFILE) as f1:
  79. assert f1.read() != ""
  80. uniargs = [u"-E", u"testmmąöî", u"mmąöî"]
  81. dgm_cli.dbremove(self.db, self.config, uniargs)
  82. assert os.path.exists(TESTFILE)
  83. with open(TESTFILE) as f1:
  84. assert f1.read() == ""
  85. def test_dbremove_ruleset(self):
  86. uniargs_add = [u"testmmąöî", u"mmąöî2", u"fake@mmąöî", u"mmąöî", \
  87. u"mmąöî", u"mmąöî", u"mmąöî", u"mmąöî", u"mmąöî"]
  88. dgm_cli.dbadd(self.db, self.config, uniargs_add)
  89. uniargs = [u"-E", u"testmmąöî"]
  90. dgm_cli.dbremove(self.db, self.config, uniargs)
  91. assert os.path.exists(TESTFILE) #it's not a bug, it's a feature...
  92. with open(TESTFILE) as f1:
  93. assert f1.read() == ""
  94. def test_dbupdate(self):
  95. uniargs = [u"testmmąöî", u"mmąöî", u"help=updatemmąöî", \
  96. u"comments=updatemmąöî"]
  97. dgm_cli.dbupdate(self.db, self.config, uniargs)
  98. assert fetch_and_check(TESTFILE, u"mmąöî", "help", u"updatemmąöî")
  99. assert fetch_and_check(TESTFILE, u"mmąöî", "comments", u"updatemmąöî")
  100. def test_dbupdate_deps(self):
  101. # Add a rule that depends on the one in the test file (mmąöî)
  102. uniargs_add = [u"testmmąöî", u"mmąöî2", u"fake@mmąöî", u"mmąöî", \
  103. u"mmąöî", u"mmąöî", u"mmąöî", u"mmąöî", u"mmąöî"]
  104. dgm_cli.dbadd(self.db, self.config, uniargs_add)
  105. # Update the name of the first one and update references
  106. uniargs = [u"testmmąöî", u"mmąöî", u"name=updatemmąöî"]
  107. dgm_cli.dbupdate(self.db, self.config, uniargs)
  108. assert not fetch_and_check(TESTFILE, u"mmąöî", "help", u"mmąöî")
  109. assert fetch_and_check(TESTFILE, u"updatemmąöî", "help", u"mmąöî")
  110. assert fetch_and_check(TESTFILE, u"mmąöî2", "dependson", u"updatemmąöî")
  111. def test_dbupdate_nodeps(self):
  112. # Add a rule that depends on the one in the test file (mmąöî)
  113. uniargs_add = [u"testmmąöî", u"mmąöî2", u"fake@mmąöî", u"mmąöî", \
  114. u"mmąöî", u"mmąöî", u"mmąöî", u"mmąöî", u"mmąöî"]
  115. dgm_cli.dbadd(self.db, self.config, uniargs_add)
  116. # Update the name of the first one and don't update references
  117. uniargs = [u"--nodeps", u"testmmąöî", u"mmąöî", u"name=updatemmąöî"]
  118. dgm_cli.dbupdate(self.db, self.config, uniargs)
  119. # TODO is this a bug? cf dgm.model:312. The tests should be:
  120. #assert not fetch_and_check(TESTFILE, u"mmąöî", "help", u"mmąöî")
  121. #assert fetch_and_check(TESTFILE, u"updatemmąöî", "help", u"mmąöî")
  122. #assert fetch_and_check(TESTFILE, u"mmąöî2", "dependson", u"mmąöî")
  123. assert fetch_and_check(TESTFILE, u"mmąöî", "help", u"mmąöî")
  124. assert fetch_and_check(TESTFILE, u"mmąöî2", "dependson", u"mmąöî")
  125. def test_dbupdate_change_ruleset_name(self):
  126. uniargs = [u"testmmąöî", u"mmąöî", u"ruleset=updatemmąöî", \
  127. u"comments=updatemmąöî"]
  128. dgm_cli.dbupdate(self.db, self.config, uniargs)
  129. filepath = os.path.join(BASEDIR, u"updatemmąöî.rs")
  130. assert os.path.exists(filepath)
  131. assert fetch_and_check(filepath, u"mmąöî", "comments", u"updatemmąöî")
  132. assert not fetch_and_check(TESTFILE, u"mmąöî", "comments", \
  133. u"updatemmąöî")
  134. def test_dbcopy_rule_create(self):
  135. uniargs = [u"testmmąöî:mmąöî", u"mmąöî666"]
  136. dgm_cli.dbcopy(self.db, self.config, uniargs)
  137. filepath = os.path.join(BASEDIR, u"mmąöî666.rs")
  138. assert os.path.exists(filepath)
  139. with open(filepath) as f1:
  140. with open(TESTFILE) as f2:
  141. assert f1.read() == f2.read()
  142. def test_dbcopy_rule_nocreate(self):
  143. # Create a second rs
  144. uniargs_add = [u"mmąöî666", u"mmąöî", u"fake@mmąöî", u"mmąöî", u"mmąöî", \
  145. u"mmąöî", u"mmąöî", u"mmąöî", u"mmąöî"]
  146. dgm_cli.dbadd(self.db, self.config, uniargs_add)
  147. # Empty second rs
  148. uniargs_rem = [u"-E", u"mmąöî666"]
  149. dgm_cli.dbremove(self.db, self.config, uniargs_rem)
  150. filepath = os.path.join(BASEDIR, u"mmąöî666.rs")
  151. # Test if still here and empty
  152. assert os.path.exists(filepath)
  153. with open(filepath) as f1:
  154. assert f1.read() == ""
  155. # Copy from test to second rs
  156. uniargs = [u"testmmąöî:mmąöî", u"mmąöî666"]
  157. dgm_cli.dbcopy(self.db, self.config, uniargs)
  158. with open(filepath) as f1:
  159. with open(TESTFILE) as f2:
  160. assert f1.read() == f2.read()
  161. def test_dbcopy_ruleset_create(self):
  162. uniargs = [u"testmmąöî", u"mmąöî666"]
  163. dgm_cli.dbcopy(self.db, self.config, uniargs)
  164. filepath = os.path.join(BASEDIR, u"mmąöî666.rs")
  165. assert os.path.exists(filepath)
  166. with open(filepath) as f1:
  167. with open(TESTFILE) as f2:
  168. assert f1.read() == f2.read()
  169. def test_dbcopy_ruleset_nocreate(self):
  170. # Create a second rs
  171. uniargs_add = [u"mmąöî666", u"mmąöî", u"fake@mmąöî", u"mmąöî", u"mmąöî", \
  172. u"mmąöî", u"mmąöî", u"mmąöî", u"mmąöî"]
  173. dgm_cli.dbadd(self.db, self.config, uniargs_add)
  174. # Empty second rs
  175. uniargs_rem = [u"-E", u"mmąöî666"]
  176. dgm_cli.dbremove(self.db, self.config, uniargs_rem)
  177. filepath = os.path.join(BASEDIR, u"mmąöî666.rs")
  178. # Test if still here and empty
  179. assert os.path.exists(filepath)
  180. with open(filepath) as f1:
  181. assert f1.read() == ""
  182. # Copy from test to second rs
  183. uniargs = [u"testmmąöî", u"mmąöî666"]
  184. dgm_cli.dbcopy(self.db, self.config, uniargs)
  185. with open(filepath) as f1:
  186. with open(TESTFILE) as f2:
  187. assert f1.read() == f2.read()
  188. def test_dbchecksum(self):
  189. #TODO catch stdout
  190. pass
  191. def test_graphrules_nofile(self):
  192. uniargs = [u"testmmąöî"]
  193. # The graph is sent to output using logger.output()
  194. capturer = Capturer(self.logger)
  195. dgm_cli.graphrules(self.db, self.config, uniargs)
  196. data = capturer.getCaptured().decode('utf-8')
  197. val = compare_to_ref("test_graphrules_nofile", "graph")
  198. assert data == val
  199. def test_graphrules_nofile_2rules(self):
  200. # Add a rule that depends on the one in the test file (mmąöî)
  201. uniargs_add = [u"testmmąöî", u"mmąöî2", u"fake@mmąöî", u"mmąöî", \
  202. u"mmąöî", u"mmąöî", u"mmąöî", u"mmąöî", u"mmąöî"]
  203. dgm_cli.dbadd(self.db, self.config, uniargs_add)
  204. uniargs = [u"testmmąöî"]
  205. # The graph is sent to output using logger.output()
  206. capturer = Capturer(self.logger)
  207. dgm_cli.graphrules(self.db, self.config, uniargs)
  208. data = capturer.getCaptured().decode('utf-8')
  209. val = compare_to_ref("test_graphrules_nofile_2rules", "graph")
  210. assert data == val
  211. def test_graphrules_file(self):
  212. pathgraph = os.path.join(BASEDIR, u"mmąöîgraph")
  213. uniargs = [u"--out="+pathgraph, u"testmmąöî"]
  214. dgm_cli.graphrules(self.db, self.config, uniargs)
  215. assert os.path.exists(pathgraph)
  216. with open(pathgraph) as f1:
  217. f1r = f1.read().decode('utf-8')
  218. val = compare_to_ref("test_graphrules_file", "graph")
  219. assert val == f1r
  220. def test_graphrules_file_2rules(self):
  221. # Add a rule that depends on the one in the test file (mmąöî)
  222. uniargs_add = [u"testmmąöî", u"mmąöî2", u"fake@mmąöî", u"mmąöî", \
  223. u"mmąöî", u"mmąöî", u"mmąöî", u"mmąöî", u"mmąöî"]
  224. dgm_cli.dbadd(self.db, self.config, uniargs_add)
  225. pathgraph = os.path.join(BASEDIR, u"mmąöîgraph")
  226. uniargs = [u"--out="+pathgraph, u"testmmąöî"]
  227. dgm_cli.graphrules(self.db, self.config, uniargs)
  228. assert os.path.exists(pathgraph)
  229. with open(pathgraph) as f1:
  230. f1r = f1.read().decode('utf-8')
  231. val = compare_to_ref("test_graphrules_file_2rules", "graph")
  232. assert val == f1r
  233. def test_knowntypes(self):
  234. uniargs = [u"testmmąöî"]
  235. # The table is sent to output using logger.output()
  236. capturer = Capturer(self.logger)
  237. dgm_cli.knowntypes(self.db, self.config, uniargs)
  238. data = capturer.getCaptured().decode('utf-8')
  239. val = compare_to_ref("test_knowntypes", "table")
  240. assert data == val
  241. def test_depmake(self):
  242. pathdep = os.path.join(BASEDIR, u"mmąöîdep")
  243. pathdot = os.path.join(BASEDIR, u"mmąöîdepdot")
  244. uniargs = [u"testmmąöî", u"127.0.0.1", u"-o", pathdep, \
  245. u"--depgraphto="+pathdot]
  246. dgm_cli.depmake(self.db, self.config, uniargs)
  247. with open(pathdep) as f1:
  248. with open(pathdot) as f2:
  249. print("F1\n%s" % f1.read())
  250. print("F2\n%s" % f2.read())
  251. #assert False
  252. #TODO test with real cmd? pings... having a rule that applies to the
  253. # components would be good
  254. #TODO compare files to something
  255. # def test_depmake_real(self):
  256. # uniargs_add = [u"pings", u"cmd", u"ALL", u"ALL", \
  257. # u"/bin/bash -c 'ping -nq -c 1 %name > /dev/null && echo Alive \
  258. # || echo Unreachable'", u"'ping -nq -c 1 %name'", u"NONE", \
  259. # u"NONE", u"'Parallel Ping'", u"Some Help"]
  260. # depmake avec un depsfinder uni name