Skip to content

Commit e150ff5

Browse files
authored
Merge pull request #85 from chmeliik/refactor-cli-executor
Refactor CLI executor interface
2 parents 1a6f74f + 43227f4 commit e150ff5

File tree

17 files changed

+348
-305
lines changed

17 files changed

+348
-305
lines changed

integration_tests/framework/common.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func CompileKonfluxCli() error {
9898
compileArgs = append(compileArgs, "-gcflags", "all=-N -l")
9999
}
100100
compileArgs = append(compileArgs, "-o", cliBinPath, mainGoPath)
101-
stdout, stderr, _, err := executor.Execute("go", compileArgs...)
101+
stdout, stderr, _, err := executor.Execute(cliWrappers.Command("go", compileArgs...))
102102
if err != nil {
103103
fmt.Printf("failed to build CLI: %s\n[stdout]:\n%s\n[stderr]:\n%s\n", err.Error(), stdout, stderr)
104104
}
@@ -208,7 +208,7 @@ func CreateTestImage(config TestImageConfig) error {
208208
for filePathInContainer, filePathOnHost := range config.Files {
209209
fileNameInContextDir := filepath.Base(filePathOnHost)
210210
dockerfileContent = append(dockerfileContent, fmt.Sprintf("COPY %s %s", fileNameInContextDir, filePathInContainer))
211-
stdout, stderr, _, err := executor.Execute("cp", filePathOnHost, path.Join(testImageDir, fileNameInContextDir))
211+
stdout, stderr, _, err := executor.Execute(cliWrappers.Command("cp", filePathOnHost, path.Join(testImageDir, fileNameInContextDir)))
212212
if err != nil {
213213
fmt.Printf("failed to copy test file: %s\n[stdout]:\n%s\n[stderr]:\n%s\n", err.Error(), stdout, stderr)
214214
return err
@@ -232,7 +232,7 @@ func CreateTestImage(config TestImageConfig) error {
232232
buildArgs = append(buildArgs, "--platform", config.Platform)
233233
}
234234
buildArgs = append(buildArgs, ".")
235-
stdout, stderr, _, err := executor.ExecuteInDir(testImageDir, containerTool, buildArgs...)
235+
stdout, stderr, _, err := executor.Execute(cliWrappers.Cmd{Name: containerTool, Args: buildArgs, Dir: testImageDir})
236236
if err != nil {
237237
fmt.Printf("failed to build test image: %s\n[stdout]:\n%s\n[stderr]:\n%s\n", err.Error(), stdout, stderr)
238238
return err
@@ -262,14 +262,14 @@ func CreateAndPushImageIndex(indexRef string, images []string) (string, error) {
262262
createManifestArgs = append(createManifestArgs, "--amend")
263263
createManifestArgs = append(createManifestArgs, indexRef)
264264
createManifestArgs = append(createManifestArgs, images...)
265-
if stdout, stderr, _, err := executor.Execute(containerTool, createManifestArgs...); err != nil {
265+
if stdout, stderr, _, err := executor.Execute(cliWrappers.Command(containerTool, createManifestArgs...)); err != nil {
266266
fmt.Printf("failed to create image index: %s\n[stdout]:\n%s\n[stderr]:\n%s\n", err.Error(), stdout, stderr)
267267
return "", err
268268
}
269269

270270
// Clean up local image index
271271
defer func() {
272-
if stdout, stderr, _, err := executor.Execute(containerTool, "manifest", "rm", indexRef); err != nil {
272+
if stdout, stderr, _, err := executor.Execute(cliWrappers.Command(containerTool, "manifest", "rm", indexRef)); err != nil {
273273
fmt.Printf("failed to clean up local image index: %s\n[stdout]:\n%s\n[stderr]:\n%s\n", err.Error(), stdout, stderr)
274274
}
275275
}()
@@ -279,7 +279,7 @@ func CreateAndPushImageIndex(indexRef string, images []string) (string, error) {
279279

280280
switch containerTool {
281281
case "docker":
282-
stdout, stderr, _, err := executor.Execute(containerTool, "manifest", "push", indexRef)
282+
stdout, stderr, _, err := executor.Execute(cliWrappers.Command(containerTool, "manifest", "push", indexRef))
283283
if err != nil {
284284
fmt.Printf("failed to push image index: %s\n[stdout]:\n%s\n[stderr]:\n%s\n", err.Error(), stdout, stderr)
285285
return "", err
@@ -288,7 +288,7 @@ func CreateAndPushImageIndex(indexRef string, images []string) (string, error) {
288288

289289
case "podman":
290290
const digestfilePath = "/tmp/index-digestfile"
291-
stdout, stderr, _, err := executor.Execute("podman", "push", "--digestfile", digestfilePath, indexRef)
291+
stdout, stderr, _, err := executor.Execute(cliWrappers.Command("podman", "push", "--digestfile", digestfilePath, indexRef))
292292
if err != nil {
293293
fmt.Printf("failed to push image index: %s\n[stdout]:\n%s\n[stderr]:\n%s\n", err.Error(), stdout, stderr)
294294
return "", err
@@ -307,7 +307,7 @@ func CreateAndPushImageIndex(indexRef string, images []string) (string, error) {
307307

308308
func DeleteLocalImage(imageRef string) error {
309309
executor := cliWrappers.NewCliExecutor()
310-
stdout, stderr, _, err := executor.Execute(containerTool, "rmi", imageRef)
310+
stdout, stderr, _, err := executor.Execute(cliWrappers.Command(containerTool, "rmi", imageRef))
311311
if err != nil {
312312
fmt.Printf("failed to remove test image: %s\n[stdout]:\n%s\n[stderr]:\n%s\n", err.Error(), stdout, stderr)
313313
}
@@ -322,7 +322,7 @@ func PushImage(imageRef string) (string, error) {
322322

323323
switch containerTool {
324324
case "docker":
325-
stdout, stderr, _, err := executor.Execute("docker", "push", imageRef)
325+
stdout, stderr, _, err := executor.Execute(cliWrappers.Command("docker", "push", imageRef))
326326
if err != nil {
327327
fmt.Printf("failed to push test image: %s\n[stdout]:\n%s\n[stderr]:\n%s\n", err.Error(), stdout, stderr)
328328
return "", err
@@ -331,7 +331,7 @@ func PushImage(imageRef string) (string, error) {
331331

332332
case "podman":
333333
const digestfilePath = "/tmp/digestfile"
334-
stdout, stderr, _, err := executor.Execute("podman", "push", "--digestfile", digestfilePath, imageRef)
334+
stdout, stderr, _, err := executor.Execute(cliWrappers.Command("podman", "push", "--digestfile", digestfilePath, imageRef))
335335
if err != nil {
336336
fmt.Printf("failed to push test image: %s\n[stdout]:\n%s\n[stderr]:\n%s\n", err.Error(), stdout, stderr)
337337
return "", err

integration_tests/framework/registry_zot_local.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ func (z *ZotRegistry) generateCerts(executor *cliWrappers.CliExecutor) error {
384384
opensslCreateCaKeyArgs := []string{
385385
"genrsa", "-out", z.rootKeyPath, "4096",
386386
}
387-
if stdout, stderr, _, err := executor.Execute("openssl", opensslCreateCaKeyArgs...); err != nil {
387+
if stdout, stderr, _, err := executor.Execute(cliWrappers.Command("openssl", opensslCreateCaKeyArgs...)); err != nil {
388388
z.logger.Errorf("failed to generate root CA key: %s\n%s", stdout, stderr)
389389
return err
390390
}
@@ -398,7 +398,7 @@ func (z *ZotRegistry) generateCerts(executor *cliWrappers.CliExecutor) error {
398398
"-addext",
399399
"basicConstraints=CA:TRUE",
400400
}
401-
if stdout, stderr, _, err := executor.Execute("openssl", opensslCreateCaCertArgs...); err != nil {
401+
if stdout, stderr, _, err := executor.Execute(cliWrappers.Command("openssl", opensslCreateCaCertArgs...)); err != nil {
402402
z.logger.Errorf("failed to generate root CA cert: %s\n%s", stdout, stderr)
403403
return err
404404
}
@@ -415,7 +415,7 @@ func (z *ZotRegistry) generateCerts(executor *cliWrappers.CliExecutor) error {
415415
"-addext",
416416
fmt.Sprintf("subjectAltName=DNS:localhost,IP:127.0.0.1,DNS:%s", zotRegistryContainerName),
417417
}
418-
if stdout, stderr, _, err := executor.Execute("openssl", opensslCreateServerCertArgs...); err != nil {
418+
if stdout, stderr, _, err := executor.Execute(cliWrappers.Command("openssl", opensslCreateServerCertArgs...)); err != nil {
419419
z.logger.Errorf("failed to generate zot registry cert: %s\n%s", stdout, stderr)
420420
return err
421421
}
@@ -458,8 +458,8 @@ func (z *ZotRegistry) createZotConfig(zotConfigFilePath string) error {
458458
}
459459

460460
func (z *ZotRegistry) createHtpasswdFile(executor *cliWrappers.CliExecutor) error {
461-
stdout, stderr, _, err := executor.Execute(
462-
"htpasswd", "-bBn", zotRegistryUser, zotRegistryPassword)
461+
stdout, stderr, _, err := executor.Execute(cliWrappers.Command(
462+
"htpasswd", "-bBn", zotRegistryUser, zotRegistryPassword))
463463
if err != nil {
464464
z.logger.Errorf("failed to generate htpasswd file content: %s\n%s", stdout, stderr)
465465
return err
@@ -507,7 +507,7 @@ func (z *ZotRegistry) ensureZotCaCertInPodmanConfig(executor *cliWrappers.CliExe
507507
}
508508

509509
// Copy the CA cert into Podman config directory.
510-
if stdout, stderr, _, err := executor.Execute("cp", z.rootCertPath, zotRegistryPodmanCaCertPath); err != nil {
510+
if stdout, stderr, _, err := executor.Execute(cliWrappers.Command("cp", z.rootCertPath, zotRegistryPodmanCaCertPath)); err != nil {
511511
z.logger.Errorf("failed to copy root CA cert into podman config dir: %s\n%s", stdout, stderr)
512512
return err
513513
}
@@ -523,7 +523,7 @@ func (z *ZotRegistry) ensureZotCaCertInPodmanConfig(executor *cliWrappers.CliExe
523523
}
524524

525525
func isPodmanMachineRunning(executor *cliWrappers.CliExecutor) bool {
526-
_, _, exitCode, _ := executor.Execute("podman", "machine", "inspect")
526+
_, _, exitCode, _ := executor.Execute(cliWrappers.Command("podman", "machine", "inspect"))
527527
return exitCode == 0
528528
}
529529

@@ -533,7 +533,7 @@ func (z *ZotRegistry) ensureZotCaCertInPodmanMachine(executor *cliWrappers.CliEx
533533
vmCertPath := vmCertsDir + "/" + zotRootCertFileName
534534

535535
// Create the directory in the VM
536-
if stdout, stderr, _, err := executor.Execute("podman", "machine", "ssh", "sudo", "mkdir", "-p", vmCertsDir); err != nil {
536+
if stdout, stderr, _, err := executor.Execute(cliWrappers.Command("podman", "machine", "ssh", "sudo", "mkdir", "-p", vmCertsDir)); err != nil {
537537
z.logger.Errorf("failed to create certs dir in podman machine: %s\n%s", stdout, stderr)
538538
return err
539539
}
@@ -547,7 +547,7 @@ func (z *ZotRegistry) ensureZotCaCertInPodmanMachine(executor *cliWrappers.CliEx
547547

548548
// Use base64 decode in the VM to write the cert
549549
sshCmd := fmt.Sprintf("echo '%s' | base64 -d | sudo tee %s > /dev/null", certBase64, vmCertPath)
550-
if stdout, stderr, _, err := executor.Execute("podman", "machine", "ssh", sshCmd); err != nil {
550+
if stdout, stderr, _, err := executor.Execute(cliWrappers.Command("podman", "machine", "ssh", sshCmd)); err != nil {
551551
z.logger.Errorf("failed to copy CA cert into podman machine: %s\n%s", stdout, stderr)
552552
return err
553553
}

integration_tests/framework/runner_container.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func (c *TestRunnerContainer) ContainerExists(isRunning bool) (bool, error) {
183183
}
184184
args = append(args, "-f", "name="+c.name)
185185

186-
stdout, stderr, _, err := c.executor.Execute(containerTool, args...)
186+
stdout, stderr, _, err := c.executor.Execute(cliWrappers.Command(containerTool, args...))
187187
if err != nil {
188188
l.Logger.Infof("[stdout]:\n%s\n", stdout)
189189
l.Logger.Infof("[stderr]:\n%s\n", stderr)
@@ -245,7 +245,7 @@ func (c *TestRunnerContainer) Start() error {
245245
args = append(args, c.image)
246246
}
247247

248-
stdout, stderr, _, err := c.executor.Execute(containerTool, args...)
248+
stdout, stderr, _, err := c.executor.Execute(cliWrappers.Command(containerTool, args...))
249249
if err != nil {
250250
l.Logger.Infof("[stdout]:\n%s\n", stdout)
251251
l.Logger.Infof("[stderr]:\n%s\n", stderr)
@@ -280,7 +280,7 @@ func (c *TestRunnerContainer) Delete() error {
280280
// a --time flag. Do the same thing for podman, for consistency and to speed up tests.
281281
args = append(args, "--time=0")
282282
}
283-
stdout, stderr, _, err := c.executor.Execute(containerTool, args...)
283+
stdout, stderr, _, err := c.executor.Execute(cliWrappers.Command(containerTool, args...))
284284
if err == nil {
285285
c.containerStatus = ContainerStatus_Deleted
286286
} else {
@@ -299,7 +299,7 @@ func (c *TestRunnerContainer) DeleteIfExists() error {
299299

300300
func (c *TestRunnerContainer) CopyFileIntoContainer(hostPath, containerPath string) error {
301301
c.ensureContainerRunning()
302-
stdout, stderr, _, err := c.executor.Execute(containerTool, "cp", hostPath, c.name+":"+containerPath)
302+
stdout, stderr, _, err := c.executor.Execute(cliWrappers.Command(containerTool, "cp", hostPath, c.name+":"+containerPath))
303303
if err != nil {
304304
l.Logger.Infof("[stdout]:\n%s\n", stdout)
305305
l.Logger.Infof("[stderr]:\n%s\n", stderr)
@@ -310,7 +310,7 @@ func (c *TestRunnerContainer) CopyFileIntoContainer(hostPath, containerPath stri
310310
// GetFileContent reads file inside the container.
311311
func (c *TestRunnerContainer) GetFileContent(path string) (string, error) {
312312
c.ensureContainerRunning()
313-
stdout, stderr, _, err := c.executor.Execute(containerTool, "exec", c.name, "cat", path)
313+
stdout, stderr, _, err := c.executor.Execute(cliWrappers.Command(containerTool, "exec", c.name, "cat", path))
314314
if err != nil {
315315
l.Logger.Infof("[stdout]:\n%s\n", stdout)
316316
l.Logger.Infof("[stderr]:\n%s\n", stderr)
@@ -337,7 +337,7 @@ func (c *TestRunnerContainer) ExecuteCommandWithOutput(command string, args ...s
337337
execArgs = append(execArgs, command)
338338
execArgs = append(execArgs, args...)
339339

340-
stdout, stderr, _, err := c.executor.ExecuteWithOutput(containerTool, execArgs...)
340+
stdout, stderr, _, err := c.executor.Execute(cliWrappers.Cmd{Name: containerTool, Args: execArgs, LogOutput: true})
341341
if err != nil {
342342
l.Logger.Infof("[stdout]:\n%s\n", stdout)
343343
l.Logger.Infof("[stderr]:\n%s\n", stderr)
@@ -369,7 +369,7 @@ func (c *TestRunnerContainer) debugBuildCli(cliArgs ...string) error {
369369
execArgs = append(execArgs, cliArgs...)
370370
}
371371

372-
stdout, stderr, _, err := c.executor.ExecuteWithOutput(containerTool, execArgs...)
372+
stdout, stderr, _, err := c.executor.Execute(cliWrappers.Cmd{Name: containerTool, Args: execArgs, LogOutput: true})
373373
if err != nil {
374374
l.Logger.Infof("[stdout]:\n%s\n", stdout)
375375
l.Logger.Infof("[stderr]:\n%s\n", stderr)
@@ -407,7 +407,7 @@ func (c *TestRunnerContainer) GetTaskResultValue(resultFilePath string) (string,
407407

408408
func (c *TestRunnerContainer) GetHomeDir() (string, error) {
409409
execCmd := []string{"exec", "-t", c.name, "bash", "-c", "echo -n $HOME"}
410-
homeDir, _, _, err := c.executor.Execute(containerTool, execCmd...)
410+
homeDir, _, _, err := c.executor.Execute(cliWrappers.Command(containerTool, execCmd...))
411411
if err != nil {
412412
return "", err
413413
}

integration_tests/prefetch_dependencies_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type prefetchDependenciesTestParams struct {
2424

2525
func cloneGitRepo(url, branch, output string) error {
2626
executor := cliwrappers.NewCliExecutor()
27-
_, _, _, err := executor.Execute("git", "clone", url, output, "--depth=1", "--branch", branch)
27+
_, _, _, err := executor.Execute(cliwrappers.Command("git", "clone", url, output, "--depth=1", "--branch", branch))
2828
return err
2929
}
3030

pkg/cliwrappers/buildah.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,11 @@ func (b *BuildahCli) Build(args *BuildahBuildArgs) error {
238238

239239
buildahLog.Debugf("Running command:\n%s %s", executable, strings.Join(buildahArgs, " "))
240240

241-
_, _, _, err := b.Executor.ExecuteWithOutput(executable, buildahArgs...)
241+
_, _, _, err := b.Executor.Execute(Cmd{
242+
Name: executable, Args: buildahArgs,
243+
// Prefix logs with "buildah" regardless of the wrappers used
244+
NameInLogs: "buildah", LogOutput: true,
245+
})
242246
if err != nil {
243247
buildahLog.Errorf("buildah build failed: %s", err.Error())
244248
return err
@@ -277,7 +281,7 @@ func (b *BuildahCli) Push(args *BuildahPushArgs) (string, error) {
277281
buildahLog.Debugf("Running command:\nbuildah %s", strings.Join(buildahArgs, " "))
278282

279283
retryer := NewRetryer(func() (string, string, int, error) {
280-
return b.Executor.ExecuteWithOutput("buildah", buildahArgs...)
284+
return b.Executor.Execute(Cmd{Name: "buildah", Args: buildahArgs, LogOutput: true})
281285
}).WithImageRegistryPreset().
282286
StopIfOutputContains("unauthorized").
283287
StopIfOutputContains("authentication required")
@@ -314,7 +318,7 @@ func (b *BuildahCli) Pull(args *BuildahPullArgs) error {
314318
buildahLog.Debugf("Running command:\nbuildah %s", strings.Join(buildahArgs, " "))
315319

316320
retryer := NewRetryer(func() (string, string, int, error) {
317-
return b.Executor.ExecuteWithOutput("buildah", buildahArgs...)
321+
return b.Executor.Execute(Cmd{Name: "buildah", Args: buildahArgs, LogOutput: true})
318322
}).WithImageRegistryPreset().
319323
StopIfOutputContains("unauthorized").
320324
StopIfOutputContains("authentication required")
@@ -351,7 +355,7 @@ func (b *BuildahCli) Inspect(args *BuildahInspectArgs) (string, error) {
351355

352356
buildahLog.Debugf("Running command:\nbuildah %s", strings.Join(buildahArgs, " "))
353357

354-
stdout, stderr, _, err := b.Executor.Execute("buildah", buildahArgs...)
358+
stdout, stderr, _, err := b.Executor.Execute(Command("buildah", buildahArgs...))
355359
if err != nil {
356360
buildahLog.Errorf("buildah inspect failed: %s", err.Error())
357361
if stderr != "" {
@@ -397,7 +401,7 @@ func (b *BuildahCli) Version() (BuildahVersionInfo, error) {
397401

398402
buildahLog.Debugf("Running command:\nbuildah %s", strings.Join(buildahArgs, " "))
399403

400-
stdout, stderr, _, err := b.Executor.Execute("buildah", buildahArgs...)
404+
stdout, stderr, _, err := b.Executor.Execute(Command("buildah", buildahArgs...))
401405
if err != nil {
402406
buildahLog.Errorf("buildah version failed: %s", err.Error())
403407
if stderr != "" {

0 commit comments

Comments
 (0)