common_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package integration
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "runtime"
  7. "strings"
  8. "testing"
  9. "time"
  10. . "gopkg.in/check.v1"
  11. )
  12. func Test(t *testing.T) { TestingT(t) }
  13. func init() {
  14. Suite(&QemuSuite{
  15. runCommand: "../scripts/run",
  16. sshCommand: "../scripts/ssh",
  17. qemuCmd: nil,
  18. })
  19. }
  20. var (
  21. BusyboxImage = map[string]string{
  22. "amd64": "busybox",
  23. "arm": "armhf/busybox",
  24. "arm64": "aarch64/busybox",
  25. }[runtime.GOARCH]
  26. NginxImage = map[string]string{
  27. "amd64": "nginx",
  28. "arm": "armhfbuild/nginx",
  29. "arm64": "armhfbuild/nginx",
  30. }[runtime.GOARCH]
  31. Version = os.Getenv("VERSION")
  32. Suffix = os.Getenv("SUFFIX")
  33. )
  34. type QemuSuite struct {
  35. runCommand string
  36. sshCommand string
  37. qemuCmd *exec.Cmd
  38. }
  39. func (s *QemuSuite) TearDownTest(c *C) {
  40. if s.qemuCmd != nil {
  41. s.Stop(c)
  42. }
  43. }
  44. // RunQemuWith requires user to specify all the `scripts/run` arguments
  45. func (s *QemuSuite) RunQemuWith(c *C, additionalArgs ...string) error {
  46. err := s.runQemu(c, additionalArgs...)
  47. c.Assert(err, IsNil)
  48. return err
  49. }
  50. func (s *QemuSuite) RunQemu(c *C, additionalArgs ...string) error {
  51. runArgs := []string{
  52. "--qemu",
  53. "--no-rebuild",
  54. "--no-rm-usr",
  55. "--fresh",
  56. }
  57. runArgs = append(runArgs, additionalArgs...)
  58. err := s.RunQemuWith(c, runArgs...)
  59. c.Assert(err, IsNil)
  60. return err
  61. }
  62. func (s *QemuSuite) RunQemuInstalled(c *C, additionalArgs ...string) error {
  63. runArgs := []string{
  64. "--fresh",
  65. }
  66. runArgs = append(runArgs, additionalArgs...)
  67. err := s.RunQemu(c, runArgs...)
  68. c.Assert(err, IsNil)
  69. return err
  70. }
  71. func (s *QemuSuite) runQemu(c *C, args ...string) error {
  72. c.Assert(s.qemuCmd, IsNil) // can't run 2 qemu's at once (yet)
  73. s.qemuCmd = exec.Command(s.runCommand, args...)
  74. if os.Getenv("DEBUG") != "" {
  75. s.qemuCmd.Stdout = os.Stdout
  76. s.qemuCmd.Stderr = os.Stderr
  77. }
  78. if err := s.qemuCmd.Start(); err != nil {
  79. return err
  80. }
  81. fmt.Printf("--- %s: starting qemu %s, %v\n", c.TestName(), s.runCommand, args)
  82. return s.WaitForSSH()
  83. }
  84. func (s *QemuSuite) WaitForSSH() error {
  85. sshArgs := []string{
  86. "--qemu",
  87. "true",
  88. }
  89. var err error
  90. for i := 0; i < 100; i++ {
  91. cmd := exec.Command(s.sshCommand, sshArgs...)
  92. if err = cmd.Run(); err == nil {
  93. break
  94. }
  95. fmt.Printf("s%d", i)
  96. time.Sleep(time.Second)
  97. }
  98. if err != nil {
  99. return fmt.Errorf("Failed to connect to SSH: %v", err)
  100. }
  101. sshArgs = []string{
  102. "--qemu",
  103. "docker",
  104. "version",
  105. ">/dev/null",
  106. "2>&1",
  107. }
  108. for i := 0; i < 20; i++ {
  109. cmd := exec.Command(s.sshCommand, sshArgs...)
  110. if err = cmd.Run(); err == nil {
  111. return nil
  112. }
  113. fmt.Printf("d%d", i)
  114. time.Sleep(500 * time.Millisecond)
  115. }
  116. return fmt.Errorf("Failed to check Docker version: %v", err)
  117. }
  118. func (s *QemuSuite) MakeCall(additionalArgs ...string) (string, error) {
  119. sshArgs := []string{
  120. "--qemu",
  121. }
  122. sshArgs = append(sshArgs, additionalArgs...)
  123. cmd := exec.Command(s.sshCommand, sshArgs...)
  124. cmd.Stderr = os.Stderr
  125. out, err := cmd.Output()
  126. str := string(out)
  127. fmt.Println(str)
  128. return str, err
  129. }
  130. func (s *QemuSuite) CheckCall(c *C, additionalArgs ...string) {
  131. _, err := s.MakeCall(additionalArgs...)
  132. c.Assert(err, IsNil)
  133. }
  134. func (s *QemuSuite) CheckOutput(c *C, result string, check Checker, additionalArgs ...string) string {
  135. out, err := s.MakeCall(additionalArgs...)
  136. c.Assert(err, IsNil)
  137. c.Assert(out, check, result)
  138. return out
  139. }
  140. func (s *QemuSuite) CheckOutputContains(c *C, result string, additionalArgs ...string) string {
  141. out, err := s.MakeCall(additionalArgs...)
  142. c.Assert(err, IsNil)
  143. c.Assert(strings.Contains(out, result), Equals, true)
  144. return out
  145. }
  146. func (s *QemuSuite) Stop(c *C) {
  147. //s.MakeCall("sudo halt")
  148. //time.Sleep(2000 * time.Millisecond)
  149. //c.Assert(s.WaitForSSH(), IsNil)
  150. //fmt.Println("%s: stopping qemu", c.TestName())
  151. c.Assert(s.qemuCmd.Process.Kill(), IsNil)
  152. s.qemuCmd.Process.Wait()
  153. //time.Sleep(time.Millisecond * 1000)
  154. s.qemuCmd = nil
  155. fmt.Printf("--- %s: qemu stopped", c.TestName())
  156. }
  157. func (s *QemuSuite) Reboot(c *C) {
  158. fmt.Printf("--- %s: qemu reboot", c.TestName())
  159. s.MakeCall("sudo reboot")
  160. time.Sleep(3000 * time.Millisecond)
  161. c.Assert(s.WaitForSSH(), IsNil)
  162. }
  163. func (s *QemuSuite) LoadInstallerImage(c *C) {
  164. cmd := exec.Command("sh", "-c", fmt.Sprintf("docker save rancher/os:%s%s | ../scripts/ssh --qemu sudo system-docker load", Version, Suffix))
  165. cmd.Stdout = os.Stdout
  166. cmd.Stderr = os.Stderr
  167. c.Assert(cmd.Run(), IsNil)
  168. }
  169. func (s *QemuSuite) PullAndLoadImage(c *C, image string) {
  170. cmd := exec.Command("sh", "-c", fmt.Sprintf("docker pull %s", image))
  171. cmd.Stdout = os.Stdout
  172. cmd.Stderr = os.Stderr
  173. c.Assert(cmd.Run(), IsNil)
  174. cmd = exec.Command("sh", "-c", fmt.Sprintf("docker save %s | ../scripts/ssh --qemu sudo system-docker load", image))
  175. cmd.Stdout = os.Stdout
  176. cmd.Stderr = os.Stderr
  177. c.Assert(cmd.Run(), IsNil)
  178. }