runtime_test.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. package docker
  2. import (
  3. "fmt"
  4. "github.com/dotcloud/docker/rcli"
  5. "io"
  6. "io/ioutil"
  7. "net"
  8. "os"
  9. "os/exec"
  10. "os/user"
  11. "sync"
  12. "testing"
  13. "time"
  14. )
  15. const unitTestImageName string = "docker-ut"
  16. const unitTestStoreBase string = "/var/lib/docker/unit-tests"
  17. func nuke(runtime *Runtime) error {
  18. var wg sync.WaitGroup
  19. for _, container := range runtime.List() {
  20. wg.Add(1)
  21. go func(c *Container) {
  22. c.Kill()
  23. wg.Done()
  24. }(container)
  25. }
  26. wg.Wait()
  27. return os.RemoveAll(runtime.root)
  28. }
  29. func CopyDirectory(source, dest string) error {
  30. if _, err := exec.Command("cp", "-ra", source, dest).Output(); err != nil {
  31. return err
  32. }
  33. return nil
  34. }
  35. func layerArchive(tarfile string) (io.Reader, error) {
  36. // FIXME: need to close f somewhere
  37. f, err := os.Open(tarfile)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return f, nil
  42. }
  43. func init() {
  44. // Hack to run sys init during unit testing
  45. if SelfPath() == "/sbin/init" {
  46. SysInit()
  47. return
  48. }
  49. if usr, err := user.Current(); err != nil {
  50. panic(err)
  51. } else if usr.Uid != "0" {
  52. panic("docker tests needs to be run as root")
  53. }
  54. NetworkBridgeIface = "testdockbr0"
  55. // Make it our Store root
  56. runtime, err := NewRuntimeFromDirectory(unitTestStoreBase)
  57. if err != nil {
  58. panic(err)
  59. }
  60. // Create the "Server"
  61. srv := &Server{
  62. runtime: runtime,
  63. }
  64. // Retrieve the Image
  65. if err := srv.CmdPull(os.Stdin, rcli.NewDockerLocalConn(os.Stdout), unitTestImageName); err != nil {
  66. panic(err)
  67. }
  68. }
  69. func newTestRuntime() (*Runtime, error) {
  70. root, err := ioutil.TempDir("", "docker-test")
  71. if err != nil {
  72. return nil, err
  73. }
  74. if err := os.Remove(root); err != nil {
  75. return nil, err
  76. }
  77. if err := CopyDirectory(unitTestStoreBase, root); err != nil {
  78. return nil, err
  79. }
  80. runtime, err := NewRuntimeFromDirectory(root)
  81. if err != nil {
  82. return nil, err
  83. }
  84. return runtime, nil
  85. }
  86. func GetTestImage(runtime *Runtime) *Image {
  87. imgs, err := runtime.graph.All()
  88. if err != nil {
  89. panic(err)
  90. } else if len(imgs) < 1 {
  91. panic("GASP")
  92. }
  93. return imgs[0]
  94. }
  95. func TestRuntimeCreate(t *testing.T) {
  96. runtime, err := newTestRuntime()
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. defer nuke(runtime)
  101. // Make sure we start we 0 containers
  102. if len(runtime.List()) != 0 {
  103. t.Errorf("Expected 0 containers, %v found", len(runtime.List()))
  104. }
  105. container, err := runtime.Create(&Config{
  106. Image: GetTestImage(runtime).Id,
  107. Cmd: []string{"ls", "-al"},
  108. },
  109. )
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. defer func() {
  114. if err := runtime.Destroy(container); err != nil {
  115. t.Error(err)
  116. }
  117. }()
  118. // Make sure we can find the newly created container with List()
  119. if len(runtime.List()) != 1 {
  120. t.Errorf("Expected 1 container, %v found", len(runtime.List()))
  121. }
  122. // Make sure the container List() returns is the right one
  123. if runtime.List()[0].Id != container.Id {
  124. t.Errorf("Unexpected container %v returned by List", runtime.List()[0])
  125. }
  126. // Make sure we can get the container with Get()
  127. if runtime.Get(container.Id) == nil {
  128. t.Errorf("Unable to get newly created container")
  129. }
  130. // Make sure it is the right container
  131. if runtime.Get(container.Id) != container {
  132. t.Errorf("Get() returned the wrong container")
  133. }
  134. // Make sure Exists returns it as existing
  135. if !runtime.Exists(container.Id) {
  136. t.Errorf("Exists() returned false for a newly created container")
  137. }
  138. }
  139. func TestDestroy(t *testing.T) {
  140. runtime, err := newTestRuntime()
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. defer nuke(runtime)
  145. container, err := runtime.Create(&Config{
  146. Image: GetTestImage(runtime).Id,
  147. Cmd: []string{"ls", "-al"},
  148. },
  149. )
  150. if err != nil {
  151. t.Fatal(err)
  152. }
  153. // Destroy
  154. if err := runtime.Destroy(container); err != nil {
  155. t.Error(err)
  156. }
  157. // Make sure runtime.Exists() behaves correctly
  158. if runtime.Exists("test_destroy") {
  159. t.Errorf("Exists() returned true")
  160. }
  161. // Make sure runtime.List() doesn't list the destroyed container
  162. if len(runtime.List()) != 0 {
  163. t.Errorf("Expected 0 container, %v found", len(runtime.List()))
  164. }
  165. // Make sure runtime.Get() refuses to return the unexisting container
  166. if runtime.Get(container.Id) != nil {
  167. t.Errorf("Unable to get newly created container")
  168. }
  169. // Make sure the container root directory does not exist anymore
  170. _, err = os.Stat(container.root)
  171. if err == nil || !os.IsNotExist(err) {
  172. t.Errorf("Container root directory still exists after destroy")
  173. }
  174. // Test double destroy
  175. if err := runtime.Destroy(container); err == nil {
  176. // It should have failed
  177. t.Errorf("Double destroy did not fail")
  178. }
  179. }
  180. func TestGet(t *testing.T) {
  181. runtime, err := newTestRuntime()
  182. if err != nil {
  183. t.Fatal(err)
  184. }
  185. defer nuke(runtime)
  186. container1, err := runtime.Create(&Config{
  187. Image: GetTestImage(runtime).Id,
  188. Cmd: []string{"ls", "-al"},
  189. },
  190. )
  191. if err != nil {
  192. t.Fatal(err)
  193. }
  194. defer runtime.Destroy(container1)
  195. container2, err := runtime.Create(&Config{
  196. Image: GetTestImage(runtime).Id,
  197. Cmd: []string{"ls", "-al"},
  198. },
  199. )
  200. if err != nil {
  201. t.Fatal(err)
  202. }
  203. defer runtime.Destroy(container2)
  204. container3, err := runtime.Create(&Config{
  205. Image: GetTestImage(runtime).Id,
  206. Cmd: []string{"ls", "-al"},
  207. },
  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. // Run a container with a TCP port allocated, and test that it can receive connections on localhost
  224. func TestAllocatePortLocalhost(t *testing.T) {
  225. runtime, err := newTestRuntime()
  226. if err != nil {
  227. t.Fatal(err)
  228. }
  229. container, err := runtime.Create(&Config{
  230. Image: GetTestImage(runtime).Id,
  231. Cmd: []string{"sh", "-c", "echo well hello there | nc -l -p 5555"},
  232. PortSpecs: []string{"5555"},
  233. },
  234. )
  235. if err != nil {
  236. t.Fatal(err)
  237. }
  238. if err := container.Start(); err != nil {
  239. t.Fatal(err)
  240. }
  241. defer container.Kill()
  242. setTimeout(t, "Waiting for the container to be started timed out", 2*time.Second, func() {
  243. for {
  244. if container.State.Running {
  245. break
  246. }
  247. time.Sleep(10 * time.Millisecond)
  248. }
  249. })
  250. conn, err := net.Dial("tcp",
  251. fmt.Sprintf(
  252. "localhost:%s", container.NetworkSettings.PortMapping["5555"],
  253. ),
  254. )
  255. if err != nil {
  256. t.Fatal(err)
  257. }
  258. defer conn.Close()
  259. output, err := ioutil.ReadAll(conn)
  260. if err != nil {
  261. t.Fatal(err)
  262. }
  263. if string(output) != "well hello there\n" {
  264. t.Fatalf("Received wrong output from network connection: should be '%s', not '%s'",
  265. "well hello there\n",
  266. string(output),
  267. )
  268. }
  269. container.Wait()
  270. }
  271. func TestRestore(t *testing.T) {
  272. root, err := ioutil.TempDir("", "docker-test")
  273. if err != nil {
  274. t.Fatal(err)
  275. }
  276. if err := os.Remove(root); err != nil {
  277. t.Fatal(err)
  278. }
  279. if err := CopyDirectory(unitTestStoreBase, root); err != nil {
  280. t.Fatal(err)
  281. }
  282. runtime1, err := NewRuntimeFromDirectory(root)
  283. if err != nil {
  284. t.Fatal(err)
  285. }
  286. // Create a container with one instance of docker
  287. container1, err := runtime1.Create(&Config{
  288. Image: GetTestImage(runtime1).Id,
  289. Cmd: []string{"ls", "-al"},
  290. },
  291. )
  292. if err != nil {
  293. t.Fatal(err)
  294. }
  295. defer runtime1.Destroy(container1)
  296. // Create a second container meant to be killed
  297. container2, err := runtime1.Create(&Config{
  298. Image: GetTestImage(runtime1).Id,
  299. Cmd: []string{"/bin/cat"},
  300. OpenStdin: true,
  301. },
  302. )
  303. if err != nil {
  304. t.Fatal(err)
  305. }
  306. defer runtime1.Destroy(container2)
  307. // Start the container non blocking
  308. if err := container2.Start(); err != nil {
  309. t.Fatal(err)
  310. }
  311. if !container2.State.Running {
  312. t.Fatalf("Container %v should appear as running but isn't", container2.Id)
  313. }
  314. // Simulate a crash/manual quit of dockerd: process dies, states stays 'Running'
  315. cStdin, _ := container2.StdinPipe()
  316. cStdin.Close()
  317. if err := container2.WaitTimeout(2 * time.Second); err != nil {
  318. t.Fatal(err)
  319. }
  320. container2.State.Running = true
  321. container2.ToDisk()
  322. if len(runtime1.List()) != 2 {
  323. t.Errorf("Expected 2 container, %v found", len(runtime1.List()))
  324. }
  325. if err := container1.Run(); err != nil {
  326. t.Fatal(err)
  327. }
  328. if !container2.State.Running {
  329. t.Fatalf("Container %v should appear as running but isn't", container2.Id)
  330. }
  331. // Here are are simulating a docker restart - that is, reloading all containers
  332. // from scratch
  333. runtime2, err := NewRuntimeFromDirectory(root)
  334. if err != nil {
  335. t.Fatal(err)
  336. }
  337. defer nuke(runtime2)
  338. if len(runtime2.List()) != 2 {
  339. t.Errorf("Expected 2 container, %v found", len(runtime2.List()))
  340. }
  341. runningCount := 0
  342. for _, c := range runtime2.List() {
  343. if c.State.Running {
  344. t.Errorf("Running container found: %v (%v)", c.Id, c.Path)
  345. runningCount++
  346. }
  347. }
  348. if runningCount != 0 {
  349. t.Fatalf("Expected 0 container alive, %d found", runningCount)
  350. }
  351. container3 := runtime2.Get(container1.Id)
  352. if container3 == nil {
  353. t.Fatal("Unable to Get container")
  354. }
  355. if err := container3.Run(); err != nil {
  356. t.Fatal(err)
  357. }
  358. container2.State.Running = false
  359. }