]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/common/test_json_formatter.cc
import quincy beta 17.1.0
[ceph.git] / ceph / src / test / common / test_json_formatter.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2018 Red Hat Inc.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 */
14
15 #include <errno.h>
16 #include <gtest/gtest.h>
17
18 #include "common/ceph_json.h"
19 #include "common/Clock.h"
20
21 #include <sstream>
22
23 using namespace std;
24
25
26 TEST(formatter, bug_37706) {
27 vector<std::string> pgs;
28
29 string outstring =
30 "{\"pg_ready\":true, \"pg_stats\":[ { \"pgid\":\"1.0\", \"version\":\"16'56\",\"reported_seq\":\"62\",\"reported_epoch\":\"20\",\"state\":\"active+clean+inconsistent\",\"last_fresh\":\"2018-12-18 15:21:22.173804\",\"last_change\":\"2018-12-18 15:21:22.173804\",\"last_active\":\"2018-12-18 15:21:22.173804\",\"last_peered\":\"2018-12-18 15:21:22.173804\",\"last_clean\":\"2018-12-18 15:21:22.173804\",\"last_became_active\":\"2018-12-18 15:21:21.347685\",\"last_became_peered\":\"2018-12-18 15:21:21.347685\",\"last_unstale\":\"2018-12-18 15:21:22.173804\",\"last_undegraded\":\"2018-12-18 15:21:22.173804\",\"last_fullsized\":\"2018-12-18 15:21:22.173804\",\"mapping_epoch\":19,\"log_start\":\"0'0\",\"ondisk_log_start\":\"0'0\",\"created\":7,\"last_epoch_clean\":20,\"parent\":\"0.0\",\"parent_split_bits\":0,\"last_scrub\":\"16'56\",\"last_scrub_stamp\":\"2018-12-18 15:21:22.173684\",\"last_deep_scrub\":\"0'0\",\"last_deep_scrub_stamp\":\"2018-12-18 15:21:06.514438\",\"last_clean_scrub_stamp\":\"2018-12-18 15:21:06.514438\",\"log_size\":56,\"ondisk_log_size\":56,\"stats_invalid\":false,\"dirty_stats_invalid\":false,\"omap_stats_invalid\":false,\"hitset_stats_invalid\":false,\"hitset_bytes_stats_invalid\":false,\"pin_stats_invalid\":false,\"manifest_stats_invalid\":false,\"snaptrimq_len\":0,\"stat_sum\":{\"num_bytes\":24448,\"num_objects\":36,\"num_object_clones\":20,\"num_object_copies\":36,\"num_objects_missing_on_primary\":0,\"num_objects_missing\":0,\"num_objects_degraded\":0,\"num_objects_misplaced\":0,\"num_objects_unfound\":0,\"num_objects_dirty\":36,\"num_whiteouts\":3,\"num_read\":0,\"num_read_kb\":0,\"num_write\":36,\"num_write_kb\":50,\"num_scrub_errors\":20,\"num_shallow_scrub_errors\":20,\"num_deep_scrub_errors\":0,\"num_objects_recovered\":0,\"num_bytes_recovered\":0,\"num_keys_recovered\":0,\"num_objects_omap\":0,\"num_objects_hit_set_archive\":0,\"num_bytes_hit_set_archive\":0,\"num_flush\":0,\"num_flush_kb\":0,\"num_evict\":0,\"num_evict_kb\":0,\"num_promote\":0,\"num_flush_mode_high\":0,\"num_flush_mode_low\":0,\"num_evict_mode_some\":0,\"num_evict_mode_full\":0,\"num_objects_pinned\":0,\"num_legacy_snapsets\":0,\"num_large_omap_objects\":0,\"num_objects_manifest\":0},\"up\":[0],\"acting\":[0],\"blocked_by\":[],\"up_primary\":0,\"acting_primary\":0,\"purged_snaps\":[] }]}";
31
32
33 JSONParser parser;
34 ASSERT_TRUE(parser.parse(outstring.c_str(), outstring.size()));
35
36 vector<string> v;
37
38 ASSERT_TRUE (!parser.is_array());
39
40 JSONObj *pgstat_obj = parser.find_obj("pg_stats");
41 ASSERT_TRUE (!!pgstat_obj);
42 auto s = pgstat_obj->get_data();
43
44 ASSERT_TRUE(!s.empty());
45 JSONParser pg_stats;
46 ASSERT_TRUE(pg_stats.parse(s.c_str(), s.length()));
47 v = pg_stats.get_array_elements();
48
49 for (auto i : v) {
50 JSONParser pg_json;
51 ASSERT_TRUE(pg_json.parse(i.c_str(), i.length()));
52 string pgid;
53 JSONDecoder::decode_json("pgid", pgid, &pg_json);
54 pgs.emplace_back(std::move(pgid));
55 }
56
57 ASSERT_EQ(pgs.back(), "1.0");
58 }
59
60 TEST(formatter, utime)
61 {
62 JSONFormatter formatter;
63
64 utime_t input = ceph_clock_now();
65 input.gmtime_nsec(formatter.dump_stream("timestamp"));
66
67 bufferlist bl;
68 formatter.flush(bl);
69
70 JSONParser parser;
71 EXPECT_TRUE(parser.parse(bl.c_str(), bl.length()));
72
73 cout << input << " -> '" << std::string(bl.c_str(), bl.length())
74 << std::endl;
75
76 utime_t output;
77 decode_json_obj(output, &parser);
78 cout << " -> " << output << std::endl;
79 EXPECT_EQ(input.sec(), output.sec());
80 EXPECT_EQ(input.nsec(), output.nsec());
81 }