mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-23 05:25:35 +03:00
[change] upgrade_test: rework tests
This commit is contained in:
parent
d318545913
commit
d9d641941c
1 changed files with 117 additions and 70 deletions
187
upgrade_test.go
187
upgrade_test.go
|
@ -6,144 +6,191 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUpgrade1to2(t *testing.T) {
|
func TestUpgrade1to2(t *testing.T) {
|
||||||
// Let's create test config
|
// let's create test config for 1 schema version
|
||||||
diskConfig := createTestDiskConfig(1)
|
diskConfig := createTestDiskConfig(1)
|
||||||
oldDNSConfig := createTestDNSConfig(1)
|
|
||||||
|
|
||||||
|
// update config
|
||||||
err := upgradeSchema1to2(&diskConfig)
|
err := upgradeSchema1to2(&diskConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Can't upgrade schema version from 1 to 2")
|
t.Fatalf("Can't upgrade schema version from 1 to 2")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ensure that schema version was bumped
|
||||||
|
compareSchemaVersion(t, diskConfig["schema_version"], 2)
|
||||||
|
|
||||||
|
// old coredns entry should be removed
|
||||||
_, ok := diskConfig["coredns"]
|
_, ok := diskConfig["coredns"]
|
||||||
if ok {
|
if ok {
|
||||||
t.Fatalf("Core DNS config was not removed after upgrade schema version from 1 to 2")
|
t.Fatalf("Core DNS config was not removed after upgrade schema version from 1 to 2")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pull out new dns config
|
||||||
dnsMap, ok := diskConfig["dns"]
|
dnsMap, ok := diskConfig["dns"]
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatalf("No DNS config after upgrade schema version from 1 to 2")
|
t.Fatalf("No DNS config after upgrade schema version from 1 to 2")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cast dns configuration to map
|
// cast dns configurations to maps and compare them
|
||||||
newDNSConfig := make(map[string]interface{})
|
oldDNSConfig := castInterfaceToMap(t, createTestDNSConfig(1))
|
||||||
switch v := dnsMap.(type) {
|
newDNSConfig := castInterfaceToMap(t, dnsMap)
|
||||||
case map[interface{}]interface{}:
|
compareConfigs(t, &oldDNSConfig, &newDNSConfig)
|
||||||
if len(oldDNSConfig) != len(v) {
|
|
||||||
t.Fatalf("We loose some data")
|
|
||||||
}
|
|
||||||
for key, value := range v {
|
|
||||||
newDNSConfig[fmt.Sprint(key)] = value
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
t.Fatalf("DNS configuration is not a map")
|
|
||||||
}
|
|
||||||
|
|
||||||
_, v, err := compareConfigs(oldDNSConfig, newDNSConfig)
|
// exclude dns config and schema version from disk config comparison
|
||||||
if err != nil {
|
oldExcludedEntries := []string{"coredns", "schema_version"}
|
||||||
t.Fatalf("Wrong data %s, %s", v, err)
|
newExcludedEntries := []string{"dns", "schema_version"}
|
||||||
}
|
oldDiskConfig := createTestDiskConfig(1)
|
||||||
|
compareConfigsWithoutEntries(t, &oldDiskConfig, &diskConfig, oldExcludedEntries, newExcludedEntries)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpgrade2to3(t *testing.T) {
|
func TestUpgrade2to3(t *testing.T) {
|
||||||
// Let's create test config
|
// let's create test config
|
||||||
diskConfig := createTestDiskConfig(2)
|
diskConfig := createTestDiskConfig(2)
|
||||||
oldDNSConfig := createTestDNSConfig(2)
|
|
||||||
|
|
||||||
// Upgrade schema from 2 to 3
|
// upgrade schema from 2 to 3
|
||||||
err := upgradeSchema2to3(&diskConfig)
|
err := upgradeSchema2to3(&diskConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Can't update schema version from 2 to 3: %s", err)
|
t.Fatalf("Can't update schema version from 2 to 3: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check new schema version
|
// check new schema version
|
||||||
newSchemaVersion := diskConfig["schema_version"]
|
compareSchemaVersion(t, diskConfig["schema_version"], 3)
|
||||||
switch v := newSchemaVersion.(type) {
|
|
||||||
case int:
|
|
||||||
if v != 3 {
|
|
||||||
t.Fatalf("Wrong schema version in new config file")
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
t.Fatalf("Schema version is not an integer after update")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Let's get new dns configuration
|
// pull out new dns configuration
|
||||||
dnsMap, ok := diskConfig["dns"]
|
dnsMap, ok := diskConfig["dns"]
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatalf("No dns config in new configuration")
|
t.Fatalf("No dns config in new configuration")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cast dns configuration to map
|
// cast dns configuration to map
|
||||||
newDNSConfig := make(map[string]interface{})
|
newDNSConfig := castInterfaceToMap(t, dnsMap)
|
||||||
switch v := dnsMap.(type) {
|
|
||||||
case map[string]interface{}:
|
|
||||||
for key, value := range v {
|
|
||||||
newDNSConfig[fmt.Sprint(key)] = value
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
t.Fatalf("DNS configuration is not a map")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if bootstrap DNS becomes an array
|
// check if bootstrap DNS becomes an array
|
||||||
bootstrapDNS := newDNSConfig["bootstrap_dns"]
|
bootstrapDNS := newDNSConfig["bootstrap_dns"]
|
||||||
switch v := bootstrapDNS.(type) {
|
switch v := bootstrapDNS.(type) {
|
||||||
case []string:
|
case []string:
|
||||||
if len(v) != 1 {
|
if len(v) != 1 {
|
||||||
t.Fatalf("Wrong count of bootsrap DNS servers")
|
t.Fatalf("Wrong count of bootsrap DNS servers: %d", len(v))
|
||||||
}
|
}
|
||||||
|
|
||||||
if v[0] != "8.8.8.8:53" {
|
if v[0] != "8.8.8.8:53" {
|
||||||
t.Fatalf("Wrong bootsrap DNS servers")
|
t.Fatalf("Bootsrap DNS server is not 8.8.8.8:53 : %s", v[0])
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
t.Fatalf("Wrong type for bootsrap DNS")
|
t.Fatalf("Wrong type for bootsrap DNS: %T", v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set old value for bootstrap_dns and compare old and new configurations
|
// exclude bootstrap DNS from DNS configs comparison
|
||||||
newDNSConfig["bootstrap_dns"] = "8.8.8.8:53"
|
excludedEntries := []string{"bootstrap_dns"}
|
||||||
_, v, err := compareConfigs(oldDNSConfig, newDNSConfig)
|
oldDNSConfig := castInterfaceToMap(t, createTestDNSConfig(2))
|
||||||
if err != nil {
|
compareConfigsWithoutEntries(t, &oldDNSConfig, &newDNSConfig, excludedEntries, excludedEntries)
|
||||||
t.Fatalf("%s value is wrong: %s", v, err)
|
|
||||||
}
|
// excluded dns config and schema version from disk config comparison
|
||||||
|
excludedEntries = []string{"dns", "schema_version"}
|
||||||
|
oldDiskConfig := createTestDiskConfig(2)
|
||||||
|
compareConfigsWithoutEntries(t, &oldDiskConfig, &diskConfig, excludedEntries, excludedEntries)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO add comparation for all possible types
|
func castInterfaceToMap(t *testing.T, oldConfig interface{}) (newConfig map[string]interface{}) {
|
||||||
func compareConfigs(oldConfig map[interface{}]interface{}, newConfig map[string]interface{}) (string, interface{}, error) {
|
newConfig = make(map[string]interface{})
|
||||||
oldConfigCasted := make(map[string]interface{})
|
switch v := oldConfig.(type) {
|
||||||
for k, v := range oldConfig {
|
case map[interface{}]interface{}:
|
||||||
oldConfigCasted[fmt.Sprint(k)] = v
|
for key, value := range v {
|
||||||
|
newConfig[fmt.Sprint(key)] = value
|
||||||
|
}
|
||||||
|
case map[string]interface{}:
|
||||||
|
for key, value := range v {
|
||||||
|
newConfig[key] = value
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
t.Fatalf("DNS configuration is not a map")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// compareConfigsWithoutEntry removes entries from configs and returns result of compareConfigs
|
||||||
|
func compareConfigsWithoutEntries(t *testing.T, oldConfig, newConfig *map[string]interface{}, oldKey, newKey []string) {
|
||||||
|
for _, k := range oldKey {
|
||||||
|
delete(*oldConfig, k)
|
||||||
|
}
|
||||||
|
for _, k := range newKey {
|
||||||
|
delete(*newConfig, k)
|
||||||
|
}
|
||||||
|
compareConfigs(t, oldConfig, newConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
// compares configs before and after schema upgrade
|
||||||
|
func compareConfigs(t *testing.T, oldConfig, newConfig *map[string]interface{}) {
|
||||||
|
if len(*oldConfig) != len(*newConfig) {
|
||||||
|
t.Fatalf("wrong config entries count! Before upgrade: %d; After upgrade: %d", len(*oldConfig), len(*oldConfig))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check old data and new data
|
// Check old and new entries
|
||||||
for k, v := range newConfig {
|
for k, v := range *newConfig {
|
||||||
switch value := v.(type) {
|
switch value := v.(type) {
|
||||||
case string:
|
case string:
|
||||||
|
if value != (*oldConfig)[k] {
|
||||||
|
t.Fatalf("wrong value for string %s. Before update: %s; After update: %s", k, (*oldConfig)[k], value)
|
||||||
|
}
|
||||||
case int:
|
case int:
|
||||||
if value != oldConfigCasted[k] {
|
if value != (*oldConfig)[k] {
|
||||||
|
t.Fatalf("wrong value for int %s. Before update: %d; After update: %d", k, (*oldConfig)[k], value)
|
||||||
}
|
}
|
||||||
case []string:
|
case []string:
|
||||||
for i, s := range value {
|
for i, line := range value {
|
||||||
if oldConfigCasted[k].([]string)[i] != s {
|
if len((*oldConfig)[k].([]string)) != len(value) {
|
||||||
return k, v, fmt.Errorf("wrong data for %s", k)
|
t.Fatalf("wrong array length for %s. Before update: %d; After update: %d", k, len((*oldConfig)[k].([]string)), len(value))
|
||||||
|
}
|
||||||
|
if (*oldConfig)[k].([]string)[i] != line {
|
||||||
|
t.Fatalf("wrong data for string array %s. Before update: %s; After update: %s", k, (*oldConfig)[k].([]string)[i], line)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case bool:
|
case bool:
|
||||||
if v != oldConfigCasted[k].(bool) {
|
if v != (*oldConfig)[k].(bool) {
|
||||||
return k, v, fmt.Errorf("wrong data for %s", k)
|
t.Fatalf("wrong boolean value for %s", k)
|
||||||
|
}
|
||||||
|
case []filter:
|
||||||
|
if len((*oldConfig)[k].([]filter)) != len(value) {
|
||||||
|
t.Fatalf("wrong filters count. Before update: %d; After update: %d", len((*oldConfig)[k].([]filter)), len(value))
|
||||||
|
}
|
||||||
|
for i, newFilter := range value {
|
||||||
|
oldFilter := (*oldConfig)[k].([]filter)[i]
|
||||||
|
if oldFilter.Enabled != newFilter.Enabled || oldFilter.Name != newFilter.Name || oldFilter.RulesCount != newFilter.RulesCount {
|
||||||
|
t.Fatalf("old filter %s not equals new filter %s", oldFilter.Name, newFilter.Name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return k, v, fmt.Errorf("unknown type in DNS configuration for %s", k)
|
t.Fatalf("uknown data type for %s: %T", k, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return "", nil, nil
|
// compareSchemaVersion check if newSchemaVersion equals schemaVersion
|
||||||
|
func compareSchemaVersion(t *testing.T, newSchemaVersion interface{}, schemaVersion int) {
|
||||||
|
switch v := newSchemaVersion.(type) {
|
||||||
|
case int:
|
||||||
|
if v != schemaVersion {
|
||||||
|
t.Fatalf("Wrong schema version in new config file")
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
t.Fatalf("Schema version is not an integer after update")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func createTestDiskConfig(schemaVersion int) (diskConfig map[string]interface{}) {
|
func createTestDiskConfig(schemaVersion int) (diskConfig map[string]interface{}) {
|
||||||
diskConfig = make(map[string]interface{})
|
diskConfig = make(map[string]interface{})
|
||||||
diskConfig["language"] = "en"
|
diskConfig["language"] = "en"
|
||||||
diskConfig["filters"] = []filter{}
|
diskConfig["filters"] = []filter{
|
||||||
|
{
|
||||||
|
URL: "https://filters.adtidy.org/android/filters/111_optimized.txt",
|
||||||
|
Name: "Latvian filter",
|
||||||
|
RulesCount: 100,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "https://easylist.to/easylistgermany/easylistgermany.txt",
|
||||||
|
Name: "Germany filter",
|
||||||
|
RulesCount: 200,
|
||||||
|
},
|
||||||
|
}
|
||||||
diskConfig["user_rules"] = []string{}
|
diskConfig["user_rules"] = []string{}
|
||||||
diskConfig["schema_version"] = schemaVersion
|
diskConfig["schema_version"] = schemaVersion
|
||||||
diskConfig["bind_host"] = "0.0.0.0"
|
diskConfig["bind_host"] = "0.0.0.0"
|
||||||
|
|
Loading…
Reference in a new issue