Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
f10f0714
Commit
f10f0714
authored
Jan 28, 2022
by
Tiffany Rea
Committed by
Andrejs Cunskis
Jan 28, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding resources filtering logic for delete task
parent
a709efe7
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
36 deletions
+51
-36
qa/qa/tools/delete_test_resources.rb
qa/qa/tools/delete_test_resources.rb
+51
-36
No files found.
qa/qa/tools/delete_test_resources.rb
View file @
f10f0714
# 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 crea
ted
#
Resource type: Sandbox, User, Fork and RSpec::Mocks::Double are not includ
ed
# This script reads from test
-resources JSON
file to collect data about resources to delete
#
Filter out resources that cannot be dele
ted
#
Then deletes all deletable resources that E2E tests creat
ed
#
# 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
"
\n
Done"
if
failures
.
empty?
Dir
.
glob
(
@file_pattern
).
each
do
|
file
|
delete_resources
(
load_file
(
file
))
puts
"
\n
Failed 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
"
\n
Done"
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
"
\n
Deleting
#{
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
"
\n
Failed 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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment