util_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright 2015 CoreOS, Inc.
  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 rafthttp
  15. import (
  16. "bytes"
  17. "net/http"
  18. "reflect"
  19. "testing"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
  21. "github.com/coreos/etcd/raft/raftpb"
  22. "github.com/coreos/etcd/version"
  23. )
  24. func TestEntry(t *testing.T) {
  25. tests := []raftpb.Entry{
  26. {},
  27. {Term: 1, Index: 1},
  28. {Term: 1, Index: 1, Data: []byte("some data")},
  29. }
  30. for i, tt := range tests {
  31. b := &bytes.Buffer{}
  32. if err := writeEntryTo(b, &tt); err != nil {
  33. t.Errorf("#%d: unexpected write ents error: %v", i, err)
  34. continue
  35. }
  36. var ent raftpb.Entry
  37. if err := readEntryFrom(b, &ent); err != nil {
  38. t.Errorf("#%d: unexpected read ents error: %v", i, err)
  39. continue
  40. }
  41. if !reflect.DeepEqual(ent, tt) {
  42. t.Errorf("#%d: ent = %+v, want %+v", i, ent, tt)
  43. }
  44. }
  45. }
  46. func TestCompareMajorMinorVersion(t *testing.T) {
  47. tests := []struct {
  48. va, vb *semver.Version
  49. w int
  50. }{
  51. // equal to
  52. {
  53. semver.Must(semver.NewVersion("2.1.0")),
  54. semver.Must(semver.NewVersion("2.1.0")),
  55. 0,
  56. },
  57. // smaller than
  58. {
  59. semver.Must(semver.NewVersion("2.0.0")),
  60. semver.Must(semver.NewVersion("2.1.0")),
  61. -1,
  62. },
  63. // bigger than
  64. {
  65. semver.Must(semver.NewVersion("2.2.0")),
  66. semver.Must(semver.NewVersion("2.1.0")),
  67. 1,
  68. },
  69. // ignore patch
  70. {
  71. semver.Must(semver.NewVersion("2.1.1")),
  72. semver.Must(semver.NewVersion("2.1.0")),
  73. 0,
  74. },
  75. // ignore prerelease
  76. {
  77. semver.Must(semver.NewVersion("2.1.0-alpha.0")),
  78. semver.Must(semver.NewVersion("2.1.0")),
  79. 0,
  80. },
  81. }
  82. for i, tt := range tests {
  83. if g := compareMajorMinorVersion(tt.va, tt.vb); g != tt.w {
  84. t.Errorf("#%d: compare = %d, want %d", i, g, tt.w)
  85. }
  86. }
  87. }
  88. func TestServerVersion(t *testing.T) {
  89. tests := []struct {
  90. h http.Header
  91. wv *semver.Version
  92. }{
  93. // backward compatibility with etcd 2.0
  94. {
  95. http.Header{},
  96. semver.Must(semver.NewVersion("2.0.0")),
  97. },
  98. {
  99. http.Header{"X-Server-Version": []string{"2.1.0"}},
  100. semver.Must(semver.NewVersion("2.1.0")),
  101. },
  102. {
  103. http.Header{"X-Server-Version": []string{"2.1.0-alpha.0+git"}},
  104. semver.Must(semver.NewVersion("2.1.0-alpha.0+git")),
  105. },
  106. }
  107. for i, tt := range tests {
  108. v := serverVersion(tt.h)
  109. if v.String() != tt.wv.String() {
  110. t.Errorf("#%d: version = %s, want %s", i, v, tt.wv)
  111. }
  112. }
  113. }
  114. func TestMinClusterVersion(t *testing.T) {
  115. tests := []struct {
  116. h http.Header
  117. wv *semver.Version
  118. }{
  119. // backward compatibility with etcd 2.0
  120. {
  121. http.Header{},
  122. semver.Must(semver.NewVersion("2.0.0")),
  123. },
  124. {
  125. http.Header{"X-Min-Cluster-Version": []string{"2.1.0"}},
  126. semver.Must(semver.NewVersion("2.1.0")),
  127. },
  128. {
  129. http.Header{"X-Min-Cluster-Version": []string{"2.1.0-alpha.0+git"}},
  130. semver.Must(semver.NewVersion("2.1.0-alpha.0+git")),
  131. },
  132. }
  133. for i, tt := range tests {
  134. v := minClusterVersion(tt.h)
  135. if v.String() != tt.wv.String() {
  136. t.Errorf("#%d: version = %s, want %s", i, v, tt.wv)
  137. }
  138. }
  139. }
  140. func TestCheckVersionCompatibility(t *testing.T) {
  141. ls := semver.Must(semver.NewVersion(version.Version))
  142. lmc := semver.Must(semver.NewVersion(version.MinClusterVersion))
  143. tests := []struct {
  144. server *semver.Version
  145. minCluster *semver.Version
  146. wok bool
  147. }{
  148. // the same version as local
  149. {
  150. ls,
  151. lmc,
  152. true,
  153. },
  154. // one version lower
  155. {
  156. lmc,
  157. &semver.Version{},
  158. true,
  159. },
  160. // one version higher
  161. {
  162. &semver.Version{Major: ls.Major + 1},
  163. ls,
  164. true,
  165. },
  166. // too low version
  167. {
  168. &semver.Version{Major: lmc.Major - 1},
  169. &semver.Version{},
  170. false,
  171. },
  172. // too high version
  173. {
  174. &semver.Version{Major: ls.Major + 1, Minor: 1},
  175. &semver.Version{Major: ls.Major + 1},
  176. false,
  177. },
  178. }
  179. for i, tt := range tests {
  180. err := checkVersionCompability("", tt.server, tt.minCluster)
  181. if ok := err == nil; ok != tt.wok {
  182. t.Errorf("#%d: ok = %v, want %v", i, ok, tt.wok)
  183. }
  184. }
  185. }