1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-29 23:36:25 -04:00
v2fly/infra/conf/merge/merge_test.go
Jebbs ff59bd37ce
v5: New multi-json loader (#451)
* scalable commands column

* new multi-json loader
For both internal & external json loader

This commit also:
* applies -confdir to other formats, e.g. "yaml" in the future
* multiple assign of -confdir is accepted
* add flag to load confdir recursively
* config loader can have alias name
* json loader also accepts .jsonc
* add merge command
* add help topics for json merge, format loader
* format loaders don't panic

* apply lint style

* add merge test

* merge same tag in array, solve v2fly/discussion#97

* apply lint style

* merge code optimize

* fix merge cmdarg.Arg

* update cmd description

* improve merge logic
* fix zero value overwrite
* fix "null" lost after array merge

* code optimize

* fix merged slices not sorted

* code optimize

* add package doc

* fix a typo
2020-11-28 22:06:03 +08:00

207 lines
3.7 KiB
Go

// Copyright 2020 Jebbs. All rights reserved.
// Use of this source code is governed by MIT
// license that can be found in the LICENSE file.
package merge_test
import (
"encoding/json"
"reflect"
"strings"
"testing"
"v2ray.com/core/infra/conf/merge"
"v2ray.com/core/infra/conf/serial"
)
func TestMergeV2Style(t *testing.T) {
json1 := `
{
"log": {"access": "some_value", "loglevel": "debug"},
"inbounds": [{"tag": "in-1"}],
"outbounds": [{"_priority": 100, "tag": "out-1"}],
"routing": {"rules": [
{"_tag":"default_route","inboundTag":["in-1"],"outboundTag":"out-1"}
]}
}
`
json2 := `
{
"log": {"loglevel": "error"},
"inbounds": [{"tag": "in-2"}],
"outbounds": [{"_priority": -100, "tag": "out-2"}],
"routing": {"rules": [
{"inboundTag":["in-2"],"outboundTag":"out-2"},
{"_tag":"default_route","inboundTag":["in-1.1"],"outboundTag":"out-1.1"}
]}
}
`
expected := `
{
"log": {"access": "some_value", "loglevel": "error"},
"inbounds": [{"tag": "in-1"},{"tag": "in-2"}],
"outbounds": [
{"tag": "out-2"},
{"tag": "out-1"}
],
"routing": {"rules": [
{"inboundTag":["in-1","in-1.1"],"outboundTag":"out-1.1"},
{"inboundTag":["in-2"],"outboundTag":"out-2"}
]}
}
`
m, err := merge.BytesToMap([][]byte{[]byte(json1), []byte(json2)})
if err != nil {
t.Error(err)
}
assertResult(t, m, expected)
}
func TestMergeTag(t *testing.T) {
json1 := `
{
"routing": {
"rules": [{
"tag":"1",
"inboundTag": ["in-1"],
"outboundTag": "out-1"
}]
}
}
`
json2 := `
{
"routing": {
"rules": [{
"_tag":"1",
"inboundTag": ["in-2"],
"outboundTag": "out-2"
}]
}
}
`
expected := `
{
"routing": {
"rules": [{
"tag":"1",
"inboundTag": ["in-1", "in-2"],
"outboundTag": "out-2"
}]
}
}
`
m, err := merge.BytesToMap([][]byte{[]byte(json1), []byte(json2)})
if err != nil {
t.Error(err)
}
assertResult(t, m, expected)
}
func TestMergeTagValueTypes(t *testing.T) {
json1 := `
{
"array_1": [{
"_tag":"1",
"array_2": [{
"_tag":"2",
"array_3.1": ["string",true,false],
"array_3.2": [1,2,3],
"number_1": 1,
"number_2": 1,
"bool_1": true,
"bool_2": true
}]
}]
}
`
json2 := `
{
"array_1": [{
"_tag":"1",
"array_2": [{
"_tag":"2",
"array_3.1": [0,1,null],
"array_3.2": null,
"number_1": 0,
"number_2": 1,
"bool_1": true,
"bool_2": false,
"null_1": null
}]
}]
}
`
expected := `
{
"array_1": [{
"array_2": [{
"array_3.1": ["string",true,false,0,1,null],
"array_3.2": [1,2,3],
"number_1": 0,
"number_2": 1,
"bool_1": true,
"bool_2": false,
"null_1": null
}]
}]
}
`
m, err := merge.BytesToMap([][]byte{[]byte(json1), []byte(json2)})
if err != nil {
t.Error(err)
}
assertResult(t, m, expected)
}
func TestMergeTagDeep(t *testing.T) {
json1 := `
{
"array_1": [{
"_tag":"1",
"array_2": [{
"_tag":"2",
"array_3": [true,false,"string"]
}]
}]
}
`
json2 := `
{
"array_1": [{
"_tag":"1",
"array_2": [{
"_tag":"2",
"_priority":-100,
"array_3": [0,1,null]
}]
}]
}
`
expected := `
{
"array_1": [{
"array_2": [{
"array_3": [0,1,null,true,false,"string"]
}]
}]
}
`
m, err := merge.BytesToMap([][]byte{[]byte(json1), []byte(json2)})
if err != nil {
t.Error(err)
}
assertResult(t, m, expected)
}
func assertResult(t *testing.T, value map[string]interface{}, expected string) {
e := make(map[string]interface{})
err := serial.DecodeJSON(strings.NewReader(expected), &e)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(value, e) {
bs, _ := json.Marshal(value)
t.Fatalf("expected:\n%s\n\nactual:\n%s", expected, string(bs))
}
}