This commit is contained in:
admju 2024-09-04 07:11:13 +00:00
parent d12d3e31bf
commit 48301bf82e
17 changed files with 210 additions and 25 deletions

View File

@ -18,7 +18,7 @@ help:
@echo @echo
@echo 'Usage:' @echo 'Usage:'
@echo ' make build Génère les exécutables.' @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 BIN_OPTS=... Go run'
@echo ' make run_install BIN_OPTS=... Go run' @echo ' make run_install BIN_OPTS=... Go run'

View File

@ -18,5 +18,5 @@ func TestHelmVersion(t *testing.T){
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, "v3.15.4+gfa9efb0", version, "TestHelmVersion error")
} }

24
src/kubectl/main_test.go Normal file
View File

@ -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)
}

View File

@ -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")
}

28
src/tool/conf_test.go Normal file
View File

@ -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")
}

View File

@ -22,7 +22,6 @@ func (this HelmInstallData) Download() (error) {
bin_dir := this.obj.Bin bin_dir := this.obj.Bin
err2 := os.MkdirAll(bin_dir, os.ModePerm) err2 := os.MkdirAll(bin_dir, os.ModePerm)
if err2 != nil { if err2 != nil {
fmt.Println(err2)
return err2 return err2
} }

24
src/tool/main_test.go Normal file
View File

@ -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)
}

View File

@ -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)
}

View File

@ -6,6 +6,7 @@ import (
"os" "os"
"errors" "errors"
"io" "io"
"path/filepath"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
log "oc-deploy/log_wrapper" log "oc-deploy/log_wrapper"
) )
@ -14,6 +15,8 @@ type versionInput struct {
Version string `yaml:"version"` Version string `yaml:"version"`
} }
var OFFLINE_DIR string = "../offline"
func GetFromFile(fileversion string) (string, error) { func GetFromFile(fileversion string) (string, error) {
fin, err := os.Open(fileversion) fin, err := os.Open(fileversion)
if err != nil { if err != nil {
@ -28,7 +31,10 @@ func GetFromFile(fileversion string) (string, error) {
var objmap versionInput var objmap versionInput
yaml.Unmarshal(byteValue, &objmap) err = yaml.Unmarshal(byteValue, &objmap)
if err != nil {
return "", err
}
return objmap.Version, nil return objmap.Version, nil
} }
@ -45,7 +51,8 @@ func Get(version string) (string, error) {
version2 = versionLatest 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 { if _, err := os.Stat(src); err != nil {
log.Log().Debug().Msg(err.Error()) log.Log().Debug().Msg(err.Error())
return "", errors.New("Version non disponible") 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 // readLatestFile : Lit le numéro de la version dans le fichier lastest.yml
func readLatestFile() (string, error) { func readLatestFile() (string, error) {
src := "../offline/latest.yml" src := filepath.Join(OFFLINE_DIR, "latest.yml")
fin, err := os.Open(src) fin, err := os.Open(src)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -71,7 +79,11 @@ func readLatestFile() (string, error) {
var objmap versionInput var objmap versionInput
yaml.Unmarshal(byteValue, &objmap) err = yaml.Unmarshal(byteValue, &objmap)
if err != nil {
return "", err
}
return objmap.Version, nil return objmap.Version, nil
} }

View File

@ -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")
}

0
test/bin/kubectl Normal file → Executable file
View File

4
test/tool/oc.yml Normal file
View File

@ -0,0 +1,4 @@
---
tools:

3
test/versionOc/oc.yml Normal file
View File

@ -0,0 +1,3 @@
---
version: 99.0

View File

@ -0,0 +1,3 @@
---
version: 99.1

View File

@ -0,0 +1 @@
---