2015-06-25 22:56:48 -04:00
|
|
|
/*
|
|
|
|
* Quick - Quick key value store for config files and persistent state files
|
|
|
|
*
|
2017-10-10 05:14:42 -04:00
|
|
|
* Quick (C) 2015, 2016, 2017 Minio, Inc.
|
2015-06-25 22:56:48 -04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-03-22 13:23:25 -04:00
|
|
|
package quick
|
2015-06-25 22:56:48 -04:00
|
|
|
|
|
|
|
import (
|
2017-10-10 05:14:42 -04:00
|
|
|
"bytes"
|
2016-08-08 23:56:29 -04:00
|
|
|
"encoding/json"
|
2017-03-03 13:22:09 -05:00
|
|
|
"io/ioutil"
|
2015-06-25 22:56:48 -04:00
|
|
|
"os"
|
2017-03-03 13:22:09 -05:00
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2015-06-25 22:56:48 -04:00
|
|
|
"testing"
|
2018-04-13 18:14:19 -04:00
|
|
|
|
|
|
|
"github.com/tidwall/gjson"
|
2015-06-25 22:56:48 -04:00
|
|
|
)
|
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
func TestReadVersion(t *testing.T) {
|
2017-05-06 13:16:59 -04:00
|
|
|
type myStruct struct {
|
|
|
|
Version string
|
|
|
|
}
|
|
|
|
saveMe := myStruct{"1"}
|
2018-04-23 14:05:56 -04:00
|
|
|
config, err := NewConfig(&saveMe, nil)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-06 13:16:59 -04:00
|
|
|
err = config.Save("test.json")
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-06 13:16:59 -04:00
|
|
|
|
2018-04-23 14:05:56 -04:00
|
|
|
version, err := GetVersion("test.json", nil)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if version != "1" {
|
|
|
|
t.Fatalf("Expected version '1', got '%v'", version)
|
|
|
|
}
|
2017-05-06 13:16:59 -04:00
|
|
|
}
|
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
func TestReadVersionErr(t *testing.T) {
|
2017-05-06 13:16:59 -04:00
|
|
|
type myStruct struct {
|
|
|
|
Version int
|
|
|
|
}
|
|
|
|
saveMe := myStruct{1}
|
2018-04-23 14:05:56 -04:00
|
|
|
_, err := NewConfig(&saveMe, nil)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unexpected should fail in initialization for bad input")
|
|
|
|
}
|
2017-05-06 13:16:59 -04:00
|
|
|
|
|
|
|
err = ioutil.WriteFile("test.json", []byte("{ \"version\":2,"), 0644)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-06 13:16:59 -04:00
|
|
|
|
2018-04-23 14:05:56 -04:00
|
|
|
_, err = GetVersion("test.json", nil)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unexpected should fail to fetch version")
|
|
|
|
}
|
2017-05-06 13:16:59 -04:00
|
|
|
|
|
|
|
err = ioutil.WriteFile("test.json", []byte("{ \"version\":2 }"), 0644)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-06 13:16:59 -04:00
|
|
|
|
2018-04-23 14:05:56 -04:00
|
|
|
_, err = GetVersion("test.json", nil)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unexpected should fail to fetch version")
|
|
|
|
}
|
2017-05-06 13:16:59 -04:00
|
|
|
}
|
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
func TestSaveFailOnDir(t *testing.T) {
|
|
|
|
defer os.RemoveAll("test-1.json")
|
|
|
|
err := os.MkdirAll("test-1.json", 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-01-14 21:17:24 -05:00
|
|
|
type myStruct struct {
|
|
|
|
Version string
|
|
|
|
}
|
|
|
|
saveMe := myStruct{"1"}
|
2018-04-23 14:05:56 -04:00
|
|
|
config, err := NewConfig(&saveMe, nil)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = config.Save("test-1.json")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unexpected should fail to save if test-1.json is a directory")
|
|
|
|
}
|
2016-01-14 21:17:24 -05:00
|
|
|
}
|
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
func TestCheckData(t *testing.T) {
|
2017-03-22 13:23:25 -04:00
|
|
|
err := checkData(nil)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unexpected should fail")
|
|
|
|
}
|
2015-06-25 22:56:48 -04:00
|
|
|
|
2016-08-06 13:22:14 -04:00
|
|
|
type myStructBadNoVersion struct {
|
2016-11-10 02:37:12 -05:00
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
Directories []string
|
2015-06-25 22:56:48 -04:00
|
|
|
}
|
2016-08-06 13:22:14 -04:00
|
|
|
saveMeBadNoVersion := myStructBadNoVersion{"guest", "nopassword", []string{"Work", "Documents", "Music"}}
|
2017-03-22 13:23:25 -04:00
|
|
|
err = checkData(&saveMeBadNoVersion)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unexpected should fail if Version is not set")
|
|
|
|
}
|
2016-08-06 13:22:14 -04:00
|
|
|
|
|
|
|
type myStructBadVersionInt struct {
|
|
|
|
Version int
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
saveMeBadVersionInt := myStructBadVersionInt{1, "guest", "nopassword"}
|
2017-03-22 13:23:25 -04:00
|
|
|
err = checkData(&saveMeBadVersionInt)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unexpected should fail if Version is integer")
|
|
|
|
}
|
2015-06-25 22:56:48 -04:00
|
|
|
|
|
|
|
type myStructGood struct {
|
2016-11-10 02:37:12 -05:00
|
|
|
Version string
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
Directories []string
|
2015-06-25 22:56:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
saveMeGood := myStructGood{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
|
2017-03-22 13:23:25 -04:00
|
|
|
err = checkData(&saveMeGood)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-25 22:56:48 -04:00
|
|
|
}
|
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
func TestLoadFile(t *testing.T) {
|
2016-08-08 23:56:29 -04:00
|
|
|
type myStruct struct {
|
2016-11-10 02:37:12 -05:00
|
|
|
Version string
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
Directories []string
|
2016-08-08 23:56:29 -04:00
|
|
|
}
|
|
|
|
saveMe := myStruct{}
|
2018-04-23 14:05:56 -04:00
|
|
|
_, err := LoadConfig("test.json", nil, &saveMe)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-08 23:56:29 -04:00
|
|
|
|
|
|
|
file, err := os.Create("test.json")
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err = file.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-04-23 14:05:56 -04:00
|
|
|
_, err = LoadConfig("test.json", nil, &saveMe)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unexpected should fail to load empty JSON")
|
|
|
|
}
|
2018-04-23 14:05:56 -04:00
|
|
|
config, err := NewConfig(&saveMe, nil)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-08 23:56:29 -04:00
|
|
|
err = config.Load("test-non-exist.json")
|
2017-10-10 05:14:42 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unexpected should fail to Load non-existent config")
|
|
|
|
}
|
|
|
|
|
2016-08-08 23:56:29 -04:00
|
|
|
err = config.Load("test.json")
|
2017-10-10 05:14:42 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unexpected should fail to load empty JSON")
|
|
|
|
}
|
2016-08-08 23:56:29 -04:00
|
|
|
|
|
|
|
saveMe = myStruct{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
|
2018-04-23 14:05:56 -04:00
|
|
|
config, err = NewConfig(&saveMe, nil)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-08 23:56:29 -04:00
|
|
|
err = config.Save("test.json")
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-08 23:56:29 -04:00
|
|
|
saveMe1 := myStruct{}
|
2018-04-23 14:05:56 -04:00
|
|
|
_, err = LoadConfig("test.json", nil, &saveMe1)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(saveMe1, saveMe) {
|
|
|
|
t.Fatalf("Expected %v, got %v", saveMe1, saveMe)
|
|
|
|
}
|
2016-08-08 23:56:29 -04:00
|
|
|
|
|
|
|
saveMe2 := myStruct{}
|
|
|
|
err = json.Unmarshal([]byte(config.String()), &saveMe2)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(saveMe2, saveMe1) {
|
|
|
|
t.Fatalf("Expected %v, got %v", saveMe2, saveMe1)
|
|
|
|
}
|
2016-08-08 23:56:29 -04:00
|
|
|
}
|
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
func TestYAMLFormat(t *testing.T) {
|
2017-03-03 13:22:09 -05:00
|
|
|
testYAML := "test.yaml"
|
|
|
|
defer os.RemoveAll(testYAML)
|
|
|
|
|
|
|
|
type myStruct struct {
|
|
|
|
Version string
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
Directories []string
|
|
|
|
}
|
|
|
|
|
|
|
|
plainYAML := `version: "1"
|
|
|
|
user: guest
|
|
|
|
password: nopassword
|
|
|
|
directories:
|
|
|
|
- Work
|
|
|
|
- Documents
|
|
|
|
- Music
|
|
|
|
`
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
plainYAML = strings.Replace(plainYAML, "\n", "\r\n", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
saveMe := myStruct{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
|
|
|
|
|
|
|
|
// Save format using
|
2018-04-23 14:05:56 -04:00
|
|
|
config, err := NewConfig(&saveMe, nil)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-03-03 13:22:09 -05:00
|
|
|
|
|
|
|
err = config.Save(testYAML)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-03-03 13:22:09 -05:00
|
|
|
|
|
|
|
// Check if the saved structure in actually an YAML format
|
2017-10-10 05:14:42 -04:00
|
|
|
b, err := ioutil.ReadFile(testYAML)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-03-03 13:22:09 -05:00
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
if !bytes.Equal([]byte(plainYAML), b) {
|
|
|
|
t.Fatalf("Expected %v, got %v", plainYAML, string(b))
|
|
|
|
}
|
2017-03-03 13:22:09 -05:00
|
|
|
|
|
|
|
// Check if the loaded data is the same as the saved one
|
|
|
|
loadMe := myStruct{}
|
2018-04-23 14:05:56 -04:00
|
|
|
config, err = NewConfig(&loadMe, nil)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-03-03 13:22:09 -05:00
|
|
|
err = config.Load(testYAML)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-03-03 13:22:09 -05:00
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
if !reflect.DeepEqual(saveMe, loadMe) {
|
|
|
|
t.Fatalf("Expected %v, got %v", saveMe, loadMe)
|
|
|
|
}
|
2017-03-03 13:22:09 -05:00
|
|
|
}
|
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
func TestJSONFormat(t *testing.T) {
|
2017-03-03 13:22:09 -05:00
|
|
|
testJSON := "test.json"
|
|
|
|
defer os.RemoveAll(testJSON)
|
|
|
|
|
|
|
|
type myStruct struct {
|
|
|
|
Version string
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
Directories []string
|
|
|
|
}
|
|
|
|
|
|
|
|
plainJSON := `{
|
|
|
|
"Version": "1",
|
|
|
|
"User": "guest",
|
|
|
|
"Password": "nopassword",
|
|
|
|
"Directories": [
|
|
|
|
"Work",
|
|
|
|
"Documents",
|
|
|
|
"Music"
|
|
|
|
]
|
|
|
|
}`
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
plainJSON = strings.Replace(plainJSON, "\n", "\r\n", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
saveMe := myStruct{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
|
|
|
|
|
|
|
|
// Save format using
|
2018-04-23 14:05:56 -04:00
|
|
|
config, err := NewConfig(&saveMe, nil)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-03-03 13:22:09 -05:00
|
|
|
|
|
|
|
err = config.Save(testJSON)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-03-03 13:22:09 -05:00
|
|
|
|
|
|
|
// Check if the saved structure in actually an JSON format
|
2017-10-10 05:14:42 -04:00
|
|
|
b, err := ioutil.ReadFile(testJSON)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-03-03 13:22:09 -05:00
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
if !bytes.Equal([]byte(plainJSON), b) {
|
|
|
|
t.Fatalf("Expected %v, got %v", plainJSON, string(b))
|
|
|
|
}
|
2017-03-03 13:22:09 -05:00
|
|
|
|
|
|
|
// Check if the loaded data is the same as the saved one
|
|
|
|
loadMe := myStruct{}
|
2018-04-23 14:05:56 -04:00
|
|
|
config, err = NewConfig(&loadMe, nil)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-03-03 13:22:09 -05:00
|
|
|
err = config.Load(testJSON)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-03-03 13:22:09 -05:00
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
if !reflect.DeepEqual(saveMe, loadMe) {
|
|
|
|
t.Fatalf("Expected %v, got %v", saveMe, loadMe)
|
|
|
|
}
|
2017-03-03 13:22:09 -05:00
|
|
|
}
|
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
func TestSaveLoad(t *testing.T) {
|
2015-06-25 22:56:48 -04:00
|
|
|
defer os.RemoveAll("test.json")
|
|
|
|
type myStruct struct {
|
2016-11-10 02:37:12 -05:00
|
|
|
Version string
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
Directories []string
|
2015-06-25 22:56:48 -04:00
|
|
|
}
|
|
|
|
saveMe := myStruct{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
|
2018-04-23 14:05:56 -04:00
|
|
|
config, err := NewConfig(&saveMe, nil)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-01-14 21:17:24 -05:00
|
|
|
err = config.Save("test.json")
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-01-14 21:17:24 -05:00
|
|
|
|
|
|
|
loadMe := myStruct{Version: "1"}
|
2018-04-23 14:05:56 -04:00
|
|
|
newConfig, err := NewConfig(&loadMe, nil)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-01-14 21:17:24 -05:00
|
|
|
err = newConfig.Load("test.json")
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-01-14 21:17:24 -05:00
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
if !reflect.DeepEqual(config.Data(), newConfig.Data()) {
|
|
|
|
t.Fatalf("Expected %v, got %v", config.Data(), newConfig.Data())
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(config.Data(), &loadMe) {
|
|
|
|
t.Fatalf("Expected %v, got %v", config.Data(), &loadMe)
|
|
|
|
}
|
2016-01-14 21:17:24 -05:00
|
|
|
|
|
|
|
mismatch := myStruct{"1.1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
|
2017-10-10 05:14:42 -04:00
|
|
|
if reflect.DeepEqual(config.Data(), &mismatch) {
|
|
|
|
t.Fatal("Expected to mismatch but succeeded instead")
|
|
|
|
}
|
2016-01-14 21:17:24 -05:00
|
|
|
}
|
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
func TestSaveBackup(t *testing.T) {
|
2016-01-14 21:17:24 -05:00
|
|
|
defer os.RemoveAll("test.json")
|
|
|
|
defer os.RemoveAll("test.json.old")
|
|
|
|
type myStruct struct {
|
2016-11-10 02:37:12 -05:00
|
|
|
Version string
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
Directories []string
|
2016-01-14 21:17:24 -05:00
|
|
|
}
|
|
|
|
saveMe := myStruct{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
|
2018-04-23 14:05:56 -04:00
|
|
|
config, err := NewConfig(&saveMe, nil)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-01-14 21:17:24 -05:00
|
|
|
err = config.Save("test.json")
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-25 22:56:48 -04:00
|
|
|
|
|
|
|
loadMe := myStruct{Version: "1"}
|
2018-04-23 14:05:56 -04:00
|
|
|
newConfig, err := NewConfig(&loadMe, nil)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-01-14 21:17:24 -05:00
|
|
|
err = newConfig.Load("test.json")
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-25 22:56:48 -04:00
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
if !reflect.DeepEqual(config.Data(), newConfig.Data()) {
|
|
|
|
t.Fatalf("Expected %v, got %v", config.Data(), newConfig.Data())
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(config.Data(), &loadMe) {
|
|
|
|
t.Fatalf("Expected %v, got %v", config.Data(), &loadMe)
|
|
|
|
}
|
2015-06-25 22:56:48 -04:00
|
|
|
|
|
|
|
mismatch := myStruct{"1.1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
|
2017-10-10 05:14:42 -04:00
|
|
|
if reflect.DeepEqual(newConfig.Data(), &mismatch) {
|
|
|
|
t.Fatal("Expected to mismatch but succeeded instead")
|
|
|
|
}
|
|
|
|
|
2018-04-23 14:05:56 -04:00
|
|
|
config, err = NewConfig(&mismatch, nil)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-01-14 21:17:24 -05:00
|
|
|
err = config.Save("test.json")
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-25 22:56:48 -04:00
|
|
|
}
|
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
func TestDiff(t *testing.T) {
|
2015-06-25 22:56:48 -04:00
|
|
|
type myStruct struct {
|
2016-11-10 02:37:12 -05:00
|
|
|
Version string
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
Directories []string
|
2015-06-25 22:56:48 -04:00
|
|
|
}
|
|
|
|
saveMe := myStruct{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
|
2018-04-23 14:05:56 -04:00
|
|
|
config, err := NewConfig(&saveMe, nil)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-25 22:56:48 -04:00
|
|
|
|
2018-04-23 14:05:56 -04:00
|
|
|
type myNewConfigStruct struct {
|
2015-06-25 22:56:48 -04:00
|
|
|
Version string
|
|
|
|
// User string
|
2016-11-10 02:37:12 -05:00
|
|
|
Password string
|
|
|
|
Directories []string
|
2015-06-25 22:56:48 -04:00
|
|
|
}
|
|
|
|
|
2018-04-23 14:05:56 -04:00
|
|
|
mismatch := myNewConfigStruct{"1", "nopassword", []string{"Work", "documents", "Music"}}
|
|
|
|
newConfig, err := NewConfig(&mismatch, nil)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-25 22:56:48 -04:00
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
fields, err := config.Diff(newConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(fields) != 1 {
|
|
|
|
t.Fatalf("Expected len 1, got %v", len(fields))
|
|
|
|
}
|
2015-06-25 22:56:48 -04:00
|
|
|
|
|
|
|
// Uncomment for debugging
|
|
|
|
// for i, field := range fields {
|
|
|
|
// fmt.Printf("Diff[%d]: %s=%v\n", i, field.Name(), field.Value())
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
func TestDeepDiff(t *testing.T) {
|
2015-06-25 22:56:48 -04:00
|
|
|
type myStruct struct {
|
2016-11-10 02:37:12 -05:00
|
|
|
Version string
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
Directories []string
|
2015-06-25 22:56:48 -04:00
|
|
|
}
|
|
|
|
saveMe := myStruct{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
|
2018-04-23 14:05:56 -04:00
|
|
|
config, err := NewConfig(&saveMe, nil)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-25 22:56:48 -04:00
|
|
|
|
|
|
|
mismatch := myStruct{"1", "Guest", "nopassword", []string{"Work", "documents", "Music"}}
|
2018-04-23 14:05:56 -04:00
|
|
|
newConfig, err := NewConfig(&mismatch, nil)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-25 22:56:48 -04:00
|
|
|
|
|
|
|
fields, err := config.DeepDiff(newConfig)
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(fields) != 2 {
|
|
|
|
t.Fatalf("Expected len 2, got %v", len(fields))
|
|
|
|
}
|
2015-06-25 22:56:48 -04:00
|
|
|
|
|
|
|
// Uncomment for debugging
|
|
|
|
// for i, field := range fields {
|
|
|
|
// fmt.Printf("DeepDiff[%d]: %s=%v\n", i, field.Name(), field.Value())
|
|
|
|
// }
|
|
|
|
}
|
2018-04-13 18:14:19 -04:00
|
|
|
|
|
|
|
func TestCheckDupJSONKeys(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
json string
|
|
|
|
shouldPass bool
|
|
|
|
}{
|
|
|
|
{`{}`, true},
|
|
|
|
{`{"version" : "13"}`, true},
|
|
|
|
{`{"version" : "13", "version": "14"}`, false},
|
|
|
|
{`{"version" : "13", "credential": {"accessKey": "12345"}}`, true},
|
|
|
|
{`{"version" : "13", "credential": {"accessKey": "12345", "accessKey":"12345"}}`, false},
|
|
|
|
{`{"version" : "13", "notify": {"amqp": {"1"}, "webhook":{"3"}}}`, true},
|
|
|
|
{`{"version" : "13", "notify": {"amqp": {"1"}, "amqp":{"3"}}}`, false},
|
|
|
|
{`{"version" : "13", "notify": {"amqp": {"1":{}, "2":{}}}}`, true},
|
|
|
|
{`{"version" : "13", "notify": {"amqp": {"1":{}, "1":{}}}}`, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
err := doCheckDupJSONKeys(gjson.Result{}, gjson.Parse(testCase.json))
|
|
|
|
if testCase.shouldPass && err != nil {
|
|
|
|
t.Errorf("Test %d, should pass but it failed with err = %v", i+1, err)
|
|
|
|
}
|
|
|
|
if !testCase.shouldPass && err == nil {
|
|
|
|
t.Errorf("Test %d, should fail but it succeed.", i+1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|