Skip to content

Commit

Permalink
home: imp test more
Browse files Browse the repository at this point in the history
  • Loading branch information
ainar-g committed Feb 9, 2021
1 parent 5b64131 commit f7bd8e0
Showing 1 changed file with 9 additions and 21 deletions.
30 changes: 9 additions & 21 deletions internal/home/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestUpgradeSchema1to2(t *testing.T) {
err := upgradeSchema1to2(&diskConf)
require.Nil(t, err)

compareSchemaVersion(t, diskConf["schema_version"], 2)
require.Equal(t, diskConf["schema_version"], 2)

_, ok := diskConf["coredns"]
require.False(t, ok)
Expand All @@ -44,7 +44,7 @@ func TestUpgradeSchema2to3(t *testing.T) {
err := upgradeSchema2to3(&diskConf)
require.Nil(t, err)

compareSchemaVersion(t, diskConf["schema_version"], 3)
require.Equal(t, diskConf["schema_version"], 3)

dnsMap, ok := diskConf["dns"]
require.True(t, ok)
Expand All @@ -68,17 +68,17 @@ func TestUpgradeSchema2to3(t *testing.T) {
assertEqualExcept(t, oldDiskConf, diskConf, excludedEntries, excludedEntries)
}

func convertToObject(t *testing.T, oldConf interface{}) (newConf map[string]interface{}) {
func convertToObject(t *testing.T, oldConf any) (newConf object) {
t.Helper()

switch v := oldConf.(type) {
case map[interface{}]interface{}:
newConf = make(map[string]interface{}, len(v))
case map[any]any:
newConf = make(object, len(v))
for key, value := range v {
newConf[fmt.Sprint(key)] = value
}
case map[string]interface{}:
newConf = make(map[string]interface{}, len(v))
case object:
newConf = make(object, len(v))
for key, value := range v {
newConf[key] = value
}
Expand All @@ -103,20 +103,6 @@ func assertEqualExcept(t *testing.T, oldConf, newConf object, oldKeys, newKeys [
assert.Equal(t, oldConf, newConf)
}

// compareSchemaVersion checks if newSchemaVersion equals schemaVersion.
func compareSchemaVersion(t *testing.T, newSchemaVersion any, schemaVersion int) {
t.Helper()

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, got %T", newSchemaVersion)
}
}

func testDiskConf(schemaVersion int) (diskConf object) {
filters := []filter{
{
Expand Down Expand Up @@ -151,6 +137,8 @@ func testDiskConf(schemaVersion int) (diskConf object) {
return diskConf
}

// testDNSConf creates a DNS config for test the way gopkg.in/yaml.v2 would
// unmarshal it. In YAML, keys aren't guaranteed to always only be strings.
func testDNSConf(schemaVersion int) (dnsConf map[any]any) {
dnsConf = map[any]any{
"port": 53,
Expand Down

0 comments on commit f7bd8e0

Please sign in to comment.