Skip to content

Commit f2bb4f3

Browse files
committed
Adds Ruby script to generate a report of tested/untested endpoints
1 parent e412881 commit f2bb4f3

7 files changed

+142
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
report/tmp

report/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Gemfile.lock

report/Gemfile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
source 'https://rubygems.org'
2+
3+
group :development, :test do
4+
gem 'debug'
5+
end

report/Rakefile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'erb'
2+
require './download_artifacts'
3+
require './reporter'
4+
5+
desc "Generate report"
6+
task :report do
7+
@reporter = Elastic::Reporter.new
8+
template = ERB.new(File.read('./template.erb'), trim_mode = true)
9+
File.write('../apis_report.md', template.result(binding))
10+
end
11+
12+
desc "Download artifacts"
13+
task :download do
14+
Elastic::download_artifacts(ENV['STACK_VERSION'] || '8.13.0-SNAPSHOT')
15+
end

report/download_artifacts.rb

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
require 'pathname'
2+
require 'open-uri'
3+
require 'json'
4+
5+
module Elastic
6+
CURRENT_PATH = Pathname(File.expand_path(__dir__))
7+
class << self
8+
def download_artifacts(version)
9+
json_filename = CURRENT_PATH.join('tmp/artifacts.json')
10+
11+
# Create ./tmp if it doesn't exist
12+
Dir.mkdir(CURRENT_PATH.join('tmp'), 0700) unless File.directory?(CURRENT_PATH.join('tmp'))
13+
14+
# Download json file with package information for version:
15+
json_url = "https://artifacts-api.elastic.co/v1/versions/#{version}"
16+
download_file!(json_url, json_filename)
17+
18+
# Parse the downloaded JSON
19+
begin
20+
artifacts = JSON.parse(File.read(json_filename))
21+
rescue StandardError => e
22+
STDERR.puts "[!] Couldn't read JSON file #{json_filename}"
23+
exit 1
24+
end
25+
26+
# Either find the artifacts for the exact same build hash from the current running cluster or
27+
# use the first one from the list of builds:
28+
build_hash_artifact = artifacts['version']['builds'].first
29+
zip_url = build_hash_artifact.dig('projects', 'elasticsearch', 'packages').select { |k, _| k =~ /rest-resources-zip/ }.map { |_, v| v['url'] }.first
30+
31+
# Dig into the elasticsearch packages, search for the rest-resources-zip package and return the URL:
32+
build_hash_artifact.dig('projects', 'elasticsearch', 'packages').select { |k, _| k =~ /rest-resources-zip/ }.map { |_, v| v['url'] }.first
33+
34+
# Download the zip file
35+
filename = CURRENT_PATH.join("tmp/#{zip_url.split('/').last}")
36+
download_file!(zip_url, filename)
37+
38+
spec = CURRENT_PATH.join('tmp/rest-api-spec')
39+
FileUtils.remove_dir(spec) if File.directory?(spec)
40+
41+
puts "Unzipping file #{filename}"
42+
`unzip -o #{filename} -d tmp/`
43+
`rm #{filename}`
44+
puts "Artifacts downloaded in ./tmp, build hash #{@build_hash}"
45+
File.write(CURRENT_PATH.join('tmp/rest-api-spec/build_hash'), @build_hash)
46+
end
47+
48+
def download_file!(url, filename)
49+
puts "Downloading #{filename} from #{url}"
50+
File.open(filename, "w") do |downloaded_file|
51+
URI.open(url, "rb") do |artifact_file|
52+
downloaded_file.write(artifact_file.read)
53+
end
54+
end
55+
puts "Successfully downloaded #{filename}"
56+
57+
unless File.exist?(filename)
58+
warn "[!] Couldn't download #{filename}"
59+
exit 1
60+
end
61+
rescue StandardError => e
62+
abort e.message
63+
end
64+
end
65+
end

report/reporter.rb

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module Elastic
2+
class Reporter
3+
JSON_FILES = "#{File.expand_path('./tmp/rest-api-spec/api')}/*.json".freeze
4+
TESTS_PATH = File.expand_path('../tests/**/*.yml')
5+
6+
attr_reader :apis, :tested, :untested
7+
8+
def initialize
9+
@apis = gather_apis
10+
@tested = []
11+
@untested = []
12+
report!
13+
end
14+
15+
def gather_apis
16+
endpoints = Dir[JSON_FILES].map do |path|
17+
path.split('/').last.gsub('.json','')
18+
end.reject { |a| a.split('/').last.gsub('.json','').start_with?('_') }
19+
end
20+
21+
def report!
22+
@apis.each do |api|
23+
if find_in_tests(api)
24+
@tested << api
25+
else
26+
@untested << api
27+
end
28+
end
29+
end
30+
31+
def find_in_tests(endpoint)
32+
Dir[TESTS_PATH].map do |path|
33+
return true if File.readlines(path).grep(/#{endpoint}/).any?
34+
end
35+
false
36+
end
37+
end
38+
end

report/template.erb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Tests report
2+
3+
Endpoints: <%= @reporter.apis.count %>
4+
[Tested](#tested): <%= @reporter.tested.count %>
5+
[Untested](#untested): <%= @reporter.untested.count %>
6+
7+
## Tested
8+
9+
<% @reporter.tested.each do |test| %>
10+
- [x] <%= test %>
11+
<% end %>
12+
13+
## Untested
14+
15+
<% @reporter.untested.each do |test| %>
16+
- [ ] <%= test %>
17+
<% end %>

0 commit comments

Comments
 (0)