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

@ -77,4 +77,3 @@ test_%:
test:
@go test ./... -coverprofile=.coverage.out -v
go tool cover -html=.coverage.out -o .coverage.html

View File

@ -16,44 +16,56 @@ var (
modules []string
)
func cobraInstallCmd() *cobra.Command {
return &cobra.Command{
Use: "install",
Short: "install",
Long: `deploy Charts`,
Args: cobra.MaximumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return InstallCmd(context, version, modules)
},
Example: "oc-deploy install --version 1.0 --context ex1",
}
}
func cobraUninstallCmd() *cobra.Command{
return &cobra.Command{
Use: "uninstall",
Short: "undeploy",
Long: `Undeploy`,
Args: cobra.MaximumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return UninstallCmd(context)
},
Example: "oc-deploy uninstall --context ex1",
}
}
func cobraGenerateCmd() *cobra.Command{
return &cobra.Command{
Use: "generate",
Short: "generate",
Long: "Value",
Args: cobra.MaximumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return GenerateCmd(context, version)
},
Example: "oc-deploy generate --version 1.0 --context ex1",
}
}
func Execute() {
log.Log().Debug().Msg("Execute")
var rootCmd = &cobra.Command{Use: "oc-deploy"}
var cmdInstall = &cobra.Command{
Use: "install",
Short: "deploy",
Long: `deploy Charts`,
Args: cobra.MaximumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
InstallCmd(context, version, modules)
},
Example: "oc-deploy install --version 1.0 --context ex1",
}
var cmdUninstall = &cobra.Command{
Use: "uninstall",
Short: "undeploy",
Long: `Undeploy`,
Args: cobra.MaximumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
UninstallCmd(context)
},
Example: "oc-deploy uninstall --context ex1",
}
var cmdGenerate = &cobra.Command{
Use: "generate",
Short: "generate",
Long: "Value",
Args: cobra.MaximumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
GenerateCmd(context, version)
},
Example: "oc-deploy generate --version 1.0 --context ex1",
}
var cmdInstall = cobraInstallCmd()
var cmdUninstall = cobraUninstallCmd()
var cmdGenerate = cobraGenerateCmd()
cmdInstall.Flags().StringVarP(&context, "context", "c", "opencloud", "Nom du context")
cmdInstall.Flags().StringVarP(&version, "version", "v", "latest", "Version")

9
src/cmd/args_test.go Normal file
View File

@ -0,0 +1,9 @@
package cmd
import (
"testing"
)
func TestExecute(t *testing.T) {
Execute()
}

View File

@ -8,7 +8,7 @@ import (
"oc-deploy/generate"
)
func GenerateCmd(project string, version string) {
func GenerateCmd(project string, version string) error {
log.Log().Info().Msg("Generate >> ")
version, err := versionOc.GetFromFile(version)
@ -24,4 +24,5 @@ func GenerateCmd(project string, version string) {
}
log.Log().Info().Msg(" >> Value : " + fic)
return err
}

View File

@ -7,7 +7,7 @@ import (
"oc-deploy/install"
)
func InstallCmd(context string, version string, modules []string) {
func InstallCmd(context string, version string, modules []string) error {
log.Log().Info().Msg("Install >> ")
log.Log().Info().Msg(" << Contexte : " + context)
@ -47,4 +47,5 @@ func InstallCmd(context string, version string, modules []string) {
log.Log().Fatal().Msg(" >> " + err.Error())
}
return err
}

View File

@ -0,0 +1,41 @@
package cmd
import (
"bytes"
"github.com/spf13/cobra"
"testing"
"github.com/stretchr/testify/assert"
)
func TestInstallCommand(t *testing.T) {
cmd := cobraInstallCmd()
inMock := false
cmd.RunE = func(cmd *cobra.Command, args []string) error {
inMock = true
return nil
}
cmd.Execute()
assert.Truef(t, inMock, "TestInstallCommand")
}
func TestInstallCommandErr(t *testing.T) {
cmd := cobraUninstallCmd()
inMock := false
cmd.RunE = func(cmd *cobra.Command, args []string) error {
inMock = true
return nil
}
cmd.SetArgs([]string{"bad"})
b := bytes.NewBufferString("")
cmd.SetOut(b)
err := cmd.Execute()
assert.Falsef(t, inMock, "TestInstallCommand args")
assert.NotNilf(t, err, "TestInstallCommand args")
}

View File

@ -11,8 +11,8 @@ import (
"oc-deploy/install"
)
func UninstallCmd(context string) {
log.Log().Info().Msg("Unnstall >> ")
func UninstallCmd(context string) error {
log.Log().Info().Msg("Uninstall >> ")
log.Log().Info().Msg(" << Contexte : " + context)
@ -40,4 +40,6 @@ func UninstallCmd(context string) {
if err != nil {
log.Log().Fatal().Msg(" >> " + err.Error())
}
return err
}

View File

@ -0,0 +1,41 @@
package cmd
import (
"bytes"
"github.com/spf13/cobra"
"testing"
"github.com/stretchr/testify/assert"
)
func TestUninstallCommand(t *testing.T) {
cmd := cobraUninstallCmd()
inMock := false
cmd.RunE = func(cmd *cobra.Command, args []string) error {
inMock = true
return nil
}
cmd.Execute()
assert.Truef(t, inMock, "TestUninstallCommand")
}
func TestUninstallCommandErr(t *testing.T) {
cmd := cobraUninstallCmd()
inMock := false
cmd.RunE = func(cmd *cobra.Command, args []string) error {
inMock = true
return nil
}
cmd.SetArgs([]string{"bad"})
b := bytes.NewBufferString("")
cmd.SetOut(b)
err := cmd.Execute()
assert.Falsef(t, inMock, "TestUninstallCommand args")
assert.NotNilf(t, err, "TestUninstallCommand args")
}

View File

@ -12,12 +12,10 @@ type toolVersion struct {
ClientVersion toolClientVersion `json:"clientVersion"`
}
func (this KubectlCommand) GetVersion() (string, error) {
cmd := this.Exec(this.Bin, "version", "-o", "json", "--client=true")
stdout, err := cmd.CombinedOutput()
if err != nil {
return "", err
}

View File

@ -3,7 +3,6 @@ package log_wrapper
// https://github.com/rs/zerolog/issues/150
import (
"fmt"
"os"
"path/filepath"
"github.com/rs/zerolog"
@ -34,7 +33,6 @@ func InitLog(filename string) bool {
ficlog := filepath.Join(filename + ".log")
fAll, _ := os.OpenFile(ficlog, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
fmt.Println("ficlog", ficlog)
output := zerolog.ConsoleWriter{Out: os.Stdout}
writerInfo := zerolog.MultiLevelWriter(output)

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

@ -8,7 +8,6 @@ import (
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

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

BIN
test/tool/helm.tgz Normal file

Binary file not shown.