k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cluster/images/etcd/migrate/versions_test.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "testing" 21 22 "github.com/blang/semver/v4" 23 ) 24 25 func TestSerializeEtcdVersionPair(t *testing.T) { 26 cases := []struct { 27 versionTxt string 28 version *EtcdVersionPair 29 match bool 30 }{ 31 {"3.1.2/etcd3", &EtcdVersionPair{&EtcdVersion{semver.MustParse("3.1.2")}, storageEtcd3}, true}, 32 {"1.1.1-rc.0/etcd3", &EtcdVersionPair{&EtcdVersion{semver.MustParse("1.1.1-rc.0")}, storageEtcd3}, true}, 33 {"10.100.1000/etcd3", &EtcdVersionPair{&EtcdVersion{semver.MustParse("10.100.1000")}, storageEtcd3}, true}, 34 } 35 36 for _, c := range cases { 37 vp, err := ParseEtcdVersionPair(c.versionTxt) 38 if err != nil { 39 t.Errorf("Failed to parse '%s': %v", c.versionTxt, err) 40 } 41 if vp.Equals(c.version) != c.match { 42 t.Errorf("Expected '%s' to be parsed as '%+v', got '%+v'", c.versionTxt, c.version, vp) 43 } 44 if vp.String() != c.versionTxt { 45 t.Errorf("Expected round trip serialization back to '%s', got '%s'", c.versionTxt, vp.String()) 46 } 47 } 48 49 unparsables := []string{ 50 "1.1/etcd3", 51 "1.1.1.1/etcd3", 52 "1.1.1/etcd4", 53 } 54 for _, unparsable := range unparsables { 55 vp, err := ParseEtcdVersionPair(unparsable) 56 if err == nil { 57 t.Errorf("Should have failed to parse '%s' but got '%s'", unparsable, vp) 58 } 59 } 60 } 61 62 func TestMajorMinorEquals(t *testing.T) { 63 cases := []struct { 64 first *EtcdVersion 65 second *EtcdVersion 66 match bool 67 }{ 68 {&EtcdVersion{semver.Version{Major: 3, Minor: 1, Patch: 2}}, &EtcdVersion{semver.Version{Major: 3, Minor: 1, Patch: 0}}, true}, 69 {&EtcdVersion{semver.Version{Major: 3, Minor: 1, Patch: 2}}, &EtcdVersion{semver.Version{Major: 3, Minor: 1, Patch: 2}}, true}, 70 71 {&EtcdVersion{semver.Version{Major: 3, Minor: 0, Patch: 0}}, &EtcdVersion{semver.Version{Major: 3, Minor: 1, Patch: 0}}, false}, 72 {&EtcdVersion{semver.Version{Major: 2, Minor: 0, Patch: 0}}, &EtcdVersion{semver.Version{Major: 3, Minor: 0, Patch: 0}}, false}, 73 } 74 75 for _, c := range cases { 76 if c.first.MajorMinorEquals(c.second) != c.match { 77 t.Errorf("Expected (%+v == %+v) == %t, got %t", c.first, c.second, c.match, !c.match) 78 } 79 } 80 }