cli: Ensure JSON flag is respected in autopilot health command. (#24655)

This commit is contained in:
James Rasell
2024-12-12 13:43:32 +00:00
committed by GitHub
parent 63e2c6aaec
commit 86bc7ed224
3 changed files with 28 additions and 2 deletions

3
.changelog/24655.txt Normal file
View File

@@ -0,0 +1,3 @@
```release-note:bug
cli: Ensure the `operator autopilot health` command only outputs JSON when the `json` flag is supplied
```

View File

@@ -59,9 +59,9 @@ func (c *OperatorAutopilotHealthCommand) Run(args []string) int {
return 1
}
c.Ui.Output(string(bytes))
}
} else {
c.Ui.Output(formatAutopilotState(state))
}
return 0
}

View File

@@ -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"
@@ -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)
}