runtime_test.go 6.5 KB

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