mirror of https://github.com/dapr/cli.git
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:
parent
96e225432a
commit
77fbc192ea
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue