common_test.go 4.4 KB

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