/vendor/github.com/akutz/gotil/gotil_test.go

https://github.com/rexray/rexray · Go · 435 lines · 368 code · 67 blank · 0 comment · 115 complexity · 6bb702b6e572600620eed6e73d46c9cd MD5 · raw file

  1. package gotil
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "net"
  7. "os"
  8. "strings"
  9. "testing"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. var (
  13. r10 string
  14. tmpPrefixDirs []string
  15. )
  16. func newTestDir(testName string, t *testing.T) string {
  17. tmpDir, err := ioutil.TempDir(
  18. "", fmt.Sprintf("gotil_test-%s", testName))
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. os.MkdirAll(tmpDir, 0755)
  23. tmpPrefixDirs = append(tmpPrefixDirs, tmpDir)
  24. return tmpDir
  25. }
  26. func TestMain(m *testing.M) {
  27. r10 = RandomString(10)
  28. exitCode := m.Run()
  29. for _, d := range tmpPrefixDirs {
  30. os.RemoveAll(d)
  31. }
  32. os.Exit(exitCode)
  33. }
  34. func TestWriteAndReadStringToFile(t *testing.T) {
  35. tmpDir := newTestDir("TestWriteAndReadStringToFile", t)
  36. tmpFile, _ := ioutil.TempFile(tmpDir, "temp")
  37. WriteStringToFile("Hello, world.", tmpFile.Name())
  38. if s, _ := ReadFileToString(tmpFile.Name()); s != "Hello, world." {
  39. t.Fatalf("s != 'Hello, world.', == %s", s)
  40. }
  41. }
  42. func TestWriteStringToFileError(t *testing.T) {
  43. if err := WriteStringToFile("error", "/badtmpdir/badfile"); err == nil {
  44. t.Fatal("error expected in writing temp file")
  45. }
  46. }
  47. func TestReadtringToFileError(t *testing.T) {
  48. if _, err := ReadFileToString("/badtmpdir/badfile"); err == nil {
  49. t.Fatal("error expected in reading temp file")
  50. }
  51. }
  52. func TestIsDirEmpty(t *testing.T) {
  53. if _, err := IsDirEmpty(r10); err == nil {
  54. t.Fatal("expected error for invalid path")
  55. }
  56. tmpDir := newTestDir("TestIsDirEmpty", t)
  57. var err error
  58. var isEmpty bool
  59. if isEmpty, err = IsDirEmpty(tmpDir); err != nil {
  60. t.Fatal(err)
  61. }
  62. if !isEmpty {
  63. t.Fatalf("%s expected to be empty", tmpDir)
  64. }
  65. WriteStringToFile(r10, fmt.Sprintf("%s/temp.log", tmpDir))
  66. if isEmpty, err = IsDirEmpty(tmpDir); err != nil {
  67. t.Fatal(err)
  68. }
  69. if isEmpty {
  70. t.Fatalf("%s expected to not be empty", tmpDir)
  71. }
  72. }
  73. func TestLineReader(t *testing.T) {
  74. r := bytes.NewReader([]byte(`
  75. 30
  76. `))
  77. c, err := LineReader(r)
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. var lines []string
  82. for s := range c {
  83. lines = append(lines, s)
  84. }
  85. assert.Equal(t, 3, len(lines))
  86. assert.Equal(t, "30", lines[2])
  87. }
  88. func TestLineReaderFrom(t *testing.T) {
  89. c, err := LineReaderFrom(r10)
  90. if err != nil {
  91. t.Fatal("expected nil channel for invalid path")
  92. }
  93. tmpDir := newTestDir("TestLineReaderFrom", t)
  94. path := fmt.Sprintf("%s/temp.log", tmpDir)
  95. if err = WriteStringToFile(r10, path); err != nil {
  96. t.Fatal(err)
  97. }
  98. if c, err = LineReaderFrom(path); err != nil {
  99. t.Fatal(err)
  100. }
  101. var lines []string
  102. for s := range c {
  103. lines = append(lines, s)
  104. }
  105. assert.Equal(t, 1, len(lines))
  106. assert.Equal(t, r10, lines[0])
  107. os.Chmod(path, 0000)
  108. c, err = LineReaderFrom(r10)
  109. if err != nil {
  110. t.Fatal("expected nil channel for access denied")
  111. }
  112. }
  113. func TestGetLocalIP(t *testing.T) {
  114. var ip string
  115. if ip = GetLocalIP(); ip == "" {
  116. t.Fatal("ip == ''")
  117. }
  118. t.Logf("ip=%s", ip)
  119. }
  120. func TestParseIpAddress(t *testing.T) {
  121. var err error
  122. var addr, proto, path string
  123. addr = "ipv4://127.0.0.1:80/hello"
  124. if proto, path, err = ParseAddress(addr); err == nil {
  125. t.Fatalf("expected error parsing %s", addr)
  126. }
  127. addr = "TCP://127.0.0.1:80/hello"
  128. if proto, path, err = ParseAddress(addr); err != nil {
  129. t.Fatalf("error parsing %s %v", addr, err)
  130. }
  131. if proto != "TCP" {
  132. t.Fatalf("proto != TCP, == %s", proto)
  133. }
  134. if path != "127.0.0.1:80/hello" {
  135. t.Fatalf("path != 127.0.0.1:80/hello == %s", path)
  136. }
  137. addr = "tcp://127.0.0.1:80/hello"
  138. if proto, path, err = ParseAddress(addr); err != nil {
  139. t.Fatalf("error parsing %s %v", addr, err)
  140. }
  141. if proto != "tcp" {
  142. t.Fatalf("proto != tcp, == %s", proto)
  143. }
  144. if path != "127.0.0.1:80/hello" {
  145. t.Fatalf("path != 127.0.0.1:80/hello == %s", path)
  146. }
  147. addr = "ip://127.0.0.1:443/secure"
  148. if proto, path, err = ParseAddress(addr); err != nil {
  149. t.Fatalf("error parsing %s %v", addr, err)
  150. }
  151. if proto != "ip" {
  152. t.Fatalf("proto != ip, == %s", proto)
  153. }
  154. if path != "127.0.0.1:443/secure" {
  155. t.Fatalf("path != 127.0.0.1:443/secure == %s", path)
  156. }
  157. }
  158. func TestParseUdpAddress(t *testing.T) {
  159. var err error
  160. var addr, proto, path string
  161. addr = "udp://127.0.0.1:443/secure"
  162. if proto, path, err = ParseAddress(addr); err != nil {
  163. t.Fatalf("error parsing %s %v", addr, err)
  164. }
  165. if proto != "udp" {
  166. t.Fatalf("proto != udp, == %s", proto)
  167. }
  168. if path != "127.0.0.1:443/secure" {
  169. t.Fatalf("path != 127.0.0.1:443/secure == %s", path)
  170. }
  171. }
  172. func TestParseUnixAddress(t *testing.T) {
  173. var err error
  174. var addr, proto, path string
  175. addr = "unix:///var/run/rexray/rexray.sock"
  176. if proto, path, err = ParseAddress(addr); err != nil {
  177. t.Fatalf("error parsing %s %v", addr, err)
  178. }
  179. if proto != "unix" {
  180. t.Fatalf("proto != unix, == %s", proto)
  181. }
  182. if path != "/var/run/rexray/rexray.sock" {
  183. t.Fatalf("path != /var/run/rexray/rexray.sock == %s", path)
  184. }
  185. addr = "unixgram:///var/run/rexray/rexray.sock"
  186. if proto, path, err = ParseAddress(addr); err != nil {
  187. t.Fatalf("error parsing %s %v", addr, err)
  188. }
  189. if proto != "unixgram" {
  190. t.Fatalf("proto != unixgram, == %s", proto)
  191. }
  192. if path != "/var/run/rexray/rexray.sock" {
  193. t.Fatalf("path != /var/run/rexray/rexray.sock == %s", path)
  194. }
  195. addr = "unixpacket:///var/run/rexray/rexray.sock"
  196. if proto, path, err = ParseAddress(addr); err != nil {
  197. t.Fatalf("error parsing %s %v", addr, err)
  198. }
  199. if proto != "unixpacket" {
  200. t.Fatalf("proto != unixpacket, == %s", proto)
  201. }
  202. if path != "/var/run/rexray/rexray.sock" {
  203. t.Fatalf("path != /var/run/rexray/rexray.sock == %s", path)
  204. }
  205. }
  206. func TestTrimSingleWord(t *testing.T) {
  207. s := Trim(`
  208. hi
  209. `)
  210. if s != "hi" {
  211. t.Fatalf("trim failed '%v'", s)
  212. }
  213. }
  214. func TestTrimMultipleWords(t *testing.T) {
  215. s := Trim(`
  216. hi
  217. there
  218. you
  219. `)
  220. if s != `hi
  221. there
  222. you` {
  223. t.Fatalf("trim failed '%v'", s)
  224. }
  225. }
  226. func TestFileExists(t *testing.T) {
  227. if FileExists(r10) {
  228. t.Fatal("file should not exist")
  229. }
  230. if !FileExists("/bin/sh") {
  231. t.Fail()
  232. }
  233. }
  234. func TestFileExistsInPath(t *testing.T) {
  235. if FileExistsInPath(r10) {
  236. t.Fatal("file should not exist")
  237. }
  238. if !FileExistsInPath("sh") {
  239. t.Fail()
  240. }
  241. }
  242. func TestGetPathParts(t *testing.T) {
  243. d, n, a := GetPathParts("/bin/sh")
  244. if d != "/bin" {
  245. t.Fatalf("dir != /bin, == %s", d)
  246. }
  247. if n != "sh" {
  248. t.Fatalf("n != sh, == %s", n)
  249. }
  250. if a != "/bin/sh" {
  251. t.Fatalf("name != /bin/sh, == %s", a)
  252. }
  253. }
  254. func TestGetThisPathParts(t *testing.T) {
  255. _, n, _ := GetThisPathParts()
  256. if !strings.Contains(n, ".test") {
  257. t.Fatalf("n !=~ .test, == %s", n)
  258. }
  259. }
  260. func TestRandomString(t *testing.T) {
  261. var lastR string
  262. for x := 0; x < 100; x++ {
  263. r := RandomString(10)
  264. if r == lastR {
  265. t.Fail()
  266. }
  267. lastR = r
  268. }
  269. }
  270. func TestStringInSlice(t *testing.T) {
  271. var r bool
  272. r = StringInSlice("hi", []string{"hello", "world"})
  273. if r {
  274. t.Fatal("hi there!")
  275. }
  276. r = StringInSlice("hi", []string{"hi", "world"})
  277. if !r {
  278. t.Fatal("hi where?")
  279. }
  280. a := []string{"a", "b", "c"}
  281. if !StringInSlice("b", a) {
  282. t.Fatal("b not in 'a, b, c'")
  283. }
  284. if !StringInSlice("A", a) {
  285. t.Fatal("A not in 'a, b, c'")
  286. }
  287. if StringInSlice("d", a) {
  288. t.Fatal("d in 'a, b, c'")
  289. }
  290. }
  291. func TestStringInSliceCS(t *testing.T) {
  292. var r bool
  293. r = StringInSliceCS("hi", []string{"hello", "world"})
  294. if r {
  295. t.Fatal("hi there!")
  296. }
  297. r = StringInSliceCS("hi", []string{"hi", "world"})
  298. if !r {
  299. t.Fatal("hi where?")
  300. }
  301. r = StringInSliceCS("hi", []string{"Hi", "world"})
  302. if r {
  303. t.Fatal("Hi where?")
  304. }
  305. a := []string{"a", "B", "c"}
  306. if !StringInSliceCS("B", a) {
  307. t.Fatal("b not in 'a, B, c'")
  308. }
  309. if StringInSliceCS("A", a) {
  310. t.Fatal("A not in 'a, B, c'")
  311. }
  312. if StringInSliceCS("d", a) {
  313. t.Fatal("d in 'a, b, c'")
  314. }
  315. }
  316. func TestWriteIndented(t *testing.T) {
  317. s := []byte("Hello,\nworld.")
  318. s2 := " Hello,\n world."
  319. s4 := " Hello,\n world."
  320. w2 := &bytes.Buffer{}
  321. if err := WriteIndentedN(w2, s, 2); err != nil {
  322. t.Fatal(err)
  323. }
  324. assert.EqualValues(t, s2, w2.String())
  325. w1 := &bytes.Buffer{}
  326. if err := WriteIndented(w1, s); err != nil {
  327. t.Fatal(err)
  328. }
  329. assert.EqualValues(t, s4, w1.String())
  330. }
  331. func TestHomeDir(t *testing.T) {
  332. assert.NotEqual(t, "", HomeDir())
  333. }
  334. func TestRandomTCPPort(t *testing.T) {
  335. p := RandomTCPPort()
  336. addr := fmt.Sprintf("127.0.0.1:%d", p)
  337. t.Logf("listening on addr %s", addr)
  338. conn, err := net.Listen("tcp", addr)
  339. if err != nil {
  340. t.Fatal(err)
  341. }
  342. conn.Close()
  343. }
  344. func TestIsTCPPortAvailable(t *testing.T) {
  345. p := RandomTCPPort()
  346. addr := fmt.Sprintf("127.0.0.1:%d", p)
  347. t.Logf("listening on addr %s", addr)
  348. conn, err := net.Listen("tcp", addr)
  349. if err != nil {
  350. t.Fatal(err)
  351. }
  352. defer conn.Close()
  353. _, err2 := net.Listen("tcp", addr)
  354. if err2 == nil {
  355. t.Fatalf("addr should be in use %s", addr)
  356. }
  357. }