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:
Satoru Takeuchi
2021-05-27 04:46:13 +00:00
parent 3eb758a174
commit ca9aa1a11c
2 changed files with 7 additions and 1 deletions
+1 -1
View File
@@ -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)
+6
View File
@@ -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)))
}