Browse Source

Maintenance: Port Package minitest to RSpec.

Florian Liebe 2 years ago
parent
commit
220c29057e
4 changed files with 147 additions and 409 deletions
  1. 0 1
      .github/ci.sh
  2. 0 1
      .gitlab/ci/test/unit.yml
  3. 147 17
      spec/models/package_spec.rb
  4. 0 390
      test/integration/package_test.rb

+ 0 - 1
.github/ci.sh

@@ -40,4 +40,3 @@ bundle exec rspec -t ~type:system -t ~searchindex -t ~required_envs
 bundle exec rake zammad:db:reset
 bundle exec rake test:units
 ruby -I test/ test/integration/object_manager_test.rb
-ruby -I test/ test/integration/package_test.rb

+ 0 - 1
.gitlab/ci/test/unit.yml

@@ -5,7 +5,6 @@
     - !reference [.scripts, zammad_db_init]
     - bundle exec rake test:units
     - bundle exec rails test test/integration/object_manager_test.rb
-    - bundle exec rails test test/integration/package_test.rb
 
 unit:mysql:
   stage: test

+ 147 - 17
spec/models/package_spec.rb

@@ -3,6 +3,35 @@
 require 'rails_helper'
 
 RSpec.describe Package, type: :model do
+
+  # cleanup package files
+  after :all do # rubocop:disable RSpec/BeforeAfterAll
+    %w[example.rb app/controllers/test_controller.rb].each do |file|
+      next if !File.exist?(Rails.root.join(file))
+
+      File.delete(Rails.root.join(file))
+    end
+  end
+
+  def get_package_structure(name, files, version = '1.0.1')
+    <<-JSON
+      {
+        "name": "#{name}",
+        "version": "#{version}",
+        "vendor": "Zammad Foundation",
+        "license": "ABC",
+        "url": "https://zammad.org/",
+        "description": [
+          {
+            "language": "en",
+            "text": "some description"
+          }
+        ],
+        "files": #{files}
+      }
+    JSON
+  end
+
   let(:package_zpm_files_json) do
     <<-JSON
       [
@@ -19,23 +48,124 @@ RSpec.describe Package, type: :model do
       ]
     JSON
   end
-  let(:package_zpm_json) do
-    <<-JSON
-    {
-      "name": "UnitTestSample",
-      "version": "1.0.1",
-      "vendor": "Zammad Foundation",
-      "license": "ABC",
-      "url": "https://zammad.org/",
-      "description": [
-        {
-          "language": "en",
-          "text": "some description"
-        }
-      ],
-      "files": #{package_zpm_files_json}
-    }
-    JSON
+
+  let(:package_name)         { 'UnitTestSample' }
+  let(:package_zpm_json)     { get_package_structure(package_name, package_zpm_files_json) }
+  let(:old_package_zpm_json) { get_package_structure(package_name, package_zpm_files_json, '1.0.0') }
+  let(:new_package_zpm_json) { get_package_structure(package_name, package_zpm_files_json, '1.0.2') }
+
+  context 'when performing different package actions' do
+    context 'when installing a package' do
+      it 'does install package' do
+        expect { described_class.install(string: package_zpm_json) }
+          .to change(described_class, :count)
+          .and change(Store, :count)
+      end
+    end
+
+    context 'when reinstalling a package' do
+      before do
+        described_class.install(string: package_zpm_json)
+      end
+
+      it 'does not reinstall package' do
+        expect { described_class.reinstall(package_name) }
+          .to not_change(described_class, :count)
+          .and not_change(Store, :count)
+      end
+    end
+
+    context 'when installing a package again' do
+      before do
+        described_class.install(string: package_zpm_json)
+      end
+
+      it 'does not install package' do
+        expect { described_class.install(string: package_zpm_json) }
+          .to raise_error(RuntimeError)
+          .and not_change(described_class, :count)
+          .and not_change(Store, :count)
+      end
+    end
+
+    context 'when installing a package with a lower version' do
+      before do
+        described_class.install(string: package_zpm_json)
+      end
+
+      it 'does not install package' do
+        expect { described_class.install(string: old_package_zpm_json) }
+          .to raise_error(RuntimeError)
+          .and not_change(described_class, :count)
+          .and not_change(Store, :count)
+      end
+    end
+
+    context 'when upgrading a package' do
+      before do
+        described_class.install(string: package_zpm_json)
+      end
+
+      it 'does install package' do
+        expect { described_class.install(string: new_package_zpm_json) }
+          .to not_raise_error
+          .and not_change(described_class, :count)
+          .and change(Store, :count)
+      end
+    end
+
+    context 'when installing + uninstalling a package' do
+      before do
+        described_class.install(string: package_zpm_json)
+      end
+
+      it 'does install + uninstall the package' do
+        expect { described_class.uninstall(string: package_zpm_json) }
+          .to not_raise_error
+          .and change(described_class, :count)
+          .and not_change(Store, :count)
+      end
+    end
+
+    context 'when auto installing' do
+      before do
+        FileUtils.mkdir_p(Rails.root.join('auto_install'))
+
+        location = Rails.root.join('auto_install/unittest.zpm')
+        file = File.new(location, 'wb')
+        file.write(package_zpm_json)
+        file.close
+      end
+
+      after do
+        File.delete(Rails.root.join('auto_install/unittest.zpm'))
+      end
+
+      it 'does install package' do
+        expect { described_class.auto_install }
+          .to change(described_class, :count)
+          .and change(Store, :count)
+      end
+    end
+
+    context 'when verify package install' do
+      context 'when verify is ok' do
+        it 'returns no verify issues' do
+          package = described_class.install(string: package_zpm_json)
+
+          expect(package.verify).to be_nil
+        end
+      end
+
+      context 'when verify is not ok' do
+        it 'returns verify issues' do
+          package = described_class.install(string: package_zpm_json)
+          File.delete(Rails.root.join('example.rb'))
+
+          expect(package.verify).not_to be_nil
+        end
+      end
+    end
   end
 
   context 'with different file locations' do

+ 0 - 390
test/integration/package_test.rb

@@ -1,390 +0,0 @@
-# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
-
-require 'integration_test_helper'
-
-class PackageTest < ActiveSupport::TestCase
-
-  test 'packages' do
-    tests = [
-
-      # test 1 - normal install
-      {
-        zpm:    '{
-  "name": "UnitTestSample",
-  "version": "1.0.1",
-  "vendor": "Zammad GmbH",
-  "license": "ABC",
-  "url": "http://zammad.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": "Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uWzQuMl0NCiAgZGVmIHNlbGYudXANCiAgIGNyZWF0ZV90YWJsZSA6c2FtcGxlX3RhYmxlcyBkbyB8dHwNCiAgICAgIHQuY29sdW1uIDpuYW1lLCAgICAgICAgICAgOnN0cmluZywgOmxpbWl0ID0+IDE1MCwgIDpudWxsID0+IHRydWUNCiAgICAgIHQuY29sdW1uIDpkYXRhLCAgICAgICAgICAgOnN0cmluZywgOmxpbWl0ID0+IDUwMDAsIDpudWxsID0+IHRydWUNCiAgICBlbmQNCiAgZW5kDQoNCiAgZGVmIHNlbGYuZG93bg0KICAgIGRyb3BfdGFibGUgOnNhbXBsZV90YWJsZXMNCiAgZW5kDQplbmQ="
-    }
-  ]
-}',
-        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": "Zammad GmbH",
-  "license": "ABC",
-  "url": "http://zammad.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": "Zammad GmbH",
-  "license": "ABC",
-  "url": "http://zammad.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": "Zammad GmbH",
-  "license": "ABC",
-  "url": "http://zammad.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": "Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uWzQuMl0NCiAgZGVmIHNlbGYudXANCiAgIGNyZWF0ZV90YWJsZSA6c2FtcGxlX3RhYmxlcyBkbyB8dHwNCiAgICAgIHQuY29sdW1uIDpuYW1lLCAgICAgICAgICAgOnN0cmluZywgOmxpbWl0ID0+IDE1MCwgIDpudWxsID0+IHRydWUNCiAgICAgIHQuY29sdW1uIDpkYXRhLCAgICAgICAgICAgOnN0cmluZywgOmxpbWl0ID0+IDUwMDAsIDpudWxsID0+IHRydWUNCiAgICBlbmQNCiAgZW5kDQoNCiAgZGVmIHNlbGYuZG93bg0KICAgIGRyb3BfdGFibGUgOnNhbXBsZV90YWJsZXMNCiAgZW5kDQplbmQ="
-    }
-  ]
-}',
-        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": "Zammad GmbH",
-  "license": "ABC",
-  "url": "http://zammad.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": "Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uWzQuMl0NCiAgZGVmIHNlbGYudXANCiAgIGNyZWF0ZV90YWJsZSA6c2FtcGxlX3RhYmxlcyBkbyB8dHwNCiAgICAgIHQuY29sdW1uIDpuYW1lLCAgICAgICAgICAgOnN0cmluZywgOmxpbWl0ID0+IDE1MCwgIDpudWxsID0+IHRydWUNCiAgICAgIHQuY29sdW1uIDpkYXRhLCAgICAgICAgICAgOnN0cmluZywgOmxpbWl0ID0+IDUwMDAsIDpudWxsID0+IHRydWUNCiAgICBlbmQNCiAgZW5kDQoNCiAgZGVmIHNlbGYuZG93bg0KICAgIGRyb3BfdGFibGUgOnNhbXBsZV90YWJsZXMNCiAgZW5kDQplbmQ="
-    }
-  ]
-}',
-        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 do |test|
-      case test[:action]
-      when '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_not(issues, 'package verify not successful')
-        else
-          assert_not(package, 'install package successful but should not')
-        end
-      when 'reinstall'
-        begin
-          package = Package.reinstall(test[:name])
-        rescue
-          package = false
-        end
-        if test[:result]
-          assert(package, 'reinstall package not successful')
-          issues = package.verify
-          assert_not(issues, 'package verify not successful')
-        else
-          assert_not(package, 'reinstall package successful but should not')
-        end
-      when '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_not(package, 'uninstall package successful but should not')
-        end
-      when 'auto_install'
-        if test[:zpm]
-
-          FileUtils.mkdir_p(Rails.root.join('auto_install'))
-
-          location = Rails.root.join('auto_install/unittest.zpm')
-          file = File.new(location, 'wb')
-          file.write(test[:zpm])
-          file.close
-        end
-        begin
-          Package.auto_install
-        rescue
-          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 do |item|
-        exists = File.exist?(item[:location])
-        if item[:result]
-          assert(exists, "'#{item[:location]}' exists")
-        else
-          assert_not(exists, "'#{item[:location]}' doesn't exists")
-        end
-      end
-    end
-
-  end
-end