common_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. })
  17. }
  18. var (
  19. BusyboxImage = map[string]string{
  20. "amd64": "busybox",
  21. "arm": "armhf/busybox",
  22. "arm64": "aarch64/busybox",
  23. }[runtime.GOARCH]
  24. NginxImage = map[string]string{
  25. "amd64": "nginx",
  26. "arm": "armhfbuild/nginx",
  27. "arm64": "armhfbuild/nginx",
  28. }[runtime.GOARCH]
  29. DockerUrl = "https://experimental.docker.com/builds/Linux/x86_64/docker-1.10.0-dev"
  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. c.Assert(s.qemuCmd.Process.Kill(), IsNil)
  40. time.Sleep(time.Millisecond * 1000)
  41. }
  42. func (s *QemuSuite) RunQemu(additionalArgs ...string) error {
  43. runArgs := []string{
  44. "--qemu",
  45. "--no-rebuild",
  46. "--no-rm-usr",
  47. "--fresh",
  48. }
  49. runArgs = append(runArgs, additionalArgs...)
  50. return s.runQemu(runArgs...)
  51. }
  52. func (s *QemuSuite) RunQemuInstalled(additionalArgs ...string) error {
  53. runArgs := []string{
  54. "--qemu",
  55. "--no-rebuild",
  56. "--no-rm-usr",
  57. "--installed",
  58. }
  59. runArgs = append(runArgs, additionalArgs...)
  60. return s.runQemu(runArgs...)
  61. }
  62. func (s *QemuSuite) runQemu(args ...string) error {
  63. s.qemuCmd = exec.Command(s.runCommand, args...)
  64. s.qemuCmd.Stdout = os.Stdout
  65. s.qemuCmd.Stderr = os.Stderr
  66. if err := s.qemuCmd.Start(); err != nil {
  67. return err
  68. }
  69. return s.WaitForSSH()
  70. }
  71. func (s *QemuSuite) WaitForSSH() error {
  72. sshArgs := []string{
  73. "--qemu",
  74. "docker",
  75. "version",
  76. ">/dev/null",
  77. "2>&1",
  78. }
  79. var err error
  80. for i := 0; i < 300; i++ {
  81. cmd := exec.Command(s.sshCommand, sshArgs...)
  82. if err = cmd.Run(); err == nil {
  83. return nil
  84. }
  85. time.Sleep(500 * time.Millisecond)
  86. }
  87. return fmt.Errorf("Failed to connect to SSH: %v", err)
  88. }
  89. func (s *QemuSuite) MakeCall(additionalArgs ...string) error {
  90. sshArgs := []string{
  91. "--qemu",
  92. }
  93. sshArgs = append(sshArgs, additionalArgs...)
  94. cmd := exec.Command(s.sshCommand, sshArgs...)
  95. cmd.Stdout = os.Stdout
  96. cmd.Stderr = os.Stderr
  97. return cmd.Run()
  98. }
  99. func (s *QemuSuite) CheckCall(c *C, additionalArgs ...string) {
  100. c.Assert(s.MakeCall(additionalArgs...), IsNil)
  101. }
  102. func (s *QemuSuite) Reboot(c *C) {
  103. s.MakeCall("sudo reboot")
  104. time.Sleep(3000 * time.Millisecond)
  105. c.Assert(s.WaitForSSH(), IsNil)
  106. }
  107. func (s *QemuSuite) LoadInstallerImage(c *C) {
  108. cmd := exec.Command("sh", "-c", fmt.Sprintf("docker save rancher/os:%s%s | ../scripts/ssh --qemu sudo system-docker load", Version, Suffix))
  109. cmd.Stdout = os.Stdout
  110. cmd.Stderr = os.Stderr
  111. c.Assert(cmd.Run(), IsNil)
  112. }