runtime_test.go 6.8 KB

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