forked from rook/rook
ceph: parse json correctly
radosgw-admin may output logs before and after JSON when this command succeeds. We should get rid of these logs before unmarshaling output. Although the logs before JSON were treated in PR7354, the logs after JSON weren't. Signed-off-by: Satoru Takeuchi <satoru.takeuchi@gmail.com>
This commit is contained in:
@@ -60,7 +60,7 @@ func NewMultisiteContext(context *clusterd.Context, clusterInfo *client.ClusterI
|
||||
func extractJSON(output string) (string, error) {
|
||||
// `radosgw-admin` sometimes leaves logs to stderr even if it succeeds.
|
||||
// So we should skip them if parsing output as json.
|
||||
pattern := regexp.MustCompile("(?ms)^{.*")
|
||||
pattern := regexp.MustCompile(`(?ms)^{.*}$`)
|
||||
match := pattern.Find([]byte(output))
|
||||
if match == nil {
|
||||
return "", errors.Errorf("didn't contain json. %s", output)
|
||||
|
||||
@@ -45,4 +45,10 @@ func TestExtractJson(t *testing.T) {
|
||||
match, err = extractJSON(s)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, json.Valid([]byte(match)))
|
||||
|
||||
s = `{"test": "test"}
|
||||
this line can't be parsed as json`
|
||||
match, err = extractJSON(s)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, json.Valid([]byte(match)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user