package_test.rb 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 => '<?xml version="1.0"?>
  9. <zpm version="1.0">
  10. <name>UnitTestSample</name>
  11. <version>1.0.1</version>
  12. <vendor>Znuny GmbH</vendor>
  13. <url>http://znuny.org/</url>
  14. <license>ABC</license>
  15. <description lang="en">some description</description>
  16. <filelist>
  17. <file permission="644" location="test.txt">YWJjw6TDtsO8w58=</file>
  18. <file permission="644" location="some/dir/test.txt">YWJjw6TDtsO8w58=</file>
  19. <file permission="644" location="db/addon/unit_test_sample/20121212000001_create_base.rb">Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uDQogIGRlZiBzZWxmLnVw
  20. DQogICBjcmVhdGVfdGFibGUgOnNhbXBsZV90YWJsZXMgZG8gfHR8DQogICAgICB0LmNvbHVtbiA6
  21. bmFtZSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiAxNTAsICA6bnVsbCA9PiB0cnVlDQog
  22. ICAgICB0LmNvbHVtbiA6ZGF0YSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiA1MDAwLCA6
  23. bnVsbCA9PiB0cnVlDQogICAgZW5kDQogIGVuZA0KDQogIGRlZiBzZWxmLmRvd24NCiAgICBkcm9w
  24. X3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k</file>
  25. </filelist>
  26. </zpm>',
  27. :action => 'install',
  28. :result => true,
  29. :verify => {
  30. :package => {
  31. :name => 'UnitTestSample',
  32. :version => '1.0.1',
  33. },
  34. :check_files => [
  35. {
  36. :location => 'test.txt',
  37. :result => true,
  38. },
  39. {
  40. :location => 'test2.txt',
  41. :result => false,
  42. },
  43. {
  44. :location => 'some/dir/test.txt',
  45. :result => true,
  46. },
  47. ],
  48. },
  49. },
  50. # test 2 - try to install same package again / should not work
  51. {
  52. :zpm => '<?xml version="1.0"?>
  53. <zpm version="1.0">
  54. <name>UnitTestSample</name>
  55. <version>1.0.1</version>
  56. <vendor>Znuny GmbH</vendor>
  57. <url>http://znuny.org/</url>
  58. <license>ABC</license>
  59. <description lang="en">some description</description>
  60. <filelist>
  61. <file permission="644" location="test.txt">YWJjw6TDtsO8w58=</file>
  62. </filelist>
  63. </zpm>',
  64. :action => 'install',
  65. :result => false,
  66. },
  67. # test 3 - try to install lower version / should not work
  68. {
  69. :zpm => '<?xml version="1.0"?>
  70. <zpm version="1.0">
  71. <name>UnitTestSample</name>
  72. <version>1.0.0</version>
  73. <vendor>Znuny GmbH</vendor>
  74. <url>http://znuny.org/</url>
  75. <license>ABC</license>
  76. <description lang="en">some description</description>
  77. <filelist>
  78. <file permission="644" location="test.txt">YWJjw6TDtsO8w58=</file>
  79. </filelist>
  80. </zpm>',
  81. :action => 'install',
  82. :result => false,
  83. },
  84. # test 4 - upgrade 7 should work
  85. {
  86. :zpm => '<?xml version="1.0"?>
  87. <zpm version="1.0">
  88. <name>UnitTestSample</name>
  89. <version>1.0.2</version>
  90. <vendor>Znuny GmbH</vendor>
  91. <url>http://znuny.org/</url>
  92. <license>ABC</license>
  93. <description lang="en">some description</description>
  94. <filelist>
  95. <file permission="644" location="test.txt2">YWJjw6TDtsO8w58=</file>
  96. <file permission="644" location="some/dir/test.txt2">YWJjw6TDtsO8w58=</file>
  97. <file permission="644" location="db/addon/unit_test_sample/20121212000001_create_base.rb">Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uDQogIGRlZiBzZWxmLnVw
  98. DQogICBjcmVhdGVfdGFibGUgOnNhbXBsZV90YWJsZXMgZG8gfHR8DQogICAgICB0LmNvbHVtbiA6
  99. bmFtZSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiAxNTAsICA6bnVsbCA9PiB0cnVlDQog
  100. ICAgICB0LmNvbHVtbiA6ZGF0YSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiA1MDAwLCA6
  101. bnVsbCA9PiB0cnVlDQogICAgZW5kDQogIGVuZA0KDQogIGRlZiBzZWxmLmRvd24NCiAgICBkcm9w
  102. X3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k</file>
  103. </filelist>
  104. </zpm>',
  105. :action => 'install',
  106. :result => true,
  107. :verify => {
  108. :package => {
  109. :name => 'UnitTestSample',
  110. :version => '1.0.2',
  111. },
  112. :check_files => [
  113. {
  114. :location => 'test.txt2',
  115. :result => true,
  116. },
  117. {
  118. :location => 'test.txt',
  119. :result => false,
  120. },
  121. {
  122. :location => 'test2.txt',
  123. :result => false,
  124. },
  125. {
  126. :location => 'some/dir/test.txt2',
  127. :result => true,
  128. },
  129. ],
  130. },
  131. },
  132. # test 4 - uninstall package / should work
  133. {
  134. :name => 'UnitTestSample',
  135. :version => '1.0.2',
  136. :action => 'uninstall',
  137. :result => true,
  138. :verify => {
  139. :check_files => [
  140. {
  141. :location => 'test.txt',
  142. :result => false,
  143. },
  144. {
  145. :location => 'test2.txt',
  146. :result => false,
  147. },
  148. ],
  149. },
  150. },
  151. # test 5 - check auto_install mechanism
  152. {
  153. :zpm => '<?xml version="1.0"?>
  154. <zpm version="1.0">
  155. <name>UnitTestSample</name>
  156. <version>1.0.2</version>
  157. <vendor>Znuny GmbH</vendor>
  158. <url>http://znuny.org/</url>
  159. <license>ABC</license>
  160. <description lang="en">some description</description>
  161. <filelist>
  162. <file permission="644" location="test.txt2">YWJjw6TDtsO8w58=</file>
  163. <file permission="644" location="some/dir/test.txt2">YWJjw6TDtsO8w58=</file>
  164. <file permission="644" location="db/addon/unit_test_sample/20121212000001_create_base.rb">Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uDQogIGRlZiBzZWxmLnVw
  165. DQogICBjcmVhdGVfdGFibGUgOnNhbXBsZV90YWJsZXMgZG8gfHR8DQogICAgICB0LmNvbHVtbiA6
  166. bmFtZSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiAxNTAsICA6bnVsbCA9PiB0cnVlDQog
  167. ICAgICB0LmNvbHVtbiA6ZGF0YSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiA1MDAwLCA6
  168. bnVsbCA9PiB0cnVlDQogICAgZW5kDQogIGVuZA0KDQogIGRlZiBzZWxmLmRvd24NCiAgICBkcm9w
  169. X3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k</file>
  170. </filelist>
  171. </zpm>',
  172. :action => 'auto_install',
  173. :result => true,
  174. :verify => {
  175. :package => {
  176. :name => 'UnitTestSample',
  177. :version => '1.0.2',
  178. },
  179. :check_files => [
  180. {
  181. :location => 'test.txt2',
  182. :result => true,
  183. },
  184. {
  185. :location => 'test.txt',
  186. :result => false,
  187. },
  188. {
  189. :location => 'test2.txt',
  190. :result => false,
  191. },
  192. {
  193. :location => 'some/dir/test.txt2',
  194. :result => true,
  195. },
  196. ],
  197. },
  198. },
  199. # test 6 - check uninstall / should work
  200. {
  201. :name => 'UnitTestSample',
  202. :version => '1.0.2',
  203. :action => 'uninstall',
  204. :result => true,
  205. :verify => {
  206. :check_files => [
  207. {
  208. :location => 'test.txt',
  209. :result => false,
  210. },
  211. {
  212. :location => 'test2.txt',
  213. :result => false,
  214. },
  215. ],
  216. },
  217. },
  218. ]
  219. tests.each { |test|
  220. if test[:action] == 'install'
  221. begin
  222. success = Package.install( :string => test[:zpm] )
  223. rescue => e
  224. puts 'ERROR: ' + e.inspect
  225. success = false
  226. end
  227. if test[:result]
  228. assert( success, "install package not successful" )
  229. else
  230. assert( !success, "install package successful but should not" )
  231. end
  232. elsif test[:action] == 'uninstall'
  233. if test[:zpm]
  234. begin
  235. success = Package.uninstall( :string => test[:zpm] )
  236. rescue
  237. success = false
  238. end
  239. else
  240. begin
  241. success = Package.uninstall( :name => test[:name], :version => test[:version] )
  242. rescue
  243. success = false
  244. end
  245. end
  246. if test[:result]
  247. assert( success, "uninstall package not successful" )
  248. else
  249. assert( !success, "uninstall package successful but should not" )
  250. end
  251. elsif test[:action] == 'auto_install'
  252. if test[:zpm]
  253. if !File.exist?( Rails.root.to_s + '/auto_install/' )
  254. Dir.mkdir( Rails.root.to_s + '/auto_install/', 0755)
  255. end
  256. location = Rails.root.to_s + '/auto_install/unittest.zpm'
  257. file = File.new( location, 'wb' )
  258. file.write( test[:zpm] )
  259. file.close
  260. end
  261. begin
  262. success = Package.auto_install()
  263. rescue
  264. success = false
  265. end
  266. if test[:zpm]
  267. File.delete( location )
  268. end
  269. end
  270. if test[:verify] && test[:verify][:package]
  271. exists = Package.where( :name => test[:verify][:package][:name], :version => test[:verify][:package][:version] ).first
  272. assert( exists, "package '#{test[:verify][:package][:name]}' is not installed" )
  273. end
  274. if test[:verify] && test[:verify][:check_files]
  275. test[:verify][:check_files].each {|item|
  276. exists = File.exist?( item[:location] )
  277. if item[:result]
  278. assert( exists, "'#{item[:location]}' exists" )
  279. else
  280. assert( !exists, "'#{item[:location]}' doesn't exists" )
  281. end
  282. }
  283. end
  284. }
  285. end
  286. end