-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unit-tests: Add utility to run container commands for tests
Add utility to run container commands during tests when a runtime is available. Add failover test for redis loadbalancer using keydb. Signed-off-by: Heathcliff <[email protected]>
- Loading branch information
1 parent
0ab14e3
commit 883fafa
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
) | ||
|
||
var ( | ||
containerRuntime string | ||
initialized = false | ||
) | ||
|
||
func findContainerRuntime() string { | ||
for _, cmd := range []string{"docker", "podman"} { | ||
path, err := exec.LookPath(cmd) | ||
if err != nil { | ||
continue | ||
} | ||
err = exec.Command(path, "ps").Run() | ||
if err == nil { | ||
fmt.Printf("Found container runtime %s, path=%s\n", cmd, path) | ||
return path | ||
} | ||
} | ||
fmt.Println("Did not find any container runtimes") | ||
return "" | ||
} | ||
|
||
func HasContainerRuntimer() bool { | ||
if !initialized { | ||
containerRuntime = findContainerRuntime() | ||
initialized = true | ||
} | ||
return containerRuntime != "" | ||
} | ||
|
||
func ExecCRI(args ...string) error { | ||
if !initialized { | ||
containerRuntime = findContainerRuntime() | ||
initialized = true | ||
} | ||
return exec.Command(containerRuntime, args...).Run() | ||
} |