cluster_util_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2015 The etcd Authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package etcdserver
  15. import (
  16. "reflect"
  17. "testing"
  18. "go.etcd.io/etcd/pkg/types"
  19. "go.etcd.io/etcd/version"
  20. "github.com/coreos/go-semver/semver"
  21. "go.uber.org/zap"
  22. )
  23. var testLogger = zap.NewExample()
  24. func TestDecideClusterVersion(t *testing.T) {
  25. tests := []struct {
  26. vers map[string]*version.Versions
  27. wdver *semver.Version
  28. }{
  29. {
  30. map[string]*version.Versions{"a": {Server: "2.0.0"}},
  31. semver.Must(semver.NewVersion("2.0.0")),
  32. },
  33. // unknown
  34. {
  35. map[string]*version.Versions{"a": nil},
  36. nil,
  37. },
  38. {
  39. map[string]*version.Versions{"a": {Server: "2.0.0"}, "b": {Server: "2.1.0"}, "c": {Server: "2.1.0"}},
  40. semver.Must(semver.NewVersion("2.0.0")),
  41. },
  42. {
  43. map[string]*version.Versions{"a": {Server: "2.1.0"}, "b": {Server: "2.1.0"}, "c": {Server: "2.1.0"}},
  44. semver.Must(semver.NewVersion("2.1.0")),
  45. },
  46. {
  47. map[string]*version.Versions{"a": nil, "b": {Server: "2.1.0"}, "c": {Server: "2.1.0"}},
  48. nil,
  49. },
  50. }
  51. for i, tt := range tests {
  52. dver := decideClusterVersion(testLogger, tt.vers)
  53. if !reflect.DeepEqual(dver, tt.wdver) {
  54. t.Errorf("#%d: ver = %+v, want %+v", i, dver, tt.wdver)
  55. }
  56. }
  57. }
  58. func TestIsCompatibleWithVers(t *testing.T) {
  59. tests := []struct {
  60. vers map[string]*version.Versions
  61. local types.ID
  62. minV, maxV *semver.Version
  63. wok bool
  64. }{
  65. // too low
  66. {
  67. map[string]*version.Versions{
  68. "a": {Server: "2.0.0", Cluster: "not_decided"},
  69. "b": {Server: "2.1.0", Cluster: "2.1.0"},
  70. "c": {Server: "2.1.0", Cluster: "2.1.0"},
  71. },
  72. 0xa,
  73. semver.Must(semver.NewVersion("2.0.0")), semver.Must(semver.NewVersion("2.0.0")),
  74. false,
  75. },
  76. {
  77. map[string]*version.Versions{
  78. "a": {Server: "2.1.0", Cluster: "not_decided"},
  79. "b": {Server: "2.1.0", Cluster: "2.1.0"},
  80. "c": {Server: "2.1.0", Cluster: "2.1.0"},
  81. },
  82. 0xa,
  83. semver.Must(semver.NewVersion("2.0.0")), semver.Must(semver.NewVersion("2.1.0")),
  84. true,
  85. },
  86. // too high
  87. {
  88. map[string]*version.Versions{
  89. "a": {Server: "2.2.0", Cluster: "not_decided"},
  90. "b": {Server: "2.0.0", Cluster: "2.0.0"},
  91. "c": {Server: "2.0.0", Cluster: "2.0.0"},
  92. },
  93. 0xa,
  94. semver.Must(semver.NewVersion("2.1.0")), semver.Must(semver.NewVersion("2.2.0")),
  95. false,
  96. },
  97. // cannot get b's version, expect ok
  98. {
  99. map[string]*version.Versions{
  100. "a": {Server: "2.1.0", Cluster: "not_decided"},
  101. "b": nil,
  102. "c": {Server: "2.1.0", Cluster: "2.1.0"},
  103. },
  104. 0xa,
  105. semver.Must(semver.NewVersion("2.0.0")), semver.Must(semver.NewVersion("2.1.0")),
  106. true,
  107. },
  108. // cannot get b and c's version, expect not ok
  109. {
  110. map[string]*version.Versions{
  111. "a": {Server: "2.1.0", Cluster: "not_decided"},
  112. "b": nil,
  113. "c": nil,
  114. },
  115. 0xa,
  116. semver.Must(semver.NewVersion("2.0.0")), semver.Must(semver.NewVersion("2.1.0")),
  117. false,
  118. },
  119. }
  120. for i, tt := range tests {
  121. ok := isCompatibleWithVers(testLogger, tt.vers, tt.local, tt.minV, tt.maxV)
  122. if ok != tt.wok {
  123. t.Errorf("#%d: ok = %+v, want %+v", i, ok, tt.wok)
  124. }
  125. }
  126. }