From e9284659c0eabade6a3016a2776ac639932b0784 Mon Sep 17 00:00:00 2001 From: Samir Faci Date: Wed, 25 Oct 2023 14:53:06 -0700 Subject: [PATCH] Adding CLI test for devel srvinfo --- Taskfile.yml | 6 ++- cmd/{backup => test}/conections_test.go | 5 +- cmd/test/devel_test.go | 50 +++++++++++++++++++ cmd/test/support.go | 66 +++++++++++++++++++++++++ cmd/{ => test}/version_test.go | 34 ++++--------- cmd/test_tools/support.go | 21 -------- 6 files changed, 131 insertions(+), 51 deletions(-) rename cmd/{backup => test}/conections_test.go (92%) create mode 100644 cmd/test/devel_test.go create mode 100644 cmd/test/support.go rename cmd/{ => test}/version_test.go (60%) delete mode 100644 cmd/test_tools/support.go diff --git a/Taskfile.yml b/Taskfile.yml index ea741a24..62a20701 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -107,14 +107,16 @@ tasks: test: desc: "test check" cmds: - - go test -v ./... -cover + - go test -v -coverpkg=./... -covermode=atomic -coverprofile=coverage.out ./... + - go tool cover -html=coverage.out env: GRAFANA_INTEGRATION: "1" TEST_TOKEN_CONFIG: "0" test_tokens: desc: "test Token Based Only" cmds: - - go test -v ./... -cover + - go test -v -coverpkg=./... -covermode=atomic -coverprofile=coverage.out ./... + - go tool cover -html=coverage.out env: GRAFANA_INTEGRATION: "1" TEST_TOKEN_CONFIG: "1" diff --git a/cmd/backup/conections_test.go b/cmd/test/conections_test.go similarity index 92% rename from cmd/backup/conections_test.go rename to cmd/test/conections_test.go index 653d4271..a12ddf00 100644 --- a/cmd/backup/conections_test.go +++ b/cmd/test/conections_test.go @@ -1,9 +1,8 @@ -package backup_test +package test import ( "github.com/esnet/gdg/cmd" "github.com/esnet/gdg/cmd/support" - "github.com/esnet/gdg/cmd/test_tools" "github.com/esnet/gdg/internal/service" "github.com/esnet/gdg/internal/service/mocks" "github.com/esnet/grafana-swagger-api-golang/goclient/models" @@ -38,7 +37,7 @@ func TestConnectionCommand(t *testing.T) { response.GrafanaSvc = getMockSvc } } - r, w, cleanup := test_tools.InterceptStdout() + r, w, cleanup := InterceptStdout() data, err := os.ReadFile("../../config/testing.yml") assert.Nil(t, err) diff --git a/cmd/test/devel_test.go b/cmd/test/devel_test.go new file mode 100644 index 00000000..50d930ac --- /dev/null +++ b/cmd/test/devel_test.go @@ -0,0 +1,50 @@ +package test + +import ( + "github.com/esnet/gdg/cmd" + "github.com/esnet/gdg/cmd/support" + "github.com/esnet/gdg/internal/service/mocks" + "github.com/stretchr/testify/assert" + "strings" + "testing" +) + +func TestDevelSrvInfo(t *testing.T) { + var execMe = func(mock *mocks.GrafanaService, data []byte, optionMockSvc func() support.RootOption) error { + expected := make(map[string]interface{}) + expected["Database"] = "db" + expected["Commit"] = "commit" + expected["Version"] = "version" + + mock.On("GetServerInfo").Return(expected) + err := cmd.Execute(string(data), []string{"tools", "devel", "srvinfo"}, optionMockSvc()) + return err + } + outStr, closeReader := setupAndExecuteMockingServices(t, execMe) + defer closeReader() + + assert.True(t, strings.Contains(outStr, "Version:")) + assert.True(t, strings.Contains(outStr, "Database:")) + assert.True(t, strings.Contains(outStr, "Commit:")) +} + +func TestDevelSrvCompletion(t *testing.T) { + fn := func(args []string) func(mock *mocks.GrafanaService, data []byte, optionMockSvc func() support.RootOption) error { + return func(mock *mocks.GrafanaService, data []byte, optionMockSvc func() support.RootOption) error { + err := cmd.Execute(string(data), args, optionMockSvc()) + return err + } + } + + outStr, closeReader := setupAndExecuteMockingServices(t, fn([]string{"tools", "devel", "completion", "fish"})) + assert.True(t, strings.Contains(outStr, "fish")) + assert.True(t, strings.Contains(outStr, "__completion_prepare_completions")) + closeReader() + outStr, closeReader = setupAndExecuteMockingServices(t, fn([]string{"tools", "devel", "completion", "bash"})) + assert.True(t, strings.Contains(outStr, "bash")) + assert.True(t, strings.Contains(outStr, "flag_parsing_disabled")) + closeReader() + outStr, closeReader = setupAndExecuteMockingServices(t, fn([]string{"tools", "devel", "completion", "zsh"})) + assert.True(t, strings.Contains(outStr, "shellCompDirectiveKeepOrder")) + closeReader() +} diff --git a/cmd/test/support.go b/cmd/test/support.go new file mode 100644 index 00000000..508575f2 --- /dev/null +++ b/cmd/test/support.go @@ -0,0 +1,66 @@ +package test + +import ( + "github.com/esnet/gdg/cmd/support" + "github.com/esnet/gdg/internal/service" + "github.com/esnet/gdg/internal/service/mocks" + log "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" + "io" + "os" + "testing" +) + +// setupAndExecuteMockingServices will create a mock for varous required entities allowing to test the CLI flag parsing +// process: function that setups mocks and invokes the Execute command +func setupAndExecuteMockingServices(t *testing.T, process func(mock *mocks.GrafanaService, data []byte, optionMockSvc func() support.RootOption) error) (string, func()) { + testSvc := new(mocks.GrafanaService) + getMockSvc := func() service.GrafanaService { + return testSvc + } + + optionMockSvc := func() support.RootOption { + return func(response *support.RootCommand) { + response.GrafanaSvc = getMockSvc + } + } + + r, w, cleanup := InterceptStdout() + data, err := os.ReadFile("../../config/testing.yml") + assert.Nil(t, err) + + err = process(testSvc, data, optionMockSvc) + assert.Nil(t, err) + defer cleanup() + err = w.Close() + if err != nil { + log.Warn("unable to close write stream") + } + clean := func() { + defer r.Close() + } + out, _ := io.ReadAll(r) + outStr := string(out) + return outStr, clean + +} + +// InterceptStdout is a test helper function that will redirect all stdout in and out to a different file stream. +// It returns the stdout, stderr, and a function to be invoked to close the streams. +func InterceptStdout() (*os.File, *os.File, func()) { + backupStd := os.Stdout + backupErr := os.Stderr + r, w, _ := os.Pipe() + //Restore streams + log.SetOutput(w) + cleanup := func() { + os.Stdout = backupStd + os.Stderr = backupErr + log.SetOutput(os.Stdout) + } + os.Stdout = w + os.Stderr = w + + return r, w, cleanup + +} diff --git a/cmd/version_test.go b/cmd/test/version_test.go similarity index 60% rename from cmd/version_test.go rename to cmd/test/version_test.go index 35c38f33..54e82d64 100644 --- a/cmd/version_test.go +++ b/cmd/test/version_test.go @@ -1,42 +1,26 @@ -package cmd +package test import ( "fmt" + "github.com/esnet/gdg/cmd" "github.com/esnet/gdg/cmd/support" - "github.com/esnet/gdg/cmd/test_tools" "github.com/esnet/gdg/internal/service" "github.com/esnet/gdg/internal/service/mocks" "github.com/esnet/gdg/internal/version" "github.com/stretchr/testify/assert" - "io" "os" "strings" "testing" ) func TestVersionCommand(t *testing.T) { - testSvc := new(mocks.GrafanaService) - getMockSvc := func() service.GrafanaService { - return testSvc + var execMe = func(mock *mocks.GrafanaService, data []byte, optionMockSvc func() support.RootOption) error { + err := cmd.Execute(string(data), []string{"version"}, optionMockSvc()) + return err } + outStr, closeReader := setupAndExecuteMockingServices(t, execMe) + defer closeReader() - optionMockSvc := func() support.RootOption { - return func(response *support.RootCommand) { - response.GrafanaSvc = getMockSvc - } - } - path, _ := os.Getwd() - fmt.Println(path) - r, w, cleanup := test_tools.InterceptStdout() - data, err := os.ReadFile("../config/testing.yml") - assert.Nil(t, err) - - err = Execute(string(data), []string{"version"}, optionMockSvc()) - assert.Nil(t, err) - defer cleanup() - w.Close() - out, _ := io.ReadAll(r) - outStr := string(out) assert.True(t, strings.Contains(outStr, "Build Date:")) assert.True(t, strings.Contains(outStr, "Git Commit:")) assert.True(t, strings.Contains(outStr, "Version:")) @@ -59,10 +43,10 @@ func TestVersionErrCommand(t *testing.T) { } path, _ := os.Getwd() fmt.Println(path) - data, err := os.ReadFile("../config/testing.yml") + data, err := os.ReadFile("../../config/testing.yml") assert.Nil(t, err) - err = Execute(string(data), []string{"dumb", "dumb"}, optionMockSvc()) + err = cmd.Execute(string(data), []string{"dumb", "dumb"}, optionMockSvc()) assert.NotNil(t, err) assert.Equal(t, err.Error(), `command error: unknown command "dumb" for "gdg"`) } diff --git a/cmd/test_tools/support.go b/cmd/test_tools/support.go deleted file mode 100644 index 5c23ecba..00000000 --- a/cmd/test_tools/support.go +++ /dev/null @@ -1,21 +0,0 @@ -package test_tools - -import "os" - -// InterceptStdout is a test helper function that will redirect all stdout in and out to a different file stream. -// It returns the stdout, stderr, and a function to be invoked to close the streams. -func InterceptStdout() (*os.File, *os.File, func()) { - backupStd := os.Stdout - backupErr := os.Stderr - r, w, _ := os.Pipe() - //Restore streams - cleanup := func() { - os.Stdout = backupStd - os.Stderr = backupErr - } - os.Stdout = w - os.Stderr = w - - return r, w, cleanup - -}