utils_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package testutil
  2. import (
  3. "io"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10. "testing"
  11. "time"
  12. )
  13. func TestRunCommandPipelineWithOutputWithNotEnoughCmds(t *testing.T) {
  14. _, _, err := RunCommandPipelineWithOutput(exec.Command("ls"))
  15. expectedError := "pipeline does not have multiple cmds"
  16. if err == nil || err.Error() != expectedError {
  17. t.Fatalf("Expected an error with %s, got err:%s", expectedError, err)
  18. }
  19. }
  20. func TestRunCommandPipelineWithOutputErrors(t *testing.T) {
  21. p := "$PATH"
  22. if runtime.GOOS == "windows" {
  23. p = "%PATH%"
  24. }
  25. cmd1 := exec.Command("ls")
  26. cmd1.Stdout = os.Stdout
  27. cmd2 := exec.Command("anything really")
  28. _, _, err := RunCommandPipelineWithOutput(cmd1, cmd2)
  29. if err == nil || err.Error() != "cannot set stdout pipe for anything really: exec: Stdout already set" {
  30. t.Fatalf("Expected an error, got %v", err)
  31. }
  32. cmdWithError := exec.Command("doesnotexists")
  33. cmdCat := exec.Command("cat")
  34. _, _, err = RunCommandPipelineWithOutput(cmdWithError, cmdCat)
  35. if err == nil || err.Error() != `starting doesnotexists failed with error: exec: "doesnotexists": executable file not found in `+p {
  36. t.Fatalf("Expected an error, got %v", err)
  37. }
  38. }
  39. func TestRunCommandPipelineWithOutput(t *testing.T) {
  40. //TODO: Should run on Solaris
  41. if runtime.GOOS == "solaris" {
  42. t.Skip()
  43. }
  44. cmds := []*exec.Cmd{
  45. // Print 2 characters
  46. exec.Command("echo", "-n", "11"),
  47. // Count the number or char from stdin (previous command)
  48. exec.Command("wc", "-m"),
  49. }
  50. out, exitCode, err := RunCommandPipelineWithOutput(cmds...)
  51. expectedOutput := "2\n"
  52. if out != expectedOutput || exitCode != 0 || err != nil {
  53. t.Fatalf("Expected %s for commands %v, got out:%s, exitCode:%d, err:%v", expectedOutput, cmds, out, exitCode, err)
  54. }
  55. }
  56. // FIXME make an "unhappy path" test for ListTar without "panicking" :-)
  57. func TestListTar(t *testing.T) {
  58. // TODO Windows: Figure out why this fails. Should be portable.
  59. if runtime.GOOS == "windows" {
  60. t.Skip("Failing on Windows - needs further investigation")
  61. }
  62. tmpFolder, err := ioutil.TempDir("", "integration-cli-utils-list-tar")
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. defer os.RemoveAll(tmpFolder)
  67. // Let's create a Tar file
  68. srcFile := filepath.Join(tmpFolder, "src")
  69. tarFile := filepath.Join(tmpFolder, "src.tar")
  70. os.Create(srcFile)
  71. cmd := exec.Command("sh", "-c", "tar cf "+tarFile+" "+srcFile)
  72. _, err = cmd.CombinedOutput()
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. reader, err := os.Open(tarFile)
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. defer reader.Close()
  81. entries, err := ListTar(reader)
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. if len(entries) != 1 && entries[0] != "src" {
  86. t.Fatalf("Expected a tar file with 1 entry (%s), got %v", srcFile, entries)
  87. }
  88. }
  89. func TestRandomTmpDirPath(t *testing.T) {
  90. path := RandomTmpDirPath("something", runtime.GOOS)
  91. prefix := "/tmp/something"
  92. if runtime.GOOS == "windows" {
  93. prefix = os.Getenv("TEMP") + `\something`
  94. }
  95. expectedSize := len(prefix) + 11
  96. if !strings.HasPrefix(path, prefix) {
  97. t.Fatalf("Expected generated path to have '%s' as prefix, got %s'", prefix, path)
  98. }
  99. if len(path) != expectedSize {
  100. t.Fatalf("Expected generated path to be %d, got %d", expectedSize, len(path))
  101. }
  102. }
  103. func TestConsumeWithSpeed(t *testing.T) {
  104. reader := strings.NewReader("1234567890")
  105. chunksize := 2
  106. bytes1, err := ConsumeWithSpeed(reader, chunksize, 10*time.Millisecond, nil)
  107. if err != nil {
  108. t.Fatal(err)
  109. }
  110. if bytes1 != 10 {
  111. t.Fatalf("Expected to have read 10 bytes, got %d", bytes1)
  112. }
  113. }
  114. func TestConsumeWithSpeedWithStop(t *testing.T) {
  115. reader := strings.NewReader("1234567890")
  116. chunksize := 2
  117. stopIt := make(chan bool)
  118. go func() {
  119. time.Sleep(1 * time.Millisecond)
  120. stopIt <- true
  121. }()
  122. bytes1, err := ConsumeWithSpeed(reader, chunksize, 20*time.Millisecond, stopIt)
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. if bytes1 != 2 {
  127. t.Fatalf("Expected to have read 2 bytes, got %d", bytes1)
  128. }
  129. }
  130. func TestParseCgroupPathsEmpty(t *testing.T) {
  131. cgroupMap := ParseCgroupPaths("")
  132. if len(cgroupMap) != 0 {
  133. t.Fatalf("Expected an empty map, got %v", cgroupMap)
  134. }
  135. cgroupMap = ParseCgroupPaths("\n")
  136. if len(cgroupMap) != 0 {
  137. t.Fatalf("Expected an empty map, got %v", cgroupMap)
  138. }
  139. cgroupMap = ParseCgroupPaths("something:else\nagain:here")
  140. if len(cgroupMap) != 0 {
  141. t.Fatalf("Expected an empty map, got %v", cgroupMap)
  142. }
  143. }
  144. func TestParseCgroupPaths(t *testing.T) {
  145. cgroupMap := ParseCgroupPaths("2:memory:/a\n1:cpuset:/b")
  146. if len(cgroupMap) != 2 {
  147. t.Fatalf("Expected a map with 2 entries, got %v", cgroupMap)
  148. }
  149. if value, ok := cgroupMap["memory"]; !ok || value != "/a" {
  150. t.Fatalf("Expected cgroupMap to contains an entry for 'memory' with value '/a', got %v", cgroupMap)
  151. }
  152. if value, ok := cgroupMap["cpuset"]; !ok || value != "/b" {
  153. t.Fatalf("Expected cgroupMap to contains an entry for 'cpuset' with value '/b', got %v", cgroupMap)
  154. }
  155. }
  156. func TestChannelBufferTimeout(t *testing.T) {
  157. expected := "11"
  158. buf := &ChannelBuffer{make(chan []byte, 1)}
  159. defer buf.Close()
  160. done := make(chan struct{}, 1)
  161. go func() {
  162. time.Sleep(100 * time.Millisecond)
  163. io.Copy(buf, strings.NewReader(expected))
  164. done <- struct{}{}
  165. }()
  166. // Wait long enough
  167. b := make([]byte, 2)
  168. _, err := buf.ReadTimeout(b, 50*time.Millisecond)
  169. if err == nil && err.Error() != "timeout reading from channel" {
  170. t.Fatalf("Expected an error, got %s", err)
  171. }
  172. <-done
  173. }
  174. func TestChannelBuffer(t *testing.T) {
  175. expected := "11"
  176. buf := &ChannelBuffer{make(chan []byte, 1)}
  177. defer buf.Close()
  178. go func() {
  179. time.Sleep(100 * time.Millisecond)
  180. io.Copy(buf, strings.NewReader(expected))
  181. }()
  182. // Wait long enough
  183. b := make([]byte, 2)
  184. _, err := buf.ReadTimeout(b, 200*time.Millisecond)
  185. if err != nil {
  186. t.Fatal(err)
  187. }
  188. if string(b) != expected {
  189. t.Fatalf("Expected '%s', got '%s'", expected, string(b))
  190. }
  191. }