versions_test.go 2.6 KB

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