Skip to content

Commit

Permalink
cli: Ensure JSON flag is respected in autopilot health command. (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasell authored Dec 12, 2024
1 parent 63e2c6a commit 86bc7ed
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/24655.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
cli: Ensure the `operator autopilot health` command only outputs JSON when the `json` flag is supplied
```
4 changes: 2 additions & 2 deletions command/operator_autopilot_health.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ func (c *OperatorAutopilotHealthCommand) Run(args []string) int {
return 1
}
c.Ui.Output(string(bytes))
} else {
c.Ui.Output(formatAutopilotState(state))
}

c.Ui.Output(formatAutopilotState(state))

return 0
}

Expand Down
23 changes: 23 additions & 0 deletions command/operator_autopilot_health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
package command

import (
"encoding/json"
"testing"

"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/ci"
"github.com/mitchellh/cli"
"github.com/shoenig/test/must"
Expand All @@ -31,3 +33,24 @@ func TestOperatorAutopilotStateCommand(t *testing.T) {
out := ui.OutputWriter.String()
must.StrContains(t, out, "Healthy")
}

func TestOperatorAutopilotStateCommand_JSON(t *testing.T) {
ci.Parallel(t)
s, _, addr := testServer(t, false, nil)
defer s.Shutdown()

ui := cli.NewMockUi()
c := &OperatorAutopilotHealthCommand{Meta: Meta{Ui: ui}}
args := []string{"-address=" + addr, "-json"}

code := c.Run(args)
must.Eq(t, 0, code, must.Sprintf("got error for exit code: %v", ui.ErrorWriter.String()))

// Attempt to unmarshal the data which tests that the output is JSON and
// peak into the data, checking that healthy is an expected and no-default
// value.
operatorHealthyReply := api.OperatorHealthReply{}

must.NoError(t, json.Unmarshal(ui.OutputWriter.Bytes(), &operatorHealthyReply))
must.True(t, operatorHealthyReply.Healthy)
}

0 comments on commit 86bc7ed

Please sign in to comment.