From a7b3560714b4d9cc4ab32dffcd1f74a284b93580 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Fri, 18 Feb 2022 09:45:46 +0000 Subject: Add latest changes from gitlab-org/gitlab@14-8-stable-ee --- qa/spec/resource/base_spec.rb | 100 +++++++++++++++--------- qa/spec/resource/reusable_collection_spec.rb | 110 +++++++++++++++++++++++++++ 2 files changed, 172 insertions(+), 38 deletions(-) create mode 100644 qa/spec/resource/reusable_collection_spec.rb (limited to 'qa/spec/resource') diff --git a/qa/spec/resource/base_spec.rb b/qa/spec/resource/base_spec.rb index 2dd25f983bf..eab205ec5d1 100644 --- a/qa/spec/resource/base_spec.rb +++ b/qa/spec/resource/base_spec.rb @@ -3,10 +3,45 @@ RSpec.describe QA::Resource::Base do include QA::Support::Helpers::StubEnv - let(:resource) { spy('resource', username: 'qa') } + let(:resource) { spy('resource') } let(:location) { 'http://location' } let(:log_regex) { %r{==> Built a MyResource with username 'qa' via #{method} in [\d.\-e]+ seconds+} } + before do + allow(QA::Tools::TestResourceDataProcessor).to receive(:collect) + allow(QA::Tools::TestResourceDataProcessor).to receive(:write_to_file) + end + + shared_context 'with simple resource' do + subject do + Class.new(QA::Resource::Base) do + def self.name + 'MyResource' + end + + attribute :test do + 'block' + end + + attribute :username do + 'qa' + end + + attribute :no_block + + def fabricate!(*args) + 'any' + end + + def self.current_url + 'http://stub' + end + end + end + + let(:resource) { subject.new } + end + shared_context 'with fabrication context' do subject do Class.new(described_class) do @@ -56,23 +91,29 @@ RSpec.describe QA::Resource::Base do end describe '.fabricate_via_api!' do - include_context 'with fabrication context' + context 'when fabricating' do + include_context 'with fabrication context' - it_behaves_like 'fabrication method', :fabricate_via_api! + it_behaves_like 'fabrication method', :fabricate_via_api! - it 'instantiates the resource, calls resource method returns the resource' do - expect(resource).to receive(:fabricate_via_api!).and_return(location) + it 'instantiates the resource, calls resource method returns the resource' do + expect(resource).to receive(:fabricate_via_api!).and_return(location) - result = subject.fabricate_via_api!(resource: resource, parents: []) + result = subject.fabricate_via_api!(resource: resource, parents: []) - expect(result).to eq(resource) + expect(result).to eq(resource) + end end context "with debug log level" do + include_context 'with simple resource' + let(:method) { 'api' } before do allow(QA::Runtime::Logger).to receive(:debug) + allow(resource).to receive(:api_support?).and_return(true) + allow(resource).to receive(:fabricate_via_api!) end it 'logs the resource and build method' do @@ -88,27 +129,32 @@ RSpec.describe QA::Resource::Base do end describe '.fabricate_via_browser_ui!' do - include_context 'with fabrication context' + context 'when fabricating' do + include_context 'with fabrication context' - it_behaves_like 'fabrication method', :fabricate_via_browser_ui!, :fabricate! + it_behaves_like 'fabrication method', :fabricate_via_browser_ui!, :fabricate! - it 'instantiates the resource and calls resource method' do - subject.fabricate_via_browser_ui!('something', resource: resource, parents: []) + it 'instantiates the resource and calls resource method' do + subject.fabricate_via_browser_ui!('something', resource: resource, parents: []) - expect(resource).to have_received(:fabricate!).with('something') - end + expect(resource).to have_received(:fabricate!).with('something') + end - it 'returns fabrication resource' do - result = subject.fabricate_via_browser_ui!('something', resource: resource, parents: []) + it 'returns fabrication resource' do + result = subject.fabricate_via_browser_ui!('something', resource: resource, parents: []) - expect(result).to eq(resource) + expect(result).to eq(resource) + end end context "with debug log level" do + include_context 'with simple resource' + let(:method) { 'browser_ui' } before do allow(QA::Runtime::Logger).to receive(:debug) + # allow(resource).to receive(:fabricate!) end it 'logs the resource and build method' do @@ -123,28 +169,6 @@ RSpec.describe QA::Resource::Base do end end - shared_context 'with simple resource' do - subject do - Class.new(QA::Resource::Base) do - attribute :test do - 'block' - end - - attribute :no_block - - def fabricate! - 'any' - end - - def self.current_url - 'http://stub' - end - end - end - - let(:resource) { subject.new } - end - describe '.attribute' do include_context 'with simple resource' diff --git a/qa/spec/resource/reusable_collection_spec.rb b/qa/spec/resource/reusable_collection_spec.rb new file mode 100644 index 00000000000..9116462b396 --- /dev/null +++ b/qa/spec/resource/reusable_collection_spec.rb @@ -0,0 +1,110 @@ +# frozen_string_literal: true + +RSpec.describe QA::Resource::ReusableCollection do + let(:reusable_resource_class) do + Class.new do + prepend QA::Resource::Reusable + + attr_reader :removed + + def self.name + 'FooReusableResource' + end + + def comparable + self.class.name + end + + def remove_via_api! + @removed = true + end + + def exists?() end + end + end + + let(:another_reusable_resource_class) do + Class.new(reusable_resource_class) do + def self.name + 'BarReusableResource' + end + end + end + + let(:a_resource_instance) { reusable_resource_class.new } + let(:another_resource_instance) { another_reusable_resource_class.new } + + it 'is a singleton class' do + expect { described_class.new }.to raise_error(NoMethodError) + end + + subject(:collection) do + described_class.instance + end + + before do + described_class.register_resource_classes do |c| + reusable_resource_class.register(c) + another_reusable_resource_class.register(c) + end + + collection.resource_classes = { + 'FooReusableResource' => { + reuse_as_identifier: { + resource: a_resource_instance + } + }, + 'BarReusableResource' => { + another_reuse_as_identifier: { + resource: another_resource_instance + } + } + } + + allow(a_resource_instance).to receive(:validate_reuse) + allow(another_resource_instance).to receive(:validate_reuse) + end + + after do + collection.resource_classes = {} + end + + describe '#each_resource' do + it 'yields each resource and reuse_as identifier in the collection' do + expect { |blk| collection.each_resource(&blk) } + .to yield_successive_args( + [:reuse_as_identifier, a_resource_instance], + [:another_reuse_as_identifier, another_resource_instance] + ) + end + end + + describe '.remove_all_via_api!' do + before do + allow(a_resource_instance).to receive(:exists?).and_return(true) + allow(another_resource_instance).to receive(:exists?).and_return(true) + end + + it 'removes each instance of each resource class' do + described_class.remove_all_via_api! + + expect(a_resource_instance.removed).to be true + expect(another_resource_instance.removed).to be true + end + end + + describe '.validate_resource_reuse' do + it 'validates each instance of each resource class' do + expect(a_resource_instance).to receive(:validate_reuse) + expect(another_resource_instance).to receive(:validate_reuse) + + described_class.validate_resource_reuse + end + end + + describe '.register_resource_classes' do + it 'yields the hash of resource classes in the collection' do + expect { |blk| described_class.register_resource_classes(&blk) }.to yield_with_args(collection.resource_classes) + end + end +end -- cgit v1.2.1