package_test.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class PackageTest < ActiveSupport::TestCase
  4. self.use_transactional_tests = false
  5. test 'packages' do
  6. tests = [
  7. # test 1 - normal install
  8. {
  9. zpm: '{
  10. "name": "UnitTestSample",
  11. "version": "1.0.1",
  12. "vendor": "Znuny GmbH",
  13. "license": "ABC",
  14. "url": "http://znuny.org/",
  15. "description": [
  16. {
  17. "language": "en",
  18. "text": "some description"
  19. }
  20. ],
  21. "files": [
  22. {
  23. "permission": "644",
  24. "location": "test.txt",
  25. "content": "YWJjw6TDtsO8w58="
  26. },
  27. {
  28. "permission": "644",
  29. "location": "some/dir/test.txt",
  30. "content": "YWJjw6TDtsO8w58="
  31. },
  32. {
  33. "permission": "644",
  34. "location": "db/addon/unit_test_sample/20121212000001_create_base.rb",
  35. "content": "Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uWzQuMl0NCiAgZGVmIHNlbGYudXANCiAgIGNyZWF0ZV90YWJsZSA6c2FtcGxlX3RhYmxlcyBkbyB8dHwNCiAgICAgIHQuY29sdW1uIDpuYW1lLCAgICAgICAgICAgOnN0cmluZywgOmxpbWl0ID0+IDE1MCwgIDpudWxsID0+IHRydWUNCiAgICAgIHQuY29sdW1uIDpkYXRhLCAgICAgICAgICAgOnN0cmluZywgOmxpbWl0ID0+IDUwMDAsIDpudWxsID0+IHRydWUNCiAgICBlbmQNCiAgZW5kDQoNCiAgZGVmIHNlbGYuZG93bg0KICAgIGRyb3BfdGFibGUgOnNhbXBsZV90YWJsZXMNCiAgZW5kDQplbmQ="
  36. }
  37. ]
  38. }',
  39. action: 'install',
  40. result: true,
  41. verify: {
  42. package: {
  43. name: 'UnitTestSample',
  44. version: '1.0.1',
  45. },
  46. check_files: [
  47. {
  48. location: 'test.txt',
  49. result: true,
  50. },
  51. {
  52. location: 'test2.txt',
  53. result: false,
  54. },
  55. {
  56. location: 'some/dir/test.txt',
  57. result: true,
  58. },
  59. ],
  60. },
  61. },
  62. # test 2 - renstall
  63. {
  64. action: 'reinstall',
  65. name: 'UnitTestSample',
  66. result: true,
  67. verify: {
  68. package: {
  69. name: 'UnitTestSample',
  70. version: '1.0.1',
  71. },
  72. check_files: [
  73. {
  74. location: 'test.txt',
  75. result: true,
  76. },
  77. {
  78. location: 'test2.txt',
  79. result: false,
  80. },
  81. {
  82. location: 'some/dir/test.txt',
  83. result: true,
  84. },
  85. ],
  86. },
  87. },
  88. # test 3 - try to install same package again / should not work
  89. {
  90. zpm: '{
  91. "name": "UnitTestSample",
  92. "version": "1.0.1",
  93. "vendor": "Znuny GmbH",
  94. "license": "ABC",
  95. "url": "http://znuny.org/",
  96. "description": [
  97. {
  98. "language": "en",
  99. "text": "some description"
  100. }
  101. ],
  102. "files": [
  103. {
  104. "permission": "644",
  105. "location": "test.txt",
  106. "content": "YWJjw6TDtsO8w58="
  107. }
  108. ]
  109. }',
  110. action: 'install',
  111. result: false,
  112. },
  113. # test 4 - try to install lower version / should not work
  114. {
  115. zpm: '{
  116. "name": "UnitTestSample",
  117. "version": "1.0.0",
  118. "vendor": "Znuny GmbH",
  119. "license": "ABC",
  120. "url": "http://znuny.org/",
  121. "description": [
  122. {
  123. "language": "en",
  124. "text": "some description"
  125. }
  126. ],
  127. "files": [
  128. {
  129. "permission": "644",
  130. "location": "test.txt",
  131. "content": "YWJjw6TDtsO8w58="
  132. }
  133. ]
  134. }',
  135. action: 'install',
  136. result: false,
  137. },
  138. # test 5 - upgrade 7 should work
  139. {
  140. zpm: '{
  141. "name": "UnitTestSample",
  142. "version": "1.0.2",
  143. "vendor": "Znuny GmbH",
  144. "license": "ABC",
  145. "url": "http://znuny.org/",
  146. "description": [
  147. {
  148. "language": "en",
  149. "text": "some description"
  150. }
  151. ],
  152. "files": [
  153. {
  154. "permission": "644",
  155. "location": "test.txt2",
  156. "content": "YWJjw6TDtsO8w58="
  157. },
  158. {
  159. "permission": "644",
  160. "location": "some/dir/test.txt2",
  161. "content": "YWJjw6TDtsO8w58="
  162. },
  163. {
  164. "permission": "644",
  165. "location": "db/addon/unit_test_sample/20121212000001_create_base.rb",
  166. "content": "Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uWzQuMl0NCiAgZGVmIHNlbGYudXANCiAgIGNyZWF0ZV90YWJsZSA6c2FtcGxlX3RhYmxlcyBkbyB8dHwNCiAgICAgIHQuY29sdW1uIDpuYW1lLCAgICAgICAgICAgOnN0cmluZywgOmxpbWl0ID0+IDE1MCwgIDpudWxsID0+IHRydWUNCiAgICAgIHQuY29sdW1uIDpkYXRhLCAgICAgICAgICAgOnN0cmluZywgOmxpbWl0ID0+IDUwMDAsIDpudWxsID0+IHRydWUNCiAgICBlbmQNCiAgZW5kDQoNCiAgZGVmIHNlbGYuZG93bg0KICAgIGRyb3BfdGFibGUgOnNhbXBsZV90YWJsZXMNCiAgZW5kDQplbmQ="
  167. }
  168. ]
  169. }',
  170. action: 'install',
  171. result: true,
  172. verify: {
  173. package: {
  174. name: 'UnitTestSample',
  175. version: '1.0.2',
  176. },
  177. check_files: [
  178. {
  179. location: 'test.txt2',
  180. result: true,
  181. },
  182. {
  183. location: 'test.txt',
  184. result: false,
  185. },
  186. {
  187. location: 'test2.txt',
  188. result: false,
  189. },
  190. {
  191. location: 'some/dir/test.txt2',
  192. result: true,
  193. },
  194. ],
  195. },
  196. },
  197. # test 6 - uninstall package / should work
  198. {
  199. name: 'UnitTestSample',
  200. version: '1.0.2',
  201. action: 'uninstall',
  202. result: true,
  203. verify: {
  204. check_files: [
  205. {
  206. location: 'test.txt',
  207. result: false,
  208. },
  209. {
  210. location: 'test2.txt',
  211. result: false,
  212. },
  213. ],
  214. },
  215. },
  216. # test 7 - check auto_install mechanism
  217. {
  218. zpm: '{
  219. "name": "UnitTestSample",
  220. "version": "1.0.2",
  221. "vendor": "Znuny GmbH",
  222. "license": "ABC",
  223. "url": "http://znuny.org/",
  224. "description": [
  225. {
  226. "language": "en",
  227. "text": "some description"
  228. }
  229. ],
  230. "files": [
  231. {
  232. "permission": "644",
  233. "location": "test.txt2",
  234. "content": "YWJjw6TDtsO8w58="
  235. },
  236. {
  237. "permission": "644",
  238. "location": "some/dir/test.txt2",
  239. "content": "YWJjw6TDtsO8w58="
  240. },
  241. {
  242. "permission": "644",
  243. "location": "db/addon/unit_test_sample/20121212000001_create_base.rb",
  244. "content": "Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uWzQuMl0NCiAgZGVmIHNlbGYudXANCiAgIGNyZWF0ZV90YWJsZSA6c2FtcGxlX3RhYmxlcyBkbyB8dHwNCiAgICAgIHQuY29sdW1uIDpuYW1lLCAgICAgICAgICAgOnN0cmluZywgOmxpbWl0ID0+IDE1MCwgIDpudWxsID0+IHRydWUNCiAgICAgIHQuY29sdW1uIDpkYXRhLCAgICAgICAgICAgOnN0cmluZywgOmxpbWl0ID0+IDUwMDAsIDpudWxsID0+IHRydWUNCiAgICBlbmQNCiAgZW5kDQoNCiAgZGVmIHNlbGYuZG93bg0KICAgIGRyb3BfdGFibGUgOnNhbXBsZV90YWJsZXMNCiAgZW5kDQplbmQ="
  245. }
  246. ]
  247. }',
  248. action: 'auto_install',
  249. result: true,
  250. verify: {
  251. package: {
  252. name: 'UnitTestSample',
  253. version: '1.0.2',
  254. },
  255. check_files: [
  256. {
  257. location: 'test.txt2',
  258. result: true,
  259. },
  260. {
  261. location: 'test.txt',
  262. result: false,
  263. },
  264. {
  265. location: 'test2.txt',
  266. result: false,
  267. },
  268. {
  269. location: 'some/dir/test.txt2',
  270. result: true,
  271. },
  272. ],
  273. },
  274. },
  275. # test 8 - check uninstall / should work
  276. {
  277. name: 'UnitTestSample',
  278. version: '1.0.2',
  279. action: 'uninstall',
  280. result: true,
  281. verify: {
  282. check_files: [
  283. {
  284. location: 'test.txt',
  285. result: false,
  286. },
  287. {
  288. location: 'test2.txt',
  289. result: false,
  290. },
  291. ],
  292. },
  293. },
  294. ]
  295. tests.each do |test|
  296. if test[:action] == 'install'
  297. begin
  298. package = Package.install(string: test[:zpm])
  299. rescue => e
  300. puts 'ERROR: ' + e.inspect
  301. end
  302. if test[:result]
  303. assert(package, 'install package not successful')
  304. issues = package.verify
  305. assert(!issues, 'package verify not successful')
  306. else
  307. assert(!package, 'install package successful but should not')
  308. end
  309. elsif test[:action] == 'reinstall'
  310. begin
  311. package = Package.reinstall(test[:name])
  312. rescue
  313. package = false
  314. end
  315. if test[:result]
  316. assert(package, 'reinstall package not successful')
  317. issues = package.verify
  318. assert(!issues, 'package verify not successful')
  319. else
  320. assert(!package, 'reinstall package successful but should not')
  321. end
  322. elsif test[:action] == 'uninstall'
  323. if test[:zpm]
  324. begin
  325. package = Package.uninstall(string: test[:zpm])
  326. rescue
  327. package = false
  328. end
  329. else
  330. begin
  331. package = Package.uninstall(name: test[:name], version: test[:version])
  332. rescue
  333. package = false
  334. end
  335. end
  336. if test[:result]
  337. assert(package, 'uninstall package not successful')
  338. else
  339. assert(!package, 'uninstall package successful but should not')
  340. end
  341. elsif test[:action] == 'auto_install'
  342. if test[:zpm]
  343. if !File.exist?(Rails.root.to_s + '/auto_install/')
  344. Dir.mkdir(Rails.root.to_s + '/auto_install/', 0o755)
  345. end
  346. location = Rails.root.to_s + '/auto_install/unittest.zpm'
  347. file = File.new(location, 'wb')
  348. file.write(test[:zpm])
  349. file.close
  350. end
  351. begin
  352. success = Package.auto_install()
  353. rescue
  354. success = false
  355. end
  356. if test[:zpm]
  357. File.delete(location )
  358. end
  359. end
  360. if test[:verify] && test[:verify][:package]
  361. exists = Package.where(name: test[:verify][:package][:name], version: test[:verify][:package][:version]).first
  362. assert(exists, "package '#{test[:verify][:package][:name]}' is not installed" )
  363. end
  364. next if !test[:verify]
  365. next if !test[:verify][:check_files]
  366. test[:verify][:check_files].each do |item|
  367. exists = File.exist?(item[:location])
  368. if item[:result]
  369. assert(exists, "'#{item[:location]}' exists" )
  370. else
  371. assert(!exists, "'#{item[:location]}' doesn't exists" )
  372. end
  373. end
  374. end
  375. end
  376. end