Commit 8300d59c authored by Mark Lapierre's avatar Mark Lapierre Committed by Andrejs Cunskis

Log QA resource reuse

Changes the debug log message shown when a resource is fabricated.
- "Built" as previously, when a resource is created.
- "Retrieved" when an existing resource is fetched.
- "Retrieved for reuse" when an existing resource is fetched as a
  reusable resource.

Adds a ReusableGroup class so that ResuableProject is truly reusable
(otherwise ResuableProject would create a new group in a different
project each time).
parent dfdb363a
......@@ -63,6 +63,10 @@ module QA
process_api_response(parse_body(response))
end
def api_fabrication_http_method
@api_fabrication_http_method ||= :post
end
private
def resource_web_url(resource)
......@@ -85,6 +89,8 @@ module QA
raise ResourceNotFoundError, "Resource at #{request.mask_url} could not be found (#{response.code}): `#{response}`."
end
@api_fabrication_http_method = :get # rubocop:disable Gitlab/ModuleWithInstanceVariables
response
end
......
......@@ -94,10 +94,20 @@ module QA
nil
end
fabrication_http_method = if resource.api_fabrication_http_method == :get
if self.include?(Reusable)
"Retrieved for reuse"
else
"Retrieved"
end
else
"Built"
end
Support::FabricationTracker.save_fabrication(:"#{method}_fabrication", fabrication_time)
Runtime::Logger.debug do
msg = ["==#{'=' * parents.size}>"]
msg << "Built a #{name}"
msg << "#{fabrication_http_method} a #{name}"
msg << resource_identifier if resource_identifier
msg << "as a dependency of #{parents.last}" if parents.any?
msg << "via #{method}"
......
# frozen_string_literal: true
module QA
module Resource
class ReusableGroup < Group
prepend Reusable
def initialize
super
@path = "reusable_group"
@description = "QA reusable group"
@reuse_as = :default_group
end
# Confirms that the group can be reused
#
# @return [nil] returns nil unless an error is raised
def validate_reuse_preconditions
unless reused_path_unique?
raise ResourceReuseError,
"Reusable groups must have the same name. The group reused as #{reuse_as} has the path '#{path}' but it should be '#{self.class.resources[reuse_as].path}'"
end
end
# Confirms that reuse of the resource did not change it in a way that breaks later reuse. This raises an error if
# the current group path doesn't match the original path.
def validate_reuse
reload!
if api_resource[:path] != @path
raise ResourceReuseError, "The group now has the path '#{api_resource[:path]}' but it should be '#{path}'"
end
end
# Checks if the group is being reused with the same path.
#
# @return [Boolean] true if the group's path is different from another group with the same reuse symbol (reuse_as)
def reused_path_unique?
return true unless self.class.resources.key?(reuse_as)
self.class.resources[reuse_as].path == path
end
# Overrides QA::Resource::Group#remove_via_api! to log a debug message stating that removal will happen after
# the suite completes rather than now.
#
# @return [nil]
def remove_via_api!
QA::Runtime::Logger.debug("#{self.class.name} - deferring removal until after suite")
end
end
end
end
......@@ -5,6 +5,12 @@ module QA
class ReusableProject < Project
prepend Reusable
attribute :group do
ReusableGroup.fabricate_via_api! do |resource|
resource.api_client = api_client
end
end
def initialize
super
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment