Skip to content

Commit 3f78a32

Browse files
committed
Refactor containerd host.toml generation
Refactors the code to make the test code readable.
1 parent 15c8ace commit 3f78a32

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

pkg/registries/registries.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,13 @@ func (ms *mirrorSet) generateConfig(secretsConfig credentialprovider.DockerConfi
210210
fallbackServer = ms.mirrors[0].host
211211
}
212212
result += fmt.Sprintf("server = \"https://%s\"", fallbackServer)
213-
result += "\r\n"
213+
result += "\n"
214214

215215
// Each mirror should result in an entry followed by a set of settings for interacting with the mirror host
216216
for _, m := range ms.mirrors {
217-
result += "\r\n"
217+
result += "\n"
218218
result += fmt.Sprintf("[host.\"https://%s\"]", m.host)
219-
result += "\r\n"
219+
result += "\n"
220220

221221
// Specify the operations the registry host may perform. IDMS mirrors can only be pulled by directly by digest,
222222
// whereas ITMS mirrors have the additional resolve capability, which allows converting a tag name into a digest
@@ -227,7 +227,7 @@ func (ms *mirrorSet) generateConfig(secretsConfig credentialprovider.DockerConfi
227227
hostCapabilities = " capabilities = [\"pull\"]"
228228
}
229229
result += hostCapabilities
230-
result += "\r\n"
230+
result += "\n"
231231

232232
// Extract the mirror repo's authorization credentials, if one exists
233233
if entry, ok := secretsConfig.Auths[extractHostname(m.host)]; ok {
@@ -236,9 +236,9 @@ func (ms *mirrorSet) generateConfig(secretsConfig credentialprovider.DockerConfi
236236

237237
// Add the access token as a request header
238238
result += fmt.Sprintf(" [host.\"https://%s\".header]", m.host)
239-
result += "\r\n"
239+
result += "\n"
240240
result += fmt.Sprintf(" authorization = \"Basic %s\"", token)
241-
result += "\r\n"
241+
result += "\n"
242242
}
243243
}
244244

pkg/registries/registries_test.go

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,11 @@ func TestGenerateConfig(t *testing.T) {
252252
},
253253
mirrorSourcePolicy: config.AllowContactingSource,
254254
},
255-
expectedOutput: "server = \"https://registry.access.redhat.com/ubi9/ubi-minimal\"\r\n\r\n[host.\"https://example.io/example/ubi-minimal\"]\r\n capabilities = [\"pull\"]\r\n",
255+
expectedOutput: `server = "https://registry.access.redhat.com/ubi9/ubi-minimal"
256+
257+
[host."https://example.io/example/ubi-minimal"]
258+
capabilities = ["pull"]
259+
`,
256260
},
257261
{
258262
name: "basic one tag mirror",
@@ -263,7 +267,11 @@ func TestGenerateConfig(t *testing.T) {
263267
},
264268
mirrorSourcePolicy: config.AllowContactingSource,
265269
},
266-
expectedOutput: "server = \"https://registry.access.redhat.com/ubi9/ubi-minimal\"\r\n\r\n[host.\"https://example.io/example/ubi-minimal\"]\r\n capabilities = [\"pull\", \"resolve\"]\r\n",
270+
expectedOutput: `server = "https://registry.access.redhat.com/ubi9/ubi-minimal"
271+
272+
[host."https://example.io/example/ubi-minimal"]
273+
capabilities = ["pull", "resolve"]
274+
`,
267275
},
268276
{
269277
name: "one digest mirror never contact source",
@@ -274,7 +282,11 @@ func TestGenerateConfig(t *testing.T) {
274282
},
275283
mirrorSourcePolicy: config.NeverContactSource,
276284
},
277-
expectedOutput: "server = \"https://example.io/example/ubi-minimal\"\r\n\r\n[host.\"https://example.io/example/ubi-minimal\"]\r\n capabilities = [\"pull\"]\r\n",
285+
expectedOutput: `server = "https://example.io/example/ubi-minimal"
286+
287+
[host."https://example.io/example/ubi-minimal"]
288+
capabilities = ["pull"]
289+
`,
278290
},
279291
{
280292
name: "tags mirror never contact source",
@@ -285,7 +297,11 @@ func TestGenerateConfig(t *testing.T) {
285297
},
286298
mirrorSourcePolicy: config.NeverContactSource,
287299
},
288-
expectedOutput: "server = \"https://example.io/example/ubi-minimal\"\r\n\r\n[host.\"https://example.io/example/ubi-minimal\"]\r\n capabilities = [\"pull\", \"resolve\"]\r\n",
300+
expectedOutput: `server = "https://example.io/example/ubi-minimal"
301+
302+
[host."https://example.io/example/ubi-minimal"]
303+
capabilities = ["pull", "resolve"]
304+
`,
289305
},
290306
{
291307
name: "multiple mirrors",
@@ -298,7 +314,21 @@ func TestGenerateConfig(t *testing.T) {
298314
},
299315
mirrorSourcePolicy: config.AllowContactingSource,
300316
},
301-
expectedOutput: "server = \"https://registry.access.redhat.com/ubi9/ubi-minimal\"\r\n\r\n[host.\"https://example.io/example/ubi-minimal\"]\r\n capabilities = [\"pull\"]\r\n\r\n[host.\"https://mirror.example.com/redhat\"]\r\n capabilities = [\"pull\"]\r\n [host.\"https://mirror.example.com/redhat\".header]\r\n authorization = \"Basic dXNlcjpwYXNz\"\r\n\r\n[host.\"https://mirror.example.net/image\"]\r\n capabilities = [\"pull\", \"resolve\"]\r\n [host.\"https://mirror.example.net/image\".header]\r\n authorization = \"Basic dXNlcm5hbWU6cGFzc3dvcmQ=\"\r\n",
317+
expectedOutput: `server = "https://registry.access.redhat.com/ubi9/ubi-minimal"
318+
319+
[host."https://example.io/example/ubi-minimal"]
320+
capabilities = ["pull"]
321+
322+
[host."https://mirror.example.com/redhat"]
323+
capabilities = ["pull"]
324+
[host."https://mirror.example.com/redhat".header]
325+
authorization = "Basic dXNlcjpwYXNz"
326+
327+
[host."https://mirror.example.net/image"]
328+
capabilities = ["pull", "resolve"]
329+
[host."https://mirror.example.net/image".header]
330+
authorization = "Basic dXNlcm5hbWU6cGFzc3dvcmQ="
331+
`,
302332
},
303333
{
304334
name: "multiple mirrors never contact source",
@@ -311,7 +341,21 @@ func TestGenerateConfig(t *testing.T) {
311341
},
312342
mirrorSourcePolicy: config.NeverContactSource,
313343
},
314-
expectedOutput: "server = \"https://example.io/example/ubi-minimal\"\r\n\r\n[host.\"https://example.io/example/ubi-minimal\"]\r\n capabilities = [\"pull\"]\r\n\r\n[host.\"https://mirror.example.com/redhat\"]\r\n capabilities = [\"pull\"]\r\n [host.\"https://mirror.example.com/redhat\".header]\r\n authorization = \"Basic dXNlcjpwYXNz\"\r\n\r\n[host.\"https://mirror.example.net\"]\r\n capabilities = [\"pull\", \"resolve\"]\r\n [host.\"https://mirror.example.net\".header]\r\n authorization = \"Basic dXNlcm5hbWU6cGFzc3dvcmQ=\"\r\n",
344+
expectedOutput: `server = "https://example.io/example/ubi-minimal"
345+
346+
[host."https://example.io/example/ubi-minimal"]
347+
capabilities = ["pull"]
348+
349+
[host."https://mirror.example.com/redhat"]
350+
capabilities = ["pull"]
351+
[host."https://mirror.example.com/redhat".header]
352+
authorization = "Basic dXNlcjpwYXNz"
353+
354+
[host."https://mirror.example.net"]
355+
capabilities = ["pull", "resolve"]
356+
[host."https://mirror.example.net".header]
357+
authorization = "Basic dXNlcm5hbWU6cGFzc3dvcmQ="
358+
`,
315359
},
316360
}
317361

0 commit comments

Comments
 (0)