github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/pkg/utils/json_test.go (about) 1 package utils 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "reflect" 7 8 . "github.com/pingcap/check" 9 ) 10 11 type testJSONSuite struct{} 12 13 var _ = Suite(&testJSONSuite{}) 14 15 var testMetaJSONs = [][]byte{ 16 []byte(`{ 17 "files": [ 18 { 19 "sha256": "aa5cefba077644dbb2aa1d7fae2a0f879b56411195ad62d18caaf4ec76fae48f", 20 "start_key": "7480000000000000365f720000000000000000", 21 "end_key": "7480000000000000365f72ffffffffffffffff00", 22 "name": "1_2_29_6e97c3b17c657c4413724f614a619f5b665b990187b159e7d2b92552076144b6_1617351201040_write.sst", 23 "end_version": 423978913229963260, 24 "crc64xor": 8093018294706077000, 25 "total_kvs": 1, 26 "total_bytes": 27, 27 "cf": "write", 28 "size": 1423 29 } 30 ], 31 "schemas": [ 32 { 33 "table": { 34 "Lock": null, 35 "ShardRowIDBits": 0, 36 "auto_id_cache": 0, 37 "auto_inc_id": 0, 38 "auto_rand_id": 0, 39 "auto_random_bits": 0, 40 "charset": "utf8mb4", 41 "collate": "utf8mb4_bin", 42 "cols": [ 43 { 44 "change_state_info": null, 45 "comment": "", 46 "default": null, 47 "default_bit": null, 48 "default_is_expr": false, 49 "dependences": null, 50 "generated_expr_string": "", 51 "generated_stored": false, 52 "hidden": false, 53 "id": 1, 54 "name": { 55 "L": "pk", 56 "O": "pk" 57 }, 58 "offset": 0, 59 "origin_default": null, 60 "origin_default_bit": null, 61 "state": 5, 62 "type": { 63 "Charset": "utf8mb4", 64 "Collate": "utf8mb4_bin", 65 "Decimal": 0, 66 "Elems": null, 67 "Flag": 4099, 68 "Flen": 256, 69 "Tp": 15 70 }, 71 "version": 2 72 } 73 ], 74 "comment": "", 75 "common_handle_version": 1, 76 "compression": "", 77 "constraint_info": null, 78 "fk_info": null, 79 "id": 54, 80 "index_info": [ 81 { 82 "comment": "", 83 "id": 1, 84 "idx_cols": [ 85 { 86 "length": -1, 87 "name": { 88 "L": "pk", 89 "O": "pk" 90 }, 91 "offset": 0 92 } 93 ], 94 "idx_name": { 95 "L": "primary", 96 "O": "PRIMARY" 97 }, 98 "index_type": 1, 99 "is_global": false, 100 "is_invisible": false, 101 "is_primary": true, 102 "is_unique": true, 103 "state": 5, 104 "tbl_name": { 105 "L": "", 106 "O": "" 107 } 108 } 109 ], 110 "is_columnar": false, 111 "is_common_handle": true, 112 "max_col_id": 1, 113 "max_cst_id": 0, 114 "max_idx_id": 1, 115 "max_shard_row_id_bits": 0, 116 "name": { 117 "L": "test", 118 "O": "test" 119 }, 120 "partition": null, 121 "pk_is_handle": false, 122 "pre_split_regions": 0, 123 "sequence": null, 124 "state": 5, 125 "tiflash_replica": null, 126 "update_timestamp": 423978913176223740, 127 "version": 4, 128 "view": null 129 }, 130 "db": { 131 "charset": "utf8mb4", 132 "collate": "utf8mb4_bin", 133 "db_name": { 134 "L": "test", 135 "O": "test" 136 }, 137 "id": 1, 138 "state": 5 139 }, 140 "crc64xor": 8093018294706077000, 141 "total_kvs": 1, 142 "total_bytes": 27 143 } 144 ], 145 "ddls": [], 146 "cluster_id": 6946469498797568000, 147 "cluster_version": "\"5.0.0-rc.x\"\n", 148 "end_version": 423978913229963260, 149 "br_version": "BR\nRelease Version: v5.0.0-master\nGit Commit Hash: c0d60dae4998cf9ac40f02e5444731c15f0b2522\nGit Branch: HEAD\nGo Version: go1.13.4\nUTC Build Time: 2021-03-25 08:10:08\nRace Enabled: false" 150 } 151 `), 152 []byte(`{ 153 "files": [ 154 { 155 "sha256": "5759c4c73789d6ecbd771b374d42e72a309245d31911efc8553423303c95f22c", 156 "end_key": "7480000000000000ff0500000000000000f8", 157 "name": "1_4_2_default.sst", 158 "total_kvs": 153, 159 "total_bytes": 824218, 160 "cf": "default", 161 "size": 44931 162 }, 163 { 164 "sha256": "87597535ce0edbc9a9ef124777ad1d23388467e60c0409309ad33af505c1ea5b", 165 "start_key": "7480000000000000ff0f00000000000000f8", 166 "end_key": "7480000000000000ff1100000000000000f8", 167 "name": "1_16_8_58be9b5dfa92efb6a7de2127c196e03c5ddc3dd8ff3a9b3e7cd4c4aa7c969747_1617689203876_default.sst", 168 "total_kvs": 1, 169 "total_bytes": 396, 170 "cf": "default", 171 "size": 1350 172 }, 173 { 174 "sha256": "97bd1b07f9cc218df089c70d454e23c694113fae63a226ae0433165a9c3d75d9", 175 "start_key": "7480000000000000ff1700000000000000f8", 176 "end_key": "7480000000000000ff1900000000000000f8", 177 "name": "1_24_12_72fa67937dd58d654197abadeb9e633d92ebccc5fd993a8e54819a1bd7f81a8c_1617689203853_default.sst", 178 "total_kvs": 35, 179 "total_bytes": 761167, 180 "cf": "default", 181 "size": 244471 182 }, 183 { 184 "sha256": "6dcb6ba2ff11f4e7db349effc98210ba372bebbf2470e6cd600ed5f2294330e7", 185 "start_key": "7480000000000000ff3100000000000000f8", 186 "end_key": "7480000000000000ff3300000000000000f8", 187 "name": "1_50_25_2f1abd76c185ec355039f5b4a64f04637af91f80e6cb05099601ec6b9b1910e8_1617689203867_default.sst", 188 "total_kvs": 22, 189 "total_bytes": 1438283, 190 "cf": "default", 191 "size": 1284851 192 }, 193 { 194 "sha256": "ba603af7ecb2e21c8f145d995ae85eea3625480cd8186d4cffb53ab1974d8679", 195 "start_key": "7480000000000000ff385f72ffffffffffffffffff0000000000fb", 196 "name": "1_2_33_07b745c3d5a614ed6cc1cf21723b161fcb3e8e7d537546839afd82a4f392988c_1617689203895_default.sst", 197 "total_kvs": 260000, 198 "total_bytes": 114425025, 199 "cf": "default", 200 "size": 66048845 201 } 202 ], 203 "raw_ranges": [ 204 { 205 "cf": "default" 206 } 207 ], 208 "cluster_id": 6946469498797568000, 209 "cluster_version": "\"5.0.0-rc.x\"\n", 210 "is_raw_kv": true, 211 "br_version": "BR\nRelease Version: v5.0.0-master\nGit Commit Hash: c0d60dae4998cf9ac40f02e5444731c15f0b2522\nGit Branch: HEAD\nGo Version: go1.13.4\nUTC Build Time: 2021-03-25 08:10:08\nRace Enabled: false" 212 }`), 213 } 214 215 type jsonEquals struct{} 216 217 func (j jsonEquals) Info() *CheckerInfo { 218 return &CheckerInfo{ 219 Name: "JSONEquals", 220 Params: []string{"lhs", "rhs"}, 221 } 222 } 223 224 func (j jsonEquals) Check(params []interface{}, _ []string) (result bool, error string) { 225 lhs := params[0].([]byte) 226 rhs := params[1].([]byte) 227 228 lhsMap := map[string]interface{}{} 229 rhsMap := map[string]interface{}{} 230 231 if err := json.Unmarshal(lhs, &lhsMap); err != nil { 232 return false, fmt.Sprintf("failed to unmarshal lhs: %s", error) 233 } 234 if err := json.Unmarshal(rhs, &rhsMap); err != nil { 235 return false, fmt.Sprintf("failed to unmarshal rhs: %s", error) 236 } 237 if !reflect.DeepEqual(lhsMap, rhsMap) { 238 return false, fmt.Sprintf("lhs %s not equals rhs %s", lhsMap, rhsMap) 239 } 240 return true, "" 241 } 242 243 func (testJSONSuite) TestEncodeAndDecode(c *C) { 244 for _, testMetaJSON := range testMetaJSONs { 245 meta, err := UnmarshalBackupMeta(testMetaJSON) 246 c.Assert(err, IsNil) 247 metaJSON, err := MarshalBackupMeta(meta) 248 c.Assert(err, IsNil) 249 c.Assert(metaJSON, jsonEquals{}, testMetaJSON) 250 } 251 }