utils_test.go 4.7 KB

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