From 977cc3d981bc8a36636e43119c30510519507279 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Sun, 31 Jan 2016 18:25:31 +0100 Subject: [PATCH] - Replaced package XML structure by JSON. - Removed Package.build() in favor of the 'zammad-szpm' gem. --- app/models/package.rb | 111 +++++--------------- test/unit/package_test.rb | 211 +++++++++++++++++++++++--------------- 2 files changed, 153 insertions(+), 169 deletions(-) diff --git a/app/models/package.rb b/app/models/package.rb index c5749adc2..51ce9801b 100644 --- a/app/models/package.rb +++ b/app/models/package.rb @@ -1,48 +1,8 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ -require 'rexml/document' class Package < ApplicationModel @@root = Rails.root.to_s # rubocop:disable Style/ClassVars - # build package based on .szpm - # Package.build( - # file: 'package.szpm', - # root: '/path/to/src/extention/', - # output: '/path/to/package_location/' - # ) - def self.build(data) - - if data[:file] - xml = _read_file(data[:file], data[:root] || true) - package = _parse(xml) - elsif data[:string] - package = _parse(data[:string]) - end - - build_date = REXML::Element.new('build_date') - build_date.text = Time.zone.now.iso8601 - build_host = REXML::Element.new('build_host') - build_host.text = Socket.gethostname - - package.root.insert_after('//zpm/description', build_date) - package.root.insert_after('//zpm/description', build_host) - package.elements.each('zpm/filelist/file') do |element| - location = element.attributes['location'] - content = _read_file(location, data[:root]) - base64 = Base64.encode64(content) - element.text = base64 - end - if data[:output] - location = "#{data[:output]}/#{package.elements['zpm/name'].text}-#{package.elements['zpm/version'].text}.zpm" - logger.info "NOTICE: writting package to '#{location}'" - file = File.new(location, 'wb') - file.write(package.to_s) - file.close - return true - end - package.to_s - end - # Package.auto_install # install all packages located under auto_install/*.zpm def self.auto_install @@ -178,17 +138,17 @@ class Package < ApplicationModel # Package.install(string: zpm_as_string) def self.install(data) if data[:file] - xml = _read_file(data[:file], true) - package = _parse(xml) + json = _read_file(data[:file], true) + package = JSON.parse(json) elsif data[:string] - package = _parse(data[:string]) + package = JSON.parse(data[:string]) end # package meta data meta = { - name: package.elements['zpm/name'].text, - version: package.elements['zpm/version'].text, - vendor: package.elements['zpm/vendor'].text, + name: package['name'], + version: package['version'], + vendor: package['vendor'], state: 'uninstalled', created_by_id: 1, updated_by_id: 1, @@ -220,7 +180,7 @@ class Package < ApplicationModel Store.add( object: 'Package', o_id: record.id, - data: package.to_s, + data: package.to_json, filename: "#{meta[:name]}-#{meta[:version]}.zpm", preferences: {}, created_by_id: UserInfo.current_user_id || 1, @@ -228,13 +188,11 @@ class Package < ApplicationModel end # write files - package.elements.each('zpm/filelist/file') do |element| - location = element.attributes['location'] - permission = element.attributes['permission'] || '644' - base64 = element.text - content = Base64.decode64(base64) - content = _write_file(location, permission, content) - end + package['files'].each { |file| + permission = file['permission'] || '644' + content = Base64.decode64(file['content']) + _write_file(file['location'], permission, content) + } # update package state record.state = 'installed' @@ -264,55 +222,33 @@ class Package < ApplicationModel def self.uninstall(data) if data[:string] - package = _parse(data[:string]) + package = JSON.parse(data[:string]) else - file = _get_bin(data[:name], data[:version]) - package = _parse(file) + json_file = _get_bin(data[:name], data[:version]) + package = JSON.parse(json_file) end - # package meta data - meta = { - name: package.elements['zpm/name'].text, - version: package.elements['zpm/version'].text, - } - # down migrations if !data[:migration_not_down] - Package::Migration.migrate(meta[:name], 'reverse') + Package::Migration.migrate(package['name'], 'reverse') end - package.elements.each('zpm/filelist/file') do |element| - location = element.attributes['location'] - permission = element.attributes['permission'] || '644' - base64 = element.text - content = Base64.decode64(base64) - content = _delete_file(location, permission, content) - end - - # prebuild assets + package['files'].each { |file| + permission = file['permission'] || '644' + content = Base64.decode64(file['content']) + _delete_file(file['location'], permission, content) + } # delete package record = Package.find_by( - name: meta[:name], - version: meta[:version], + name: package['name'], + version: package['version'], ) record.destroy true end - def self._parse(xml) - logger.debug xml.inspect - begin - package = REXML::Document.new(xml) - rescue => e - logger.error 'ERROR: ' + e.inspect - return - end - logger.debug package.inspect - package - end - def self._get_bin(name, version) package = Package.find_by( name: name, @@ -470,7 +406,6 @@ class Package < ApplicationModel Kernel.const_get(classname).up Package::Migration.create(name: package.underscore, version: version) end - } end end diff --git a/test/unit/package_test.rb b/test/unit/package_test.rb index 0a8b8e8f1..413354930 100644 --- a/test/unit/package_test.rb +++ b/test/unit/package_test.rb @@ -7,25 +7,36 @@ class PackageTest < ActiveSupport::TestCase # test 1 - normal install { - zpm: ' - - UnitTestSample - 1.0.1 - Znuny GmbH - http://znuny.org/ - ABC - some description - - YWJjw6TDtsO8w58= - YWJjw6TDtsO8w58= - Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uDQogIGRlZiBzZWxmLnVw -DQogICBjcmVhdGVfdGFibGUgOnNhbXBsZV90YWJsZXMgZG8gfHR8DQogICAgICB0LmNvbHVtbiA6 -bmFtZSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiAxNTAsICA6bnVsbCA9PiB0cnVlDQog -ICAgICB0LmNvbHVtbiA6ZGF0YSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiA1MDAwLCA6 -bnVsbCA9PiB0cnVlDQogICAgZW5kDQogIGVuZA0KDQogIGRlZiBzZWxmLmRvd24NCiAgICBkcm9w -X3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k - -', + 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: { @@ -52,61 +63,88 @@ X3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k # test 2 - try to install same package again / should not work { - zpm: ' - - UnitTestSample - 1.0.1 - Znuny GmbH - http://znuny.org/ - ABC - some description - - YWJjw6TDtsO8w58= - -', + 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 3 - try to install lower version / should not work { - zpm: ' - - UnitTestSample - 1.0.0 - Znuny GmbH - http://znuny.org/ - ABC - some description - - YWJjw6TDtsO8w58= - -', + 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 4 - upgrade 7 should work { - zpm: ' - - UnitTestSample - 1.0.2 - Znuny GmbH - http://znuny.org/ - ABC - some description - - YWJjw6TDtsO8w58= - YWJjw6TDtsO8w58= - Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uDQogIGRlZiBzZWxmLnVw -DQogICBjcmVhdGVfdGFibGUgOnNhbXBsZV90YWJsZXMgZG8gfHR8DQogICAgICB0LmNvbHVtbiA6 -bmFtZSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiAxNTAsICA6bnVsbCA9PiB0cnVlDQog -ICAgICB0LmNvbHVtbiA6ZGF0YSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiA1MDAwLCA6 -bnVsbCA9PiB0cnVlDQogICAgZW5kDQogIGVuZA0KDQogIGRlZiBzZWxmLmRvd24NCiAgICBkcm9w -X3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k - -', + 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: { @@ -157,25 +195,36 @@ X3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k # test 5 - check auto_install mechanism { - zpm: ' - - UnitTestSample - 1.0.2 - Znuny GmbH - http://znuny.org/ - ABC - some description - - YWJjw6TDtsO8w58= - YWJjw6TDtsO8w58= - Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uDQogIGRlZiBzZWxmLnVw -DQogICBjcmVhdGVfdGFibGUgOnNhbXBsZV90YWJsZXMgZG8gfHR8DQogICAgICB0LmNvbHVtbiA6 -bmFtZSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiAxNTAsICA6bnVsbCA9PiB0cnVlDQog -ICAgICB0LmNvbHVtbiA6ZGF0YSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiA1MDAwLCA6 -bnVsbCA9PiB0cnVlDQogICAgZW5kDQogIGVuZA0KDQogIGRlZiBzZWxmLmRvd24NCiAgICBkcm9w -X3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k - -', + 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: {