utils_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package utils
  2. import (
  3. "bytes"
  4. "errors"
  5. "io"
  6. "io/ioutil"
  7. "testing"
  8. )
  9. func TestBufReader(t *testing.T) {
  10. reader, writer := io.Pipe()
  11. bufreader := NewBufReader(reader)
  12. // Write everything down to a Pipe
  13. // Usually, a pipe should block but because of the buffered reader,
  14. // the writes will go through
  15. done := make(chan bool)
  16. go func() {
  17. writer.Write([]byte("hello world"))
  18. writer.Close()
  19. done <- true
  20. }()
  21. // Drain the reader *after* everything has been written, just to verify
  22. // it is indeed buffering
  23. <-done
  24. output, err := ioutil.ReadAll(bufreader)
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. if !bytes.Equal(output, []byte("hello world")) {
  29. t.Error(string(output))
  30. }
  31. }
  32. type dummyWriter struct {
  33. buffer bytes.Buffer
  34. failOnWrite bool
  35. }
  36. func (dw *dummyWriter) Write(p []byte) (n int, err error) {
  37. if dw.failOnWrite {
  38. return 0, errors.New("Fake fail")
  39. }
  40. return dw.buffer.Write(p)
  41. }
  42. func (dw *dummyWriter) String() string {
  43. return dw.buffer.String()
  44. }
  45. func (dw *dummyWriter) Close() error {
  46. return nil
  47. }
  48. func TestWriteBroadcaster(t *testing.T) {
  49. writer := NewWriteBroadcaster()
  50. // Test 1: Both bufferA and bufferB should contain "foo"
  51. bufferA := &dummyWriter{}
  52. writer.AddWriter(bufferA)
  53. bufferB := &dummyWriter{}
  54. writer.AddWriter(bufferB)
  55. writer.Write([]byte("foo"))
  56. if bufferA.String() != "foo" {
  57. t.Errorf("Buffer contains %v", bufferA.String())
  58. }
  59. if bufferB.String() != "foo" {
  60. t.Errorf("Buffer contains %v", bufferB.String())
  61. }
  62. // Test2: bufferA and bufferB should contain "foobar",
  63. // while bufferC should only contain "bar"
  64. bufferC := &dummyWriter{}
  65. writer.AddWriter(bufferC)
  66. writer.Write([]byte("bar"))
  67. if bufferA.String() != "foobar" {
  68. t.Errorf("Buffer contains %v", bufferA.String())
  69. }
  70. if bufferB.String() != "foobar" {
  71. t.Errorf("Buffer contains %v", bufferB.String())
  72. }
  73. if bufferC.String() != "bar" {
  74. t.Errorf("Buffer contains %v", bufferC.String())
  75. }
  76. // Test3: Test removal
  77. writer.RemoveWriter(bufferB)
  78. writer.Write([]byte("42"))
  79. if bufferA.String() != "foobar42" {
  80. t.Errorf("Buffer contains %v", bufferA.String())
  81. }
  82. if bufferB.String() != "foobar" {
  83. t.Errorf("Buffer contains %v", bufferB.String())
  84. }
  85. if bufferC.String() != "bar42" {
  86. t.Errorf("Buffer contains %v", bufferC.String())
  87. }
  88. // Test4: Test eviction on failure
  89. bufferA.failOnWrite = true
  90. writer.Write([]byte("fail"))
  91. if bufferA.String() != "foobar42" {
  92. t.Errorf("Buffer contains %v", bufferA.String())
  93. }
  94. if bufferC.String() != "bar42fail" {
  95. t.Errorf("Buffer contains %v", bufferC.String())
  96. }
  97. // Even though we reset the flag, no more writes should go in there
  98. bufferA.failOnWrite = false
  99. writer.Write([]byte("test"))
  100. if bufferA.String() != "foobar42" {
  101. t.Errorf("Buffer contains %v", bufferA.String())
  102. }
  103. if bufferC.String() != "bar42failtest" {
  104. t.Errorf("Buffer contains %v", bufferC.String())
  105. }
  106. writer.CloseWriters()
  107. }
  108. type devNullCloser int
  109. func (d devNullCloser) Close() error {
  110. return nil
  111. }
  112. func (d devNullCloser) Write(buf []byte) (int, error) {
  113. return len(buf), nil
  114. }
  115. // This test checks for races. It is only useful when run with the race detector.
  116. func TestRaceWriteBroadcaster(t *testing.T) {
  117. writer := NewWriteBroadcaster()
  118. c := make(chan bool)
  119. go func() {
  120. writer.AddWriter(devNullCloser(0))
  121. c <- true
  122. }()
  123. writer.Write([]byte("hello"))
  124. <-c
  125. }
  126. // Test the behavior of TruncIndex, an index for querying IDs from a non-conflicting prefix.
  127. func TestTruncIndex(t *testing.T) {
  128. index := NewTruncIndex()
  129. // Get on an empty index
  130. if _, err := index.Get("foobar"); err == nil {
  131. t.Fatal("Get on an empty index should return an error")
  132. }
  133. // Spaces should be illegal in an id
  134. if err := index.Add("I have a space"); err == nil {
  135. t.Fatalf("Adding an id with ' ' should return an error")
  136. }
  137. id := "99b36c2c326ccc11e726eee6ee78a0baf166ef96"
  138. // Add an id
  139. if err := index.Add(id); err != nil {
  140. t.Fatal(err)
  141. }
  142. // Get a non-existing id
  143. assertIndexGet(t, index, "abracadabra", "", true)
  144. // Get the exact id
  145. assertIndexGet(t, index, id, id, false)
  146. // The first letter should match
  147. assertIndexGet(t, index, id[:1], id, false)
  148. // The first half should match
  149. assertIndexGet(t, index, id[:len(id)/2], id, false)
  150. // The second half should NOT match
  151. assertIndexGet(t, index, id[len(id)/2:], "", true)
  152. id2 := id[:6] + "blabla"
  153. // Add an id
  154. if err := index.Add(id2); err != nil {
  155. t.Fatal(err)
  156. }
  157. // Both exact IDs should work
  158. assertIndexGet(t, index, id, id, false)
  159. assertIndexGet(t, index, id2, id2, false)
  160. // 6 characters or less should conflict
  161. assertIndexGet(t, index, id[:6], "", true)
  162. assertIndexGet(t, index, id[:4], "", true)
  163. assertIndexGet(t, index, id[:1], "", true)
  164. // 7 characters should NOT conflict
  165. assertIndexGet(t, index, id[:7], id, false)
  166. assertIndexGet(t, index, id2[:7], id2, false)
  167. // Deleting a non-existing id should return an error
  168. if err := index.Delete("non-existing"); err == nil {
  169. t.Fatalf("Deleting a non-existing id should return an error")
  170. }
  171. // Deleting id2 should remove conflicts
  172. if err := index.Delete(id2); err != nil {
  173. t.Fatal(err)
  174. }
  175. // id2 should no longer work
  176. assertIndexGet(t, index, id2, "", true)
  177. assertIndexGet(t, index, id2[:7], "", true)
  178. assertIndexGet(t, index, id2[:11], "", true)
  179. // conflicts between id and id2 should be gone
  180. assertIndexGet(t, index, id[:6], id, false)
  181. assertIndexGet(t, index, id[:4], id, false)
  182. assertIndexGet(t, index, id[:1], id, false)
  183. // non-conflicting substrings should still not conflict
  184. assertIndexGet(t, index, id[:7], id, false)
  185. assertIndexGet(t, index, id[:15], id, false)
  186. assertIndexGet(t, index, id, id, false)
  187. }
  188. func assertIndexGet(t *testing.T, index *TruncIndex, input, expectedResult string, expectError bool) {
  189. if result, err := index.Get(input); err != nil && !expectError {
  190. t.Fatalf("Unexpected error getting '%s': %s", input, err)
  191. } else if err == nil && expectError {
  192. t.Fatalf("Getting '%s' should return an error", input)
  193. } else if result != expectedResult {
  194. t.Fatalf("Getting '%s' returned '%s' instead of '%s'", input, result, expectedResult)
  195. }
  196. }
  197. func assertKernelVersion(t *testing.T, a, b *KernelVersionInfo, result int) {
  198. if r := CompareKernelVersion(a, b); r != result {
  199. t.Fatalf("Unepected kernel version comparaison result. Found %d, expected %d", r, result)
  200. }
  201. }
  202. func TestCompareKernelVersion(t *testing.T) {
  203. assertKernelVersion(t,
  204. &KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0},
  205. &KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0},
  206. 0)
  207. assertKernelVersion(t,
  208. &KernelVersionInfo{Kernel: 2, Major: 6, Minor: 0},
  209. &KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0},
  210. -1)
  211. assertKernelVersion(t,
  212. &KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0},
  213. &KernelVersionInfo{Kernel: 2, Major: 6, Minor: 0},
  214. 1)
  215. assertKernelVersion(t,
  216. &KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0, Flavor: "0"},
  217. &KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0, Flavor: "16"},
  218. 0)
  219. assertKernelVersion(t,
  220. &KernelVersionInfo{Kernel: 3, Major: 8, Minor: 5},
  221. &KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0},
  222. 1)
  223. assertKernelVersion(t,
  224. &KernelVersionInfo{Kernel: 3, Major: 0, Minor: 20, Flavor: "25"},
  225. &KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0, Flavor: "0"},
  226. -1)
  227. }