runtime_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package docker
  2. import (
  3. "github.com/dotcloud/docker/rcli"
  4. "io"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. "os/user"
  9. "sync"
  10. "testing"
  11. "time"
  12. )
  13. // FIXME: this is no longer needed
  14. const testLayerPath string = "/var/lib/docker/docker-ut.tar"
  15. const unitTestImageName string = "docker-ut"
  16. var unitTestStoreBase string
  17. func nuke(runtime *Runtime) error {
  18. var wg sync.WaitGroup
  19. for _, container := range runtime.List() {
  20. wg.Add(1)
  21. go func(c *Container) {
  22. c.Kill()
  23. wg.Done()
  24. }(container)
  25. }
  26. wg.Wait()
  27. return os.RemoveAll(runtime.root)
  28. }
  29. func CopyDirectory(source, dest string) error {
  30. if _, err := exec.Command("cp", "-ra", source, dest).Output(); err != nil {
  31. return err
  32. }
  33. return nil
  34. }
  35. func layerArchive(tarfile string) (io.Reader, error) {
  36. // FIXME: need to close f somewhere
  37. f, err := os.Open(tarfile)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return f, nil
  42. }
  43. func init() {
  44. NO_MEMORY_LIMIT = os.Getenv("NO_MEMORY_LIMIT") == "1"
  45. // Hack to run sys init during unit testing
  46. if SelfPath() == "/sbin/init" {
  47. SysInit()
  48. return
  49. }
  50. if usr, err := user.Current(); err != nil {
  51. panic(err)
  52. } else if usr.Uid != "0" {
  53. panic("docker tests needs to be run as root")
  54. }
  55. // Create a temp directory
  56. root, err := ioutil.TempDir("", "docker-test")
  57. if err != nil {
  58. panic(err)
  59. }
  60. unitTestStoreBase = root
  61. // Make it our Store root
  62. runtime, err := NewRuntimeFromDirectory(root)
  63. if err != nil {
  64. panic(err)
  65. }
  66. // Create the "Server"
  67. srv := &Server{
  68. runtime: runtime,
  69. }
  70. // Retrieve the Image
  71. if err := srv.CmdPull(os.Stdin, rcli.NewDockerLocalConn(os.Stdout), unitTestImageName); err != nil {
  72. panic(err)
  73. }
  74. }
  75. func newTestRuntime() (*Runtime, error) {
  76. root, err := ioutil.TempDir("", "docker-test")
  77. if err != nil {
  78. return nil, err
  79. }
  80. if err := os.Remove(root); err != nil {
  81. return nil, err
  82. }
  83. if err := CopyDirectory(unitTestStoreBase, root); err != nil {
  84. return nil, err
  85. }
  86. runtime, err := NewRuntimeFromDirectory(root)
  87. if err != nil {
  88. return nil, err
  89. }
  90. return runtime, nil
  91. }
  92. func GetTestImage(runtime *Runtime) *Image {
  93. imgs, err := runtime.graph.All()
  94. if err != nil {
  95. panic(err)
  96. } else if len(imgs) < 1 {
  97. panic("GASP")
  98. }
  99. return imgs[0]
  100. }
  101. func TestRuntimeCreate(t *testing.T) {
  102. runtime, err := newTestRuntime()
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. defer nuke(runtime)
  107. // Make sure we start we 0 containers
  108. if len(runtime.List()) != 0 {
  109. t.Errorf("Expected 0 containers, %v found", len(runtime.List()))
  110. }
  111. container, err := runtime.Create(&Config{
  112. Image: GetTestImage(runtime).Id,
  113. Cmd: []string{"ls", "-al"},
  114. },
  115. )
  116. if err != nil {
  117. t.Fatal(err)
  118. }
  119. defer func() {
  120. if err := runtime.Destroy(container); err != nil {
  121. t.Error(err)
  122. }
  123. }()
  124. // Make sure we can find the newly created container with List()
  125. if len(runtime.List()) != 1 {
  126. t.Errorf("Expected 1 container, %v found", len(runtime.List()))
  127. }
  128. // Make sure the container List() returns is the right one
  129. if runtime.List()[0].Id != container.Id {
  130. t.Errorf("Unexpected container %v returned by List", runtime.List()[0])
  131. }
  132. // Make sure we can get the container with Get()
  133. if runtime.Get(container.Id) == nil {
  134. t.Errorf("Unable to get newly created container")
  135. }
  136. // Make sure it is the right container
  137. if runtime.Get(container.Id) != container {
  138. t.Errorf("Get() returned the wrong container")
  139. }
  140. // Make sure Exists returns it as existing
  141. if !runtime.Exists(container.Id) {
  142. t.Errorf("Exists() returned false for a newly created container")
  143. }
  144. }
  145. func TestDestroy(t *testing.T) {
  146. runtime, err := newTestRuntime()
  147. if err != nil {
  148. t.Fatal(err)
  149. }
  150. defer nuke(runtime)
  151. container, err := runtime.Create(&Config{
  152. Image: GetTestImage(runtime).Id,
  153. Cmd: []string{"ls", "-al"},
  154. },
  155. )
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. // Destroy
  160. if err := runtime.Destroy(container); err != nil {
  161. t.Error(err)
  162. }
  163. // Make sure runtime.Exists() behaves correctly
  164. if runtime.Exists("test_destroy") {
  165. t.Errorf("Exists() returned true")
  166. }
  167. // Make sure runtime.List() doesn't list the destroyed container
  168. if len(runtime.List()) != 0 {
  169. t.Errorf("Expected 0 container, %v found", len(runtime.List()))
  170. }
  171. // Make sure runtime.Get() refuses to return the unexisting container
  172. if runtime.Get(container.Id) != nil {
  173. t.Errorf("Unable to get newly created container")
  174. }
  175. // Make sure the container root directory does not exist anymore
  176. _, err = os.Stat(container.root)
  177. if err == nil || !os.IsNotExist(err) {
  178. t.Errorf("Container root directory still exists after destroy")
  179. }
  180. // Test double destroy
  181. if err := runtime.Destroy(container); err == nil {
  182. // It should have failed
  183. t.Errorf("Double destroy did not fail")
  184. }
  185. }
  186. func TestGet(t *testing.T) {
  187. runtime, err := newTestRuntime()
  188. if err != nil {
  189. t.Fatal(err)
  190. }
  191. defer nuke(runtime)
  192. container1, err := runtime.Create(&Config{
  193. Image: GetTestImage(runtime).Id,
  194. Cmd: []string{"ls", "-al"},
  195. },
  196. )
  197. if err != nil {
  198. t.Fatal(err)
  199. }
  200. defer runtime.Destroy(container1)
  201. container2, err := runtime.Create(&Config{
  202. Image: GetTestImage(runtime).Id,
  203. Cmd: []string{"ls", "-al"},
  204. },
  205. )
  206. if err != nil {
  207. t.Fatal(err)
  208. }
  209. defer runtime.Destroy(container2)
  210. container3, err := runtime.Create(&Config{
  211. Image: GetTestImage(runtime).Id,
  212. Cmd: []string{"ls", "-al"},
  213. },
  214. )
  215. if err != nil {
  216. t.Fatal(err)
  217. }
  218. defer runtime.Destroy(container3)
  219. if runtime.Get(container1.Id) != container1 {
  220. t.Errorf("Get(test1) returned %v while expecting %v", runtime.Get(container1.Id), container1)
  221. }
  222. if runtime.Get(container2.Id) != container2 {
  223. t.Errorf("Get(test2) returned %v while expecting %v", runtime.Get(container2.Id), container2)
  224. }
  225. if runtime.Get(container3.Id) != container3 {
  226. t.Errorf("Get(test3) returned %v while expecting %v", runtime.Get(container3.Id), container3)
  227. }
  228. }
  229. func TestRestore(t *testing.T) {
  230. root, err := ioutil.TempDir("", "docker-test")
  231. if err != nil {
  232. t.Fatal(err)
  233. }
  234. if err := os.Remove(root); err != nil {
  235. t.Fatal(err)
  236. }
  237. if err := CopyDirectory(unitTestStoreBase, root); err != nil {
  238. t.Fatal(err)
  239. }
  240. runtime1, err := NewRuntimeFromDirectory(root)
  241. if err != nil {
  242. t.Fatal(err)
  243. }
  244. // Create a container with one instance of docker
  245. container1, err := runtime1.Create(&Config{
  246. Image: GetTestImage(runtime1).Id,
  247. Cmd: []string{"ls", "-al"},
  248. },
  249. )
  250. if err != nil {
  251. t.Fatal(err)
  252. }
  253. defer runtime1.Destroy(container1)
  254. // Create a second container meant to be killed
  255. container2, err := runtime1.Create(&Config{
  256. Image: GetTestImage(runtime1).Id,
  257. Cmd: []string{"/bin/cat"},
  258. OpenStdin: true,
  259. },
  260. )
  261. if err != nil {
  262. t.Fatal(err)
  263. }
  264. defer runtime1.Destroy(container2)
  265. // Start the container non blocking
  266. if err := container2.Start(); err != nil {
  267. t.Fatal(err)
  268. }
  269. if !container2.State.Running {
  270. t.Fatalf("Container %v should appear as running but isn't", container2.Id)
  271. }
  272. // Simulate a crash/manual quit of dockerd: process dies, states stays 'Running'
  273. cStdin, _ := container2.StdinPipe()
  274. cStdin.Close()
  275. if err := container2.WaitTimeout(2 * time.Second); err != nil {
  276. t.Fatal(err)
  277. }
  278. container2.State.Running = true
  279. container2.ToDisk()
  280. if len(runtime1.List()) != 2 {
  281. t.Errorf("Expected 2 container, %v found", len(runtime1.List()))
  282. }
  283. if err := container1.Run(); err != nil {
  284. t.Fatal(err)
  285. }
  286. if !container2.State.Running {
  287. t.Fatalf("Container %v should appear as running but isn't", container2.Id)
  288. }
  289. // Here are are simulating a docker restart - that is, reloading all containers
  290. // from scratch
  291. runtime2, err := NewRuntimeFromDirectory(root)
  292. if err != nil {
  293. t.Fatal(err)
  294. }
  295. defer nuke(runtime2)
  296. if len(runtime2.List()) != 2 {
  297. t.Errorf("Expected 2 container, %v found", len(runtime2.List()))
  298. }
  299. runningCount := 0
  300. for _, c := range runtime2.List() {
  301. if c.State.Running {
  302. t.Errorf("Running container found: %v (%v)", c.Id, c.Path)
  303. runningCount++
  304. }
  305. }
  306. if runningCount != 0 {
  307. t.Fatalf("Expected 0 container alive, %d found", runningCount)
  308. }
  309. container3 := runtime2.Get(container1.Id)
  310. if container3 == nil {
  311. t.Fatal("Unable to Get container")
  312. }
  313. if err := container3.Run(); err != nil {
  314. t.Fatal(err)
  315. }
  316. container2.State.Running = false
  317. }