diff options
Diffstat (limited to 'qa/qa/tools/test_resource_data_processor.rb')
-rw-r--r-- | qa/qa/tools/test_resource_data_processor.rb | 107 |
1 files changed, 64 insertions, 43 deletions
diff --git a/qa/qa/tools/test_resource_data_processor.rb b/qa/qa/tools/test_resource_data_processor.rb index 78fb6ef6cd0..965919dc516 100644 --- a/qa/qa/tools/test_resource_data_processor.rb +++ b/qa/qa/tools/test_resource_data_processor.rb @@ -6,60 +6,81 @@ module QA module Tools class TestResourceDataProcessor - @resources ||= Hash.new { |hsh, key| hsh[key] = [] } + include Singleton + + def initialize + @resources = Hash.new { |hsh, key| hsh[key] = [] } + end class << self - # Ignoring rspec-mocks, sandbox, user and fork resources - # TODO: Will need to figure out which user resources can be collected, ignore for now - # - # Collecting resources created in E2E tests - # Data is a Hash of resources with keys as resource type (group, project, issue, etc.) - # Each type contains an array of resource object (hash) of the same type - # E.g: { "QA::Resource::Project": [ { info: 'foo', api_path: '/foo'}, {...} ] } - def collect(resource, info) - return if resource.api_response.nil? || - resource.is_a?(RSpec::Mocks::Double) || - resource.is_a?(Resource::Sandbox) || - resource.is_a?(Resource::User) || - resource.is_a?(Resource::Fork) + delegate :collect, :write_to_file, :resources, to: :instance + end - api_path = if resource.respond_to?(:api_delete_path) - resource.api_delete_path.gsub('%2F', '/') - elsif resource.respond_to?(:api_get_path) - resource.api_get_path.gsub('%2F', '/') - else - 'Cannot find resource API path' - end + # @return [Hash<String, Array>] + attr_reader :resources - type = resource.class.name + # Collecting resources created in E2E tests + # Data is a Hash of resources with keys as resource type (group, project, issue, etc.) + # Each type contains an array of resource object (hash) of the same type + # E.g: { "QA::Resource::Project": [ { info: 'foo', api_path: '/foo'}, {...} ] } + # + # @param [QA::Resource::Base] resource fabricated resource + # @param [String] info resource info + # @param [Symbol] method fabrication method, api or browser_ui + # @param [Integer] time fabrication time + # @return [Hash] + def collect(resource:, info:, fabrication_method:, fabrication_time:) + api_path = resource_api_path(resource) + type = resource.class.name - @resources[type] << { info: info, api_path: api_path } - end + resources[type] << { + info: info, + api_path: api_path, + fabrication_method: fabrication_method, + fabrication_time: fabrication_time, + http_method: resource.api_fabrication_http_method, + timestamp: Time.now.to_s + } + end + + # If JSON file exists and not empty, read and load file content + # Merge what is saved in @resources into the content from file + # Overwrite file content with the new data hash + # Otherwise create file and write data hash to file for the first time + # + # @return [void] + def write_to_file + return if resources.empty? - # If JSON file exists and not empty, read and load file content - # Merge what is saved in @resources into the content from file - # Overwrite file content with the new data hash - # Otherwise create file and write data hash to file for the first time - def write_to_file - return if @resources.empty? + file = Pathname.new(Runtime::Env.test_resources_created_filepath) + FileUtils.mkdir_p(file.dirname) - file = Runtime::Env.test_resources_created_filepath - FileUtils.mkdir_p('tmp/') - FileUtils.touch(file) - data = nil + data = resources.deep_dup + # merge existing json if present + JSON.parse(File.read(file)).deep_merge!(data) { |key, val, other_val| val + other_val } if file.exist? + + File.write(file, JSON.pretty_generate(data)) + end - if File.zero?(file) - data = @resources - else - data = JSON.parse(File.read(file)) + private - @resources.each_pair do |key, val| - data[key].nil? ? data[key] = val : val.each { |item| data[key] << item } - end - end + # Determine resource api path or return default value + # Some resources fabricated via UI can raise no attribute error + # + # @param [QA::Resource::Base] resource + # @return [String] + def resource_api_path(resource) + default = 'Cannot find resource API path' - File.open(file, 'w') { |f| f.write(JSON.pretty_generate(data.each_value(&:uniq!))) } + if resource.respond_to?(:api_delete_path) + resource.api_delete_path.gsub('%2F', '/') + elsif resource.respond_to?(:api_get_path) + resource.api_get_path.gsub('%2F', '/') + else + default end + rescue QA::Resource::Base::NoValueError + default end end end |