|
1
|
+ |
defmodule OpenAgents.Forge.ReceiptRepositoryTest do
|
|
2
|
+ |
@moduledoc """
|
|
3
|
+ |
Which repository a build or deploy receipt belongs to, #181.
|
|
4
|
+ |
|
|
5
|
+ |
`forge_builds.repo` and `forge_deploys.repo` hold a repository *name*, and
|
|
6
|
+ |
`repositories` is unique on `{namespace_id, name_key}` rather than on `name`,
|
|
7
|
+ |
so a name can answer for two repositories. This file pins what the string
|
|
8
|
+ |
could not decide and what the key now does.
|
|
9
|
+ |
"""
|
|
10
|
+ |
|
|
11
|
+ |
use OpenAgents.DataCase, async: false
|
|
12
|
+ |
|
|
13
|
+ |
import Ecto.Query
|
|
14
|
+ |
|
|
15
|
+ |
alias OpenAgents.AccountsFixtures
|
|
16
|
+ |
alias OpenAgents.Forge.{BuildReceipt, DeployReceipt, ReceiptRepository}
|
|
17
|
+ |
alias OpenAgents.Repo
|
|
18
|
+ |
alias OpenAgents.Repositories.Repository
|
|
19
|
+ |
|
|
20
|
+ |
describe "what each receipt column actually holds" do
|
|
21
|
+ |
test "a push receipt's repo is a storage key, which is already unambiguous" do
|
|
22
|
+ |
# `forge_pushes` deliberately has no `repository_id`. This is the reason:
|
|
23
|
+ |
# its `repo` is `Repository.storage_key`, which carries a unique index,
|
|
24
|
+ |
# so it names exactly one repository already. EXIT-003 keeps every column
|
|
25
|
+ |
# there re-derivable from the WAL, and a key only PostgreSQL can produce
|
|
26
|
+ |
# would make the database a second opinion about a push.
|
|
27
|
+ |
%{rows: [[unique?]]} =
|
|
28
|
+ |
Repo.query!("""
|
|
29
|
+ |
SELECT count(*) = 1
|
|
30
|
+ |
FROM pg_indexes
|
|
31
|
+ |
WHERE tablename = 'repositories'
|
|
32
|
+ |
AND indexdef LIKE 'CREATE UNIQUE INDEX%(storage_key)'
|
|
33
|
+ |
""")
|
|
34
|
+ |
|
|
35
|
+ |
assert unique?
|
|
36
|
+ |
|
|
37
|
+ |
refute :repository_id in OpenAgents.Forge.PushReceipt.__schema__(:fields)
|
|
38
|
+ |
|
|
39
|
+ |
%{rows: [[present]]} =
|
|
40
|
+ |
Repo.query!("""
|
|
41
|
+ |
SELECT count(*) FROM information_schema.columns
|
|
42
|
+ |
WHERE table_name = 'forge_pushes' AND column_name = 'repository_id'
|
|
43
|
+ |
""")
|
|
44
|
+ |
|
|
45
|
+ |
assert present == 0
|
|
46
|
+ |
end
|
|
47
|
+ |
|
|
48
|
+ |
test "a build or deploy receipt's repo is a name, which two repositories can share" do
|
|
49
|
+ |
first = AccountsFixtures.repository_fixture(%{owner: "FirstOrg", name: "shared-name"})
|
|
50
|
+ |
second = AccountsFixtures.repository_fixture(%{owner: "SecondOrg", name: "shared-name"})
|
|
51
|
+ |
|
|
52
|
+ |
assert first.name == second.name
|
|
53
|
+ |
refute first.storage_key == second.storage_key
|
|
54
|
+ |
|
|
55
|
+ |
assert Repo.aggregate(from(r in Repository, where: r.name == "shared-name"), :count) == 2
|
|
56
|
+ |
end
|
|
57
|
+ |
end
|
|
58
|
+ |
|
|
59
|
+ |
describe "resolve/1" do
|
|
60
|
+ |
test "answers for a storage key, a bare name, and an owner/name path" do
|
|
61
|
+ |
repository = AccountsFixtures.repository_fixture(%{owner: "ResolveOrg", name: "resolvable"})
|
|
62
|
+ |
|
|
63
|
+ |
assert ReceiptRepository.resolve(repository.storage_key).id == repository.id
|
|
64
|
+ |
assert ReceiptRepository.resolve("resolvable").id == repository.id
|
|
65
|
+ |
assert ReceiptRepository.resolve("ResolveOrg/resolvable").id == repository.id
|
|
66
|
+ |
end
|
|
67
|
+ |
|
|
68
|
+ |
test "refuses a name two repositories answer to, and a name none answers to" do
|
|
69
|
+ |
AccountsFixtures.repository_fixture(%{owner: "AmbiguousA", name: "two-answers"})
|
|
70
|
+ |
AccountsFixtures.repository_fixture(%{owner: "AmbiguousB", name: "two-answers"})
|
|
71
|
+ |
|
|
72
|
+ |
assert ReceiptRepository.resolve("two-answers") == nil
|
|
73
|
+ |
assert ReceiptRepository.resolve("no-such-repository") == nil
|
|
74
|
+ |
assert ReceiptRepository.resolve(nil) == nil
|
|
75
|
+ |
end
|
|
76
|
+ |
end
|
|
77
|
+ |
|
|
78
|
+ |
describe "scope/3" do
|
|
79
|
+ |
test "a keyed receipt is matched by its key and never by another repository's name" do
|
|
80
|
+ |
mine = AccountsFixtures.repository_fixture(%{owner: "ScopeMine", name: "collide"})
|
|
81
|
+ |
theirs = AccountsFixtures.repository_fixture(%{owner: "ScopeTheirs", name: "collide"})
|
|
82
|
+ |
|
|
83
|
+ |
ours = build_receipt!("collide", mine.id)
|
|
84
|
+ |
not_ours = build_receipt!("collide", theirs.id)
|
|
85
|
+ |
|
|
86
|
+ |
found =
|
|
87
|
+ |
BuildReceipt
|
|
88
|
+ |
|> ReceiptRepository.scope(mine, ["collide"])
|
|
89
|
+ |
|> select([b], b.id)
|
|
90
|
+ |
|> Repo.all()
|
|
91
|
+ |
|
|
92
|
+ |
assert ours.id in found
|
|
93
|
+ |
refute not_ours.id in found
|
|
94
|
+ |
end
|
|
95
|
+ |
|
|
96
|
+ |
test "a receipt with no key still reads by its string" do
|
|
97
|
+ |
repository = AccountsFixtures.repository_fixture(%{owner: "ScopeOld", name: "historical"})
|
|
98
|
+ |
unkeyed = build_receipt!("historical", nil)
|
|
99
|
+ |
|
|
100
|
+ |
found =
|
|
101
|
+ |
BuildReceipt
|
|
102
|
+ |
|> ReceiptRepository.scope(repository, ["historical"])
|
|
103
|
+ |
|> select([b], b.id)
|
|
104
|
+ |
|> Repo.all()
|
|
105
|
+ |
|
|
106
|
+ |
assert unkeyed.id in found
|
|
107
|
+ |
end
|
|
108
|
+ |
|
|
109
|
+ |
test "with no repository to name, the string is all there is" do
|
|
110
|
+ |
unkeyed = build_receipt!("unsettled-name", nil)
|
|
111
|
+ |
|
|
112
|
+ |
found =
|
|
113
|
+ |
BuildReceipt
|
|
114
|
+ |
|> ReceiptRepository.scope(nil, ["unsettled-name"])
|
|
115
|
+ |
|> select([b], b.id)
|
|
116
|
+ |
|> Repo.all()
|
|
117
|
+ |
|
|
118
|
+ |
assert found == [unkeyed.id]
|
|
119
|
+ |
end
|
|
120
|
+ |
end
|
|
121
|
+ |
|
|
122
|
+ |
describe "backfill!/1" do
|
|
123
|
+ |
test "fills a name exactly one repository answers to and leaves the rest null" do
|
|
124
|
+ |
repository = AccountsFixtures.repository_fixture(%{owner: "FillOrg", name: "fillable"})
|
|
125
|
+ |
AccountsFixtures.repository_fixture(%{owner: "ClashA", name: "clashing"})
|
|
126
|
+ |
AccountsFixtures.repository_fixture(%{owner: "ClashB", name: "clashing"})
|
|
127
|
+ |
|
|
128
|
+ |
by_name = build_receipt!("fillable", nil)
|
|
129
|
+ |
by_path = build_receipt!("FillOrg/fillable", nil)
|
|
130
|
+ |
by_storage_key = build_receipt!(repository.storage_key, nil)
|
|
131
|
+ |
ambiguous = build_receipt!("clashing", nil)
|
|
132
|
+ |
absent = build_receipt!("gone-from-the-forge", nil)
|
|
133
|
+ |
|
|
134
|
+ |
assert ReceiptRepository.backfill!("forge_builds") >= 3
|
|
135
|
+ |
|
|
136
|
+ |
assert reload(by_name).repository_id == repository.id
|
|
137
|
+ |
assert reload(by_path).repository_id == repository.id
|
|
138
|
+ |
assert reload(by_storage_key).repository_id == repository.id
|
|
139
|
+ |
|
|
140
|
+ |
# A backfill that guesses is worse than a null. Both of these stay null,
|
|
141
|
+ |
# and a null means "not settled", never "no repository".
|
|
142
|
+ |
assert reload(ambiguous).repository_id == nil
|
|
143
|
+ |
assert reload(absent).repository_id == nil
|
|
144
|
+ |
end
|
|
145
|
+ |
|
|
146
|
+ |
test "it is idempotent and never re-points a receipt that already names one" do
|
|
147
|
+ |
first = AccountsFixtures.repository_fixture(%{owner: "IdemA", name: "idempotent"})
|
|
148
|
+ |
second = AccountsFixtures.repository_fixture(%{owner: "IdemB", name: "elsewhere"})
|
|
149
|
+ |
|
|
150
|
+ |
# Deliberately pointed at the repository its name does not name.
|
|
151
|
+ |
receipt = build_receipt!("idempotent", second.id)
|
|
152
|
+ |
|
|
153
|
+ |
assert ReceiptRepository.backfill!("forge_builds") >= 0
|
|
154
|
+ |
assert reload(receipt).repository_id == second.id
|
|
155
|
+ |
refute reload(receipt).repository_id == first.id
|
|
156
|
+ |
end
|
|
157
|
+ |
|
|
158
|
+ |
test "it does not carry the authority to rewrite a deploy receipt" do
|
|
159
|
+ |
repository = AccountsFixtures.repository_fixture(%{owner: "TriggerOrg", name: "triggered"})
|
|
160
|
+ |
receipt = deploy_receipt!("triggered", nil)
|
|
161
|
+ |
|
|
162
|
+ |
# `forge_deploys` refuses every UPDATE. The migration suspends the trigger
|
|
163
|
+ |
# for the length of its own transaction; `backfill!/1` does not, so a
|
|
164
|
+ |
# caller cannot quietly acquire that authority.
|
|
165
|
+ |
assert_raise Postgrex.Error, ~r/forge deployment receipts are immutable/, fn ->
|
|
166
|
+ |
ReceiptRepository.backfill!("forge_deploys")
|
|
167
|
+ |
end
|
|
168
|
+ |
|
|
169
|
+ |
Repo.query!("ALTER TABLE forge_deploys DISABLE TRIGGER forge_deploy_receipts_immutable")
|
|
170
|
+ |
assert ReceiptRepository.backfill!("forge_deploys") >= 1
|
|
171
|
+ |
Repo.query!("ALTER TABLE forge_deploys ENABLE TRIGGER forge_deploy_receipts_immutable")
|
|
172
|
+ |
|
|
173
|
+ |
assert Repo.get(DeployReceipt, receipt.id).repository_id == repository.id
|
|
174
|
+ |
end
|
|
175
|
+ |
end
|
|
176
|
+ |
|
|
177
|
+ |
## helpers
|
|
178
|
+ |
|
|
179
|
+ |
defp build_receipt!(repo, repository_id) do
|
|
180
|
+ |
%BuildReceipt{}
|
|
181
|
+ |
|> BuildReceipt.changeset(%{
|
|
182
|
+ |
repo: repo,
|
|
183
|
+ |
repository_id: repository_id,
|
|
184
|
+ |
sha: String.duplicate("a", 40),
|
|
185
|
+ |
target_id: Ecto.UUID.generate(),
|
|
186
|
+ |
status: "complete"
|
|
187
|
+ |
})
|
|
188
|
+ |
|> Repo.insert!()
|
|
189
|
+ |
end
|
|
190
|
+ |
|
|
191
|
+ |
defp deploy_receipt!(repo, repository_id) do
|
|
192
|
+ |
%DeployReceipt{}
|
|
193
|
+ |
|> DeployReceipt.changeset(%{
|
|
194
|
+ |
repo: repo,
|
|
195
|
+ |
repository_id: repository_id,
|
|
196
|
+ |
sha: String.duplicate("b", 40),
|
|
197
|
+ |
target_id: Ecto.UUID.generate(),
|
|
198
|
+ |
result: "live"
|
|
199
|
+ |
})
|
|
200
|
+ |
|> Repo.insert!()
|
|
201
|
+ |
end
|
|
202
|
+ |
|
|
203
|
+ |
defp reload(%BuildReceipt{id: id}), do: Repo.get!(BuildReceipt, id)
|
|
204
|
+ |
end
|