package_test.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. if test[:action] == 'install'
  295. begin
  296. package = Package.install(string: test[:zpm])
  297. rescue => e
  298. puts 'ERROR: ' + e.inspect
  299. end
  300. if test[:result]
  301. assert(package, 'install package not successful')
  302. issues = package.verify
  303. assert_not(issues, 'package verify not successful')
  304. else
  305. assert_not(package, 'install package successful but should not')
  306. end
  307. elsif test[:action] == 'reinstall'
  308. begin
  309. package = Package.reinstall(test[:name])
  310. rescue
  311. package = false
  312. end
  313. if test[:result]
  314. assert(package, 'reinstall package not successful')
  315. issues = package.verify
  316. assert_not(issues, 'package verify not successful')
  317. else
  318. assert_not(package, 'reinstall package successful but should not')
  319. end
  320. elsif test[:action] == 'uninstall'
  321. if test[:zpm]
  322. begin
  323. package = Package.uninstall(string: test[:zpm])
  324. rescue
  325. package = false
  326. end
  327. else
  328. begin
  329. package = Package.uninstall(name: test[:name], version: test[:version])
  330. rescue
  331. package = false
  332. end
  333. end
  334. if test[:result]
  335. assert(package, 'uninstall package not successful')
  336. else
  337. assert_not(package, 'uninstall package successful but should not')
  338. end
  339. elsif test[:action] == 'auto_install'
  340. if test[:zpm]
  341. if !File.exist?(Rails.root.to_s + '/auto_install/')
  342. Dir.mkdir(Rails.root.to_s + '/auto_install/', 0o755)
  343. end
  344. location = Rails.root.to_s + '/auto_install/unittest.zpm'
  345. file = File.new(location, 'wb')
  346. file.write(test[:zpm])
  347. file.close
  348. end
  349. begin
  350. success = Package.auto_install()
  351. rescue
  352. success = false
  353. end
  354. if test[:zpm]
  355. File.delete(location )
  356. end
  357. end
  358. if test[:verify] && test[:verify][:package]
  359. exists = Package.where(name: test[:verify][:package][:name], version: test[:verify][:package][:version]).first
  360. assert(exists, "package '#{test[:verify][:package][:name]}' is not installed" )
  361. end
  362. next if !test[:verify]
  363. next if !test[:verify][:check_files]
  364. test[:verify][:check_files].each do |item|
  365. exists = File.exist?(item[:location])
  366. if item[:result]
  367. assert(exists, "'#{item[:location]}' exists" )
  368. else
  369. assert_not(exists, "'#{item[:location]}' doesn't exists" )
  370. end
  371. end
  372. end
  373. end
  374. end