Fix helmmanager

Issue-ID: NONRTRIC-814
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
Change-Id: Id9e435492a03f4ce10b261fde2f50723bc664b1f
diff --git a/capifcore/internal/helmmanagement/helm.go b/capifcore/internal/helmmanagement/helm.go
index 9473bda..bf245f0 100644
--- a/capifcore/internal/helmmanagement/helm.go
+++ b/capifcore/internal/helmmanagement/helm.go
@@ -26,7 +26,6 @@
 	"path/filepath"
 	"strings"
 
-	"github.com/pkg/errors"
 	log "github.com/sirupsen/logrus"
 	"gopkg.in/yaml.v2"
 	"helm.sh/helm/v3/pkg/action"
@@ -64,43 +63,17 @@
 		return nil
 	}
 	log.Debugf("Adding %s to Helm Repo\n", url)
-	repoFile := filepath.Join(filepath.Dir(hm.settings.RepositoryConfig), "index.yaml")
-	log.Debug("Repo file: ", repoFile)
-
-	c := repo.Entry{
-		Name: filepath.Dir(repoFile),
-		URL:  url,
-	}
-
-	var err error
-	r := hm.repo
-	if r == nil {
-		r, err = repo.NewChartRepository(&c, getter.All(hm.settings))
-		if err != nil {
-			return err
-		}
-	}
+	repoFile := hm.settings.RepositoryConfig
 
 	//Ensure the file directory exists as it is required for file locking
-	err = os.MkdirAll(filepath.Dir(repoFile), os.ModePerm)
-	if err != nil && !errors.Is(err, os.ErrNotExist) {
-		log.Error("Unable to create folder for Helm.")
+	err := os.MkdirAll(filepath.Dir(repoFile), os.ModePerm)
+	if err != nil && !os.IsExist(err) {
 		return err
 	}
 
 	b, err := os.ReadFile(repoFile)
-	if err != nil {
-		log.Info("Creating repo file: ", repoFile)
-		err = r.Index()
-		if err != nil {
-			log.Error("Unable to create repo file: ", repoFile)
-			return err
-		}
-		b, err = os.ReadFile(repoFile)
-		if err != nil {
-			log.Error("Unable to read repo file: ", repoFile)
-			return err
-		}
+	if err != nil && !os.IsNotExist(err) {
+		return err
 	}
 
 	var f repo.File
@@ -110,21 +83,35 @@
 
 	if f.Has(repoName) {
 		log.Debugf("repository name (%s) already exists\n", repoName)
+		hm.setUp = true
 		return nil
 	}
 
-	if _, err := r.DownloadIndexFile(); err != nil {
-		err = errors.Wrapf(err, "looks like %q is not a valid chart repository or cannot be reached", url)
+	c := repo.Entry{
+		Name: repoName,
+		URL:  url,
+	}
+
+	r := hm.repo
+	if r == nil {
+		r, err = repo.NewChartRepository(&c, getter.All(hm.settings))
+		if err != nil {
+			return err
+		}
+	}
+
+	if _, err = r.DownloadIndexFile(); err != nil {
+		log.Errorf("looks like %q is not a valid chart repository or cannot be reached", url)
 		return err
 	}
 
 	f.Update(&c)
 
-	if err := f.WriteFile(repoFile, 0644); err != nil {
+	if err = f.WriteFile(repoFile, 0644); err != nil {
 		return err
 	}
-	hm.setUp = true
 	log.Debugf("%q has been added to your repositories\n", repoName)
+	hm.setUp = true
 	return nil
 }
 
@@ -142,13 +129,13 @@
 
 	cp, err := install.ChartPathOptions.LocateChart(fmt.Sprintf("%s/%s", repoName, chartName), hm.settings)
 	if err != nil {
-		log.Error("Unable to locate chart!")
+		log.Errorf("Unable to locate chart: %s", chartName)
 		return err
 	}
 
 	chartRequested, err := loader.Load(cp)
 	if err != nil {
-		log.Error("Unable to load chart path!")
+		log.Errorf("Unable to load chart path for chart: %s", chartName)
 		return err
 	}
 
@@ -156,7 +143,7 @@
 	install.ReleaseName = releaseName
 	_, err = install.Run(chartRequested, nil)
 	if err != nil {
-		log.Error("Unable to run chart!")
+		log.Errorf("Unable to run chart: %s", chartName)
 		return err
 	}
 	log.Debug("Successfully onboarded ", namespace, repoName, chartName, releaseName)
diff --git a/capifcore/internal/helmmanagement/helm_test.go b/capifcore/internal/helmmanagement/helm_test.go
index 5eb83f3..fe3ee50 100644
--- a/capifcore/internal/helmmanagement/helm_test.go
+++ b/capifcore/internal/helmmanagement/helm_test.go
@@ -25,6 +25,7 @@
 	"bytes"
 	"log"
 	"os"
+	"path/filepath"
 	"strings"
 	"testing"
 
@@ -38,7 +39,7 @@
 	"oransc.org/nonrtric/capifcore/internal/helmmanagement/mocks"
 )
 
-func TestNoChartURL_reoNotSetUp(t *testing.T) {
+func TestNoChartURL_repoNotSetUp(t *testing.T) {
 	managerUnderTest := NewHelmManager(nil)
 
 	res := managerUnderTest.SetUpRepo("repoName", "")
@@ -47,21 +48,21 @@
 	assert.False(t, managerUnderTest.setUp)
 }
 
-// func TestSetUpRepo_repoShouldBeAddedToReposFile(t *testing.T) {
-// 	settings := createReposFile(t)
+func TestSetUpRepoExistingRepoFile_repoShouldBeAddedToReposFile(t *testing.T) {
+	settings := createReposFile(t)
 
-// 	managerUnderTest := NewHelmManager(settings)
+	managerUnderTest := NewHelmManager(settings)
 
-// 	repoName := "repoName"
-// 	repoURL := "http://url"
-// 	managerUnderTest.repo = getChartRepo(settings)
+	repoName := filepath.Dir(settings.RepositoryConfig)
+	repoURL := "http://url"
+	managerUnderTest.repo = getChartRepo(settings)
 
-// 	res := managerUnderTest.SetUpRepo(repoName, repoURL)
+	res := managerUnderTest.SetUpRepo(repoName, repoURL)
 
-// 	assert.Nil(t, res)
-// 	assert.True(t, containsRepo(settings.RepositoryConfig, repoName))
-// 	assert.True(t, managerUnderTest.setUp)
-// }
+	assert.Nil(t, res)
+	assert.True(t, containsRepo(settings.RepositoryConfig, repoName))
+	assert.True(t, managerUnderTest.setUp)
+}
 
 func TestSetUpRepoFail_shouldNotBeSetUp(t *testing.T) {
 	settings := createReposFile(t)
@@ -83,7 +84,7 @@
 		os.RemoveAll(reposDir)
 	})
 
-	reposFile := reposDir + "/repositories.yaml"
+	reposFile := reposDir + "/index.yaml"
 	settings := &cli.EnvSettings{
 		RepositoryConfig: reposFile,
 	}
@@ -106,7 +107,7 @@
 func getChartRepo(settings *cli.EnvSettings) *repo.ChartRepository {
 	repoURL := "http://repoURL"
 	c := repo.Entry{
-		Name: "",
+		Name: filepath.Dir(settings.RepositoryConfig),
 		URL:  repoURL,
 	}
 	r, _ := repo.NewChartRepository(&c, getter.All(settings))