Commit f10f0714 authored by Tiffany Rea's avatar Tiffany Rea Committed by Andrejs Cunskis

Adding resources filtering logic for delete task

parent a709efe7
# frozen_string_literal: true
# This script reads from test_resources.txt file to collect data about resources to delete
# Deletes all deletable resources that E2E tests created
# Resource type: Sandbox, User, Fork and RSpec::Mocks::Double are not included
# This script reads from test-resources JSON file to collect data about resources to delete
# Filter out resources that cannot be deleted
# Then deletes all deletable resources that E2E tests created
#
# Required environment variables: GITLAB_QA_ACCESS_TOKEN and GITLAB_ADDRESS
# When in CI also requires: QA_TEST_RESOURCES_FILE_PATTERN
......@@ -13,7 +13,15 @@ module QA
class DeleteTestResources
include Support::API
def initialize(file_pattern = nil)
IGNORED_RESOURCES = [
'QA::Resource::PersonalAccessToken',
'QA::Resource::CiVariable',
'QA::Resource::Repository::Commit',
'QA::EE::Resource::GroupIteration',
'QA::EE::Resource::Settings::Elasticsearch'
].freeze
def initialize(file_pattern = Runtime::Env.test_resources_created_filepath)
raise ArgumentError, "Please provide GITLAB_ADDRESS" unless ENV['GITLAB_ADDRESS']
raise ArgumentError, "Please provide GITLAB_QA_ACCESS_TOKEN" unless ENV['GITLAB_QA_ACCESS_TOKEN']
......@@ -22,45 +30,58 @@ module QA
end
def run
puts 'Deleting test created resources...'
failures = files.flat_map do |file|
resources = read_file(file)
filtered_resources = filter_resources(resources)
delete_resources(filtered_resources)
end
if Runtime::Env.running_in_ci?
raise ArgumentError, 'Please provide QA_TEST_RESOURCES_FILE_PATTERN' unless ENV['QA_TEST_RESOURCES_FILE_PATTERN']
return puts "\nDone" if failures.empty?
Dir.glob(@file_pattern).each do |file|
delete_resources(load_file(file))
puts "\nFailed to delete #{failures.size} resources:\n"
puts failures
end
else
file = Runtime::Env.test_resources_created_filepath
raise ArgumentError, "'#{file}' either does not exist or empty." if !File.exist?(file) || File.zero?(file)
delete_resources(load_file(file))
private
def files
puts "Gathering JSON files...\n"
files = Dir.glob(@file_pattern)
abort("There is no file with this pattern #{@file_pattern}") if files.empty?
files.reject { |file| File.zero?(file) }
files
end
puts "\nDone"
def read_file(file)
JSON.parse(File.read(file))
end
private
def filter_resources(resources)
puts "Filtering resources - Only keep deletable resources...\n"
def load_file(json)
JSON.parse(File.read(json))
transformed_values = resources.transform_values! do |v|
v.reject do |attributes|
attributes['info'] == "with full_path 'gitlab-qa-sandbox-group'" ||
attributes['http_method'] == 'get' && !attributes['info'].include?("with username 'qa-") ||
attributes['api_path'] == 'Cannot find resource API path'
end
end
def delete_resources(resources)
failures = []
resources.each_key do |type|
next if resources[type].empty?
transformed_values.reject! { |k, v| v.empty? || IGNORED_RESOURCES.include?(k) }
end
resources[type].each do |resource|
def delete_resources(resources)
resources.each_with_object([]) do |(key, value), failures|
value.each do |resource|
next if resource_not_found?(resource['api_path'])
msg = resource['info'] ? "#{type} - #{resource['info']}" : "#{type} at #{resource['api_path']}"
msg = resource['info'] ? "#{key} - #{resource['info']}" : "#{key} at #{resource['api_path']}"
puts "\nDeleting #{msg}..."
delete_response = delete(Runtime::API::Request.new(@api_client, resource['api_path']).url)
if delete_response.code == 202
if delete_response.code == 202 || delete_response.code == 204
print "\e[32m.\e[0m"
else
print "\e[31mF\e[0m"
......@@ -68,17 +89,11 @@ module QA
end
end
end
unless failures.empty?
puts "\nFailed to delete #{failures.length} resources:\n"
puts failures
end
end
def resource_not_found?(api_path)
get_response = get Runtime::API::Request.new(@api_client, api_path).url
get_response.code.eql? 404
# if api path contains param "?hard_delete=<boolean>", remove it
get(Runtime::API::Request.new(@api_client, api_path.split('?').first).url).code.eql? 404
end
end
end
......
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