runtime_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package docker
  2. import (
  3. "github.com/dotcloud/docker/graph"
  4. "io"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. "os/user"
  9. "testing"
  10. )
  11. const testLayerPath string = "/var/lib/docker/docker-ut.tar"
  12. const unitTestImageName string = "busybox"
  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 := NewFromDirectory(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.CmdImport(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 := NewFromDirectory(root)
  76. if err != nil {
  77. return nil, err
  78. }
  79. return runtime, nil
  80. }
  81. func GetTestImage(runtime *Runtime) *graph.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 TestCreate(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(
  101. "ls",
  102. []string{"-al"},
  103. GetTestImage(runtime).Id,
  104. &Config{},
  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(
  142. "ls",
  143. []string{"-al"},
  144. GetTestImage(runtime).Id,
  145. &Config{},
  146. )
  147. if err != nil {
  148. t.Fatal(err)
  149. }
  150. // Destroy
  151. if err := runtime.Destroy(container); err != nil {
  152. t.Error(err)
  153. }
  154. // Make sure runtime.Exists() behaves correctly
  155. if runtime.Exists("test_destroy") {
  156. t.Errorf("Exists() returned true")
  157. }
  158. // Make sure runtime.List() doesn't list the destroyed container
  159. if len(runtime.List()) != 0 {
  160. t.Errorf("Expected 0 container, %v found", len(runtime.List()))
  161. }
  162. // Make sure runtime.Get() refuses to return the unexisting container
  163. if runtime.Get(container.Id) != nil {
  164. t.Errorf("Unable to get newly created container")
  165. }
  166. // Make sure the container root directory does not exist anymore
  167. _, err = os.Stat(container.root)
  168. if err == nil || !os.IsNotExist(err) {
  169. t.Errorf("Container root directory still exists after destroy")
  170. }
  171. // Test double destroy
  172. if err := runtime.Destroy(container); err == nil {
  173. // It should have failed
  174. t.Errorf("Double destroy did not fail")
  175. }
  176. }
  177. func TestGet(t *testing.T) {
  178. runtime, err := newTestRuntime()
  179. if err != nil {
  180. t.Fatal(err)
  181. }
  182. defer nuke(runtime)
  183. container1, err := runtime.Create(
  184. "ls",
  185. []string{"-al"},
  186. GetTestImage(runtime).Id,
  187. &Config{},
  188. )
  189. if err != nil {
  190. t.Fatal(err)
  191. }
  192. defer runtime.Destroy(container1)
  193. container2, err := runtime.Create(
  194. "ls",
  195. []string{"-al"},
  196. GetTestImage(runtime).Id,
  197. &Config{},
  198. )
  199. if err != nil {
  200. t.Fatal(err)
  201. }
  202. defer runtime.Destroy(container2)
  203. container3, err := runtime.Create(
  204. "ls",
  205. []string{"-al"},
  206. GetTestImage(runtime).Id,
  207. &Config{},
  208. )
  209. if err != nil {
  210. t.Fatal(err)
  211. }
  212. defer runtime.Destroy(container3)
  213. if runtime.Get(container1.Id) != container1 {
  214. t.Errorf("Get(test1) returned %v while expecting %v", runtime.Get(container1.Id), container1)
  215. }
  216. if runtime.Get(container2.Id) != container2 {
  217. t.Errorf("Get(test2) returned %v while expecting %v", runtime.Get(container2.Id), container2)
  218. }
  219. if runtime.Get(container3.Id) != container3 {
  220. t.Errorf("Get(test3) returned %v while expecting %v", runtime.Get(container3.Id), container3)
  221. }
  222. }
  223. func TestRestore(t *testing.T) {
  224. root, err := ioutil.TempDir("", "docker-test")
  225. if err != nil {
  226. t.Fatal(err)
  227. }
  228. if err := os.Remove(root); err != nil {
  229. t.Fatal(err)
  230. }
  231. if err := CopyDirectory(unitTestStoreBase, root); err != nil {
  232. t.Fatal(err)
  233. }
  234. runtime1, err := NewFromDirectory(root)
  235. if err != nil {
  236. t.Fatal(err)
  237. }
  238. // Create a container with one instance of docker
  239. container1, err := runtime1.Create(
  240. "ls",
  241. []string{"-al"},
  242. GetTestImage(runtime1).Id,
  243. &Config{},
  244. )
  245. if err != nil {
  246. t.Fatal(err)
  247. }
  248. defer runtime1.Destroy(container1)
  249. if len(runtime1.List()) != 1 {
  250. t.Errorf("Expected 1 container, %v found", len(runtime1.List()))
  251. }
  252. if err := container1.Run(); err != nil {
  253. t.Fatal(err)
  254. }
  255. // Here are are simulating a docker restart - that is, reloading all containers
  256. // from scratch
  257. runtime2, err := NewFromDirectory(root)
  258. if err != nil {
  259. t.Fatal(err)
  260. }
  261. defer nuke(runtime2)
  262. if len(runtime2.List()) != 1 {
  263. t.Errorf("Expected 1 container, %v found", len(runtime2.List()))
  264. }
  265. container2 := runtime2.Get(container1.Id)
  266. if container2 == nil {
  267. t.Fatal("Unable to Get container")
  268. }
  269. if err := container2.Run(); err != nil {
  270. t.Fatal(err)
  271. }
  272. }