AdGuardHome/internal/confmigrate/migrator_test.go

209 lines
5 KiB
Go
Raw Normal View History

Pull request 1991: AG-25392 confmigrate vol.2 Merge in DNS/adguard-home from AG-25392-confmigrate-vol.2 to master Squashed commit of the following: commit 7bcdf443135523022a7e5ed6b2da05fcd784a896 Merge: 4c25331b5 f84ff2bd0 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Sep 4 20:33:08 2023 +0300 Merge branch 'master' into AG-25392-confmigrate-vol.2 commit 4c25331b507078670e7f3505384b53179a210292 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Sep 4 20:14:23 2023 +0300 confmigrate: fix fmt commit d7dbfc46a7f825d248cb028d50b60eede941bf67 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Sep 4 20:07:25 2023 +0300 confmigrate: imp code commit cd29729a23d1782a0b5fbd1e9153fe61ff3dbc71 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Sep 4 18:21:14 2023 +0300 confmigrate: imp code, split files commit f075a14c251c68a13152709e14a326ad17c2f889 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Sep 4 17:19:47 2023 +0300 confmigrate: imp code commit 37335597e7870184528c1d868e2d5bd5525a74bb Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Sep 4 16:54:13 2023 +0300 confmigrate: imp code, docs commit 1df0ddcad99e5056b4989d17490ca4b44ae17838 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Sep 4 14:41:52 2023 +0300 all: add testdata commit e62690dbe8e458811bbd6b115b4449affa729560 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Aug 31 16:13:14 2023 +0300 all: imp code commit 0934d5974b31a58b31b12412c3cd1a50810ffc4f Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Aug 30 15:50:52 2023 +0300 home: don't reread config commit 7566ab6f330460bd608aae6dc0428a631e2fa4e3 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Aug 30 15:43:57 2023 +0300 all: imp code commit 6f6aae6290f8d3101f3678ecc705eee7030ad8d5 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Aug 30 15:25:46 2023 +0300 all: introduce confmigrate
2023-09-04 20:45:50 +03:00
package confmigrate_test
import (
"io/fs"
"os"
"path/filepath"
"testing"
"github.com/AdguardTeam/AdGuardHome/internal/confmigrate"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/bcrypt"
yaml "gopkg.in/yaml.v3"
)
// getField returns the value located at the given indexes in the given object.
// It fails the test if the value is not found or of the expected type. The
// indexes can be either strings or integers, and are interpreted as map keys or
// array indexes, respectively.
func getField[T any](t require.TestingT, obj any, indexes ...any) (val T) {
for _, index := range indexes {
switch index := index.(type) {
case string:
require.IsType(t, map[string]any(nil), obj)
typedObj := obj.(map[string]any)
require.Contains(t, typedObj, index)
obj = typedObj[index]
case int:
require.IsType(t, []any(nil), obj)
typedObj := obj.([]any)
require.Less(t, index, len(typedObj))
obj = typedObj[index]
default:
t.Errorf("unexpected index type: %T", index)
t.FailNow()
}
}
require.IsType(t, val, obj)
return obj.(T)
}
// testdata is a virtual filesystem containing test data.
var testdata = os.DirFS("testdata")
func TestMigrateConfig_Migrate(t *testing.T) {
const (
inputFileName = "input.yml"
outputFileName = "output.yml"
)
testCases := []struct {
yamlEqFunc func(t require.TestingT, expected, actual string, msgAndArgs ...any)
name string
targetVersion uint
}{{
yamlEqFunc: require.YAMLEq,
name: "v1",
targetVersion: 1,
}, {
yamlEqFunc: require.YAMLEq,
name: "v2",
targetVersion: 2,
}, {
yamlEqFunc: require.YAMLEq,
name: "v3",
targetVersion: 3,
}, {
yamlEqFunc: require.YAMLEq,
name: "v4",
targetVersion: 4,
}, {
// Compare passwords separately because bcrypt hashes those with a
// different salt every time.
yamlEqFunc: func(t require.TestingT, expected, actual string, msgAndArgs ...any) {
if h, ok := t.(interface{ Helper() }); ok {
h.Helper()
}
var want, got map[string]any
err := yaml.Unmarshal([]byte(expected), &want)
require.NoError(t, err)
err = yaml.Unmarshal([]byte(actual), &got)
require.NoError(t, err)
gotPass := getField[string](t, got, "users", 0, "password")
wantPass := getField[string](t, want, "users", 0, "password")
require.NoError(t, bcrypt.CompareHashAndPassword([]byte(gotPass), []byte(wantPass)))
delete(getField[map[string]any](t, got, "users", 0), "password")
delete(getField[map[string]any](t, want, "users", 0), "password")
require.Equal(t, want, got, msgAndArgs...)
},
name: "v5",
targetVersion: 5,
}, {
yamlEqFunc: require.YAMLEq,
name: "v6",
targetVersion: 6,
}, {
yamlEqFunc: require.YAMLEq,
name: "v7",
targetVersion: 7,
}, {
yamlEqFunc: require.YAMLEq,
name: "v8",
targetVersion: 8,
}, {
yamlEqFunc: require.YAMLEq,
name: "v9",
targetVersion: 9,
}, {
yamlEqFunc: require.YAMLEq,
name: "v10",
targetVersion: 10,
}, {
yamlEqFunc: require.YAMLEq,
name: "v11",
targetVersion: 11,
}, {
yamlEqFunc: require.YAMLEq,
name: "v12",
targetVersion: 12,
}, {
yamlEqFunc: require.YAMLEq,
name: "v13",
targetVersion: 13,
}, {
yamlEqFunc: require.YAMLEq,
name: "v14",
targetVersion: 14,
}, {
yamlEqFunc: require.YAMLEq,
name: "v15",
targetVersion: 15,
}, {
yamlEqFunc: require.YAMLEq,
name: "v16",
targetVersion: 16,
}, {
yamlEqFunc: require.YAMLEq,
name: "v17",
targetVersion: 17,
}, {
yamlEqFunc: require.YAMLEq,
name: "v18",
targetVersion: 18,
}, {
yamlEqFunc: require.YAMLEq,
name: "v19",
targetVersion: 19,
}, {
yamlEqFunc: require.YAMLEq,
name: "v20",
targetVersion: 20,
}, {
yamlEqFunc: require.YAMLEq,
name: "v21",
targetVersion: 21,
}, {
yamlEqFunc: require.YAMLEq,
name: "v22",
targetVersion: 22,
}, {
yamlEqFunc: require.YAMLEq,
name: "v23",
targetVersion: 23,
}, {
yamlEqFunc: require.YAMLEq,
name: "v24",
targetVersion: 24,
}, {
yamlEqFunc: require.YAMLEq,
name: "v25",
targetVersion: 25,
}, {
yamlEqFunc: require.YAMLEq,
name: "v26",
targetVersion: 26,
Pull request 1983: 5720-wildcard-ignored-domains Updates #5720. Squashed commit of the following: commit e8093c990f15e2efc496f1a04f87360825e34e96 Merge: df5413eef 28fefaff1 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Sep 5 15:06:33 2023 +0300 Merge branch 'master' into 5720-wildcard-ignored-domains commit df5413eefeac2c7e34eb725db9e2908b5b2d08cb Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Sep 5 14:49:05 2023 +0300 confmigrate: imp docs commit 1644d99b730cc7f22c9d75b8e990149d3ce5b32a Merge: 9542ee161 1e4517898 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Sep 5 14:23:42 2023 +0300 Merge branch 'master' into 5720-wildcard-ignored-domains commit 9542ee1616c1dd4bdb6ec9a2af79a2af3858a7e3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Sep 5 12:48:48 2023 +0300 all: upd chlog commit 183f84a7f73c7bd33669bd108076f60514ca101e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Sep 1 17:11:31 2023 +0300 all: imp chlog commit a704325352a577a9b6652f011b82180ec3a6e095 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Aug 31 18:59:52 2023 +0300 all: imp code commit fe99c3b883500850399b1feb72c914ab878b3107 Merge: 7f11e9460 0182b9ec1 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Aug 31 18:43:09 2023 +0300 Merge branch 'master' into 5720-wildcard-ignored-domains commit 7f11e94609027ed821a125d27a1ffde03f37334a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Aug 30 19:57:51 2023 +0300 aghnet: add tests commit f10f9190ce1064a5d03155e8b6bba61db977897b Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Aug 30 18:32:07 2023 +0300 all: add conf migration commit a53c14df129765366966c5230dd53aa29bdd25c5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Aug 30 13:37:30 2023 +0300 all: add ignore engine
2023-09-05 15:13:35 +03:00
}, {
yamlEqFunc: require.YAMLEq,
name: "v27",
targetVersion: 27,
Pull request 1991: AG-25392 confmigrate vol.2 Merge in DNS/adguard-home from AG-25392-confmigrate-vol.2 to master Squashed commit of the following: commit 7bcdf443135523022a7e5ed6b2da05fcd784a896 Merge: 4c25331b5 f84ff2bd0 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Sep 4 20:33:08 2023 +0300 Merge branch 'master' into AG-25392-confmigrate-vol.2 commit 4c25331b507078670e7f3505384b53179a210292 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Sep 4 20:14:23 2023 +0300 confmigrate: fix fmt commit d7dbfc46a7f825d248cb028d50b60eede941bf67 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Sep 4 20:07:25 2023 +0300 confmigrate: imp code commit cd29729a23d1782a0b5fbd1e9153fe61ff3dbc71 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Sep 4 18:21:14 2023 +0300 confmigrate: imp code, split files commit f075a14c251c68a13152709e14a326ad17c2f889 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Sep 4 17:19:47 2023 +0300 confmigrate: imp code commit 37335597e7870184528c1d868e2d5bd5525a74bb Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Sep 4 16:54:13 2023 +0300 confmigrate: imp code, docs commit 1df0ddcad99e5056b4989d17490ca4b44ae17838 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Sep 4 14:41:52 2023 +0300 all: add testdata commit e62690dbe8e458811bbd6b115b4449affa729560 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Aug 31 16:13:14 2023 +0300 all: imp code commit 0934d5974b31a58b31b12412c3cd1a50810ffc4f Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Aug 30 15:50:52 2023 +0300 home: don't reread config commit 7566ab6f330460bd608aae6dc0428a631e2fa4e3 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Aug 30 15:43:57 2023 +0300 all: imp code commit 6f6aae6290f8d3101f3678ecc705eee7030ad8d5 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Aug 30 15:25:46 2023 +0300 all: introduce confmigrate
2023-09-04 20:45:50 +03:00
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
body, err := fs.ReadFile(testdata, filepath.Join(t.Name(), inputFileName))
require.NoError(t, err)
wantBody, err := fs.ReadFile(testdata, filepath.Join(t.Name(), outputFileName))
require.NoError(t, err)
migrator := confmigrate.New(&confmigrate.Config{
WorkingDir: t.Name(),
})
newBody, upgraded, err := migrator.Migrate(body, tc.targetVersion)
require.NoError(t, err)
require.True(t, upgraded)
tc.yamlEqFunc(t, string(wantBody), string(newBody))
})
}
}