2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-06-25 22:56:48 -04:00
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
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) {
|
2018-08-15 00:41:47 -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"}}
|
2018-08-15 00:41:47 -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"}
|
2018-08-15 00:41:47 -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"}}
|
2018-08-15 00:41:47 -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())
|
|
|
|
// }
|
|
|
|
}
|