From 4bce096e1fdd09c1bea9123161752bf7f57fece0 Mon Sep 17 00:00:00 2001 From: admju Date: Tue, 10 Sep 2024 17:01:54 +0000 Subject: [PATCH] test --- src/Makefile | 1 - src/cmd/args.go | 96 ++++++++++++++++++--------------- src/cmd/args_test.go | 9 ++++ src/cmd/generateCmd.go | 3 +- src/cmd/installCmd.go | 3 +- src/cmd/installCmd_test.go | 41 ++++++++++++++ src/cmd/uninstallCmd.go | 6 ++- src/cmd/uninstallCmd_test.go | 41 ++++++++++++++ src/kubectl/version.go | 2 - src/log_wrapper/log_wrapper.go | 2 - src/tool/conf_test.go | 3 -- src/tool/helm.go | 12 +++-- src/tool/helm_test.go | 61 +++++++++++++++++++++ src/tool/kubectl.go | 8 +-- src/tool/kubectl_test.go | 79 +++++++++++++++++++++++++++ src/tool/main_test.go | 19 ++++--- src/tool/tool.go | 4 +- test/tool/helm.tgz | Bin 0 -> 142 bytes 18 files changed, 315 insertions(+), 75 deletions(-) create mode 100644 src/cmd/args_test.go create mode 100644 src/cmd/installCmd_test.go create mode 100644 src/cmd/uninstallCmd_test.go create mode 100644 src/tool/kubectl_test.go create mode 100644 test/tool/helm.tgz diff --git a/src/Makefile b/src/Makefile index 75c1267..3d3b438 100644 --- a/src/Makefile +++ b/src/Makefile @@ -77,4 +77,3 @@ test_%: test: @go test ./... -coverprofile=.coverage.out -v go tool cover -html=.coverage.out -o .coverage.html - diff --git a/src/cmd/args.go b/src/cmd/args.go index 5d4f04c..4f04f09 100644 --- a/src/cmd/args.go +++ b/src/cmd/args.go @@ -16,58 +16,70 @@ 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 cmdInstall = cobraInstallCmd() + var cmdUninstall = cobraUninstallCmd() + var cmdGenerate = cobraGenerateCmd() - 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", - } + cmdInstall.Flags().StringVarP(&context, "context", "c", "opencloud", "Nom du context") + cmdInstall.Flags().StringVarP(&version, "version", "v", "latest", "Version") + cmdInstall.Flags().StringArrayVarP(&modules, "modules", "m", []string{}, "modules, ...") - 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", - } + cmdUninstall.Flags().StringVarP(&context, "context", "c", "opencloud", "Nom du context") - cmdInstall.Flags().StringVarP(&context, "context", "c", "opencloud", "Nom du context") - cmdInstall.Flags().StringVarP(&version, "version", "v", "latest", "Version") - cmdInstall.Flags().StringArrayVarP(&modules, "modules", "m", []string{}, "modules, ...") + cmdGenerate.Flags().StringVarP(&context, "context", "c", "opencloud", "Nom du context") + cmdGenerate.Flags().StringVarP(&version, "version", "v", "latest", "Version") - cmdUninstall.Flags().StringVarP(&context, "context", "c", "opencloud", "Nom du context") + rootCmd.AddCommand(cmdInstall) + rootCmd.AddCommand(cmdUninstall) + rootCmd.AddCommand(cmdGenerate) - cmdGenerate.Flags().StringVarP(&context, "context", "c", "opencloud", "Nom du context") - cmdGenerate.Flags().StringVarP(&version, "version", "v", "latest", "Version") - - rootCmd.AddCommand(cmdInstall) - rootCmd.AddCommand(cmdUninstall) - rootCmd.AddCommand(cmdGenerate) - - cobra.CheckErr(rootCmd.Execute()) + cobra.CheckErr(rootCmd.Execute()) } \ No newline at end of file diff --git a/src/cmd/args_test.go b/src/cmd/args_test.go new file mode 100644 index 0000000..fc14535 --- /dev/null +++ b/src/cmd/args_test.go @@ -0,0 +1,9 @@ +package cmd + +import ( + "testing" +) + +func TestExecute(t *testing.T) { + Execute() +} \ No newline at end of file diff --git a/src/cmd/generateCmd.go b/src/cmd/generateCmd.go index f01d47e..5607b76 100644 --- a/src/cmd/generateCmd.go +++ b/src/cmd/generateCmd.go @@ -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 } \ No newline at end of file diff --git a/src/cmd/installCmd.go b/src/cmd/installCmd.go index b910c3c..9120d26 100644 --- a/src/cmd/installCmd.go +++ b/src/cmd/installCmd.go @@ -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 } \ No newline at end of file diff --git a/src/cmd/installCmd_test.go b/src/cmd/installCmd_test.go new file mode 100644 index 0000000..80cf228 --- /dev/null +++ b/src/cmd/installCmd_test.go @@ -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") +} diff --git a/src/cmd/uninstallCmd.go b/src/cmd/uninstallCmd.go index 3e77d64..e33296f 100644 --- a/src/cmd/uninstallCmd.go +++ b/src/cmd/uninstallCmd.go @@ -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 } \ No newline at end of file diff --git a/src/cmd/uninstallCmd_test.go b/src/cmd/uninstallCmd_test.go new file mode 100644 index 0000000..79a197e --- /dev/null +++ b/src/cmd/uninstallCmd_test.go @@ -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") +} diff --git a/src/kubectl/version.go b/src/kubectl/version.go index 0916bd6..8cf0057 100644 --- a/src/kubectl/version.go +++ b/src/kubectl/version.go @@ -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 } diff --git a/src/log_wrapper/log_wrapper.go b/src/log_wrapper/log_wrapper.go index c2f8ecb..364d3d0 100644 --- a/src/log_wrapper/log_wrapper.go +++ b/src/log_wrapper/log_wrapper.go @@ -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) diff --git a/src/tool/conf_test.go b/src/tool/conf_test.go index 1d723f7..e014728 100644 --- a/src/tool/conf_test.go +++ b/src/tool/conf_test.go @@ -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) } \ No newline at end of file diff --git a/src/tool/helm.go b/src/tool/helm.go index c7d3b52..2bab62c 100644 --- a/src/tool/helm.go +++ b/src/tool/helm.go @@ -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() diff --git a/src/tool/helm_test.go b/src/tool/helm_test.go index 05b1676..8dc7365 100644 --- a/src/tool/helm_test.go +++ b/src/tool/helm_test.go @@ -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") +} diff --git a/src/tool/kubectl.go b/src/tool/kubectl.go index 5bb45ce..d5e3be4 100644 --- a/src/tool/kubectl.go +++ b/src/tool/kubectl.go @@ -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() diff --git a/src/tool/kubectl_test.go b/src/tool/kubectl_test.go new file mode 100644 index 0000000..05cd39f --- /dev/null +++ b/src/tool/kubectl_test.go @@ -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 <I;(5Ef6P4PY{GVn#$9T@I zsWq}Gp}(dj{q~&SRQLRiz|rl7E87HStGue6+!PV}{aTLH{JV#vUj9;yUv_`@(z?ap ntjlfg--+lsxx8rm)b(49{@1ZWtb~#gpP1I%_-@Fc!N33jy?s86 literal 0 HcmV?d00001