Fix checks for container runtime installation (#1307)

* Check container runtime while installing

Signed-off-by: Shubham Sharma <shubhash@microsoft.com>

* Fix lint

Signed-off-by: Shubham Sharma <shubhash@microsoft.com>

* Fix lint (another attempt)

Signed-off-by: Shubham Sharma <shubhash@microsoft.com>

---------

Signed-off-by: Shubham Sharma <shubhash@microsoft.com>
This commit is contained in:
Shubham Sharma 2023-06-09 11:14:23 +05:30 committed by GitHub
parent 96e225432a
commit 77fbc192ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 11 deletions

View File

@ -162,7 +162,7 @@ func Init(runtimeVersion, dashboardVersion string, dockerNetwork string, slimMod
setAirGapInit(fromDir)
if !slimMode {
// If --slim installation is not requested, check if docker is installed.
containerRuntimeAvailable := utils.IsDockerInstalled() || utils.IsPodmanInstalled()
containerRuntimeAvailable := utils.IsContainerRuntimeInstalled(containerRuntime)
if !containerRuntimeAvailable {
return fmt.Errorf("could not connect to %s. %s may not be installed or running", containerRuntime, containerRuntime)
}

View File

@ -321,12 +321,13 @@ func TestInitLogActualContainerRuntimeName(t *testing.T) {
{"podman", "Init should log podman as container runtime"},
{"docker", "Init should log docker as container runtime"},
}
containerRuntimeAvailable := utils.IsDockerInstalled() || utils.IsPodmanInstalled()
if containerRuntimeAvailable {
t.Skip("Skipping test as container runtime is available")
}
for _, test := range tests {
t.Run(test.testName, func(t *testing.T) {
containerRuntimeAvailable := utils.IsContainerRuntimeInstalled(test.containerRuntime)
if containerRuntimeAvailable {
t.Skip("Skipping test as container runtime is available")
}
err := Init(latestVersion, latestVersion, "", false, "", "", test.containerRuntime, "", "")
assert.NotNil(t, err)
assert.Contains(t, err.Error(), test.containerRuntime)

View File

@ -93,7 +93,7 @@ func Uninstall(uninstallAll bool, dockerNetwork string, containerRuntime string,
containerRuntime = strings.TrimSpace(containerRuntime)
runtimeCmd := utils.GetContainerRuntimeCmd(containerRuntime)
containerRuntimeAvailable := false
containerRuntimeAvailable = utils.IsDockerInstalled() || utils.IsPodmanInstalled()
containerRuntimeAvailable = utils.IsContainerRuntimeInstalled(containerRuntime)
if containerRuntimeAvailable {
containerErrs = removeContainers(uninstallPlacementContainer, uninstallAll, dockerNetwork, runtimeCmd)
}

View File

@ -173,10 +173,21 @@ func CreateDirectory(dir string) error {
return os.Mkdir(dir, 0o777)
}
// IsDockerInstalled checks whether docker is installed/running.
func IsDockerInstalled() bool {
//nolint:staticcheck
cli, err := client.NewEnvClient()
// IsContainerRuntimeInstalled checks whether the given container runtime is installed.
// If the container runtime is unsupported, false is returned.
func IsContainerRuntimeInstalled(containerRuntime string) bool {
if containerRuntime == string(PODMAN) {
return isPodmanInstalled()
} else if containerRuntime == string(DOCKER) {
return isDockerInstalled()
}
// This should never happen.
return false
}
// isDockerInstalled checks whether docker is installed.
func isDockerInstalled() bool {
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return false
}
@ -184,7 +195,8 @@ func IsDockerInstalled() bool {
return err == nil
}
func IsPodmanInstalled() bool {
// isPodmanInstalled checks whether podman is installed.
func isPodmanInstalled() bool {
cmd := exec.Command("podman", "version")
if err := cmd.Run(); err != nil {
return false