kubernetes/migration: support simple downgrade cases (#172)

* support basic downgrades

* add unit test for validDownMigration

* add no-op test for downward migration

* document
This commit is contained in:
Chris O'Haver 2019-05-20 16:11:03 -04:00 committed by GitHub
parent 487cb8878d
commit 38ae5466f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 264 additions and 12 deletions

View file

@ -242,6 +242,108 @@ mystub-2.example.org {
}
}
func TestMigrateDown(t *testing.T) {
testCases := []struct {
name string
fromVersion string
toVersion string
deprecations bool
startCorefile string
expectedCorefile string
}{
{
name: "from 1.5.0 to 1.1.3",
fromVersion: "1.5.0",
toVersion: "1.1.3",
startCorefile: `.:53 {
errors
health
ready
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
upstream
fallthrough in-addr.arpa ip6.arpa
}
prometheus :9153
forward . /etc/resolv.conf
cache 30
loop
reload
loadbalance
}
`,
expectedCorefile: `.:53 {
errors
health
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
upstream
fallthrough in-addr.arpa ip6.arpa
}
prometheus :9153
forward . /etc/resolv.conf
cache 30
reload
loadbalance
}
`,
},
{
name: "no-op same version migration",
fromVersion: "1.3.1",
toVersion: "1.3.1",
deprecations: true,
startCorefile: `.:53 {
errors
health
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
upstream
fallthrough in-addr.arpa ip6.arpa
}
prometheus :9153
proxy . /etc/resolv.conf
cache 30
loop
reload
loadbalance
}
`,
expectedCorefile: `.:53 {
errors
health
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
upstream
fallthrough in-addr.arpa ip6.arpa
}
prometheus :9153
proxy . /etc/resolv.conf
cache 30
loop
reload
loadbalance
}
`,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
result, err := MigrateDown(testCase.fromVersion, testCase.toVersion, testCase.startCorefile)
if err != nil {
t.Errorf("%v", err)
}
if result != testCase.expectedCorefile {
t.Errorf("expected -> got diffs:\n%v", diff.LineDiff(testCase.expectedCorefile, result))
}
})
}
}
func TestDeprecated(t *testing.T) {
startCorefile := `.:53 {
errors
@ -418,7 +520,7 @@ stubzone.org:53 {
}
}
func TestValidateVersions(t *testing.T) {
func TestValidUpMigration(t *testing.T) {
testCases := []struct {
from string
to string
@ -433,7 +535,33 @@ func TestValidateVersions(t *testing.T) {
}
for _, tc := range testCases {
err := validateVersions(tc.from, tc.to)
err := validUpMigration(tc.from, tc.to)
if !tc.shouldErr && err != nil {
t.Errorf("expected '%v' to '%v' to be valid versions.", tc.from, tc.to)
}
if tc.shouldErr && err == nil {
t.Errorf("expected '%v' to '%v' to be invalid versions.", tc.from, tc.to)
}
}
}
func TestValidDownMigration(t *testing.T) {
testCases := []struct {
from string
to string
shouldErr bool
}{
{"1.3.1", "1.3.1", true},
{"1.3.1", "1.5.0", true},
{"1.5.0", "1.3.1", false},
{"banana", "1.5.0", true},
{"1.3.1", "apple", true},
{"banana", "apple", true},
}
for _, tc := range testCases {
err := validDownMigration(tc.from, tc.to)
if !tc.shouldErr && err != nil {
t.Errorf("expected '%v' to '%v' to be valid versions.", tc.from, tc.to)