123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- # encoding: utf-8
- require 'test_helper'
- class PackageTest < ActiveSupport::TestCase
- test 'packages' do
- tests = [
- # test 1 - normal install
- {
- zpm: '{
- "name": "UnitTestSample",
- "version": "1.0.1",
- "vendor": "Znuny GmbH",
- "license": "ABC",
- "url": "http://znuny.org/",
- "description": [
- {
- "language": "en",
- "text": "some description"
- }
- ],
- "files": [
- {
- "permission": "644",
- "location": "test.txt",
- "content": "YWJjw6TDtsO8w58="
- },
- {
- "permission": "644",
- "location": "some/dir/test.txt",
- "content": "YWJjw6TDtsO8w58="
- },
- {
- "permission": "644",
- "location": "db/addon/unit_test_sample/20121212000001_create_base.rb",
- "content": "Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uDQogIGRlZiBzZWxmLnVw\nDQogICBjcmVhdGVfdGFibGUgOnNhbXBsZV90YWJsZXMgZG8gfHR8DQogICAgICB0LmNvbHVtbiA6\nbmFtZSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiAxNTAsICA6bnVsbCA9PiB0cnVlDQog\nICAgICB0LmNvbHVtbiA6ZGF0YSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiA1MDAwLCA6\nbnVsbCA9PiB0cnVlDQogICAgZW5kDQogIGVuZA0KDQogIGRlZiBzZWxmLmRvd24NCiAgICBkcm9w\nX3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k"
- }
- ]
- }',
- action: 'install',
- result: true,
- verify: {
- package: {
- name: 'UnitTestSample',
- version: '1.0.1',
- },
- check_files: [
- {
- location: 'test.txt',
- result: true,
- },
- {
- location: 'test2.txt',
- result: false,
- },
- {
- location: 'some/dir/test.txt',
- result: true,
- },
- ],
- },
- },
- # test 2 - renstall
- {
- action: 'reinstall',
- name: 'UnitTestSample',
- result: true,
- verify: {
- package: {
- name: 'UnitTestSample',
- version: '1.0.1',
- },
- check_files: [
- {
- location: 'test.txt',
- result: true,
- },
- {
- location: 'test2.txt',
- result: false,
- },
- {
- location: 'some/dir/test.txt',
- result: true,
- },
- ],
- },
- },
- # test 3 - try to install same package again / should not work
- {
- zpm: '{
- "name": "UnitTestSample",
- "version": "1.0.1",
- "vendor": "Znuny GmbH",
- "license": "ABC",
- "url": "http://znuny.org/",
- "description": [
- {
- "language": "en",
- "text": "some description"
- }
- ],
- "files": [
- {
- "permission": "644",
- "location": "test.txt",
- "content": "YWJjw6TDtsO8w58="
- }
- ]
- }',
- action: 'install',
- result: false,
- },
- # test 4 - try to install lower version / should not work
- {
- zpm: '{
- "name": "UnitTestSample",
- "version": "1.0.0",
- "vendor": "Znuny GmbH",
- "license": "ABC",
- "url": "http://znuny.org/",
- "description": [
- {
- "language": "en",
- "text": "some description"
- }
- ],
- "files": [
- {
- "permission": "644",
- "location": "test.txt",
- "content": "YWJjw6TDtsO8w58="
- }
- ]
- }',
- action: 'install',
- result: false,
- },
- # test 5 - upgrade 7 should work
- {
- zpm: '{
- "name": "UnitTestSample",
- "version": "1.0.2",
- "vendor": "Znuny GmbH",
- "license": "ABC",
- "url": "http://znuny.org/",
- "description": [
- {
- "language": "en",
- "text": "some description"
- }
- ],
- "files": [
- {
- "permission": "644",
- "location": "test.txt2",
- "content": "YWJjw6TDtsO8w58="
- },
- {
- "permission": "644",
- "location": "some/dir/test.txt2",
- "content": "YWJjw6TDtsO8w58="
- },
- {
- "permission": "644",
- "location": "db/addon/unit_test_sample/20121212000001_create_base.rb",
- "content": "Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uDQogIGRlZiBzZWxmLnVw\nDQogICBjcmVhdGVfdGFibGUgOnNhbXBsZV90YWJsZXMgZG8gfHR8DQogICAgICB0LmNvbHVtbiA6\nbmFtZSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiAxNTAsICA6bnVsbCA9PiB0cnVlDQog\nICAgICB0LmNvbHVtbiA6ZGF0YSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiA1MDAwLCA6\nbnVsbCA9PiB0cnVlDQogICAgZW5kDQogIGVuZA0KDQogIGRlZiBzZWxmLmRvd24NCiAgICBkcm9w\nX3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k"
- }
- ]
- }',
- action: 'install',
- result: true,
- verify: {
- package: {
- name: 'UnitTestSample',
- version: '1.0.2',
- },
- check_files: [
- {
- location: 'test.txt2',
- result: true,
- },
- {
- location: 'test.txt',
- result: false,
- },
- {
- location: 'test2.txt',
- result: false,
- },
- {
- location: 'some/dir/test.txt2',
- result: true,
- },
- ],
- },
- },
- # test 6 - uninstall package / should work
- {
- name: 'UnitTestSample',
- version: '1.0.2',
- action: 'uninstall',
- result: true,
- verify: {
- check_files: [
- {
- location: 'test.txt',
- result: false,
- },
- {
- location: 'test2.txt',
- result: false,
- },
- ],
- },
- },
- # test 7 - check auto_install mechanism
- {
- zpm: '{
- "name": "UnitTestSample",
- "version": "1.0.2",
- "vendor": "Znuny GmbH",
- "license": "ABC",
- "url": "http://znuny.org/",
- "description": [
- {
- "language": "en",
- "text": "some description"
- }
- ],
- "files": [
- {
- "permission": "644",
- "location": "test.txt2",
- "content": "YWJjw6TDtsO8w58="
- },
- {
- "permission": "644",
- "location": "some/dir/test.txt2",
- "content": "YWJjw6TDtsO8w58="
- },
- {
- "permission": "644",
- "location": "db/addon/unit_test_sample/20121212000001_create_base.rb",
- "content": "Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uDQogIGRlZiBzZWxmLnVw\nDQogICBjcmVhdGVfdGFibGUgOnNhbXBsZV90YWJsZXMgZG8gfHR8DQogICAgICB0LmNvbHVtbiA6\nbmFtZSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiAxNTAsICA6bnVsbCA9PiB0cnVlDQog\nICAgICB0LmNvbHVtbiA6ZGF0YSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiA1MDAwLCA6\nbnVsbCA9PiB0cnVlDQogICAgZW5kDQogIGVuZA0KDQogIGRlZiBzZWxmLmRvd24NCiAgICBkcm9w\nX3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k"
- }
- ]
- }',
- action: 'auto_install',
- result: true,
- verify: {
- package: {
- name: 'UnitTestSample',
- version: '1.0.2',
- },
- check_files: [
- {
- location: 'test.txt2',
- result: true,
- },
- {
- location: 'test.txt',
- result: false,
- },
- {
- location: 'test2.txt',
- result: false,
- },
- {
- location: 'some/dir/test.txt2',
- result: true,
- },
- ],
- },
- },
- # test 8 - check uninstall / should work
- {
- name: 'UnitTestSample',
- version: '1.0.2',
- action: 'uninstall',
- result: true,
- verify: {
- check_files: [
- {
- location: 'test.txt',
- result: false,
- },
- {
- location: 'test2.txt',
- result: false,
- },
- ],
- },
- },
- ]
- tests.each { |test|
- if test[:action] == 'install'
- begin
- package = Package.install( string: test[:zpm] )
- rescue => e
- puts 'ERROR: ' + e.inspect
- end
- if test[:result]
- assert( package, 'install package not successful' )
- issues = package.verify
- assert( !issues, 'package verify not successful' )
- else
- assert( !package, 'install package successful but should not' )
- end
- elsif test[:action] == 'reinstall'
- begin
- package = Package.reinstall( test[:name] )
- rescue
- package = false
- end
- if test[:result]
- assert( package, 'reinstall package not successful' )
- issues = package.verify
- assert( !issues, 'package verify not successful' )
- else
- assert( !package, 'reinstall package successful but should not' )
- end
- elsif test[:action] == 'uninstall'
- if test[:zpm]
- begin
- package = Package.uninstall( string: test[:zpm] )
- rescue
- package = false
- end
- else
- begin
- package = Package.uninstall( name: test[:name], version: test[:version] )
- rescue
- package = false
- end
- end
- if test[:result]
- assert( package, 'uninstall package not successful' )
- else
- assert( !package, 'uninstall package successful but should not' )
- end
- elsif test[:action] == 'auto_install'
- if test[:zpm]
- if !File.exist?( Rails.root.to_s + '/auto_install/' )
- Dir.mkdir( Rails.root.to_s + '/auto_install/', 0o755)
- end
- location = Rails.root.to_s + '/auto_install/unittest.zpm'
- file = File.new( location, 'wb' )
- file.write( test[:zpm] )
- file.close
- end
- begin
- success = Package.auto_install()
- rescue
- success = false
- end
- if test[:zpm]
- File.delete( location )
- end
- end
- if test[:verify] && test[:verify][:package]
- exists = Package.where( name: test[:verify][:package][:name], version: test[:verify][:package][:version] ).first
- assert( exists, "package '#{test[:verify][:package][:name]}' is not installed" )
- end
- next if !test[:verify]
- next if !test[:verify][:check_files]
- test[:verify][:check_files].each { |item|
- exists = File.exist?( item[:location] )
- if item[:result]
- assert( exists, "'#{item[:location]}' exists" )
- else
- assert( !exists, "'#{item[:location]}' doesn't exists" )
- end
- }
- }
- end
- end
|