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

View File

@ -1,23 +1,48 @@
package helm package helm
import ( import (
// "os" "fmt"
// "path/filepath" // "os"
// "path/filepath"
"testing" "testing"
// "github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestHelmRepoAdd(t *testing.T){ func TestHelmRepoAdd(t *testing.T) {
// fmt.Println(" TestVersion ", TEST_BIN_DIR)
// bin := filepath.Join(TEST_BIN_DIR, "helm") repo := HelmRepo{
// os.Chmod(bin, 0700) 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") // assert.FileExists(t, bin, "TestHelmVersion error")
// version, err := Version(bin) // version, err := Version(bin)
// assert.Nilf(t, err, "error message %s", bin) // assert.Nilf(t, err, "error message %s", bin)
// assert.Equal(t, version, "v3.15.4+gfa9efb0", "TestHelmVersion error") // 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 Version string
Workspace string Workspace string
// versionFile string
tools []tool.ToolData tools []tool.ToolData
toolsBin map[string]string toolsBin map[string]string
charts []chart.ChartRepoData charts []chart.ChartRepoData

View File

@ -1,13 +1,12 @@
package tool package tool
import ( import (
// "os"
"fmt" "fmt"
"path/filepath" "path/filepath"
"testing" "testing"
// "github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestToolConf(t *testing.T) { func TestToolConf(t *testing.T) {
@ -17,12 +16,12 @@ func TestToolConf(t *testing.T) {
data, err := FromConfigFile(src) data, err := FromConfigFile(src)
fmt.Println("data", src, data, err) fmt.Println("data", src, data, err)
// bin := filepath.Join(TEST_BIN_DIR, "kubectl") bin := filepath.Join(TEST_BIN_DIR, "kubectl")
// os.Chmod(bin, 0700) os.Chmod(bin, 0700)
// assert.FileExists(t, bin, "TestKubectlVersion error") assert.FileExists(t, bin, "TestKubectlVersion error")
// version, err := Version(bin) version, err := Version(bin)
// assert.Nilf(t, err, "error message %s", bin) assert.Nilf(t, err, "error message %s", bin)
// assert.Equal(t, "v1.30.3", version, "TestKubectlVersion error") 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" "github.com/stretchr/testify/assert"
) )
func TestCopyFileExist(t *testing.T){ func TestCopyFileExist(t *testing.T) {
src := filepath.Join(TEST_SRC_DIR, "fichier1") src := filepath.Join(TEST_SRC_DIR, "fichier1")
dest := filepath.Join(TEST_DEST_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) 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" "github.com/jarcoal/httpmock"
) )
func TestDownload(t *testing.T){ func TestDownload(t *testing.T) {
httpmock.Activate() httpmock.Activate()
defer httpmock.DeactivateAndReset() defer httpmock.DeactivateAndReset()

View File

@ -15,6 +15,7 @@ import (
func GetFromOnline(version string) (string, string, error) { func GetFromOnline(version string) (string, string, error) {
fmt.Println(" ONLINE 18")
version2 := version version2 := version
if version == "latest" { if version == "latest" {
versionLatest, err := readLatestFromOnline() versionLatest, err := readLatestFromOnline()
@ -90,7 +91,6 @@ func getFileVersion(version string) (string, error) {
} }
var data ocJsonStruct var data ocJsonStruct
err = json.Unmarshal(body, &data) err = json.Unmarshal(body, &data)
if err != nil { if err != nil {
return "", err return "", err
@ -103,30 +103,3 @@ func getFileVersion(version string) (string, error) {
return string(data64), nil 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) assert.NotNilf(t, err, "error message %s", ocyaml)
} }
// func TestGet(t *testing.T) { func TestGetFromFileErr2(t *testing.T) {
// _, err := Get("99.1") ocyaml := filepath.Join(TEST_SRC_DIR, "oc_error.yml")
// assert.NotNilf(t, err, "error message %s", err) _, err := GetFromFile(ocyaml)
// }
// func TestGetLatest(t *testing.T) { assert.NotNilf(t, err, "error message %s", ocyaml)
}
// 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")
// }

View File

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