runtime_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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.Add(-1)
  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. panic(err)
  83. return nil, err
  84. }
  85. runtime, err := NewRuntimeFromDirectory(root)
  86. if err != nil {
  87. return nil, err
  88. }
  89. return runtime, nil
  90. }
  91. func GetTestImage(runtime *Runtime) *Image {
  92. imgs, err := runtime.graph.All()
  93. if err != nil {
  94. panic(err)
  95. } else if len(imgs) < 1 {
  96. panic("GASP")
  97. }
  98. return imgs[0]
  99. }
  100. func TestRuntimeCreate(t *testing.T) {
  101. runtime, err := newTestRuntime()
  102. if err != nil {
  103. t.Fatal(err)
  104. }
  105. defer nuke(runtime)
  106. // Make sure we start we 0 containers
  107. if len(runtime.List()) != 0 {
  108. t.Errorf("Expected 0 containers, %v found", len(runtime.List()))
  109. }
  110. container, err := runtime.Create(&Config{
  111. Image: GetTestImage(runtime).Id,
  112. Cmd: []string{"ls", "-al"},
  113. },
  114. )
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. defer func() {
  119. if err := runtime.Destroy(container); err != nil {
  120. t.Error(err)
  121. }
  122. }()
  123. // Make sure we can find the newly created container with List()
  124. if len(runtime.List()) != 1 {
  125. t.Errorf("Expected 1 container, %v found", len(runtime.List()))
  126. }
  127. // Make sure the container List() returns is the right one
  128. if runtime.List()[0].Id != container.Id {
  129. t.Errorf("Unexpected container %v returned by List", runtime.List()[0])
  130. }
  131. // Make sure we can get the container with Get()
  132. if runtime.Get(container.Id) == nil {
  133. t.Errorf("Unable to get newly created container")
  134. }
  135. // Make sure it is the right container
  136. if runtime.Get(container.Id) != container {
  137. t.Errorf("Get() returned the wrong container")
  138. }
  139. // Make sure Exists returns it as existing
  140. if !runtime.Exists(container.Id) {
  141. t.Errorf("Exists() returned false for a newly created container")
  142. }
  143. }
  144. func TestDestroy(t *testing.T) {
  145. runtime, err := newTestRuntime()
  146. if err != nil {
  147. t.Fatal(err)
  148. }
  149. defer nuke(runtime)
  150. container, err := runtime.Create(&Config{
  151. Image: GetTestImage(runtime).Id,
  152. Cmd: []string{"ls", "-al"},
  153. },
  154. )
  155. if err != nil {
  156. t.Fatal(err)
  157. }
  158. // Destroy
  159. if err := runtime.Destroy(container); err != nil {
  160. t.Error(err)
  161. }
  162. // Make sure runtime.Exists() behaves correctly
  163. if runtime.Exists("test_destroy") {
  164. t.Errorf("Exists() returned true")
  165. }
  166. // Make sure runtime.List() doesn't list the destroyed container
  167. if len(runtime.List()) != 0 {
  168. t.Errorf("Expected 0 container, %v found", len(runtime.List()))
  169. }
  170. // Make sure runtime.Get() refuses to return the unexisting container
  171. if runtime.Get(container.Id) != nil {
  172. t.Errorf("Unable to get newly created container")
  173. }
  174. // Make sure the container root directory does not exist anymore
  175. _, err = os.Stat(container.root)
  176. if err == nil || !os.IsNotExist(err) {
  177. t.Errorf("Container root directory still exists after destroy")
  178. }
  179. // Test double destroy
  180. if err := runtime.Destroy(container); err == nil {
  181. // It should have failed
  182. t.Errorf("Double destroy did not fail")
  183. }
  184. }
  185. func TestGet(t *testing.T) {
  186. runtime, err := newTestRuntime()
  187. if err != nil {
  188. t.Fatal(err)
  189. }
  190. defer nuke(runtime)
  191. container1, 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(container1)
  200. container2, 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(container2)
  209. container3, err := runtime.Create(&Config{
  210. Image: GetTestImage(runtime).Id,
  211. Cmd: []string{"ls", "-al"},
  212. },
  213. )
  214. if err != nil {
  215. t.Fatal(err)
  216. }
  217. defer runtime.Destroy(container3)
  218. if runtime.Get(container1.Id) != container1 {
  219. t.Errorf("Get(test1) returned %v while expecting %v", runtime.Get(container1.Id), container1)
  220. }
  221. if runtime.Get(container2.Id) != container2 {
  222. t.Errorf("Get(test2) returned %v while expecting %v", runtime.Get(container2.Id), container2)
  223. }
  224. if runtime.Get(container3.Id) != container3 {
  225. t.Errorf("Get(test3) returned %v while expecting %v", runtime.Get(container3.Id), container3)
  226. }
  227. }
  228. func TestRestore(t *testing.T) {
  229. root, err := ioutil.TempDir("", "docker-test")
  230. if err != nil {
  231. t.Fatal(err)
  232. }
  233. if err := os.Remove(root); err != nil {
  234. t.Fatal(err)
  235. }
  236. if err := CopyDirectory(unitTestStoreBase, root); err != nil {
  237. t.Fatal(err)
  238. }
  239. runtime1, err := NewRuntimeFromDirectory(root)
  240. if err != nil {
  241. t.Fatal(err)
  242. }
  243. // Create a container with one instance of docker
  244. container1, err := runtime1.Create(&Config{
  245. Image: GetTestImage(runtime1).Id,
  246. Cmd: []string{"ls", "-al"},
  247. },
  248. )
  249. if err != nil {
  250. t.Fatal(err)
  251. }
  252. defer runtime1.Destroy(container1)
  253. if len(runtime1.List()) != 1 {
  254. t.Errorf("Expected 1 container, %v found", len(runtime1.List()))
  255. }
  256. if err := container1.Run(); err != nil {
  257. t.Fatal(err)
  258. }
  259. // Here are are simulating a docker restart - that is, reloading all containers
  260. // from scratch
  261. runtime2, err := NewRuntimeFromDirectory(root)
  262. if err != nil {
  263. t.Fatal(err)
  264. }
  265. defer nuke(runtime2)
  266. if len(runtime2.List()) != 1 {
  267. t.Errorf("Expected 1 container, %v found", len(runtime2.List()))
  268. }
  269. container2 := runtime2.Get(container1.Id)
  270. if container2 == nil {
  271. t.Fatal("Unable to Get container")
  272. }
  273. if err := container2.Run(); err != nil {
  274. t.Fatal(err)
  275. }
  276. }