utils_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. package utils
  2. import (
  3. "bytes"
  4. "errors"
  5. "io"
  6. "io/ioutil"
  7. "strings"
  8. "testing"
  9. )
  10. func TestBufReader(t *testing.T) {
  11. reader, writer := io.Pipe()
  12. bufreader := NewBufReader(reader)
  13. // Write everything down to a Pipe
  14. // Usually, a pipe should block but because of the buffered reader,
  15. // the writes will go through
  16. done := make(chan bool)
  17. go func() {
  18. writer.Write([]byte("hello world"))
  19. writer.Close()
  20. done <- true
  21. }()
  22. // Drain the reader *after* everything has been written, just to verify
  23. // it is indeed buffering
  24. <-done
  25. output, err := ioutil.ReadAll(bufreader)
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. if !bytes.Equal(output, []byte("hello world")) {
  30. t.Error(string(output))
  31. }
  32. }
  33. type dummyWriter struct {
  34. buffer bytes.Buffer
  35. failOnWrite bool
  36. }
  37. func (dw *dummyWriter) Write(p []byte) (n int, err error) {
  38. if dw.failOnWrite {
  39. return 0, errors.New("Fake fail")
  40. }
  41. return dw.buffer.Write(p)
  42. }
  43. func (dw *dummyWriter) String() string {
  44. return dw.buffer.String()
  45. }
  46. func (dw *dummyWriter) Close() error {
  47. return nil
  48. }
  49. func TestWriteBroadcaster(t *testing.T) {
  50. writer := NewWriteBroadcaster()
  51. // Test 1: Both bufferA and bufferB should contain "foo"
  52. bufferA := &dummyWriter{}
  53. writer.AddWriter(bufferA, "")
  54. bufferB := &dummyWriter{}
  55. writer.AddWriter(bufferB, "")
  56. writer.Write([]byte("foo"))
  57. if bufferA.String() != "foo" {
  58. t.Errorf("Buffer contains %v", bufferA.String())
  59. }
  60. if bufferB.String() != "foo" {
  61. t.Errorf("Buffer contains %v", bufferB.String())
  62. }
  63. // Test2: bufferA and bufferB should contain "foobar",
  64. // while bufferC should only contain "bar"
  65. bufferC := &dummyWriter{}
  66. writer.AddWriter(bufferC, "")
  67. writer.Write([]byte("bar"))
  68. if bufferA.String() != "foobar" {
  69. t.Errorf("Buffer contains %v", bufferA.String())
  70. }
  71. if bufferB.String() != "foobar" {
  72. t.Errorf("Buffer contains %v", bufferB.String())
  73. }
  74. if bufferC.String() != "bar" {
  75. t.Errorf("Buffer contains %v", bufferC.String())
  76. }
  77. // Test3: Test eviction on failure
  78. bufferA.failOnWrite = true
  79. writer.Write([]byte("fail"))
  80. if bufferA.String() != "foobar" {
  81. t.Errorf("Buffer contains %v", bufferA.String())
  82. }
  83. if bufferC.String() != "barfail" {
  84. t.Errorf("Buffer contains %v", bufferC.String())
  85. }
  86. // Even though we reset the flag, no more writes should go in there
  87. bufferA.failOnWrite = false
  88. writer.Write([]byte("test"))
  89. if bufferA.String() != "foobar" {
  90. t.Errorf("Buffer contains %v", bufferA.String())
  91. }
  92. if bufferC.String() != "barfailtest" {
  93. t.Errorf("Buffer contains %v", bufferC.String())
  94. }
  95. writer.CloseWriters()
  96. }
  97. type devNullCloser int
  98. func (d devNullCloser) Close() error {
  99. return nil
  100. }
  101. func (d devNullCloser) Write(buf []byte) (int, error) {
  102. return len(buf), nil
  103. }
  104. // This test checks for races. It is only useful when run with the race detector.
  105. func TestRaceWriteBroadcaster(t *testing.T) {
  106. writer := NewWriteBroadcaster()
  107. c := make(chan bool)
  108. go func() {
  109. writer.AddWriter(devNullCloser(0), "")
  110. c <- true
  111. }()
  112. writer.Write([]byte("hello"))
  113. <-c
  114. }
  115. // Test the behavior of TruncIndex, an index for querying IDs from a non-conflicting prefix.
  116. func TestTruncIndex(t *testing.T) {
  117. index := NewTruncIndex()
  118. // Get on an empty index
  119. if _, err := index.Get("foobar"); err == nil {
  120. t.Fatal("Get on an empty index should return an error")
  121. }
  122. // Spaces should be illegal in an id
  123. if err := index.Add("I have a space"); err == nil {
  124. t.Fatalf("Adding an id with ' ' should return an error")
  125. }
  126. id := "99b36c2c326ccc11e726eee6ee78a0baf166ef96"
  127. // Add an id
  128. if err := index.Add(id); err != nil {
  129. t.Fatal(err)
  130. }
  131. // Get a non-existing id
  132. assertIndexGet(t, index, "abracadabra", "", true)
  133. // Get the exact id
  134. assertIndexGet(t, index, id, id, false)
  135. // The first letter should match
  136. assertIndexGet(t, index, id[:1], id, false)
  137. // The first half should match
  138. assertIndexGet(t, index, id[:len(id)/2], id, false)
  139. // The second half should NOT match
  140. assertIndexGet(t, index, id[len(id)/2:], "", true)
  141. id2 := id[:6] + "blabla"
  142. // Add an id
  143. if err := index.Add(id2); err != nil {
  144. t.Fatal(err)
  145. }
  146. // Both exact IDs should work
  147. assertIndexGet(t, index, id, id, false)
  148. assertIndexGet(t, index, id2, id2, false)
  149. // 6 characters or less should conflict
  150. assertIndexGet(t, index, id[:6], "", true)
  151. assertIndexGet(t, index, id[:4], "", true)
  152. assertIndexGet(t, index, id[:1], "", true)
  153. // 7 characters should NOT conflict
  154. assertIndexGet(t, index, id[:7], id, false)
  155. assertIndexGet(t, index, id2[:7], id2, false)
  156. // Deleting a non-existing id should return an error
  157. if err := index.Delete("non-existing"); err == nil {
  158. t.Fatalf("Deleting a non-existing id should return an error")
  159. }
  160. // Deleting id2 should remove conflicts
  161. if err := index.Delete(id2); err != nil {
  162. t.Fatal(err)
  163. }
  164. // id2 should no longer work
  165. assertIndexGet(t, index, id2, "", true)
  166. assertIndexGet(t, index, id2[:7], "", true)
  167. assertIndexGet(t, index, id2[:11], "", true)
  168. // conflicts between id and id2 should be gone
  169. assertIndexGet(t, index, id[:6], id, false)
  170. assertIndexGet(t, index, id[:4], id, false)
  171. assertIndexGet(t, index, id[:1], id, false)
  172. // non-conflicting substrings should still not conflict
  173. assertIndexGet(t, index, id[:7], id, false)
  174. assertIndexGet(t, index, id[:15], id, false)
  175. assertIndexGet(t, index, id, id, false)
  176. }
  177. func assertIndexGet(t *testing.T, index *TruncIndex, input, expectedResult string, expectError bool) {
  178. if result, err := index.Get(input); err != nil && !expectError {
  179. t.Fatalf("Unexpected error getting '%s': %s", input, err)
  180. } else if err == nil && expectError {
  181. t.Fatalf("Getting '%s' should return an error", input)
  182. } else if result != expectedResult {
  183. t.Fatalf("Getting '%s' returned '%s' instead of '%s'", input, result, expectedResult)
  184. }
  185. }
  186. func assertKernelVersion(t *testing.T, a, b *KernelVersionInfo, result int) {
  187. if r := CompareKernelVersion(a, b); r != result {
  188. t.Fatalf("Unepected kernel version comparaison result. Found %d, expected %d", r, result)
  189. }
  190. }
  191. func TestCompareKernelVersion(t *testing.T) {
  192. assertKernelVersion(t,
  193. &KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0},
  194. &KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0},
  195. 0)
  196. assertKernelVersion(t,
  197. &KernelVersionInfo{Kernel: 2, Major: 6, Minor: 0},
  198. &KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0},
  199. -1)
  200. assertKernelVersion(t,
  201. &KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0},
  202. &KernelVersionInfo{Kernel: 2, Major: 6, Minor: 0},
  203. 1)
  204. assertKernelVersion(t,
  205. &KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0, Flavor: "0"},
  206. &KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0, Flavor: "16"},
  207. 0)
  208. assertKernelVersion(t,
  209. &KernelVersionInfo{Kernel: 3, Major: 8, Minor: 5},
  210. &KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0},
  211. 1)
  212. assertKernelVersion(t,
  213. &KernelVersionInfo{Kernel: 3, Major: 0, Minor: 20, Flavor: "25"},
  214. &KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0, Flavor: "0"},
  215. -1)
  216. }
  217. func TestHumanSize(t *testing.T) {
  218. size := strings.Trim(HumanSize(1000), " \t")
  219. expect := "1 kB"
  220. if size != expect {
  221. t.Errorf("1000 -> expected '%s', got '%s'", expect, size)
  222. }
  223. size = strings.Trim(HumanSize(1024), " \t")
  224. expect = "1.024 kB"
  225. if size != expect {
  226. t.Errorf("1024 -> expected '%s', got '%s'", expect, size)
  227. }
  228. }
  229. func TestParseHost(t *testing.T) {
  230. if addr := ParseHost("127.0.0.1", 4243, "0.0.0.0"); addr != "tcp://0.0.0.0:4243" {
  231. t.Errorf("0.0.0.0 -> expected tcp://0.0.0.0:4243, got %s", addr)
  232. }
  233. if addr := ParseHost("127.0.0.1", 4243, "0.0.0.1:5555"); addr != "tcp://0.0.0.1:5555" {
  234. t.Errorf("0.0.0.1:5555 -> expected tcp://0.0.0.1:5555, got %s", addr)
  235. }
  236. if addr := ParseHost("127.0.0.1", 4243, ":6666"); addr != "tcp://127.0.0.1:6666" {
  237. t.Errorf(":6666 -> expected tcp://127.0.0.1:6666, got %s", addr)
  238. }
  239. if addr := ParseHost("127.0.0.1", 4243, "tcp://:7777"); addr != "tcp://127.0.0.1:7777" {
  240. t.Errorf("tcp://:7777 -> expected tcp://127.0.0.1:7777, got %s", addr)
  241. }
  242. if addr := ParseHost("127.0.0.1", 4243, "unix:///var/run/docker.sock"); addr != "unix:///var/run/docker.sock" {
  243. t.Errorf("unix:///var/run/docker.sock -> expected unix:///var/run/docker.sock, got %s", addr)
  244. }
  245. }
  246. func TestParseRepositoryTag(t *testing.T) {
  247. if repo, tag := ParseRepositoryTag("root"); repo != "root" || tag != "" {
  248. t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "root", "", repo, tag)
  249. }
  250. if repo, tag := ParseRepositoryTag("root:tag"); repo != "root" || tag != "tag" {
  251. t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "root", "tag", repo, tag)
  252. }
  253. if repo, tag := ParseRepositoryTag("user/repo"); repo != "user/repo" || tag != "" {
  254. t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "user/repo", "", repo, tag)
  255. }
  256. if repo, tag := ParseRepositoryTag("user/repo:tag"); repo != "user/repo" || tag != "tag" {
  257. t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "user/repo", "tag", repo, tag)
  258. }
  259. if repo, tag := ParseRepositoryTag("url:5000/repo"); repo != "url:5000/repo" || tag != "" {
  260. t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "url:5000/repo", "", repo, tag)
  261. }
  262. if repo, tag := ParseRepositoryTag("url:5000/repo:tag"); repo != "url:5000/repo" || tag != "tag" {
  263. t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "url:5000/repo", "tag", repo, tag)
  264. }
  265. }
  266. func TestGetResolvConf(t *testing.T) {
  267. resolvConfUtils, err := GetResolvConf()
  268. if err != nil {
  269. t.Fatal(err)
  270. }
  271. resolvConfSystem, err := ioutil.ReadFile("/etc/resolv.conf")
  272. if err != nil {
  273. t.Fatal(err)
  274. }
  275. if string(resolvConfUtils) != string(resolvConfSystem) {
  276. t.Fatalf("/etc/resolv.conf and GetResolvConf have different content.")
  277. }
  278. }
  279. func TestCheclLocalDns(t *testing.T) {
  280. for resolv, result := range map[string]bool{`# Dynamic
  281. nameserver 10.0.2.3
  282. search dotcloud.net`: false,
  283. `# Dynamic
  284. nameserver 127.0.0.1
  285. search dotcloud.net`: true,
  286. `# Dynamic
  287. nameserver 127.0.1.1
  288. search dotcloud.net`: true,
  289. `# Dynamic
  290. `: true,
  291. ``: true,
  292. } {
  293. if CheckLocalDns([]byte(resolv)) != result {
  294. t.Fatalf("Wrong local dns detection: {%s} should be %v", resolv, result)
  295. }
  296. }
  297. }