1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
|
# frozen_string_literal: true
describe QA::Tools::ReliableReport do
include QA::Support::Helpers::StubEnv
before do
stub_env("QA_INFLUXDB_URL", "url")
stub_env("QA_INFLUXDB_TOKEN", "token")
stub_env("SLACK_WEBHOOK", "slack_url")
stub_env("CI_API_V4_URL", "gitlab_api_url")
stub_env("GITLAB_ACCESS_TOKEN", "gitlab_token")
allow(RestClient::Request).to receive(:execute)
allow(Slack::Notifier).to receive(:new).and_return(slack_notifier)
allow(InfluxDB2::Client).to receive(:new).and_return(influx_client)
allow(query_api).to receive(:query).with(query: flux_query(blocking: false)).and_return(runs)
allow(query_api).to receive(:query).with(query: flux_query(blocking: true)).and_return(reliable_runs)
end
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" }
let(:range) { 14 }
let(:issue_url) { "https://gitlab.com/issue/1" }
let(:time) { "2021-12-07T04:05:25.000000000+00:00" }
let(:failure_message) { 'random failure message' }
let(:run_values) do
{
"name" => "stable spec1",
"status" => "passed",
"file_path" => "/some/spec.rb",
"stage" => "create",
"product_group" => "code_review",
"testcase" => "https://testcase/url",
"run_type" => "e2e-test-on-omnibus",
"_time" => time
}
end
let(:run_more_values) do
{
"name" => "stable spec2",
"status" => "passed",
"file_path" => "/some/spec.rb",
"stage" => "manage",
"product_group" => "import_and_integrate",
"testcase" => "https://testcase/url",
"run_type" => "e2e-test-on-omnibus",
"_time" => time
}
end
let(:runs) do
[
instance_double(
"InfluxDB2::FluxTable",
records: [
instance_double("InfluxDB2::FluxRecord", values: run_values),
instance_double("InfluxDB2::FluxRecord", values: run_values),
instance_double("InfluxDB2::FluxRecord", values: run_values.merge({ "_time" => Time.now.to_s }))
]
),
instance_double(
"InfluxDB2::FluxTable",
records: [
instance_double("InfluxDB2::FluxRecord", values: run_more_values),
instance_double("InfluxDB2::FluxRecord", values: run_more_values),
instance_double("InfluxDB2::FluxRecord", values: run_more_values.merge({ "_time" => Time.now.to_s }))
]
)
]
end
let(:reliable_run_values) do
{
"name" => "unstable spec",
"status" => "failed",
"file_path" => "/some/spec.rb",
"stage" => "create",
"product_group" => "code_review",
"failure_exception" => failure_message,
"job_url" => "https://job/url",
"testcase" => "https://testcase/url",
"failure_issue" => "https://issues/url",
"run_type" => "e2e-test-on-omnibus",
"_time" => time
}
end
let(:reliable_run_more_values) do
{
"name" => "unstable spec",
"status" => "failed",
"file_path" => "/some/spec.rb",
"stage" => "manage",
"product_group" => "import_and_integrate",
"failure_exception" => failure_message,
"job_url" => "https://job/url",
"testcase" => "https://testcase/url",
"failure_issue" => "https://issues/url",
"run_type" => "e2e-test-on-omnibus",
"_time" => time
}
end
let(:reliable_runs) do
[
instance_double(
"InfluxDB2::FluxTable",
records: [
instance_double("InfluxDB2::FluxRecord", values: { **reliable_run_values, "status" => "passed" }),
instance_double("InfluxDB2::FluxRecord", values: reliable_run_values),
instance_double("InfluxDB2::FluxRecord", values: reliable_run_values.merge({ "_time" => Time.now.to_s }))
]
),
instance_double(
"InfluxDB2::FluxTable",
records: [
instance_double("InfluxDB2::FluxRecord", values: { **reliable_run_more_values, "status" => "passed" }),
instance_double("InfluxDB2::FluxRecord", values: reliable_run_more_values),
instance_double("InfluxDB2::FluxRecord", values: reliable_run_more_values.merge({ "_time" => Time.now.to_s }))
]
)
]
end
def flux_query(blocking:)
<<~QUERY
from(bucket: "e2e-test-stats-main")
|> 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 == "production-full" or
r.run_type == "production-sanity" or
r.run_type == "e2e-test-on-omnibus" or
r.run_type == "e2e-test-on-gdk" or
r.run_type == "nightly"
)
|> filter(fn: (r) => r.job_name != "airgapped" and
r.job_name != "nplus1-instance-image"
)
|> filter(fn: (r) => r.status != "pending" and
r.merge_request == "false" and
r.quarantined == "false" and
r.smoke == "false" and
r.blocking == "#{blocking}"
)
|> filter(fn: (r) => r["_field"] == "job_url" or
r["_field"] == "failure_exception" or
r["_field"] == "id" or
r["_field"] == "failure_issue"
)
|> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
|> group(columns: ["name"])
QUERY
end
def expected_stage_markdown(result, stage, product_group, type)
<<~SECTION.strip
## #{stage.capitalize} (1)
<details>
<summary>Executions table ~\"group::#{product_group}\" (1)</summary>
#{table(result, ['NAME', 'RUNS', 'FAILURES', 'FAILURE RATE'], "Top #{type} specs in '#{stage}' stage for past #{range} days", true)}
</details>
SECTION
end
def expected_summary_table(summary, type, markdown = false)
table(summary, %w[STAGE COUNT], "#{type.capitalize} spec summary for past #{range} days".ljust(50), markdown)
end
def table(rows, headings, title, markdown = false)
Terminal::Table.new(
headings: headings,
title: markdown ? nil : title,
rows: rows,
style: markdown ? { border: :markdown } : { all_separators: true }
)
end
def name_column(spec_name, exceptions_and_related_urls = {})
"**Name**: #{spec_name}<br>**File**: [spec.rb](https://gitlab.com/gitlab-org/gitlab/-/blob/master/qa/qa/specs/features/some/spec.rb)#{exceptions_markdown(exceptions_and_related_urls)}"
end
def exceptions_markdown(exceptions_and_related_urls)
exceptions_and_related_urls.empty? ? '' : "<br>**Exceptions**:<br>- [`#{failure_message}`](https://issues/url)"
end
describe '.run' do
subject(:run) { described_class.run(range: range, report_in_issue_and_slack: create_issue) }
context "without report creation" do
let(:create_issue) { "false" }
it "does not create report issue", :aggregate_failures do
expect { run }.to output.to_stdout
expect(RestClient::Request).not_to have_received(:execute)
expect(slack_notifier).not_to have_received(:post)
end
end
context "with report creation" do
let(:create_issue) { "true" }
let(:iid) { 2 }
let(:old_iid) { 1 }
let(:issue_endpoint) { "gitlab_api_url/projects/278964/issues" }
let(:common_api_args) do
{
verify_ssl: false,
headers: { "PRIVATE-TOKEN" => "gitlab_token" }
}
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
before do
allow(RestClient::Request).to receive(:execute).exactly(4).times.and_return(
create_issue_response,
open_issues_response,
success_response,
success_response
)
end
shared_examples 'report creation' do
it "creates report issue" do
expect { run }.to output.to_stdout
expect(RestClient::Request).to have_received(:execute).with(
method: :post,
url: issue_endpoint,
payload: {
title: "Reliable e2e test report",
description: expected_issue_body,
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
)
expect(slack_notifier).to have_received(:post).with(
icon_emoji: ":tanuki-protect:",
text: expected_slack_text
)
end
end
context "with disallowed exception" do
let(:failure_message) { 'random failure message' }
let(:expected_issue_body) do
<<~TXT.strip
[[_TOC_]]
# Candidates for promotion to blocking (#{Date.today - range} - #{Date.today})
**Note: MRs will be auto-created for promoting the top #{QA::Tools::ReliableReport::PROMOTION_BATCH_LIMIT} specs sorted by most number of successful runs**
Total amount: **2**
#{expected_summary_table([['create', 1], ['manage', 1]], :stable, true)}
#{expected_stage_markdown([[name_column('stable spec1'), 3, 0, '0%']], 'create', 'code review', :stable)}
#{expected_stage_markdown([[name_column('stable spec2'), 3, 0, '0%']], 'manage', 'import and integrate', :stable)}
# Blocking specs with failures (#{Date.today - range} - #{Date.today})
**Note:**
* Only failures from the nightly, e2e-test-on-omnibus and e2e-test-on-gdk pipelines are considered
* Only specs that have a failure rate of equal or greater than 1 percent are considered
* Quarantine MRs will be created for all specs listed below
Total amount: **2**
#{expected_summary_table([['create', 1], ['manage', 1]], :unstable, true)}
#{expected_stage_markdown([[name_column('unstable spec', { failure_message => 'https://job/url' }), 3, 2, '66.67%']], 'create', 'code review', :unstable)}
#{expected_stage_markdown([[name_column('unstable spec', { failure_message => 'https://job/url' }), 3, 2, '66.67%']], 'manage', 'import and integrate', :unstable)}
TXT
end
let(:expected_slack_text) do
<<~TEXT
```#{expected_summary_table([['create', 1], ['manage', 1]], :stable)}```
```#{expected_summary_table([['create', 1], ['manage', 1]], :unstable)}```
#{issue_url}
TEXT
end
it_behaves_like "report creation"
end
context "with allowed exception" do
let(:failure_message) { 'Ambiguous match' }
let(:expected_issue_body) do
<<~TXT.strip
[[_TOC_]]
# Candidates for promotion to blocking (#{Date.today - range} - #{Date.today})
**Note: MRs will be auto-created for promoting the top #{QA::Tools::ReliableReport::PROMOTION_BATCH_LIMIT} specs sorted by most number of successful runs**
Total amount: **2**
#{expected_summary_table([['create', 1], ['manage', 1]], :stable, true)}
#{expected_stage_markdown([[name_column('stable spec1'), 3, 0, '0%']], 'create', 'code review', :stable)}
#{expected_stage_markdown([[name_column('stable spec2'), 3, 0, '0%']], 'manage', 'import and integrate', :stable)}
TXT
end
let(:expected_slack_text) do
<<~TEXT
```#{expected_summary_table([['create', 1], ['manage', 1]], :stable)}```
```#{expected_summary_table([], :unstable)}```
#{issue_url}
TEXT
end
it_behaves_like "report creation"
end
end
context "with failure" do
let(:create_issue) { "true" }
before do
allow(query_api).to receive(:query).and_raise("Connection error!")
end
it "notifies failure", :aggregate_failures do
expect { expect { run }.to raise_error("Connection error!") }.to output.to_stdout
expect(slack_notifier).to have_received(:post).with(
icon_emoji: ":sadpanda:",
text: "Reliable reporter failed to create report. Error: ```Connection error!```"
)
end
end
end
describe "#allowed_failure?" do
subject(:reliable_report) { described_class.new(14) }
it "returns true for an allowed failure" do
expect(reliable_report.send(:allowed_failure?, "Couldn't find option named abc")).to be true
end
it "returns false for disallowed failure" do
expect(reliable_report.send(:allowed_failure?,
%q([Unable to find css "[data-testid=\"user_action_dropdown\"]"]))).to be false
end
end
describe "#issue_for_most_failures" do
subject(:reliable_report) { described_class.new(14) }
let(:failure_message_1) { "This is a failure exception 1" }
let(:failure_message_2) { "This is a failure exception 2" }
let(:job_url) { "https://example.com/job/url" }
let(:failure_issue_url_1) { "https://example.com/failure/issue_1" }
let(:failure_issue_url_2) { "https://example.com/failure/issue_2" }
let(:records) do
[
instance_double("InfluxDB2::FluxRecord", values: values_1),
instance_double("InfluxDB2::FluxRecord", values: values_2),
instance_double("InfluxDB2::FluxRecord", values: values_2)
]
end
let(:values_1) do
{
"failure_exception" => failure_message_1,
"failure_issue" => failure_issue_url_1,
"job_url" => job_url
}
end
let(:values_2) do
{
"failure_exception" => failure_message_2,
"failure_issue" => failure_issue_url_2,
"job_url" => job_url
}
end
let(:values_3) do
{
"failure_exception" => failure_message_2,
"failure_issue" => failure_issue_url_2,
"job_url" => job_url
}
end
it 'returns the failure issue with most failures' do
expect(reliable_report.send(:issue_for_most_failures, records)).to eq(failure_issue_url_2)
end
end
describe "#exceptions_and_related_urls" do
subject(:reliable_report) { described_class.new(14) }
let(:failure_message) { "This is a failure exception" }
let(:job_url) { "https://example.com/job/url" }
let(:failure_issue_url) { "https://example.com/failure/issue" }
let(:records) do
[instance_double("InfluxDB2::FluxRecord", values: values)]
end
context "without failure_exception" do
let(:values) do
{
"failure_exception" => nil,
"job_url" => job_url,
"failure_issue" => failure_issue_url
}
end
it "returns an empty hash" do
expect(reliable_report.send(:exceptions_and_related_urls, records)).to be_empty
end
end
context "with failure_exception" do
context "without failure_issue" do
let(:values) do
{
"failure_exception" => failure_message,
"job_url" => job_url
}
end
it "returns job_url as value" do
expect(reliable_report.send(:exceptions_and_related_urls, records).values).to eq([job_url])
end
end
context "with failure_issue and job_url" do
let(:values) do
{
"failure_exception" => failure_message,
"failure_issue" => failure_issue_url,
"job_url" => job_url
}
end
it "returns failure_issue as value" do
expect(reliable_report.send(:exceptions_and_related_urls, records).values).to eq([failure_issue_url])
end
end
end
end
describe "#specs_attributes" do
subject(:reliable_report) { described_class.new(14) }
let(:promotion_batch_limit) { 10 }
let(:report_web_url) { 'https://report/url' }
before do
allow(reliable_report).to receive(:report_web_url).and_return(report_web_url)
end
shared_examples "spec attributes" do |blocking|
it "returns #{blocking} spec attributes" do
stub_const("QA::Tools::ReliableReport::PROMOTION_BATCH_LIMIT", promotion_batch_limit)
expect(reliable_report.send(:specs_attributes, blocking: blocking)).to eq(expected_specs_attributes)
end
end
context "with blocking true" do
let(:expected_specs_attributes) do
{ type: "Unstable Specs",
report_issue: "https://report/url",
specs:
[
{ stage: "create",
product_group: "code_review",
name: "unstable spec",
file: "spec.rb",
link: "https://gitlab.com/gitlab-org/gitlab/-/blob/master/qa/qa/specs/features/some/spec.rb",
runs: 3,
failed: 2,
failure_issue: "https://issues/url",
failure_rate: 66.67,
testcase: "https://testcase/url",
file_path: "/qa/qa/specs/features/some/spec.rb",
all_run_type: ["e2e-test-on-omnibus"],
failed_run_type: ["e2e-test-on-omnibus"] },
{ stage: "manage",
product_group: "import_and_integrate",
name: "unstable spec",
file: "spec.rb",
link: "https://gitlab.com/gitlab-org/gitlab/-/blob/master/qa/qa/specs/features/some/spec.rb",
runs: 3,
failed: 2,
failure_issue: "https://issues/url",
failure_rate: 66.67,
testcase: "https://testcase/url",
file_path: "/qa/qa/specs/features/some/spec.rb",
all_run_type: ["e2e-test-on-omnibus"],
failed_run_type: ["e2e-test-on-omnibus"] }
] }
end
it_behaves_like "spec attributes", true
end
context "with blocking false" do
let(:expected_specs_attributes) do
{
type: "Stable Specs",
report_issue: "https://report/url",
specs:
[
{ stage: "manage",
product_group: "import_and_integrate",
name: "stable spec2",
file: "spec.rb",
link: "https://gitlab.com/gitlab-org/gitlab/-/blob/master/qa/qa/specs/features/some/spec.rb",
runs: 3,
failed: 0,
failure_issue: "",
failure_rate: 0,
testcase: "https://testcase/url",
file_path: "/qa/qa/specs/features/some/spec.rb",
all_run_type: ["e2e-test-on-omnibus"],
failed_run_type: [] },
{ stage: "create",
product_group: "code_review",
name: "stable spec1",
file: "spec.rb",
link: "https://gitlab.com/gitlab-org/gitlab/-/blob/master/qa/qa/specs/features/some/spec.rb",
runs: 3,
failed: 0,
failure_issue: "",
failure_rate: 0,
testcase: "https://testcase/url",
file_path: "/qa/qa/specs/features/some/spec.rb",
all_run_type: ["e2e-test-on-omnibus"],
failed_run_type: [] }
]
}
end
it_behaves_like "spec attributes", false
context "with specific PROMOTION_BATCH_LIMIT" do
let(:promotion_batch_limit) { 1 }
let(:runs) do
[
instance_double(
"InfluxDB2::FluxTable",
records: [
instance_double("InfluxDB2::FluxRecord", values: run_values),
instance_double("InfluxDB2::FluxRecord", values: run_values),
instance_double("InfluxDB2::FluxRecord", values: run_values.merge({ "_time" => Time.now.to_s }))
]
),
instance_double(
"InfluxDB2::FluxTable",
records: [
instance_double("InfluxDB2::FluxRecord", values: run_more_values),
instance_double("InfluxDB2::FluxRecord", values: run_more_values)
]
)
]
end
let(:expected_specs_attributes) do
{
type: "Stable Specs",
report_issue: "https://report/url",
specs:
[
{ stage: "create",
product_group: "code_review",
name: "stable spec1",
file: "spec.rb",
link: "https://gitlab.com/gitlab-org/gitlab/-/blob/master/qa/qa/specs/features/some/spec.rb",
runs: 3,
failed: 0,
failure_issue: "",
failure_rate: 0,
testcase: "https://testcase/url",
file_path: "/qa/qa/specs/features/some/spec.rb",
all_run_type: ["e2e-test-on-omnibus"],
failed_run_type: [] }
]
}
end
it_behaves_like "spec attributes", false
end
end
end
describe "#skip_blocking_spec_record?" do
subject(:reliable_report) { described_class.new(14) }
using RSpec::Parameterized::TableSyntax
where(:failed_count, :failure_issue, :failed_run_type, :failure_rate, :result) do
1 | 'https://failure/issues/url' | ['e2e-test-on-omnibus'] | 2 | false
1 | 'https://failure/issues/url' | ['e2e-test-on-gdk'] | 2 | false
1 | 'https://failure/issues/url' | ['nightly'] | 2 | false
0 | 'https://failure/issues/url' | ['e2e-test-on-gdk'] | 2 | true
1 | 'https://failure/issue/url' | ['e2e-test-on-gdk'] | 2 | true
1 | 'https://failure/issues/url' | ['abc'] | 2 | true
1 | 'https://failure/issues/url' | ['e2e-test-on-gdk'] | 0 | true
end
with_them do
it do
expect(reliable_report.send(:skip_blocking_spec_record?, failed_count: failed_count,
failure_issue: failure_issue, failed_run_type: failed_run_type, failure_rate: failure_rate))
.to eq result
end
end
end
end
|