cluster_util_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. "github.com/coreos/etcd/pkg/types"
  19. "github.com/coreos/etcd/version"
  20. "github.com/coreos/go-semver/semver"
  21. )
  22. func TestDecideClusterVersion(t *testing.T) {
  23. tests := []struct {
  24. vers map[string]*version.Versions
  25. wdver *semver.Version
  26. }{
  27. {
  28. map[string]*version.Versions{"a": {Server: "2.0.0"}},
  29. semver.Must(semver.NewVersion("2.0.0")),
  30. },
  31. // unknown
  32. {
  33. map[string]*version.Versions{"a": nil},
  34. nil,
  35. },
  36. {
  37. map[string]*version.Versions{"a": {Server: "2.0.0"}, "b": {Server: "2.1.0"}, "c": {Server: "2.1.0"}},
  38. semver.Must(semver.NewVersion("2.0.0")),
  39. },
  40. {
  41. map[string]*version.Versions{"a": {Server: "2.1.0"}, "b": {Server: "2.1.0"}, "c": {Server: "2.1.0"}},
  42. semver.Must(semver.NewVersion("2.1.0")),
  43. },
  44. {
  45. map[string]*version.Versions{"a": nil, "b": {Server: "2.1.0"}, "c": {Server: "2.1.0"}},
  46. nil,
  47. },
  48. }
  49. for i, tt := range tests {
  50. dver := decideClusterVersion(tt.vers)
  51. if !reflect.DeepEqual(dver, tt.wdver) {
  52. t.Errorf("#%d: ver = %+v, want %+v", i, dver, tt.wdver)
  53. }
  54. }
  55. }
  56. func TestIsCompatibleWithVers(t *testing.T) {
  57. tests := []struct {
  58. vers map[string]*version.Versions
  59. local types.ID
  60. minV, maxV *semver.Version
  61. wok bool
  62. }{
  63. // too low
  64. {
  65. map[string]*version.Versions{
  66. "a": {Server: "2.0.0", Cluster: "not_decided"},
  67. "b": {Server: "2.1.0", Cluster: "2.1.0"},
  68. "c": {Server: "2.1.0", Cluster: "2.1.0"},
  69. },
  70. 0xa,
  71. semver.Must(semver.NewVersion("2.0.0")), semver.Must(semver.NewVersion("2.0.0")),
  72. false,
  73. },
  74. {
  75. map[string]*version.Versions{
  76. "a": {Server: "2.1.0", Cluster: "not_decided"},
  77. "b": {Server: "2.1.0", Cluster: "2.1.0"},
  78. "c": {Server: "2.1.0", Cluster: "2.1.0"},
  79. },
  80. 0xa,
  81. semver.Must(semver.NewVersion("2.0.0")), semver.Must(semver.NewVersion("2.1.0")),
  82. true,
  83. },
  84. // too high
  85. {
  86. map[string]*version.Versions{
  87. "a": {Server: "2.2.0", Cluster: "not_decided"},
  88. "b": {Server: "2.0.0", Cluster: "2.0.0"},
  89. "c": {Server: "2.0.0", Cluster: "2.0.0"},
  90. },
  91. 0xa,
  92. semver.Must(semver.NewVersion("2.1.0")), semver.Must(semver.NewVersion("2.2.0")),
  93. false,
  94. },
  95. // cannot get b's version, expect ok
  96. {
  97. map[string]*version.Versions{
  98. "a": {Server: "2.1.0", Cluster: "not_decided"},
  99. "b": nil,
  100. "c": {Server: "2.1.0", Cluster: "2.1.0"},
  101. },
  102. 0xa,
  103. semver.Must(semver.NewVersion("2.0.0")), semver.Must(semver.NewVersion("2.1.0")),
  104. true,
  105. },
  106. // cannot get b and c's version, expect not ok
  107. {
  108. map[string]*version.Versions{
  109. "a": {Server: "2.1.0", Cluster: "not_decided"},
  110. "b": nil,
  111. "c": nil,
  112. },
  113. 0xa,
  114. semver.Must(semver.NewVersion("2.0.0")), semver.Must(semver.NewVersion("2.1.0")),
  115. false,
  116. },
  117. }
  118. for i, tt := range tests {
  119. ok := isCompatibleWithVers(tt.vers, tt.local, tt.minV, tt.maxV)
  120. if ok != tt.wok {
  121. t.Errorf("#%d: ok = %+v, want %+v", i, ok, tt.wok)
  122. }
  123. }
  124. }