Skip to content

Commit

Permalink
Adding CLI test for devel srvinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
safaci2000 committed Oct 25, 2023
1 parent f748ad4 commit e928465
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 51 deletions.
6 changes: 4 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 2 additions & 3 deletions cmd/backup/conections_test.go → cmd/test/conections_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)

Expand Down
50 changes: 50 additions & 0 deletions cmd/test/devel_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
66 changes: 66 additions & 0 deletions cmd/test/support.go
Original file line number Diff line number Diff line change
@@ -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

}
34 changes: 9 additions & 25 deletions cmd/version_test.go → cmd/test/version_test.go
Original file line number Diff line number Diff line change
@@ -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:"))
Expand All @@ -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"`)
}
21 changes: 0 additions & 21 deletions cmd/test_tools/support.go

This file was deleted.

0 comments on commit e928465

Please sign in to comment.