package_test.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. # encoding: utf-8
  2. require '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": "Znuny GmbH",
  12. "license": "ABC",
  13. "url": "http://znuny.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": "Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uDQogIGRlZiBzZWxmLnVw\nDQogICBjcmVhdGVfdGFibGUgOnNhbXBsZV90YWJsZXMgZG8gfHR8DQogICAgICB0LmNvbHVtbiA6\nbmFtZSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiAxNTAsICA6bnVsbCA9PiB0cnVlDQog\nICAgICB0LmNvbHVtbiA6ZGF0YSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiA1MDAwLCA6\nbnVsbCA9PiB0cnVlDQogICAgZW5kDQogIGVuZA0KDQogIGRlZiBzZWxmLmRvd24NCiAgICBkcm9w\nX3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k"
  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": "Znuny GmbH",
  93. "license": "ABC",
  94. "url": "http://znuny.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": "Znuny GmbH",
  118. "license": "ABC",
  119. "url": "http://znuny.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": "Znuny GmbH",
  143. "license": "ABC",
  144. "url": "http://znuny.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": "Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uDQogIGRlZiBzZWxmLnVw\nDQogICBjcmVhdGVfdGFibGUgOnNhbXBsZV90YWJsZXMgZG8gfHR8DQogICAgICB0LmNvbHVtbiA6\nbmFtZSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiAxNTAsICA6bnVsbCA9PiB0cnVlDQog\nICAgICB0LmNvbHVtbiA6ZGF0YSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiA1MDAwLCA6\nbnVsbCA9PiB0cnVlDQogICAgZW5kDQogIGVuZA0KDQogIGRlZiBzZWxmLmRvd24NCiAgICBkcm9w\nX3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k"
  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": "Znuny GmbH",
  221. "license": "ABC",
  222. "url": "http://znuny.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": "Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uDQogIGRlZiBzZWxmLnVw\nDQogICBjcmVhdGVfdGFibGUgOnNhbXBsZV90YWJsZXMgZG8gfHR8DQogICAgICB0LmNvbHVtbiA6\nbmFtZSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiAxNTAsICA6bnVsbCA9PiB0cnVlDQog\nICAgICB0LmNvbHVtbiA6ZGF0YSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiA1MDAwLCA6\nbnVsbCA9PiB0cnVlDQogICAgZW5kDQogIGVuZA0KDQogIGRlZiBzZWxmLmRvd24NCiAgICBkcm9w\nX3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k"
  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 { |test|
  295. if test[:action] == '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( !issues, 'package verify not successful' )
  305. else
  306. assert( !package, 'install package successful but should not' )
  307. end
  308. elsif test[:action] == '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( !issues, 'package verify not successful' )
  318. else
  319. assert( !package, 'reinstall package successful but should not' )
  320. end
  321. elsif test[:action] == '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( !package, 'uninstall package successful but should not' )
  339. end
  340. elsif test[:action] == 'auto_install'
  341. if test[:zpm]
  342. if !File.exist?( Rails.root.to_s + '/auto_install/' )
  343. Dir.mkdir( Rails.root.to_s + '/auto_install/', 0o755)
  344. end
  345. location = Rails.root.to_s + '/auto_install/unittest.zpm'
  346. file = File.new( location, 'wb' )
  347. file.write( test[:zpm] )
  348. file.close
  349. end
  350. begin
  351. success = Package.auto_install()
  352. rescue
  353. success = 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 { |item|
  366. exists = File.exist?( item[:location] )
  367. if item[:result]
  368. assert( exists, "'#{item[:location]}' exists" )
  369. else
  370. assert( !exists, "'#{item[:location]}' doesn't exists" )
  371. end
  372. }
  373. }
  374. end
  375. end