-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f748ad4
commit e928465
Showing
6 changed files
with
131 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.