package_test.rb 10 KB

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