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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::BackgroundMigration::BackfillClusterAgentsHasVulnerabilities, :migration do # rubocop:disable Layout/LineLength
let(:migration) do
described_class.new(start_id: 1, end_id: 10,
batch_table: table_name, batch_column: batch_column,
sub_batch_size: sub_batch_size, pause_ms: pause_ms,
connection: ApplicationRecord.connection)
end
let(:users_table) { table(:users) }
let(:vulnerability_reads_table) { table(:vulnerability_reads) }
let(:vulnerability_scanners_table) { table(:vulnerability_scanners) }
let(:vulnerabilities_table) { table(:vulnerabilities) }
let(:namespaces_table) { table(:namespaces) }
let(:projects_table) { table(:projects) }
let(:cluster_agents_table) { table(:cluster_agents) }
let(:table_name) { 'cluster_agents' }
let(:batch_column) { :id }
let(:sub_batch_size) { 100 }
let(:pause_ms) { 0 }
subject(:perform_migration) { migration.perform }
before do
users_table.create!(id: 1, name: 'John Doe', email: 'test@example.com', projects_limit: 5)
namespaces_table.create!(id: 1, name: 'Namespace 1', path: 'namespace-1')
namespaces_table.create!(id: 2, name: 'Namespace 2', path: 'namespace-2')
namespaces_table.create!(id: 3, name: 'Namespace 3', path: 'namespace-3')
projects_table.create!(id: 1, namespace_id: 1, name: 'Project 1', path: 'project-1', project_namespace_id: 1)
projects_table.create!(id: 2, namespace_id: 2, name: 'Project 2', path: 'project-2', project_namespace_id: 2)
projects_table.create!(id: 3, namespace_id: 2, name: 'Project 3', path: 'project-3', project_namespace_id: 3)
cluster_agents_table.create!(id: 1, name: 'Agent 1', project_id: 1)
cluster_agents_table.create!(id: 2, name: 'Agent 2', project_id: 2)
cluster_agents_table.create!(id: 3, name: 'Agent 3', project_id: 1)
cluster_agents_table.create!(id: 4, name: 'Agent 4', project_id: 1)
cluster_agents_table.create!(id: 5, name: 'Agent 5', project_id: 1)
cluster_agents_table.create!(id: 6, name: 'Agent 6', project_id: 1)
cluster_agents_table.create!(id: 7, name: 'Agent 7', project_id: 3)
cluster_agents_table.create!(id: 8, name: 'Agent 8', project_id: 1)
cluster_agents_table.create!(id: 9, name: 'Agent 9', project_id: 1)
cluster_agents_table.create!(id: 10, name: 'Agent 10', project_id: 3)
cluster_agents_table.create!(id: 11, name: 'Agent 11', project_id: 1)
vulnerability_scanners_table.create!(id: 1, project_id: 1, external_id: 'starboard', name: 'Starboard')
vulnerability_scanners_table.create!(id: 2, project_id: 2, external_id: 'starboard', name: 'Starboard')
vulnerability_scanners_table.create!(id: 3, project_id: 3, external_id: 'starboard', name: 'Starboard')
add_vulnerability_read!(1, project_id: 1, cluster_agent_id: 1, report_type: 7)
add_vulnerability_read!(2, project_id: 1, cluster_agent_id: nil, report_type: 7)
add_vulnerability_read!(3, project_id: 1, cluster_agent_id: 3, report_type: 7)
add_vulnerability_read!(4, project_id: 1, cluster_agent_id: nil, report_type: 7)
add_vulnerability_read!(5, project_id: 2, cluster_agent_id: 5, report_type: 5)
add_vulnerability_read!(7, project_id: 2, cluster_agent_id: 7, report_type: 7)
add_vulnerability_read!(9, project_id: 3, cluster_agent_id: 9, report_type: 7)
add_vulnerability_read!(10, project_id: 1, cluster_agent_id: 10, report_type: 7)
add_vulnerability_read!(11, project_id: 2, cluster_agent_id: 11, report_type: 7)
end
it 'backfills `has_vulnerabilities` for the selected records', :aggregate_failures do
queries = ActiveRecord::QueryRecorder.new do
perform_migration
end
expect(queries.count).to eq(3)
expect(cluster_agents_table.where(has_vulnerabilities: true).count).to eq 2
expect(cluster_agents_table.where(has_vulnerabilities: true).pluck(:id)).to match_array([1, 3])
end
it 'tracks timings of queries' do
expect(migration.batch_metrics.timings).to be_empty
expect { perform_migration }.to change { migration.batch_metrics.timings }
end
private
def add_vulnerability_read!(id, project_id:, cluster_agent_id:, report_type:)
vulnerabilities_table.create!(
id: id,
project_id: project_id,
author_id: 1,
title: "Vulnerability #{id}",
severity: 5,
confidence: 5,
report_type: report_type
)
vulnerability_reads_table.create!(
id: id,
uuid: SecureRandom.uuid,
severity: 5,
state: 1,
vulnerability_id: id,
scanner_id: project_id,
casted_cluster_agent_id: cluster_agent_id,
project_id: project_id,
report_type: report_type
)
end
end
|