runtime_test.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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, false)
  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, false)
  81. if err != nil {
  82. return nil, err
  83. }
  84. runtime.UpdateCapabilities(true)
  85. return runtime, nil
  86. }
  87. func GetTestImage(runtime *Runtime) *Image {
  88. imgs, err := runtime.graph.All()
  89. if err != nil {
  90. panic(err)
  91. } else if len(imgs) < 1 {
  92. panic("GASP")
  93. }
  94. return imgs[0]
  95. }
  96. func TestRuntimeCreate(t *testing.T) {
  97. runtime, err := newTestRuntime()
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. defer nuke(runtime)
  102. // Make sure we start we 0 containers
  103. if len(runtime.List()) != 0 {
  104. t.Errorf("Expected 0 containers, %v found", len(runtime.List()))
  105. }
  106. container, err := NewBuilder(runtime).Create(&Config{
  107. Image: GetTestImage(runtime).Id,
  108. Cmd: []string{"ls", "-al"},
  109. },
  110. )
  111. if err != nil {
  112. t.Fatal(err)
  113. }
  114. defer func() {
  115. if err := runtime.Destroy(container); err != nil {
  116. t.Error(err)
  117. }
  118. }()
  119. // Make sure we can find the newly created container with List()
  120. if len(runtime.List()) != 1 {
  121. t.Errorf("Expected 1 container, %v found", len(runtime.List()))
  122. }
  123. // Make sure the container List() returns is the right one
  124. if runtime.List()[0].Id != container.Id {
  125. t.Errorf("Unexpected container %v returned by List", runtime.List()[0])
  126. }
  127. // Make sure we can get the container with Get()
  128. if runtime.Get(container.Id) == nil {
  129. t.Errorf("Unable to get newly created container")
  130. }
  131. // Make sure it is the right container
  132. if runtime.Get(container.Id) != container {
  133. t.Errorf("Get() returned the wrong container")
  134. }
  135. // Make sure Exists returns it as existing
  136. if !runtime.Exists(container.Id) {
  137. t.Errorf("Exists() returned false for a newly created container")
  138. }
  139. }
  140. func TestDestroy(t *testing.T) {
  141. runtime, err := newTestRuntime()
  142. if err != nil {
  143. t.Fatal(err)
  144. }
  145. defer nuke(runtime)
  146. container, err := NewBuilder(runtime).Create(&Config{
  147. Image: GetTestImage(runtime).Id,
  148. Cmd: []string{"ls", "-al"},
  149. },
  150. )
  151. if err != nil {
  152. t.Fatal(err)
  153. }
  154. // Destroy
  155. if err := runtime.Destroy(container); err != nil {
  156. t.Error(err)
  157. }
  158. // Make sure runtime.Exists() behaves correctly
  159. if runtime.Exists("test_destroy") {
  160. t.Errorf("Exists() returned true")
  161. }
  162. // Make sure runtime.List() doesn't list the destroyed container
  163. if len(runtime.List()) != 0 {
  164. t.Errorf("Expected 0 container, %v found", len(runtime.List()))
  165. }
  166. // Make sure runtime.Get() refuses to return the unexisting container
  167. if runtime.Get(container.Id) != nil {
  168. t.Errorf("Unable to get newly created container")
  169. }
  170. // Make sure the container root directory does not exist anymore
  171. _, err = os.Stat(container.root)
  172. if err == nil || !os.IsNotExist(err) {
  173. t.Errorf("Container root directory still exists after destroy")
  174. }
  175. // Test double destroy
  176. if err := runtime.Destroy(container); err == nil {
  177. // It should have failed
  178. t.Errorf("Double destroy did not fail")
  179. }
  180. }
  181. func TestGet(t *testing.T) {
  182. runtime, err := newTestRuntime()
  183. if err != nil {
  184. t.Fatal(err)
  185. }
  186. defer nuke(runtime)
  187. builder := NewBuilder(runtime)
  188. container1, err := builder.Create(&Config{
  189. Image: GetTestImage(runtime).Id,
  190. Cmd: []string{"ls", "-al"},
  191. },
  192. )
  193. if err != nil {
  194. t.Fatal(err)
  195. }
  196. defer runtime.Destroy(container1)
  197. container2, err := builder.Create(&Config{
  198. Image: GetTestImage(runtime).Id,
  199. Cmd: []string{"ls", "-al"},
  200. },
  201. )
  202. if err != nil {
  203. t.Fatal(err)
  204. }
  205. defer runtime.Destroy(container2)
  206. container3, err := builder.Create(&Config{
  207. Image: GetTestImage(runtime).Id,
  208. Cmd: []string{"ls", "-al"},
  209. },
  210. )
  211. if err != nil {
  212. t.Fatal(err)
  213. }
  214. defer runtime.Destroy(container3)
  215. if runtime.Get(container1.Id) != container1 {
  216. t.Errorf("Get(test1) returned %v while expecting %v", runtime.Get(container1.Id), container1)
  217. }
  218. if runtime.Get(container2.Id) != container2 {
  219. t.Errorf("Get(test2) returned %v while expecting %v", runtime.Get(container2.Id), container2)
  220. }
  221. if runtime.Get(container3.Id) != container3 {
  222. t.Errorf("Get(test3) returned %v while expecting %v", runtime.Get(container3.Id), container3)
  223. }
  224. }
  225. // Run a container with a TCP port allocated, and test that it can receive connections on localhost
  226. func TestAllocatePortLocalhost(t *testing.T) {
  227. runtime, err := newTestRuntime()
  228. if err != nil {
  229. t.Fatal(err)
  230. }
  231. container, err := NewBuilder(runtime).Create(&Config{
  232. Image: GetTestImage(runtime).Id,
  233. Cmd: []string{"sh", "-c", "echo well hello there | nc -l -p 5555"},
  234. PortSpecs: []string{"5555"},
  235. },
  236. )
  237. if err != nil {
  238. t.Fatal(err)
  239. }
  240. if err := container.Start(); err != nil {
  241. t.Fatal(err)
  242. }
  243. defer container.Kill()
  244. setTimeout(t, "Waiting for the container to be started timed out", 2*time.Second, func() {
  245. for {
  246. if container.State.Running {
  247. break
  248. }
  249. time.Sleep(10 * time.Millisecond)
  250. }
  251. })
  252. conn, err := net.Dial("tcp",
  253. fmt.Sprintf(
  254. "localhost:%s", container.NetworkSettings.PortMapping["5555"],
  255. ),
  256. )
  257. if err != nil {
  258. t.Fatal(err)
  259. }
  260. defer conn.Close()
  261. output, err := ioutil.ReadAll(conn)
  262. if err != nil {
  263. t.Fatal(err)
  264. }
  265. if string(output) != "well hello there\n" {
  266. t.Fatalf("Received wrong output from network connection: should be '%s', not '%s'",
  267. "well hello there\n",
  268. string(output),
  269. )
  270. }
  271. container.Wait()
  272. }
  273. func TestRestore(t *testing.T) {
  274. root, err := ioutil.TempDir("", "docker-test")
  275. if err != nil {
  276. t.Fatal(err)
  277. }
  278. if err := os.Remove(root); err != nil {
  279. t.Fatal(err)
  280. }
  281. if err := CopyDirectory(unitTestStoreBase, root); err != nil {
  282. t.Fatal(err)
  283. }
  284. runtime1, err := NewRuntimeFromDirectory(root, false)
  285. if err != nil {
  286. t.Fatal(err)
  287. }
  288. builder := NewBuilder(runtime1)
  289. // Create a container with one instance of docker
  290. container1, err := builder.Create(&Config{
  291. Image: GetTestImage(runtime1).Id,
  292. Cmd: []string{"ls", "-al"},
  293. },
  294. )
  295. if err != nil {
  296. t.Fatal(err)
  297. }
  298. defer runtime1.Destroy(container1)
  299. // Create a second container meant to be killed
  300. container2, err := builder.Create(&Config{
  301. Image: GetTestImage(runtime1).Id,
  302. Cmd: []string{"/bin/cat"},
  303. OpenStdin: true,
  304. },
  305. )
  306. if err != nil {
  307. t.Fatal(err)
  308. }
  309. defer runtime1.Destroy(container2)
  310. // Start the container non blocking
  311. if err := container2.Start(); err != nil {
  312. t.Fatal(err)
  313. }
  314. if !container2.State.Running {
  315. t.Fatalf("Container %v should appear as running but isn't", container2.Id)
  316. }
  317. // Simulate a crash/manual quit of dockerd: process dies, states stays 'Running'
  318. cStdin, _ := container2.StdinPipe()
  319. cStdin.Close()
  320. if err := container2.WaitTimeout(2 * time.Second); err != nil {
  321. t.Fatal(err)
  322. }
  323. container2.State.Running = true
  324. container2.ToDisk()
  325. if len(runtime1.List()) != 2 {
  326. t.Errorf("Expected 2 container, %v found", len(runtime1.List()))
  327. }
  328. if err := container1.Run(); err != nil {
  329. t.Fatal(err)
  330. }
  331. if !container2.State.Running {
  332. t.Fatalf("Container %v should appear as running but isn't", container2.Id)
  333. }
  334. // Here are are simulating a docker restart - that is, reloading all containers
  335. // from scratch
  336. runtime2, err := NewRuntimeFromDirectory(root, false)
  337. if err != nil {
  338. t.Fatal(err)
  339. }
  340. defer nuke(runtime2)
  341. if len(runtime2.List()) != 2 {
  342. t.Errorf("Expected 2 container, %v found", len(runtime2.List()))
  343. }
  344. runningCount := 0
  345. for _, c := range runtime2.List() {
  346. if c.State.Running {
  347. t.Errorf("Running container found: %v (%v)", c.Id, c.Path)
  348. runningCount++
  349. }
  350. }
  351. if runningCount != 0 {
  352. t.Fatalf("Expected 0 container alive, %d found", runningCount)
  353. }
  354. container3 := runtime2.Get(container1.Id)
  355. if container3 == nil {
  356. t.Fatal("Unable to Get container")
  357. }
  358. if err := container3.Run(); err != nil {
  359. t.Fatal(err)
  360. }
  361. container2.State.Running = false
  362. }