runtime_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. package docker
  2. import (
  3. "fmt"
  4. "github.com/dotcloud/docker/utils"
  5. "io"
  6. "io/ioutil"
  7. "log"
  8. "net"
  9. "os"
  10. "strconv"
  11. "strings"
  12. "sync"
  13. "syscall"
  14. "testing"
  15. "time"
  16. )
  17. const (
  18. unitTestImageName = "docker-unit-tests"
  19. unitTestImageId = "e9aa60c60128cad1"
  20. unitTestStoreBase = "/var/lib/docker/unit-tests"
  21. testDaemonAddr = "127.0.0.1:4270"
  22. testDaemonProto = "tcp"
  23. )
  24. var globalRuntime *Runtime
  25. func nuke(runtime *Runtime) error {
  26. var wg sync.WaitGroup
  27. for _, container := range runtime.List() {
  28. wg.Add(1)
  29. go func(c *Container) {
  30. c.Kill()
  31. wg.Done()
  32. }(container)
  33. }
  34. wg.Wait()
  35. return os.RemoveAll(runtime.root)
  36. }
  37. func cleanup(runtime *Runtime) error {
  38. for _, container := range runtime.List() {
  39. container.Kill()
  40. runtime.Destroy(container)
  41. }
  42. images, err := runtime.graph.All()
  43. if err != nil {
  44. return err
  45. }
  46. for _, image := range images {
  47. if image.ID != unitTestImageId {
  48. runtime.graph.Delete(image.ID)
  49. }
  50. }
  51. return nil
  52. }
  53. func layerArchive(tarfile string) (io.Reader, error) {
  54. // FIXME: need to close f somewhere
  55. f, err := os.Open(tarfile)
  56. if err != nil {
  57. return nil, err
  58. }
  59. return f, nil
  60. }
  61. func init() {
  62. // Hack to run sys init during unit testing
  63. if utils.SelfPath() == "/sbin/init" {
  64. SysInit()
  65. return
  66. }
  67. if uid := syscall.Geteuid(); uid != 0 {
  68. log.Fatal("docker tests needs to be run as root")
  69. }
  70. NetworkBridgeIface = "testdockbr0"
  71. // Make it our Store root
  72. runtime, err := NewRuntimeFromDirectory(unitTestStoreBase, false)
  73. if err != nil {
  74. panic(err)
  75. }
  76. globalRuntime = runtime
  77. // Create the "Server"
  78. srv := &Server{
  79. runtime: runtime,
  80. enableCors: false,
  81. pullingPool: make(map[string]struct{}),
  82. pushingPool: make(map[string]struct{}),
  83. }
  84. // Retrieve the Image
  85. if err := srv.ImagePull(unitTestImageName, "", "", os.Stdout, utils.NewStreamFormatter(false), nil); err != nil {
  86. panic(err)
  87. }
  88. // Spawn a Daemon
  89. go func() {
  90. if err := ListenAndServe(testDaemonProto, testDaemonAddr, srv, os.Getenv("DEBUG") != ""); err != nil {
  91. panic(err)
  92. }
  93. }()
  94. // Give some time to ListenAndServer to actually start
  95. time.Sleep(time.Second)
  96. }
  97. // FIXME: test that ImagePull(json=true) send correct json output
  98. func newTestRuntime() (*Runtime, error) {
  99. root, err := ioutil.TempDir("", "docker-test")
  100. if err != nil {
  101. return nil, err
  102. }
  103. if err := os.Remove(root); err != nil {
  104. return nil, err
  105. }
  106. if err := utils.CopyDirectory(unitTestStoreBase, root); err != nil {
  107. return nil, err
  108. }
  109. runtime, err := NewRuntimeFromDirectory(root, false)
  110. if err != nil {
  111. return nil, err
  112. }
  113. runtime.UpdateCapabilities(true)
  114. return runtime, nil
  115. }
  116. func GetTestImage(runtime *Runtime) *Image {
  117. imgs, err := runtime.graph.All()
  118. if err != nil {
  119. panic(err)
  120. }
  121. for i := range imgs {
  122. if imgs[i].ID == unitTestImageId {
  123. return imgs[i]
  124. }
  125. }
  126. panic(fmt.Errorf("Test image %v not found", unitTestImageId))
  127. }
  128. func TestRuntimeCreate(t *testing.T) {
  129. runtime, err := newTestRuntime()
  130. if err != nil {
  131. t.Fatal(err)
  132. }
  133. defer nuke(runtime)
  134. // Make sure we start we 0 containers
  135. if len(runtime.List()) != 0 {
  136. t.Errorf("Expected 0 containers, %v found", len(runtime.List()))
  137. }
  138. builder := NewBuilder(runtime)
  139. container, err := builder.Create(&Config{
  140. Image: GetTestImage(runtime).ID,
  141. Cmd: []string{"ls", "-al"},
  142. },
  143. )
  144. if err != nil {
  145. t.Fatal(err)
  146. }
  147. defer func() {
  148. if err := runtime.Destroy(container); err != nil {
  149. t.Error(err)
  150. }
  151. }()
  152. // Make sure we can find the newly created container with List()
  153. if len(runtime.List()) != 1 {
  154. t.Errorf("Expected 1 container, %v found", len(runtime.List()))
  155. }
  156. // Make sure the container List() returns is the right one
  157. if runtime.List()[0].ID != container.ID {
  158. t.Errorf("Unexpected container %v returned by List", runtime.List()[0])
  159. }
  160. // Make sure we can get the container with Get()
  161. if runtime.Get(container.ID) == nil {
  162. t.Errorf("Unable to get newly created container")
  163. }
  164. // Make sure it is the right container
  165. if runtime.Get(container.ID) != container {
  166. t.Errorf("Get() returned the wrong container")
  167. }
  168. // Make sure Exists returns it as existing
  169. if !runtime.Exists(container.ID) {
  170. t.Errorf("Exists() returned false for a newly created container")
  171. }
  172. // Make sure crete with bad parameters returns an error
  173. _, err = builder.Create(
  174. &Config{
  175. Image: GetTestImage(runtime).ID,
  176. },
  177. )
  178. if err == nil {
  179. t.Fatal("Builder.Create should throw an error when Cmd is missing")
  180. }
  181. _, err = builder.Create(
  182. &Config{
  183. Image: GetTestImage(runtime).ID,
  184. Cmd: []string{},
  185. },
  186. )
  187. if err == nil {
  188. t.Fatal("Builder.Create should throw an error when Cmd is empty")
  189. }
  190. }
  191. func TestDestroy(t *testing.T) {
  192. runtime, err := newTestRuntime()
  193. if err != nil {
  194. t.Fatal(err)
  195. }
  196. defer nuke(runtime)
  197. container, err := NewBuilder(runtime).Create(&Config{
  198. Image: GetTestImage(runtime).ID,
  199. Cmd: []string{"ls", "-al"},
  200. },
  201. )
  202. if err != nil {
  203. t.Fatal(err)
  204. }
  205. // Destroy
  206. if err := runtime.Destroy(container); err != nil {
  207. t.Error(err)
  208. }
  209. // Make sure runtime.Exists() behaves correctly
  210. if runtime.Exists("test_destroy") {
  211. t.Errorf("Exists() returned true")
  212. }
  213. // Make sure runtime.List() doesn't list the destroyed container
  214. if len(runtime.List()) != 0 {
  215. t.Errorf("Expected 0 container, %v found", len(runtime.List()))
  216. }
  217. // Make sure runtime.Get() refuses to return the unexisting container
  218. if runtime.Get(container.ID) != nil {
  219. t.Errorf("Unable to get newly created container")
  220. }
  221. // Make sure the container root directory does not exist anymore
  222. _, err = os.Stat(container.root)
  223. if err == nil || !os.IsNotExist(err) {
  224. t.Errorf("Container root directory still exists after destroy")
  225. }
  226. // Test double destroy
  227. if err := runtime.Destroy(container); err == nil {
  228. // It should have failed
  229. t.Errorf("Double destroy did not fail")
  230. }
  231. }
  232. func TestGet(t *testing.T) {
  233. runtime, err := newTestRuntime()
  234. if err != nil {
  235. t.Fatal(err)
  236. }
  237. defer nuke(runtime)
  238. builder := NewBuilder(runtime)
  239. container1, err := builder.Create(&Config{
  240. Image: GetTestImage(runtime).ID,
  241. Cmd: []string{"ls", "-al"},
  242. },
  243. )
  244. if err != nil {
  245. t.Fatal(err)
  246. }
  247. defer runtime.Destroy(container1)
  248. container2, err := builder.Create(&Config{
  249. Image: GetTestImage(runtime).ID,
  250. Cmd: []string{"ls", "-al"},
  251. },
  252. )
  253. if err != nil {
  254. t.Fatal(err)
  255. }
  256. defer runtime.Destroy(container2)
  257. container3, err := builder.Create(&Config{
  258. Image: GetTestImage(runtime).ID,
  259. Cmd: []string{"ls", "-al"},
  260. },
  261. )
  262. if err != nil {
  263. t.Fatal(err)
  264. }
  265. defer runtime.Destroy(container3)
  266. if runtime.Get(container1.ID) != container1 {
  267. t.Errorf("Get(test1) returned %v while expecting %v", runtime.Get(container1.ID), container1)
  268. }
  269. if runtime.Get(container2.ID) != container2 {
  270. t.Errorf("Get(test2) returned %v while expecting %v", runtime.Get(container2.ID), container2)
  271. }
  272. if runtime.Get(container3.ID) != container3 {
  273. t.Errorf("Get(test3) returned %v while expecting %v", runtime.Get(container3.ID), container3)
  274. }
  275. }
  276. func findAvailalblePort(runtime *Runtime, port int) (*Container, error) {
  277. strPort := strconv.Itoa(port)
  278. container, err := NewBuilder(runtime).Create(&Config{
  279. Image: GetTestImage(runtime).ID,
  280. Cmd: []string{"sh", "-c", "echo well hello there | nc -l -p " + strPort},
  281. PortSpecs: []string{strPort},
  282. },
  283. )
  284. if err != nil {
  285. return nil, err
  286. }
  287. hostConfig := &HostConfig{}
  288. if err := container.Start(hostConfig); err != nil {
  289. if strings.Contains(err.Error(), "address already in use") {
  290. return nil, nil
  291. }
  292. return nil, err
  293. }
  294. return container, nil
  295. }
  296. // Run a container with a TCP port allocated, and test that it can receive connections on localhost
  297. func TestAllocatePortLocalhost(t *testing.T) {
  298. runtime, err := newTestRuntime()
  299. if err != nil {
  300. t.Fatal(err)
  301. }
  302. port := 5554
  303. var container *Container
  304. for {
  305. port += 1
  306. log.Println("Trying port", port)
  307. t.Log("Trying port", port)
  308. container, err = findAvailalblePort(runtime, port)
  309. if container != nil {
  310. break
  311. }
  312. if err != nil {
  313. t.Fatal(err)
  314. }
  315. log.Println("Port", port, "already in use")
  316. t.Log("Port", port, "already in use")
  317. }
  318. defer container.Kill()
  319. setTimeout(t, "Waiting for the container to be started timed out", 2*time.Second, func() {
  320. for !container.State.Running {
  321. time.Sleep(10 * time.Millisecond)
  322. }
  323. })
  324. // Even if the state is running, lets give some time to lxc to spawn the process
  325. container.WaitTimeout(500 * time.Millisecond)
  326. conn, err := net.Dial("tcp",
  327. fmt.Sprintf(
  328. "localhost:%s", container.NetworkSettings.PortMapping[strconv.Itoa(port)],
  329. ),
  330. )
  331. if err != nil {
  332. t.Fatal(err)
  333. }
  334. defer conn.Close()
  335. output, err := ioutil.ReadAll(conn)
  336. if err != nil {
  337. t.Fatal(err)
  338. }
  339. if string(output) != "well hello there\n" {
  340. t.Fatalf("Received wrong output from network connection: should be '%s', not '%s'",
  341. "well hello there\n",
  342. string(output),
  343. )
  344. }
  345. container.Wait()
  346. }
  347. func TestRestore(t *testing.T) {
  348. root, err := ioutil.TempDir("", "docker-test")
  349. if err != nil {
  350. t.Fatal(err)
  351. }
  352. if err := os.Remove(root); err != nil {
  353. t.Fatal(err)
  354. }
  355. if err := utils.CopyDirectory(unitTestStoreBase, root); err != nil {
  356. t.Fatal(err)
  357. }
  358. runtime1, err := NewRuntimeFromDirectory(root, false)
  359. if err != nil {
  360. t.Fatal(err)
  361. }
  362. builder := NewBuilder(runtime1)
  363. // Create a container with one instance of docker
  364. container1, err := builder.Create(&Config{
  365. Image: GetTestImage(runtime1).ID,
  366. Cmd: []string{"ls", "-al"},
  367. },
  368. )
  369. if err != nil {
  370. t.Fatal(err)
  371. }
  372. defer runtime1.Destroy(container1)
  373. // Create a second container meant to be killed
  374. container2, err := builder.Create(&Config{
  375. Image: GetTestImage(runtime1).ID,
  376. Cmd: []string{"/bin/cat"},
  377. OpenStdin: true,
  378. },
  379. )
  380. if err != nil {
  381. t.Fatal(err)
  382. }
  383. defer runtime1.Destroy(container2)
  384. // Start the container non blocking
  385. hostConfig := &HostConfig{}
  386. if err := container2.Start(hostConfig); err != nil {
  387. t.Fatal(err)
  388. }
  389. if !container2.State.Running {
  390. t.Fatalf("Container %v should appear as running but isn't", container2.ID)
  391. }
  392. // Simulate a crash/manual quit of dockerd: process dies, states stays 'Running'
  393. cStdin, _ := container2.StdinPipe()
  394. cStdin.Close()
  395. if err := container2.WaitTimeout(2 * time.Second); err != nil {
  396. t.Fatal(err)
  397. }
  398. container2.State.Running = true
  399. container2.ToDisk()
  400. if len(runtime1.List()) != 2 {
  401. t.Errorf("Expected 2 container, %v found", len(runtime1.List()))
  402. }
  403. if err := container1.Run(); err != nil {
  404. t.Fatal(err)
  405. }
  406. if !container2.State.Running {
  407. t.Fatalf("Container %v should appear as running but isn't", container2.ID)
  408. }
  409. // Here are are simulating a docker restart - that is, reloading all containers
  410. // from scratch
  411. runtime2, err := NewRuntimeFromDirectory(root, false)
  412. if err != nil {
  413. t.Fatal(err)
  414. }
  415. defer nuke(runtime2)
  416. if len(runtime2.List()) != 2 {
  417. t.Errorf("Expected 2 container, %v found", len(runtime2.List()))
  418. }
  419. runningCount := 0
  420. for _, c := range runtime2.List() {
  421. if c.State.Running {
  422. t.Errorf("Running container found: %v (%v)", c.ID, c.Path)
  423. runningCount++
  424. }
  425. }
  426. if runningCount != 0 {
  427. t.Fatalf("Expected 0 container alive, %d found", runningCount)
  428. }
  429. container3 := runtime2.Get(container1.ID)
  430. if container3 == nil {
  431. t.Fatal("Unable to Get container")
  432. }
  433. if err := container3.Run(); err != nil {
  434. t.Fatal(err)
  435. }
  436. container2.State.Running = false
  437. }