This commit is contained in:
admju 2024-09-05 07:26:32 +00:00
parent a7a5465a22
commit dddd5d1831
11 changed files with 152 additions and 91 deletions

View File

@ -1,53 +1,53 @@
package helm
import (
"fmt"
"strings"
"os/exec"
"fmt"
"strings"
"os/exec"
log "oc-deploy/log_wrapper"
)
type HelmRepo struct {
Bin string // Chemin vers le binaire
Bin string // Chemin vers le binaire
Name string
Repository string // Url du dépôt
ForceUpdate bool
Opts string
Repository string // Url du dépôt
ForceUpdate bool
Opts string
}
func (this HelmRepo) AddRepository() (string, error) {
helm_bin := this.Bin
helm_bin := this.Bin
force_update := "--force-update=false"
if this.ForceUpdate {
force_update = "--force-update=true"
}
force_update := "--force-update=false"
if this.ForceUpdate {
force_update = "--force-update=true"
}
msg := fmt.Sprintf("%s repo add %s %s %s %s", helm_bin, this.Name, this.Repository, force_update, this.Opts)
log.Log().Debug().Msg(msg)
msg := fmt.Sprintf("%s repo add %s %s %s %s", helm_bin, this.Name, this.Repository, force_update, this.Opts)
log.Log().Debug().Msg(msg)
cmd := exec.Command(helm_bin, "repo", "add", this.Name, this.Repository, force_update)
stdout, err := cmd.CombinedOutput()
cmd := exec.Command(helm_bin, "repo", "add", this.Name, this.Repository, force_update)
stdout, err := cmd.CombinedOutput()
res := string(stdout)
res = strings.TrimSuffix(res, "\n")
res := string(stdout)
res = strings.TrimSuffix(res, "\n")
return res, err
return res, err
}
// helm repo remove [NAME]
func (this HelmRepo) RemoveRepository() (string, error) {
helm_bin := this.Bin
helm_bin := this.Bin
msg := fmt.Sprintf("%s repo remove %s", helm_bin, this.Name)
log.Log().Debug().Msg(msg)
msg := fmt.Sprintf("%s repo remove %s", helm_bin, this.Name)
log.Log().Debug().Msg(msg)
cmd := exec.Command(helm_bin, "repo", "remove", this.Name)
stdout, err := cmd.CombinedOutput()
cmd := exec.Command(helm_bin, "repo", "remove", this.Name)
stdout, err := cmd.CombinedOutput()
res := string(stdout)
res = strings.TrimSuffix(res, "\n")
res := string(stdout)
res = strings.TrimSuffix(res, "\n")
return res, err
return res, err
}

View File

@ -1,23 +1,48 @@
package helm
import (
// "os"
// "path/filepath"
"fmt"
// "os"
// "path/filepath"
"testing"
"testing"
// "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
)
func TestHelmRepoAdd(t *testing.T){
// fmt.Println(" TestVersion ", TEST_BIN_DIR)
// bin := filepath.Join(TEST_BIN_DIR, "helm")
// os.Chmod(bin, 0700)
func TestHelmRepoAdd(t *testing.T) {
repo := HelmRepo{
Bin: "./helm",
Name: "repooc",
Repository: "https://url",
ForceUpdate: true,
Opts: ""}
fmt.Println(" TestHelmRepoAdd ", repo)
res, err := repo.AddRepository()
fmt.Println("TestHelmRepoAdd.24", res, err)
// bin := filepath.Join(TEST_BIN_DIR, "helm")
// os.Chmod(bin, 0700)
// assert.FileExists(t, bin, "TestHelmVersion error")
// version, err := Version(bin)
// version, err := Version(bin)
// assert.Nilf(t, err, "error message %s", bin)
// assert.Equal(t, version, "v3.15.4+gfa9efb0", "TestHelmVersion error")
// assert.Nilf(t, err, "error message %s", bin)
// assert.Equal(t, version, "v3.15.4+gfa9efb0", "TestHelmVersion error")
}
// helm : not found
func TestHelmRepoAddErr(t *testing.T) {
repo := HelmRepo{
Bin: "./helm",
Name: "repooc",
Repository: "https://url",
ForceUpdate: true,
Opts: ""}
_, err := repo.AddRepository()
assert.NotNilf(t, err, "error message %s", "./helm")
}

View File

@ -18,7 +18,6 @@ type InstallClass struct {
Version string
Workspace string
// versionFile string
tools []tool.ToolData
toolsBin map[string]string
charts []chart.ChartRepoData

View File

@ -1,13 +1,12 @@
package tool
import (
// "os"
"fmt"
"path/filepath"
"testing"
// "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
)
func TestToolConf(t *testing.T) {
@ -17,12 +16,12 @@ func TestToolConf(t *testing.T) {
data, err := FromConfigFile(src)
fmt.Println("data", src, data, err)
// bin := filepath.Join(TEST_BIN_DIR, "kubectl")
// os.Chmod(bin, 0700)
// assert.FileExists(t, bin, "TestKubectlVersion error")
bin := filepath.Join(TEST_BIN_DIR, "kubectl")
os.Chmod(bin, 0700)
assert.FileExists(t, bin, "TestKubectlVersion error")
// version, err := Version(bin)
version, err := Version(bin)
// assert.Nilf(t, err, "error message %s", bin)
// assert.Equal(t, "v1.30.3", version, "TestKubectlVersion error")
assert.Nilf(t, err, "error message %s", bin)
assert.Equal(t, "v1.30.3", version, "TestKubectlVersion error")
}

0
src/tool/helm_test.go Normal file
View File

View File

@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/assert"
)
func TestCopyFileExist(t *testing.T){
func TestCopyFileExist(t *testing.T) {
src := filepath.Join(TEST_SRC_DIR, "fichier1")
dest := filepath.Join(TEST_DEST_DIR, "fichier1")
@ -45,3 +45,13 @@ func TestCopyErrSrcWrite(t *testing.T) {
assert.NotNilf(t, err, "CopyFile error %s", src)
}
func TestCopyContentFile(t *testing.T) {
content := "TestCopyContentFileExist"
dest := filepath.Join(TEST_DEST_DIR, "fichier1")
err := CopyContentFile(content, dest)
assert.Nilf(t, err, "error message")
assert.FileExists(t, dest, "CopyFile error")
}

View File

@ -11,7 +11,7 @@ import (
"github.com/jarcoal/httpmock"
)
func TestDownload(t *testing.T){
func TestDownload(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

View File

@ -15,6 +15,7 @@ import (
func GetFromOnline(version string) (string, string, error) {
fmt.Println(" ONLINE 18")
version2 := version
if version == "latest" {
versionLatest, err := readLatestFromOnline()
@ -90,7 +91,6 @@ func getFileVersion(version string) (string, error) {
}
var data ocJsonStruct
err = json.Unmarshal(body, &data)
if err != nil {
return "", err
@ -103,30 +103,3 @@ func getFileVersion(version string) (string, error) {
return string(data64), nil
}
// Cherche l'Id de la release
func getIdRelease(version string) (int, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/releases/tags/%s",
occonst.ONLINE_URL,
occonst.ONLINE_VERSION,
version)
res, err := http.Get(url)
if err != nil {
return 0, err
}
body, err := io.ReadAll(res.Body)
if err != nil {
return 0, err
}
var data releaseStruct
err = json.Unmarshal(body, &data)
if err != nil {
return 0, err
}
return data.Id, nil
}

View File

@ -0,0 +1,62 @@
package versionOc
import (
"fmt"
"testing"
// "encoding/base64"
"oc-deploy/occonst"
"github.com/stretchr/testify/assert"
"github.com/jarcoal/httpmock"
)
//
func TestGetOnline(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
version := "99.1"
url := fmt.Sprintf("%s/%s/releases/download/%s/oc.json",
occonst.ONLINE_URL,
occonst.ONLINE_VERSION,
version)
httpmock.RegisterResponder("GET", url,
httpmock.NewStringResponder(200, `{"value": "e30K"}`))
res, _, err := GetFromOnline(version)
assert.Nilf(t, err, "error message %s", err)
assert.Equal(t, version, res, "TestGetOnline error")
}
func TestGetOnlineLatest(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// version := "99.1"
url := fmt.Sprintf("%s/api/v1/repos/%s/releases/latest",
occonst.ONLINE_URL,
occonst.ONLINE_VERSION)
httpmock.RegisterResponder("GET", url,
httpmock.NewStringResponder(200, `{"name": "99.0", "id": 2}`))
version := "99.0"
url2 := fmt.Sprintf("%s/%s/releases/download/%s/oc.json",
occonst.ONLINE_URL,
occonst.ONLINE_VERSION,
version)
httpmock.RegisterResponder("GET", url2,
httpmock.NewStringResponder(200, `{"value": "e30K"}`))
version, _, err := GetFromOnline("latest")
assert.Nilf(t, err, "error message %s", err)
assert.Equal(t, "99.0", version, "TestGetFromFile error")
fmt.Println("TestGetOnlineLatest ", version, err)
}

View File

@ -25,18 +25,10 @@ func TestGetFromFileErr(t *testing.T) {
assert.NotNilf(t, err, "error message %s", ocyaml)
}
// func TestGet(t *testing.T) {
func TestGetFromFileErr2(t *testing.T) {
// _, err := Get("99.1")
// assert.NotNilf(t, err, "error message %s", err)
// }
ocyaml := filepath.Join(TEST_SRC_DIR, "oc_error.yml")
_, err := GetFromFile(ocyaml)
// func TestGetLatest(t *testing.T) {
// OFFLINE_DIR = filepath.Join(TEST_SRC_DIR, "offline")
// version, err := Get("latest")
// assert.Nilf(t, err, "error message %s", err)
// assert.Equal(t, "99.1", version, "TestGetFromFile error")
// }
assert.NotNilf(t, err, "error message %s", ocyaml)
}

View File

@ -0,0 +1 @@
N'est pas un fichier