Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test_resource_data_processor.rb « tools « qa « qa - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 965919dc516e9ffdf5beaf5a3d1dcb040f478144 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# frozen_string_literal: true

# This script collects all resources created during each test execution
# Save the data and write it to a JSON file at the end of suite

module QA
  module Tools
    class TestResourceDataProcessor
      include Singleton

      def initialize
        @resources = Hash.new { |hsh, key| hsh[key] = [] }
      end

      class << self
        delegate :collect, :write_to_file, :resources, to: :instance
      end

      # @return [Hash<String, Array>]
      attr_reader :resources

      # 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,
          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?

        file = Pathname.new(Runtime::Env.test_resources_created_filepath)
        FileUtils.mkdir_p(file.dirname)

        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

      private

      # 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'

        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
end