member_test.go 3.4 KB

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