This commit is contained in:
admju
2024-09-10 17:01:54 +00:00
parent 75b7b94a50
commit 4bce096e1f
18 changed files with 315 additions and 75 deletions

View File

@@ -1,8 +1,6 @@
package tool
import (
"fmt"
// "os"
"path/filepath"
"testing"
@@ -15,7 +13,6 @@ func TestToolConf(t *testing.T) {
src := filepath.Join(TEST_SRC_DIR, "oc.yml")
data, err := FromConfigFile(src)
fmt.Println("data", src, data, err)
assert.Equal(t, "kubectl", data[0].Name, "TestToolConf error")
assert.Nilf(t, err, "error message %s", src)
}

View File

@@ -8,16 +8,16 @@ import (
"oc-deploy/helm"
)
type HelmInstallData struct {
type HelmInstall struct {
obj ToolData
tmp string
}
func (this HelmInstallData) Get() (ToolData) {
func (this HelmInstall) Get() (ToolData) {
return this.obj
}
func (this HelmInstallData) Download() (error) {
func (this HelmInstall) Download() (error) {
bin_dir := this.obj.Bin
err2 := os.MkdirAll(bin_dir, os.ModePerm)
@@ -35,7 +35,9 @@ func (this HelmInstallData) Download() (error) {
r, _ := os.Open(tmp_file)
err1 := utils.ExtractTarGz(bin_dir, r)
if err1 != nil {return err1}
if err1 != nil {
return err1
}
os.Remove(tmp_file)
@@ -48,7 +50,7 @@ func (this HelmInstallData) Download() (error) {
}
///////////////
func (this HelmInstallData) Version(path string) (string, error) {
func (this HelmInstall) Version(path string) (string, error) {
cmd := helm.HelmCommand{Bin: path}
cmd.New()
return cmd.GetVersion()

View File

@@ -1 +1,62 @@
package tool
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/jarcoal/httpmock"
)
func TestToolHelm(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
data := ToolData{Bin: TEST_DEST_DIR,
Name: "helm",
Version: "1.0",
Url: "http://test/%s"}
fileName := filepath.Join(TEST_SRC_DIR, "helm.tgz")
httpRes, _ := os.ReadFile(fileName)
httpmock.RegisterResponder("GET", "http://test/1.0",
httpmock.NewBytesResponder(200, httpRes))
install := HelmInstall{obj: data, tmp: TEST_DEST_DIR}
data2 := install.Get()
assert.Equal(t, data.Name, data2.Name, "TestToolHelm error")
assert.Equal(t, data.Version, data2.Version, "TestToolHelm error")
err := install.Download()
assert.Nilf(t, err, "error message %s", "Download")
dest := filepath.Join(TEST_DEST_DIR, "helm")
assert.FileExists(t, dest, "TestToolHelm Download error")
version, _ := install.Version(dest)
assert.Equal(t, "1.0", version, "TestToolHelm error")
}
func TestToolHelmErr(t *testing.T) {
data := ToolData{Bin: TEST_DEST_DIR,
Name: "test",
Version: "1.0",
Url: "http://test/%s"}
install := HelmInstall{obj: data}
data2 := install.Get()
assert.Equal(t, data.Name, data2.Name, "TestToolHelm error")
err := install.Download()
assert.NotNilf(t, err, "error message %s", "Download")
}

View File

@@ -10,15 +10,15 @@ import (
"oc-deploy/kubectl"
)
type KubecltInstallData struct {
type KubectlInstall struct {
obj ToolData
}
func (this KubecltInstallData) Get() (ToolData) {
func (this KubectlInstall) Get() (ToolData) {
return this.obj
}
func (this KubecltInstallData) Download() (error) {
func (this KubectlInstall) Download() (error) {
bin_dir := this.obj.Bin
bin := filepath.Join(bin_dir, this.obj.Name)
@@ -36,7 +36,7 @@ func (this KubecltInstallData) Download() (error) {
}
///////////////
func (this KubecltInstallData) Version(path string) (string, error) {
func (this KubectlInstall) Version(path string) (string, error) {
cmd := kubectl.KubectlCommand{Bin: path}
cmd.New()
return cmd.GetVersion()

79
src/tool/kubectl_test.go Normal file
View File

@@ -0,0 +1,79 @@
package tool
import (
"fmt"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/jarcoal/httpmock"
)
func TestToolKubectl(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
data := ToolData{Bin: TEST_DEST_DIR,
Name: "kubectl",
Version: "1.0",
Url: "http://test/%s"}
binContent := `#!/bin/sh
cat <<EOF
{
"clientVersion": {
"major": "1",
"minor": "30",
"gitVersion": "v1.30.3",
"gitCommit": "6fc0a69044f1ac4c13841ec4391224a2df241460",
"gitTreeState": "clean",
"buildDate": "2024-07-16T23:54:40Z",
"goVersion": "go1.22.5",
"compiler": "gc",
"platform": "linux/amd64"
},
"kustomizeVersion": "v5.0.4-0.20230601165947-6ce0bf390ce3"
}
EOF
`
httpmock.RegisterResponder("GET", "http://test/1.0",
httpmock.NewStringResponder(200, binContent))
install := KubectlInstall{obj: data}
data2 := install.Get()
assert.Equal(t, data.Name, data2.Name, "TestToolKubectl error")
assert.Equal(t, data.Version, data2.Version, "TestToolKubectl error")
err := install.Download()
assert.Nilf(t, err, "error message %s", "Download")
dest := filepath.Join(TEST_DEST_DIR, "kubectl")
assert.FileExists(t, dest, "TestToolKubectl Download error")
version, err1 := install.Version(dest)
assert.Equal(t, "v1.30.3", version, "TestToolKubectl error")
fmt.Println(" err1 ", err1)
}
func TestToolKubectlErr(t *testing.T) {
data := ToolData{Bin: TEST_DEST_DIR,
Name: "test",
Version: "1.0",
Url: "http://test/%s"}
install := KubectlInstall{obj: data}
data2 := install.Get()
assert.Equal(t, data.Name, data2.Name, "TestToolKubectl error")
err := install.Download()
assert.NotNilf(t, err, "error message %s", "Download")
}

View File

@@ -1,24 +1,23 @@
package tool
import (
"os"
"testing"
"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
folderPath := TEST_DEST_DIR
os.RemoveAll(folderPath)
os.MkdirAll(folderPath, os.ModePerm)
os.RemoveAll(folderPath)
os.MkdirAll(folderPath, os.ModePerm)
// call flag.Parse() here if TestMain uses flags
exitCode := m.Run()
// call flag.Parse() here if TestMain uses flags
exitCode := m.Run()
os.RemoveAll(folderPath)
os.Exit(exitCode)
os.RemoveAll(folderPath)
os.Exit(exitCode)
}

View File

@@ -68,9 +68,9 @@ func factory(data ToolData) (Forme, error) {
switch data.Name {
case "kubectl":
f = KubecltInstallData{obj: data}
f = KubectlInstall{obj: data}
case "helm":
f = HelmInstallData{obj: data, tmp: "/tmp"}
f = HelmInstall{obj: data, tmp: "/tmp"}
default:
return f, fmt.Errorf("Outil Inconnu : %s", data.Name)
}