package_test.rb 10 KB

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