runtime_test.go 7.7 KB

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