fix (cli): return error msg if acl policy not found (#16485)

* fix: return error msg if acl policy not found

* changelog

* add test
This commit is contained in:
cskh 2023-03-01 14:50:03 -05:00 committed by GitHub
parent e4241fa47d
commit 452ec19a42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 0 deletions

3
.changelog/16485.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
cli: fix panic read non-existent acl policy
```

View File

@ -92,6 +92,11 @@ func (c *cmd) Run(args []string) int {
return 1 return 1
} }
if pol == nil {
c.UI.Error(fmt.Sprintf("Error policy not found: %s", c.policyName))
return 1
}
formatter, err := policy.NewFormatter(c.format, c.showMeta) formatter, err := policy.NewFormatter(c.format, c.showMeta)
if err != nil { if err != nil {
c.UI.Error(err.Error()) c.UI.Error(err.Error())

View File

@ -82,6 +82,17 @@ func TestPolicyReadCommand(t *testing.T) {
output = ui.OutputWriter.String() output = ui.OutputWriter.String()
assert.Contains(t, output, fmt.Sprintf("test-policy")) assert.Contains(t, output, fmt.Sprintf("test-policy"))
assert.Contains(t, output, policy.ID) assert.Contains(t, output, policy.ID)
// Test querying non-existent policy
argsName = []string{
"-http-addr=" + a.HTTPAddr(),
"-token=root",
"-name=test-policy-not-exist",
}
cmd = New(ui)
code = cmd.Run(argsName)
assert.Equal(t, code, 1)
} }
func TestPolicyReadCommand_JSON(t *testing.T) { func TestPolicyReadCommand_JSON(t *testing.T) {