From dddd5d18311478f35b60817bf2621b7ada8b1df8 Mon Sep 17 00:00:00 2001 From: admju Date: Thu, 5 Sep 2024 07:26:32 +0000 Subject: [PATCH] test --- src/helm/repo.go | 54 ++++++++++++++-------------- src/helm/repo_test.go | 49 +++++++++++++++++++------- src/install/install.go | 1 - src/tool/conf_test.go | 15 ++++---- src/tool/helm_test.go | 0 src/utils/copyFile_test.go | 12 ++++++- src/utils/download_test.go | 2 +- src/versionOc/online.go | 29 +-------------- src/versionOc/online_test.go | 62 +++++++++++++++++++++++++++++++++ src/versionOc/versionOc_test.go | 18 +++------- test/versionOc/oc_error.yml | 1 + 11 files changed, 152 insertions(+), 91 deletions(-) create mode 100644 src/tool/helm_test.go create mode 100644 src/versionOc/online_test.go create mode 100644 test/versionOc/oc_error.yml diff --git a/src/helm/repo.go b/src/helm/repo.go index de1dbf3..a5a9ba7 100644 --- a/src/helm/repo.go +++ b/src/helm/repo.go @@ -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 } diff --git a/src/helm/repo_test.go b/src/helm/repo_test.go index 38f8d39..37e9b7f 100644 --- a/src/helm/repo_test.go +++ b/src/helm/repo_test.go @@ -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") } \ No newline at end of file diff --git a/src/install/install.go b/src/install/install.go index ff388b6..f3cf495 100644 --- a/src/install/install.go +++ b/src/install/install.go @@ -18,7 +18,6 @@ type InstallClass struct { Version string Workspace string - // versionFile string tools []tool.ToolData toolsBin map[string]string charts []chart.ChartRepoData diff --git a/src/tool/conf_test.go b/src/tool/conf_test.go index 511ebc1..b0fd936 100644 --- a/src/tool/conf_test.go +++ b/src/tool/conf_test.go @@ -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") } \ No newline at end of file diff --git a/src/tool/helm_test.go b/src/tool/helm_test.go new file mode 100644 index 0000000..e69de29 diff --git a/src/utils/copyFile_test.go b/src/utils/copyFile_test.go index e6adb2a..1806641 100644 --- a/src/utils/copyFile_test.go +++ b/src/utils/copyFile_test.go @@ -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") +} diff --git a/src/utils/download_test.go b/src/utils/download_test.go index 2944a30..66f56ba 100644 --- a/src/utils/download_test.go +++ b/src/utils/download_test.go @@ -11,7 +11,7 @@ import ( "github.com/jarcoal/httpmock" ) -func TestDownload(t *testing.T){ +func TestDownload(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() diff --git a/src/versionOc/online.go b/src/versionOc/online.go index 2db691b..46cbbd4 100644 --- a/src/versionOc/online.go +++ b/src/versionOc/online.go @@ -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 -} diff --git a/src/versionOc/online_test.go b/src/versionOc/online_test.go new file mode 100644 index 0000000..f919fca --- /dev/null +++ b/src/versionOc/online_test.go @@ -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) + +} \ No newline at end of file diff --git a/src/versionOc/versionOc_test.go b/src/versionOc/versionOc_test.go index 3af6964..c816250 100644 --- a/src/versionOc/versionOc_test.go +++ b/src/versionOc/versionOc_test.go @@ -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") - -// } \ No newline at end of file + assert.NotNilf(t, err, "error message %s", ocyaml) +} \ No newline at end of file diff --git a/test/versionOc/oc_error.yml b/test/versionOc/oc_error.yml new file mode 100644 index 0000000..8e81abb --- /dev/null +++ b/test/versionOc/oc_error.yml @@ -0,0 +1 @@ +N'est pas un fichier \ No newline at end of file