fix log as json flag (#901)

Signed-off-by: Mukundan Sundararajan <msundar.ms@outlook.com>
This commit is contained in:
Mukundan Sundararajan 2022-03-09 18:37:44 +05:30 committed by GitHub
parent 249d8f73d2
commit f7232ee02a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 7 deletions

View File

@ -135,11 +135,7 @@ release: build archive
.PHONY: test-deps
test-deps:
# The desire here is to download this test dependency without polluting go.mod
# In golang >=1.16 there is a new way to do this with `go install gotest.tools/gotestsum@latest`
# But this doesn't work with <=1.15, so we do it the old way for now
# (see: https://golang.org/ref/mod#go-install)
GO111MODULE=off go get gotest.tools/gotestsum
go install gotest.tools/gotestsum@latest
################################################################################
# Tests #
################################################################################

View File

@ -52,6 +52,10 @@ func EnableJSONFormat() {
logAsJSON = true
}
func IsJSONLogEnabled() bool {
return logAsJSON
}
// SuccessStatusEvent reports on a success event.
func SuccessStatusEvent(w io.Writer, fmtstr string, a ...interface{}) {
if logAsJSON {

View File

@ -27,6 +27,7 @@ import (
"github.com/phayes/freeport"
"gopkg.in/yaml.v2"
"github.com/dapr/cli/pkg/print"
"github.com/dapr/dapr/pkg/components"
modes "github.com/dapr/dapr/pkg/config/modes"
)
@ -243,6 +244,10 @@ func (config *RunConfig) getArgs() []string {
}
}
if print.IsJSONLogEnabled() {
args = append(args, "--log-as-json")
}
return args
}

View File

@ -58,6 +58,7 @@ func TestStandaloneInstall(t *testing.T) {
}{
{"test install", testInstall},
{"test install from custom registry", testInstallWithCustomImageRegsitry},
{"test run log json enabled", testRunLogJSON},
{"test run", testRun},
{"test stop", testStop},
{"test publish", testPublish},
@ -377,11 +378,25 @@ func verifyArtifactsAfterInstall(t *testing.T) {
assert.Empty(t, configs)
}
func testRunLogJSON(t *testing.T) {
daprPath := getDaprPath()
t.Run(fmt.Sprintf("check JSON log"), func(t *testing.T) {
output, err := spawn.Command(daprPath, "run", "--app-id", "logjson", "--log-as-json", "--", "bash", "-c", "echo 'test'")
t.Log(output)
require.NoError(t, err, "run failed")
assert.Contains(t, output, "{\"app_id\":\"logjson\"")
assert.Contains(t, output, "\"type\":\"log\"")
assert.Contains(t, output, "Exited App successfully")
assert.Contains(t, output, "Exited Dapr successfully")
})
}
func testRun(t *testing.T) {
daprPath := getDaprPath()
for _, path := range socketCases {
t.Run(fmt.Sprintf("Normal exit, socket: %s", path), func(t *testing.T) {
t.Run(fmt.Sprintf("normal exit, socket: %s", path), func(t *testing.T) {
output, err := spawn.Command(daprPath, "run", "--unix-domain-socket", path, "--", "bash", "-c", "echo test")
t.Log(output)
require.NoError(t, err, "run failed")
@ -389,7 +404,7 @@ func testRun(t *testing.T) {
assert.Contains(t, output, "Exited Dapr successfully")
})
t.Run(fmt.Sprintf("Error exit, socket: %s", path), func(t *testing.T) {
t.Run(fmt.Sprintf("error exit, socket: %s", path), func(t *testing.T) {
output, err := spawn.Command(daprPath, "run", "--unix-domain-socket", path, "--", "bash", "-c", "exit 1")
t.Log(output)
require.NoError(t, err, "run failed")