member_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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 etcdserver
  14. import (
  15. "net/url"
  16. "reflect"
  17. "testing"
  18. "time"
  19. "github.com/coreos/etcd/pkg/types"
  20. )
  21. func timeParse(value string) *time.Time {
  22. t, err := time.Parse(time.RFC3339, value)
  23. if err != nil {
  24. panic(err)
  25. }
  26. return &t
  27. }
  28. func TestMemberTime(t *testing.T) {
  29. tests := []struct {
  30. mem *Member
  31. id types.ID
  32. }{
  33. {NewMember("mem1", []url.URL{{Scheme: "http", Host: "10.0.0.8:2379"}}, "", nil), 14544069596553697298},
  34. // Same ID, different name (names shouldn't matter)
  35. {NewMember("memfoo", []url.URL{{Scheme: "http", Host: "10.0.0.8:2379"}}, "", nil), 14544069596553697298},
  36. // Same ID, different Time
  37. {NewMember("mem1", []url.URL{{Scheme: "http", Host: "10.0.0.8:2379"}}, "", timeParse("1984-12-23T15:04:05Z")), 2448790162483548276},
  38. // Different cluster name
  39. {NewMember("mcm1", []url.URL{{Scheme: "http", Host: "10.0.0.8:2379"}}, "etcd", timeParse("1984-12-23T15:04:05Z")), 6973882743191604649},
  40. {NewMember("mem1", []url.URL{{Scheme: "http", Host: "10.0.0.1:2379"}}, "", timeParse("1984-12-23T15:04:05Z")), 1466075294948436910},
  41. // Order shouldn't matter
  42. {NewMember("mem1", []url.URL{{Scheme: "http", Host: "10.0.0.1:2379"}, {Scheme: "http", Host: "10.0.0.2:2379"}}, "", nil), 16552244735972308939},
  43. {NewMember("mem1", []url.URL{{Scheme: "http", Host: "10.0.0.2:2379"}, {Scheme: "http", Host: "10.0.0.1:2379"}}, "", nil), 16552244735972308939},
  44. }
  45. for i, tt := range tests {
  46. if tt.mem.ID != tt.id {
  47. t.Errorf("#%d: mem.ID = %v, want %v", i, tt.mem.ID, tt.id)
  48. }
  49. }
  50. }
  51. func TestMemberPick(t *testing.T) {
  52. tests := []struct {
  53. memb *Member
  54. urls map[string]bool
  55. }{
  56. {
  57. newTestMember(1, []string{"abc", "def", "ghi", "jkl", "mno", "pqr", "stu"}, "", nil),
  58. map[string]bool{
  59. "abc": true,
  60. "def": true,
  61. "ghi": true,
  62. "jkl": true,
  63. "mno": true,
  64. "pqr": true,
  65. "stu": true,
  66. },
  67. },
  68. {
  69. newTestMember(2, []string{"xyz"}, "", nil),
  70. map[string]bool{"xyz": true},
  71. },
  72. }
  73. for i, tt := range tests {
  74. for j := 0; j < 1000; j++ {
  75. a := tt.memb.PickPeerURL()
  76. if !tt.urls[a] {
  77. t.Errorf("#%d: returned ID %q not in expected range!", i, a)
  78. break
  79. }
  80. }
  81. }
  82. }
  83. func TestMemberClone(t *testing.T) {
  84. tests := []*Member{
  85. newTestMember(1, nil, "abc", nil),
  86. newTestMember(1, []string{"http://a"}, "abc", nil),
  87. newTestMember(1, nil, "abc", []string{"http://b"}),
  88. newTestMember(1, []string{"http://a"}, "abc", []string{"http://b"}),
  89. }
  90. for i, tt := range tests {
  91. nm := tt.Clone()
  92. if nm == tt {
  93. t.Errorf("#%d: the pointers are the same, and clone doesn't happen", i)
  94. }
  95. if !reflect.DeepEqual(nm, tt) {
  96. t.Errorf("#%d: member = %+v, want %+v", i, nm, tt)
  97. }
  98. }
  99. }
  100. func newTestMember(id uint64, peerURLs []string, name string, clientURLs []string) *Member {
  101. return &Member{
  102. ID: types.ID(id),
  103. RaftAttributes: RaftAttributes{PeerURLs: peerURLs},
  104. Attributes: Attributes{Name: name, ClientURLs: clientURLs},
  105. }
  106. }