runtime_test.go 6.6 KB

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