From 48301bf82ec3420ae38dcc98fbc3e40755fb7de2 Mon Sep 17 00:00:00 2001 From: admju Date: Wed, 4 Sep 2024 07:11:13 +0000 Subject: [PATCH] test --- src/Makefile | 2 +- src/helm/version.go | 8 +++--- src/helm/version_test.go | 18 ++++++------- src/kubectl/main_test.go | 24 +++++++++++++++++ src/kubectl/version.go | 12 ++++----- src/kubectl/version_test.go | 22 +++++++++++++++ src/tool/conf_test.go | 28 +++++++++++++++++++ src/tool/helm.go | 1 - src/tool/main_test.go | 24 +++++++++++++++++ src/versionOc/main_test.go | 23 ++++++++++++++++ src/versionOc/versionOc.go | 20 +++++++++++--- src/versionOc/versionOc_test.go | 42 +++++++++++++++++++++++++++++ test/bin/kubectl | Bin test/tool/oc.yml | 4 +++ test/versionOc/oc.yml | 3 +++ test/versionOc/offline/latest.yml | 3 +++ test/versionOc/offline/oc_99.1.yml | 1 + 17 files changed, 210 insertions(+), 25 deletions(-) create mode 100644 src/kubectl/main_test.go create mode 100644 src/kubectl/version_test.go create mode 100644 src/tool/conf_test.go create mode 100644 src/tool/main_test.go create mode 100644 src/versionOc/main_test.go create mode 100644 src/versionOc/versionOc_test.go mode change 100644 => 100755 test/bin/kubectl create mode 100644 test/tool/oc.yml create mode 100644 test/versionOc/oc.yml create mode 100644 test/versionOc/offline/latest.yml create mode 100644 test/versionOc/offline/oc_99.1.yml diff --git a/src/Makefile b/src/Makefile index e6e6f22..c234908 100644 --- a/src/Makefile +++ b/src/Makefile @@ -18,7 +18,7 @@ help: @echo @echo 'Usage:' @echo ' make build Génère les exécutables.' - @echo ' make get-deps runs dep ensure, mostly used for ci.' + @echo ' make get-deps Dependency download' @echo ' make run BIN_OPTS=... Go run' @echo ' make run_install BIN_OPTS=... Go run' diff --git a/src/helm/version.go b/src/helm/version.go index 5f3a088..94ac244 100644 --- a/src/helm/version.go +++ b/src/helm/version.go @@ -7,14 +7,14 @@ import ( func Version(path string) (string, error) { - cmd := exec.Command(path, "version", "--short") - stdout, err := cmd.CombinedOutput() + cmd := exec.Command(path, "version", "--short") + stdout, err := cmd.CombinedOutput() if err != nil { return "", err } - res := string(stdout) - res = strings.TrimSuffix(res, "\n") + res := string(stdout) + res = strings.TrimSuffix(res, "\n") return res, nil } diff --git a/src/helm/version_test.go b/src/helm/version_test.go index 52d244f..7a97070 100644 --- a/src/helm/version_test.go +++ b/src/helm/version_test.go @@ -1,22 +1,22 @@ package helm import ( - "os" - "path/filepath" + "os" + "path/filepath" - "testing" + "testing" "github.com/stretchr/testify/assert" ) func TestHelmVersion(t *testing.T){ - - bin := filepath.Join(TEST_BIN_DIR, "helm") - os.Chmod(bin, 0700) + + 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, "v3.15.4+gfa9efb0", version, "TestHelmVersion error") } \ No newline at end of file diff --git a/src/kubectl/main_test.go b/src/kubectl/main_test.go new file mode 100644 index 0000000..4613268 --- /dev/null +++ b/src/kubectl/main_test.go @@ -0,0 +1,24 @@ +package kubectl + +import ( + "os" + "testing" + "path/filepath" +) + +var TEST_DEST_DIR = "../wrk_kubectl" +var TEST_SRC_DIR = filepath.Join("../../test", "kubectl") +var TEST_BIN_DIR = filepath.Join("../../test", "bin") + +func TestMain(m *testing.M) { + folderPath := TEST_DEST_DIR + + os.RemoveAll(folderPath) + os.MkdirAll(folderPath, os.ModePerm) + + // call flag.Parse() here if TestMain uses flags + exitCode := m.Run() + + os.RemoveAll(folderPath) + os.Exit(exitCode) +} \ No newline at end of file diff --git a/src/kubectl/version.go b/src/kubectl/version.go index 6706e98..f0cb174 100644 --- a/src/kubectl/version.go +++ b/src/kubectl/version.go @@ -6,25 +6,25 @@ import ( ) type toolClientVersion struct { - GitVersion string `json:"gitVersion"` + GitVersion string `json:"gitVersion"` } type toolVersion struct { - ClientVersion toolClientVersion `json:"clientVersion"` + ClientVersion toolClientVersion `json:"clientVersion"` } func Version(path string) (string, error) { - cmd := exec.Command(path, "version", "-o", "json", "--client=true") - stdout, err := cmd.CombinedOutput() + cmd := exec.Command(path, "version", "-o", "json", "--client=true") + stdout, err := cmd.CombinedOutput() if err != nil { return "", err } - var objmap toolVersion + var objmap toolVersion - json.Unmarshal(stdout, &objmap) + json.Unmarshal(stdout, &objmap) res := objmap.ClientVersion.GitVersion return res, nil diff --git a/src/kubectl/version_test.go b/src/kubectl/version_test.go new file mode 100644 index 0000000..5f9e03d --- /dev/null +++ b/src/kubectl/version_test.go @@ -0,0 +1,22 @@ +package kubectl + +import ( + "os" + "path/filepath" + + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestKubectlVersion(t *testing.T){ + + bin := filepath.Join(TEST_BIN_DIR, "kubectl") + os.Chmod(bin, 0700) + assert.FileExists(t, bin, "TestKubectlVersion error") + + version, err := Version(bin) + + 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/conf_test.go b/src/tool/conf_test.go new file mode 100644 index 0000000..511ebc1 --- /dev/null +++ b/src/tool/conf_test.go @@ -0,0 +1,28 @@ +package tool + +import ( + // "os" + "fmt" + "path/filepath" + + "testing" + + // "github.com/stretchr/testify/assert" +) + +func TestToolConf(t *testing.T) { + + src := filepath.Join(TEST_SRC_DIR, "oc.yml") + + 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") + + // version, err := Version(bin) + + // 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.go b/src/tool/helm.go index fb85320..878d0d4 100644 --- a/src/tool/helm.go +++ b/src/tool/helm.go @@ -22,7 +22,6 @@ func (this HelmInstallData) Download() (error) { bin_dir := this.obj.Bin err2 := os.MkdirAll(bin_dir, os.ModePerm) if err2 != nil { - fmt.Println(err2) return err2 } diff --git a/src/tool/main_test.go b/src/tool/main_test.go new file mode 100644 index 0000000..0f5cc98 --- /dev/null +++ b/src/tool/main_test.go @@ -0,0 +1,24 @@ +package tool + +import ( + "os" + "testing" + "path/filepath" +) + +var TEST_DEST_DIR = "../wrk_tool" +var TEST_SRC_DIR = filepath.Join("../../test", "tool") +var TEST_BIN_DIR = filepath.Join("../../test", "bin") + +func TestMain(m *testing.M) { + folderPath := TEST_DEST_DIR + + os.RemoveAll(folderPath) + os.MkdirAll(folderPath, os.ModePerm) + + // call flag.Parse() here if TestMain uses flags + exitCode := m.Run() + + os.RemoveAll(folderPath) + os.Exit(exitCode) +} \ No newline at end of file diff --git a/src/versionOc/main_test.go b/src/versionOc/main_test.go new file mode 100644 index 0000000..5110b2a --- /dev/null +++ b/src/versionOc/main_test.go @@ -0,0 +1,23 @@ +package versionOc + +import ( + "os" + "testing" + "path/filepath" +) + +var TEST_DEST_DIR = "../wrk_versionOc" +var TEST_SRC_DIR = filepath.Join("../../test", "versionOc") + +func TestMain(m *testing.M) { + folderPath := TEST_DEST_DIR + + os.RemoveAll(folderPath) + os.MkdirAll(folderPath, os.ModePerm) + + // call flag.Parse() here if TestMain uses flags + exitCode := m.Run() + + os.RemoveAll(folderPath) + os.Exit(exitCode) +} \ No newline at end of file diff --git a/src/versionOc/versionOc.go b/src/versionOc/versionOc.go index 223b5fd..9e89b33 100644 --- a/src/versionOc/versionOc.go +++ b/src/versionOc/versionOc.go @@ -6,6 +6,7 @@ import ( "os" "errors" "io" + "path/filepath" "gopkg.in/yaml.v2" log "oc-deploy/log_wrapper" ) @@ -14,6 +15,8 @@ type versionInput struct { Version string `yaml:"version"` } +var OFFLINE_DIR string = "../offline" + func GetFromFile(fileversion string) (string, error) { fin, err := os.Open(fileversion) if err != nil { @@ -28,7 +31,10 @@ func GetFromFile(fileversion string) (string, error) { var objmap versionInput - yaml.Unmarshal(byteValue, &objmap) + err = yaml.Unmarshal(byteValue, &objmap) + if err != nil { + return "", err + } return objmap.Version, nil } @@ -45,7 +51,8 @@ func Get(version string) (string, error) { version2 = versionLatest } - src := fmt.Sprintf("../offline/oc_%s.yml", version2) + ficversion := fmt.Sprintf("oc_%s.yml", version2) + src := filepath.Join(OFFLINE_DIR, ficversion) if _, err := os.Stat(src); err != nil { log.Log().Debug().Msg(err.Error()) return "", errors.New("Version non disponible") @@ -56,9 +63,10 @@ func Get(version string) (string, error) { // readLatestFile : Lit le numéro de la version dans le fichier lastest.yml func readLatestFile() (string, error) { - src := "../offline/latest.yml" + src := filepath.Join(OFFLINE_DIR, "latest.yml") fin, err := os.Open(src) + if err != nil { return "", err } @@ -71,7 +79,11 @@ func readLatestFile() (string, error) { var objmap versionInput - yaml.Unmarshal(byteValue, &objmap) + err = yaml.Unmarshal(byteValue, &objmap) + + if err != nil { + return "", err + } return objmap.Version, nil } diff --git a/src/versionOc/versionOc_test.go b/src/versionOc/versionOc_test.go new file mode 100644 index 0000000..a01a8af --- /dev/null +++ b/src/versionOc/versionOc_test.go @@ -0,0 +1,42 @@ +package versionOc + +import ( + "path/filepath" + + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetFromFile(t *testing.T) { + + ocyaml := filepath.Join(TEST_SRC_DIR, "oc.yml") + version, err := GetFromFile(ocyaml) + + assert.Nilf(t, err, "error message %s", ocyaml) + assert.Equal(t, "99.0", version, "TestGetFromFile error") +} + +func TestGetFromFileErr(t *testing.T) { + + ocyaml := filepath.Join(TEST_SRC_DIR, "inconnu.yml") + _, err := GetFromFile(ocyaml) + + assert.NotNilf(t, err, "error message %s", ocyaml) +} + +func TestGet(t *testing.T) { + + _, err := Get("99.1") + assert.NotNilf(t, err, "error message %s", err) +} + +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 diff --git a/test/bin/kubectl b/test/bin/kubectl old mode 100644 new mode 100755 diff --git a/test/tool/oc.yml b/test/tool/oc.yml new file mode 100644 index 0000000..9cb17e0 --- /dev/null +++ b/test/tool/oc.yml @@ -0,0 +1,4 @@ +--- + +tools: + diff --git a/test/versionOc/oc.yml b/test/versionOc/oc.yml new file mode 100644 index 0000000..74c8148 --- /dev/null +++ b/test/versionOc/oc.yml @@ -0,0 +1,3 @@ +--- + +version: 99.0 \ No newline at end of file diff --git a/test/versionOc/offline/latest.yml b/test/versionOc/offline/latest.yml new file mode 100644 index 0000000..3f23114 --- /dev/null +++ b/test/versionOc/offline/latest.yml @@ -0,0 +1,3 @@ +--- + +version: 99.1 \ No newline at end of file diff --git a/test/versionOc/offline/oc_99.1.yml b/test/versionOc/offline/oc_99.1.yml new file mode 100644 index 0000000..7313caa --- /dev/null +++ b/test/versionOc/offline/oc_99.1.yml @@ -0,0 +1 @@ +---