From f7232ee02a0309ba1c5e864b4e41454078464d2c Mon Sep 17 00:00:00 2001 From: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com> Date: Wed, 9 Mar 2022 18:37:44 +0530 Subject: [PATCH] fix log as json flag (#901) Signed-off-by: Mukundan Sundararajan --- Makefile | 6 +----- pkg/print/print.go | 4 ++++ pkg/standalone/run.go | 5 +++++ tests/e2e/standalone/standalone_test.go | 19 +++++++++++++++++-- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index c9f1b135..5b9eb7c7 100644 --- a/Makefile +++ b/Makefile @@ -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 # ################################################################################ diff --git a/pkg/print/print.go b/pkg/print/print.go index c37886fa..42e4a6bd 100644 --- a/pkg/print/print.go +++ b/pkg/print/print.go @@ -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 { diff --git a/pkg/standalone/run.go b/pkg/standalone/run.go index c6235e6e..9e1ae79f 100644 --- a/pkg/standalone/run.go +++ b/pkg/standalone/run.go @@ -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 } diff --git a/tests/e2e/standalone/standalone_test.go b/tests/e2e/standalone/standalone_test.go index 82a040e3..784af6ef 100644 --- a/tests/e2e/standalone/standalone_test.go +++ b/tests/e2e/standalone/standalone_test.go @@ -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")