Repository: code-ready/crc
Pull request #1870 (2021-01-21 20:12:45)

Remove OcBinDir if it's a file
Apparently, we removed this code too quickly. Some users still need it.
They hit the error: mkdir /Users/name/.crc/bin/oc: not a directory

Found with telemetry.

openshift-ci-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/code-ready/crc/pull/1870#" title="Author self-approved">guillaumerose</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=code-ready%2Fcrc).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/code-ready/crc/blob/master/OWNERS)~~ [guillaumerose]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
openshift-ci-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/code-ready/crc/pull/1870#pullrequestreview-574109300" title="Approved">cfergeau</a>*, *<a href="https://github.com/code-ready/crc/pull/1870#" title="Author self-approved">guillaumerose</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=code-ready%2Fcrc).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/code-ready/crc/blob/master/OWNERS)~~ [cfergeau,guillaumerose]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
openshift-ci-robot: New changes are detected. LGTM label has been removed.

Patch:
diff --git a/pkg/crc/machine/bundle/repository.go b/pkg/crc/machine/bundle/repository.go
index 059c8b61e..60d4cb990 100644
--- a/pkg/crc/machine/bundle/repository.go
+++ b/pkg/crc/machine/bundle/repository.go
@@ -83,6 +83,14 @@ func (repo *Repository) Use(bundleName string) (*CrcBundleInfo, error) {
 func (bundle *CrcBundleInfo) createSymlinkOrCopyOpenShiftClient(ocBinDir string) error {
 	ocInBundle := bundle.resolvePath(constants.OcExecutableName)
 	ocInBinDir := filepath.Join(ocBinDir, constants.OcExecutableName)
+
+	// this is needed when upgrading from crc versions anterior to commit 1.11.0~5
+	if info, err := os.Stat(ocBinDir); err == nil && !info.IsDir() {
+		if err := os.Remove(ocBinDir); err != nil {
+			return err
+		}
+	}
+
 	if err := os.MkdirAll(ocBinDir, 0750); err != nil {
 		return err
 	}


Test patch:
diff --git a/pkg/crc/machine/bundle/repository_test.go b/pkg/crc/machine/bundle/repository_test.go
index 72f642f2d..13cc9acf8 100644
--- a/pkg/crc/machine/bundle/repository_test.go
+++ b/pkg/crc/machine/bundle/repository_test.go
@@ -21,9 +21,7 @@ func TestUse(t *testing.T) {
 	assert.NoError(t, err)
 	defer os.RemoveAll(ocBinDir)
 
-	assert.NoError(t, os.Mkdir(filepath.Join(dir, "crc_libvirt_4.6.1"), 0755))
-	writeMetadata(t, filepath.Join(dir, "crc_libvirt_4.6.1"), reference)
-	assert.NoError(t, ioutil.WriteFile(filepath.Join(dir, "crc_libvirt_4.6.1", constants.OcExecutableName), []byte("openshift-client"), 0600))
+	addBundle(t, dir, "crc_libvirt_4.6.1")
 
 	repo := &Repository{
 		CacheDir: dir,
@@ -39,6 +37,27 @@ func TestUse(t *testing.T) {
 	assert.Equal(t, "openshift-client", string(bin))
 }
 
+func TestUseWithOCFile(t *testing.T) {
+	dir, err := ioutil.TempDir("", "repo")
+	assert.NoError(t, err)
+	defer os.RemoveAll(dir)
+
+	fd, err := ioutil.TempFile("", "oc-bin-dir")
+	assert.NoError(t, err)
+	fd.Close()
+	defer os.RemoveAll(fd.Name())
+
+	addBundle(t, dir, "crc_libvirt_4.6.1")
+
+	repo := &Repository{
+		CacheDir: dir,
+		OcBinDir: fd.Name(),
+	}
+
+	_, err = repo.Use("crc_libvirt_4.6.1.crcbundle")
+	assert.NoError(t, err)
+}
+
 func TestExtract(t *testing.T) {
 	dir, err := ioutil.TempDir("", "repo")
 	assert.NoError(t, err)
@@ -105,3 +124,9 @@ func TestVersionCheck(t *testing.T) {
 func writeMetadata(t *testing.T, dir string, s string) bool {
 	return assert.NoError(t, ioutil.WriteFile(filepath.Join(dir, metadataFilename), []byte(s), 0600))
 }
+
+func addBundle(t *testing.T, dir, name string) {
+	assert.NoError(t, os.Mkdir(filepath.Join(dir, name), 0755))
+	writeMetadata(t, filepath.Join(dir, name), reference)
+	assert.NoError(t, ioutil.WriteFile(filepath.Join(dir, name, constants.OcExecutableName), []byte("openshift-client"), 0600))
+}