Skip to content

Commit

Permalink
fix monitor to only start the monitor in json format when requested (#…
Browse files Browse the repository at this point in the history
…10358)

* fix monitor to only start the monitor in json format when requested

* add release notes

* add test to validate json format when requested
  • Loading branch information
dhiaayachi authored Jun 7, 2021
1 parent 1bf3dc5 commit dda3e68
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .changelog/10358.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
monitor: fix monitor to produce json format logs when requested
```
11 changes: 6 additions & 5 deletions command/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ func (c *cmd) Run(args []string) int {
c.UI.Error(fmt.Sprintf("Error starting JSON monitor: %s", err))
return 1
}
}
logCh, err = client.Agent().Monitor(c.logLevel, eventDoneCh, nil)
if err != nil {
c.UI.Error(fmt.Sprintf("Error starting monitor: %s", err))
return 1
} else {
logCh, err = client.Agent().Monitor(c.logLevel, eventDoneCh, nil)
if err != nil {
c.UI.Error(fmt.Sprintf("Error starting monitor: %s", err))
return 1
}
}

go func() {
Expand Down
65 changes: 65 additions & 0 deletions command/monitor/monitor_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package monitor

import (
"encoding/json"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -120,3 +122,66 @@ func TestMonitorCommand_LogJSONValidFlag(t *testing.T) {
t.Fatal("timed out waiting for exit")
}
}

func TestMonitorCommand_LogJSONValidFormat(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
}

t.Parallel()
a := agent.StartTestAgent(t, agent.TestAgent{})
defer a.Shutdown()

shutdownCh := make(chan struct{})

ui := cli.NewMockUi()
c := New(ui, shutdownCh)
args := []string{"-http-addr=" + a.HTTPAddr(), "-log-json"}

// Buffer it so we don't deadlock when blocking send on shutdownCh triggers
// Run to return before we can select on it.
exitCode := make(chan int, 1)

// Run the monitor in another go routine. If this doesn't exit on our "signal"
// then the whole test will hang and we'll panic (to not blow up if people run
// the suite without -timeout)
var wg sync.WaitGroup
wg.Add(1)
go func() {
wg.Done() // Signal that this goroutine is at least running now
exitCode <- c.Run(args)
}()

// Wait for that routine to at least be running
wg.Wait()

// Read the logs and try to json marshall it
go func() {
time.Sleep(1 * time.Second)
outputs := ui.OutputWriter.String()
for count, output := range strings.Split(outputs, "\n") {
if output != "" && count > 0 {
jsonLog := new(map[string]interface{})
err := json.Unmarshal([]byte(output), jsonLog)
if err != nil {
exitCode <- -1
}
if len(*jsonLog) <= 0 {
exitCode <- 1
}
}
}
shutdownCh <- struct{}{}

}()

select {
case ret := <-exitCode:
if ret != 0 {
t.Fatal("command returned with non-zero code")
}
// OK!
case <-time.After(5 * time.Second):
t.Fatal("timed out waiting for exit")
}
}

0 comments on commit dda3e68

Please sign in to comment.