2021-11-17 19:13:32 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
describe QA::Tools::ReliableReport do
|
|
|
|
include QA::Support::Helpers::StubEnv
|
|
|
|
|
2021-12-13 01:14:51 -05:00
|
|
|
subject(:run) { described_class.run(range: range, report_in_issue_and_slack: create_issue) }
|
2021-11-17 19:13:32 -05:00
|
|
|
|
|
|
|
let(:slack_notifier) { instance_double("Slack::Notifier", post: nil) }
|
|
|
|
let(:influx_client) { instance_double("InfluxDB2::Client", create_query_api: query_api) }
|
|
|
|
let(:query_api) { instance_double("InfluxDB2::QueryApi") }
|
|
|
|
|
|
|
|
let(:slack_channel) { "#quality-reports" }
|
2021-12-13 01:14:51 -05:00
|
|
|
let(:range) { 14 }
|
|
|
|
let(:issue_url) { "https://gitlab.com/issue/1" }
|
2021-12-20 16:10:56 -05:00
|
|
|
let(:time) { "2021-12-07T04:05:25.000000000+00:00" }
|
2021-12-13 01:14:51 -05:00
|
|
|
|
|
|
|
let(:runs) do
|
2021-12-20 16:10:56 -05:00
|
|
|
values = {
|
|
|
|
"name" => "stable spec",
|
|
|
|
"status" => "passed",
|
|
|
|
"file_path" => "some/spec.rb",
|
|
|
|
"stage" => "manage",
|
|
|
|
"_time" => time
|
|
|
|
}
|
2021-12-13 01:14:51 -05:00
|
|
|
{
|
|
|
|
0 => instance_double(
|
|
|
|
"InfluxDB2::FluxTable",
|
|
|
|
records: [
|
|
|
|
instance_double("InfluxDB2::FluxRecord", values: values),
|
|
|
|
instance_double("InfluxDB2::FluxRecord", values: values),
|
2021-12-28 10:11:32 -05:00
|
|
|
instance_double("InfluxDB2::FluxRecord", values: values.merge({ "_time" => Time.now.to_s }))
|
2021-12-13 01:14:51 -05:00
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
2021-11-17 19:13:32 -05:00
|
|
|
end
|
|
|
|
|
2021-12-13 01:14:51 -05:00
|
|
|
let(:reliable_runs) do
|
2021-12-20 16:10:56 -05:00
|
|
|
values = {
|
|
|
|
"name" => "unstable spec",
|
|
|
|
"status" => "failed",
|
|
|
|
"file_path" => "some/spec.rb",
|
|
|
|
"stage" => "create",
|
|
|
|
"_time" => time
|
|
|
|
}
|
2021-12-13 01:14:51 -05:00
|
|
|
{
|
|
|
|
0 => instance_double(
|
|
|
|
"InfluxDB2::FluxTable",
|
|
|
|
records: [
|
|
|
|
instance_double("InfluxDB2::FluxRecord", values: { **values, "status" => "passed" }),
|
|
|
|
instance_double("InfluxDB2::FluxRecord", values: values),
|
2021-12-28 10:11:32 -05:00
|
|
|
instance_double("InfluxDB2::FluxRecord", values: values.merge({ "_time" => Time.now.to_s }))
|
2021-12-13 01:14:51 -05:00
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
2021-11-17 19:13:32 -05:00
|
|
|
end
|
|
|
|
|
2021-12-13 01:14:51 -05:00
|
|
|
def flux_query(reliable:)
|
2021-11-17 19:13:32 -05:00
|
|
|
<<~QUERY
|
2021-12-13 01:14:51 -05:00
|
|
|
from(bucket: "e2e-test-stats")
|
|
|
|
|> range(start: -#{range}d)
|
|
|
|
|> filter(fn: (r) => r._measurement == "test-stats")
|
|
|
|
|> filter(fn: (r) => r.run_type == "staging-full" or
|
|
|
|
r.run_type == "staging-sanity" or
|
|
|
|
r.run_type == "staging-sanity-no-admin" or
|
|
|
|
r.run_type == "production-full" or
|
|
|
|
r.run_type == "production-sanity" or
|
|
|
|
r.run_type == "package-and-qa" or
|
|
|
|
r.run_type == "nightly"
|
|
|
|
)
|
|
|
|
|> filter(fn: (r) => r.status != "pending" and
|
|
|
|
r.merge_request == "false" and
|
|
|
|
r.quarantined == "false" and
|
2022-04-13 11:08:16 -04:00
|
|
|
r.smoke == "false" and
|
2021-12-13 01:14:51 -05:00
|
|
|
r.reliable == "#{reliable}" and
|
|
|
|
r._field == "id"
|
|
|
|
)
|
|
|
|
|> group(columns: ["name"])
|
2021-11-17 19:13:32 -05:00
|
|
|
QUERY
|
|
|
|
end
|
|
|
|
|
2021-12-13 01:14:51 -05:00
|
|
|
def markdown_section(summary, result, stage, type)
|
|
|
|
<<~SECTION.strip
|
2021-12-28 10:11:32 -05:00
|
|
|
#{summary_table(summary, type, true)}
|
2021-12-13 01:14:51 -05:00
|
|
|
|
2021-12-28 10:11:32 -05:00
|
|
|
## #{stage} (1)
|
2021-12-13 01:14:51 -05:00
|
|
|
|
|
|
|
<details>
|
|
|
|
<summary>Executions table</summary>
|
|
|
|
|
2021-12-28 10:11:32 -05:00
|
|
|
#{table(result, ['NAME', 'RUNS', 'FAILURES', 'FAILURE RATE'], "Top #{type} specs in '#{stage}' stage for past #{range} days", true)}
|
2021-12-13 01:14:51 -05:00
|
|
|
|
|
|
|
</details>
|
|
|
|
SECTION
|
|
|
|
end
|
|
|
|
|
2021-12-28 10:11:32 -05:00
|
|
|
def summary_table(summary, type, markdown = false)
|
|
|
|
table(summary, %w[STAGE COUNT], "#{type.capitalize} spec summary for past #{range} days".ljust(50), markdown)
|
2021-12-13 01:14:51 -05:00
|
|
|
end
|
|
|
|
|
2021-12-28 10:11:32 -05:00
|
|
|
def table(rows, headings, title, markdown = false)
|
2021-11-17 19:13:32 -05:00
|
|
|
Terminal::Table.new(
|
2021-12-13 01:14:51 -05:00
|
|
|
headings: headings,
|
2021-12-28 10:11:32 -05:00
|
|
|
title: markdown ? nil : title,
|
|
|
|
rows: rows,
|
|
|
|
style: markdown ? { border: :markdown } : { all_separators: true }
|
2021-11-17 19:13:32 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def name_column(spec_name)
|
2021-12-28 10:11:32 -05:00
|
|
|
"**name**: #{spec_name}<br>**file**: spec.rb"
|
2021-11-17 19:13:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_env("QA_INFLUXDB_URL", "url")
|
|
|
|
stub_env("QA_INFLUXDB_TOKEN", "token")
|
2021-12-13 01:14:51 -05:00
|
|
|
stub_env("SLACK_WEBHOOK", "slack_url")
|
|
|
|
stub_env("CI_API_V4_URL", "gitlab_api_url")
|
|
|
|
stub_env("GITLAB_ACCESS_TOKEN", "gitlab_token")
|
2021-11-17 19:13:32 -05:00
|
|
|
|
2022-04-13 14:08:33 -04:00
|
|
|
allow(RestClient::Request).to receive(:execute)
|
2021-11-17 19:13:32 -05:00
|
|
|
allow(Slack::Notifier).to receive(:new).and_return(slack_notifier)
|
|
|
|
allow(InfluxDB2::Client).to receive(:new).and_return(influx_client)
|
|
|
|
|
2021-12-13 01:14:51 -05:00
|
|
|
allow(query_api).to receive(:query).with(query: flux_query(reliable: false)).and_return(runs)
|
|
|
|
allow(query_api).to receive(:query).with(query: flux_query(reliable: true)).and_return(reliable_runs)
|
|
|
|
end
|
2021-11-17 19:13:32 -05:00
|
|
|
|
2021-12-13 01:14:51 -05:00
|
|
|
context "without report creation" do
|
|
|
|
let(:create_issue) { "false" }
|
2021-11-17 19:13:32 -05:00
|
|
|
|
2021-12-13 01:14:51 -05:00
|
|
|
it "does not create report issue", :aggregate_failures do
|
|
|
|
expect { run }.to output.to_stdout
|
2021-11-17 19:13:32 -05:00
|
|
|
|
2021-12-13 01:14:51 -05:00
|
|
|
expect(RestClient::Request).not_to have_received(:execute)
|
|
|
|
expect(slack_notifier).not_to have_received(:post)
|
2021-11-17 19:13:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-13 01:14:51 -05:00
|
|
|
context "with report creation" do
|
|
|
|
let(:create_issue) { "true" }
|
2022-04-13 14:08:33 -04:00
|
|
|
let(:iid) { 2 }
|
|
|
|
let(:old_iid) { 1 }
|
|
|
|
let(:issue_endpoint) { "gitlab_api_url/projects/278964/issues" }
|
|
|
|
|
|
|
|
let(:common_api_args) do
|
|
|
|
{
|
|
|
|
verify_ssl: false,
|
2022-09-19 17:13:43 -04:00
|
|
|
headers: { "PRIVATE-TOKEN" => "gitlab_token" },
|
|
|
|
cookies: {}
|
2022-04-13 14:08:33 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:create_issue_response) do
|
|
|
|
instance_double(
|
|
|
|
"RestClient::Response",
|
|
|
|
code: 200,
|
|
|
|
body: { web_url: issue_url, iid: iid }.to_json
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:open_issues_response) do
|
|
|
|
instance_double(
|
|
|
|
"RestClient::Response",
|
|
|
|
code: 200,
|
|
|
|
body: [{ web_url: issue_url, iid: iid }, { web_url: issue_url, iid: old_iid }].to_json
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:success_response) do
|
|
|
|
instance_double("RestClient::Response", code: 200, body: {}.to_json)
|
|
|
|
end
|
|
|
|
|
2021-12-13 01:14:51 -05:00
|
|
|
let(:issue_body) do
|
|
|
|
<<~TXT.strip
|
|
|
|
[[_TOC_]]
|
2021-11-17 19:13:32 -05:00
|
|
|
|
2021-12-20 16:10:56 -05:00
|
|
|
# Candidates for promotion to reliable (#{Date.today - range} - #{Date.today})
|
2021-11-17 19:13:32 -05:00
|
|
|
|
2021-12-28 10:11:32 -05:00
|
|
|
Total amount: **1**
|
|
|
|
|
2021-12-13 01:14:51 -05:00
|
|
|
#{markdown_section([['manage', 1]], [[name_column('stable spec'), 3, 0, '0%']], 'manage', 'stable')}
|
2021-11-17 19:13:32 -05:00
|
|
|
|
2021-12-20 16:10:56 -05:00
|
|
|
# Reliable specs with failures (#{Date.today - range} - #{Date.today})
|
2021-12-13 01:14:51 -05:00
|
|
|
|
2021-12-28 10:11:32 -05:00
|
|
|
Total amount: **1**
|
|
|
|
|
2021-12-13 01:14:51 -05:00
|
|
|
#{markdown_section([['create', 1]], [[name_column('unstable spec'), 3, 2, '66.67%']], 'create', 'unstable')}
|
|
|
|
TXT
|
|
|
|
end
|
|
|
|
|
2022-04-13 14:08:33 -04:00
|
|
|
before do
|
|
|
|
allow(RestClient::Request).to receive(:execute).exactly(4).times.and_return(
|
|
|
|
create_issue_response,
|
|
|
|
open_issues_response,
|
|
|
|
success_response,
|
|
|
|
success_response
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates report issue" do
|
2021-12-13 01:14:51 -05:00
|
|
|
expect { run }.to output.to_stdout
|
|
|
|
|
|
|
|
expect(RestClient::Request).to have_received(:execute).with(
|
|
|
|
method: :post,
|
2022-04-13 14:08:33 -04:00
|
|
|
url: issue_endpoint,
|
2021-12-13 01:14:51 -05:00
|
|
|
payload: {
|
2022-01-06 04:15:51 -05:00
|
|
|
title: "Reliable e2e test report",
|
2021-12-13 01:14:51 -05:00
|
|
|
description: issue_body,
|
2022-04-13 14:08:33 -04:00
|
|
|
labels: "reliable test report,Quality,test,type::maintenance,automation:ml"
|
|
|
|
},
|
|
|
|
**common_api_args
|
|
|
|
)
|
|
|
|
expect(RestClient::Request).to have_received(:execute).with(
|
|
|
|
method: :get,
|
|
|
|
url: "#{issue_endpoint}?labels=reliable test report&state=opened",
|
|
|
|
**common_api_args
|
|
|
|
)
|
|
|
|
expect(RestClient::Request).to have_received(:execute).with(
|
|
|
|
method: :put,
|
|
|
|
url: "#{issue_endpoint}/#{old_iid}",
|
|
|
|
payload: {
|
|
|
|
state_event: "close"
|
|
|
|
},
|
|
|
|
**common_api_args
|
|
|
|
)
|
|
|
|
expect(RestClient::Request).to have_received(:execute).with(
|
|
|
|
method: :post,
|
|
|
|
url: "#{issue_endpoint}/#{old_iid}/notes",
|
|
|
|
payload: {
|
|
|
|
body: "Closed issue in favor of ##{iid}"
|
|
|
|
},
|
|
|
|
**common_api_args
|
2021-12-13 01:14:51 -05:00
|
|
|
)
|
|
|
|
expect(slack_notifier).to have_received(:post).with(
|
|
|
|
icon_emoji: ":tanuki-protect:",
|
|
|
|
text: <<~TEXT
|
|
|
|
```#{summary_table([['manage', 1]], 'stable')}```
|
|
|
|
```#{summary_table([['create', 1]], 'unstable')}```
|
|
|
|
|
|
|
|
#{issue_url}
|
|
|
|
TEXT
|
|
|
|
)
|
2021-11-17 19:13:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-13 01:14:51 -05:00
|
|
|
context "with failure" do
|
|
|
|
let(:create_issue) { "true" }
|
2021-11-17 19:13:32 -05:00
|
|
|
|
2021-12-13 01:14:51 -05:00
|
|
|
before do
|
|
|
|
allow(query_api).to receive(:query).and_raise("Connection error!")
|
2021-11-17 19:13:32 -05:00
|
|
|
end
|
|
|
|
|
2021-12-13 01:14:51 -05:00
|
|
|
it "notifies failure", :aggregate_failures do
|
2021-12-20 16:10:56 -05:00
|
|
|
expect { expect { run }.to raise_error("Connection error!") }.to output.to_stdout
|
2021-12-13 01:14:51 -05:00
|
|
|
|
|
|
|
expect(slack_notifier).to have_received(:post).with(
|
|
|
|
icon_emoji: ":sadpanda:",
|
|
|
|
text: "Reliable reporter failed to create report. Error: ```Connection error!```"
|
|
|
|
)
|
2021-11-17 19:13:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|