runtime_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. package docker
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/dotcloud/docker/sysinit"
  6. "github.com/dotcloud/docker/utils"
  7. "io"
  8. "log"
  9. "net"
  10. "os"
  11. "runtime"
  12. "strconv"
  13. "strings"
  14. "sync"
  15. "syscall"
  16. "testing"
  17. "time"
  18. )
  19. const (
  20. unitTestImageName = "docker-test-image"
  21. unitTestImageID = "83599e29c455eb719f77d799bc7c51521b9551972f5a850d7ad265bc1b5292f6" // 1.0
  22. unitTestNetworkBridge = "testdockbr0"
  23. unitTestStoreBase = "/var/lib/docker/unit-tests"
  24. testDaemonAddr = "127.0.0.1:4270"
  25. testDaemonProto = "tcp"
  26. )
  27. var (
  28. globalRuntime *Runtime
  29. startFds int
  30. startGoroutines int
  31. )
  32. func nuke(runtime *Runtime) error {
  33. var wg sync.WaitGroup
  34. for _, container := range runtime.List() {
  35. wg.Add(1)
  36. go func(c *Container) {
  37. c.Kill()
  38. wg.Done()
  39. }(container)
  40. }
  41. wg.Wait()
  42. runtime.networkManager.Close()
  43. return os.RemoveAll(runtime.config.GraphPath)
  44. }
  45. func cleanup(runtime *Runtime) error {
  46. for _, container := range runtime.List() {
  47. container.Kill()
  48. runtime.Destroy(container)
  49. }
  50. images, err := runtime.graph.Map()
  51. if err != nil {
  52. return err
  53. }
  54. for _, image := range images {
  55. if image.ID != unitTestImageID {
  56. runtime.graph.Delete(image.ID)
  57. }
  58. }
  59. return nil
  60. }
  61. func layerArchive(tarfile string) (io.Reader, error) {
  62. // FIXME: need to close f somewhere
  63. f, err := os.Open(tarfile)
  64. if err != nil {
  65. return nil, err
  66. }
  67. return f, nil
  68. }
  69. func init() {
  70. os.Setenv("TEST", "1")
  71. // Hack to run sys init during unit testing
  72. if selfPath := utils.SelfPath(); selfPath == "/sbin/init" || selfPath == "/.dockerinit" {
  73. sysinit.SysInit()
  74. return
  75. }
  76. if uid := syscall.Geteuid(); uid != 0 {
  77. log.Fatal("docker tests need to be run as root")
  78. }
  79. // Setup the base runtime, which will be duplicated for each test.
  80. // (no tests are run directly in the base)
  81. setupBaseImage()
  82. // Create the "global runtime" with a long-running daemon for integration tests
  83. spawnGlobalDaemon()
  84. startFds, startGoroutines = utils.GetTotalUsedFds(), runtime.NumGoroutine()
  85. }
  86. func setupBaseImage() {
  87. config := &DaemonConfig{
  88. GraphPath: unitTestStoreBase,
  89. AutoRestart: false,
  90. BridgeIface: unitTestNetworkBridge,
  91. }
  92. runtime, err := NewRuntimeFromDirectory(config)
  93. if err != nil {
  94. log.Fatalf("Unable to create a runtime for tests:", err)
  95. }
  96. // Create the "Server"
  97. srv := &Server{
  98. runtime: runtime,
  99. pullingPool: make(map[string]struct{}),
  100. pushingPool: make(map[string]struct{}),
  101. }
  102. // If the unit test is not found, try to download it.
  103. if img, err := runtime.repositories.LookupImage(unitTestImageName); err != nil || img.ID != unitTestImageID {
  104. // Retrieve the Image
  105. if err := srv.ImagePull(unitTestImageName, "", os.Stdout, utils.NewStreamFormatter(false), nil, nil, true); err != nil {
  106. log.Fatalf("Unable to pull the test image:", err)
  107. }
  108. }
  109. }
  110. func spawnGlobalDaemon() {
  111. if globalRuntime != nil {
  112. utils.Debugf("Global runtime already exists. Skipping.")
  113. return
  114. }
  115. globalRuntime = mkRuntime(log.New(os.Stderr, "", 0))
  116. srv := &Server{
  117. runtime: globalRuntime,
  118. pullingPool: make(map[string]struct{}),
  119. pushingPool: make(map[string]struct{}),
  120. }
  121. // Spawn a Daemon
  122. go func() {
  123. utils.Debugf("Spawning global daemon for integration tests")
  124. if err := ListenAndServe(testDaemonProto, testDaemonAddr, srv, os.Getenv("DEBUG") != ""); err != nil {
  125. log.Fatalf("Unable to spawn the test daemon:", err)
  126. }
  127. }()
  128. // Give some time to ListenAndServer to actually start
  129. // FIXME: use inmem transports instead of tcp
  130. time.Sleep(time.Second)
  131. }
  132. // FIXME: test that ImagePull(json=true) send correct json output
  133. func GetTestImage(runtime *Runtime) *Image {
  134. imgs, err := runtime.graph.Map()
  135. if err != nil {
  136. log.Fatalf("Unable to get the test image:", err)
  137. }
  138. for _, image := range imgs {
  139. if image.ID == unitTestImageID {
  140. return image
  141. }
  142. }
  143. log.Fatalf("Test image %v not found", unitTestImageID)
  144. return nil
  145. }
  146. func TestRuntimeCreate(t *testing.T) {
  147. runtime := mkRuntime(t)
  148. defer nuke(runtime)
  149. // Make sure we start we 0 containers
  150. if len(runtime.List()) != 0 {
  151. t.Errorf("Expected 0 containers, %v found", len(runtime.List()))
  152. }
  153. container, _, err := runtime.Create(&Config{
  154. Image: GetTestImage(runtime).ID,
  155. Cmd: []string{"ls", "-al"},
  156. },
  157. )
  158. if err != nil {
  159. t.Fatal(err)
  160. }
  161. defer func() {
  162. if err := runtime.Destroy(container); err != nil {
  163. t.Error(err)
  164. }
  165. }()
  166. // Make sure we can find the newly created container with List()
  167. if len(runtime.List()) != 1 {
  168. t.Errorf("Expected 1 container, %v found", len(runtime.List()))
  169. }
  170. // Make sure the container List() returns is the right one
  171. if runtime.List()[0].ID != container.ID {
  172. t.Errorf("Unexpected container %v returned by List", runtime.List()[0])
  173. }
  174. // Make sure we can get the container with Get()
  175. if runtime.Get(container.ID) == nil {
  176. t.Errorf("Unable to get newly created container")
  177. }
  178. // Make sure it is the right container
  179. if runtime.Get(container.ID) != container {
  180. t.Errorf("Get() returned the wrong container")
  181. }
  182. // Make sure Exists returns it as existing
  183. if !runtime.Exists(container.ID) {
  184. t.Errorf("Exists() returned false for a newly created container")
  185. }
  186. // Make sure create with bad parameters returns an error
  187. if _, _, err = runtime.Create(&Config{Image: GetTestImage(runtime).ID}); err == nil {
  188. t.Fatal("Builder.Create should throw an error when Cmd is missing")
  189. }
  190. if _, _, err := runtime.Create(
  191. &Config{
  192. Image: GetTestImage(runtime).ID,
  193. Cmd: []string{},
  194. },
  195. ); err == nil {
  196. t.Fatal("Builder.Create should throw an error when Cmd is empty")
  197. }
  198. config := &Config{
  199. Image: GetTestImage(runtime).ID,
  200. Cmd: []string{"/bin/ls"},
  201. PortSpecs: []string{"80"},
  202. }
  203. container, _, err = runtime.Create(config)
  204. _, err = runtime.Commit(container, "testrepo", "testtag", "", "", config)
  205. if err != nil {
  206. t.Error(err)
  207. }
  208. }
  209. func TestDestroy(t *testing.T) {
  210. runtime := mkRuntime(t)
  211. defer nuke(runtime)
  212. container, _, err := runtime.Create(&Config{
  213. Image: GetTestImage(runtime).ID,
  214. Cmd: []string{"ls", "-al"},
  215. })
  216. if err != nil {
  217. t.Fatal(err)
  218. }
  219. // Destroy
  220. if err := runtime.Destroy(container); err != nil {
  221. t.Error(err)
  222. }
  223. // Make sure runtime.Exists() behaves correctly
  224. if runtime.Exists("test_destroy") {
  225. t.Errorf("Exists() returned true")
  226. }
  227. // Make sure runtime.List() doesn't list the destroyed container
  228. if len(runtime.List()) != 0 {
  229. t.Errorf("Expected 0 container, %v found", len(runtime.List()))
  230. }
  231. // Make sure runtime.Get() refuses to return the unexisting container
  232. if runtime.Get(container.ID) != nil {
  233. t.Errorf("Unable to get newly created container")
  234. }
  235. // Make sure the container root directory does not exist anymore
  236. _, err = os.Stat(container.root)
  237. if err == nil || !os.IsNotExist(err) {
  238. t.Errorf("Container root directory still exists after destroy")
  239. }
  240. // Test double destroy
  241. if err := runtime.Destroy(container); err == nil {
  242. // It should have failed
  243. t.Errorf("Double destroy did not fail")
  244. }
  245. }
  246. func TestGet(t *testing.T) {
  247. runtime := mkRuntime(t)
  248. defer nuke(runtime)
  249. container1, _, _ := mkContainer(runtime, []string{"_", "ls", "-al"}, t)
  250. defer runtime.Destroy(container1)
  251. container2, _, _ := mkContainer(runtime, []string{"_", "ls", "-al"}, t)
  252. defer runtime.Destroy(container2)
  253. container3, _, _ := mkContainer(runtime, []string{"_", "ls", "-al"}, t)
  254. defer runtime.Destroy(container3)
  255. if runtime.Get(container1.ID) != container1 {
  256. t.Errorf("Get(test1) returned %v while expecting %v", runtime.Get(container1.ID), container1)
  257. }
  258. if runtime.Get(container2.ID) != container2 {
  259. t.Errorf("Get(test2) returned %v while expecting %v", runtime.Get(container2.ID), container2)
  260. }
  261. if runtime.Get(container3.ID) != container3 {
  262. t.Errorf("Get(test3) returned %v while expecting %v", runtime.Get(container3.ID), container3)
  263. }
  264. }
  265. func startEchoServerContainer(t *testing.T, proto string) (*Runtime, *Container, string) {
  266. var (
  267. err error
  268. container *Container
  269. strPort string
  270. runtime = mkRuntime(t)
  271. port = 5554
  272. p Port
  273. )
  274. for {
  275. port += 1
  276. strPort = strconv.Itoa(port)
  277. var cmd string
  278. if proto == "tcp" {
  279. cmd = "socat TCP-LISTEN:" + strPort + ",reuseaddr,fork EXEC:/bin/cat"
  280. } else if proto == "udp" {
  281. cmd = "socat UDP-RECVFROM:" + strPort + ",fork EXEC:/bin/cat"
  282. } else {
  283. t.Fatal(fmt.Errorf("Unknown protocol %v", proto))
  284. }
  285. ep := make(map[Port]struct{}, 1)
  286. p = Port(fmt.Sprintf("%s/%s", strPort, proto))
  287. ep[p] = struct{}{}
  288. container, _, err = runtime.Create(&Config{
  289. Image: GetTestImage(runtime).ID,
  290. Cmd: []string{"sh", "-c", cmd},
  291. PortSpecs: []string{fmt.Sprintf("%s/%s", strPort, proto)},
  292. ExposedPorts: ep,
  293. })
  294. if err != nil {
  295. nuke(runtime)
  296. t.Fatal(err)
  297. }
  298. if container != nil {
  299. break
  300. }
  301. t.Logf("Port %v already in use, trying another one", strPort)
  302. }
  303. hostConfig := &HostConfig{
  304. PortBindings: make(map[Port][]PortBinding),
  305. }
  306. hostConfig.PortBindings[p] = []PortBinding{
  307. {},
  308. }
  309. if err := container.Start(hostConfig); err != nil {
  310. nuke(runtime)
  311. t.Fatal(err)
  312. }
  313. setTimeout(t, "Waiting for the container to be started timed out", 2*time.Second, func() {
  314. for !container.State.Running {
  315. time.Sleep(10 * time.Millisecond)
  316. }
  317. })
  318. // Even if the state is running, lets give some time to lxc to spawn the process
  319. container.WaitTimeout(500 * time.Millisecond)
  320. strPort = container.NetworkSettings.Ports[p][0].HostPort
  321. return runtime, container, strPort
  322. }
  323. // Run a container with a TCP port allocated, and test that it can receive connections on localhost
  324. func TestAllocateTCPPortLocalhost(t *testing.T) {
  325. runtime, container, port := startEchoServerContainer(t, "tcp")
  326. defer nuke(runtime)
  327. defer container.Kill()
  328. for i := 0; i != 10; i++ {
  329. conn, err := net.Dial("tcp", fmt.Sprintf("localhost:%v", port))
  330. if err != nil {
  331. t.Fatal(err)
  332. }
  333. defer conn.Close()
  334. input := bytes.NewBufferString("well hello there\n")
  335. _, err = conn.Write(input.Bytes())
  336. if err != nil {
  337. t.Fatal(err)
  338. }
  339. buf := make([]byte, 16)
  340. read := 0
  341. conn.SetReadDeadline(time.Now().Add(3 * time.Second))
  342. read, err = conn.Read(buf)
  343. if err != nil {
  344. if err, ok := err.(*net.OpError); ok {
  345. if err.Err == syscall.ECONNRESET {
  346. t.Logf("Connection reset by the proxy, socat is probably not listening yet, trying again in a sec")
  347. conn.Close()
  348. time.Sleep(time.Second)
  349. continue
  350. }
  351. if err.Timeout() {
  352. t.Log("Timeout, trying again")
  353. conn.Close()
  354. continue
  355. }
  356. }
  357. t.Fatal(err)
  358. }
  359. output := string(buf[:read])
  360. if !strings.Contains(output, "well hello there") {
  361. t.Fatal(fmt.Errorf("[%v] doesn't contain [well hello there]", output))
  362. } else {
  363. return
  364. }
  365. }
  366. t.Fatal("No reply from the container")
  367. }
  368. // Run a container with an UDP port allocated, and test that it can receive connections on localhost
  369. func TestAllocateUDPPortLocalhost(t *testing.T) {
  370. runtime, container, port := startEchoServerContainer(t, "udp")
  371. defer nuke(runtime)
  372. defer container.Kill()
  373. conn, err := net.Dial("udp", fmt.Sprintf("localhost:%v", port))
  374. if err != nil {
  375. t.Fatal(err)
  376. }
  377. defer conn.Close()
  378. input := bytes.NewBufferString("well hello there\n")
  379. buf := make([]byte, 16)
  380. // Try for a minute, for some reason the select in socat may take ages
  381. // to return even though everything on the path seems fine (i.e: the
  382. // UDPProxy forwards the traffic correctly and you can see the packets
  383. // on the interface from within the container).
  384. for i := 0; i != 120; i++ {
  385. _, err := conn.Write(input.Bytes())
  386. if err != nil {
  387. t.Fatal(err)
  388. }
  389. conn.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
  390. read, err := conn.Read(buf)
  391. if err == nil {
  392. output := string(buf[:read])
  393. if strings.Contains(output, "well hello there") {
  394. return
  395. }
  396. }
  397. }
  398. t.Fatal("No reply from the container")
  399. }
  400. func TestRestore(t *testing.T) {
  401. runtime1 := mkRuntime(t)
  402. defer nuke(runtime1)
  403. // Create a container with one instance of docker
  404. container1, _, _ := mkContainer(runtime1, []string{"_", "ls", "-al"}, t)
  405. defer runtime1.Destroy(container1)
  406. // Create a second container meant to be killed
  407. container2, _, _ := mkContainer(runtime1, []string{"-i", "_", "/bin/cat"}, t)
  408. defer runtime1.Destroy(container2)
  409. // Start the container non blocking
  410. hostConfig := &HostConfig{}
  411. if err := container2.Start(hostConfig); err != nil {
  412. t.Fatal(err)
  413. }
  414. if !container2.State.Running {
  415. t.Fatalf("Container %v should appear as running but isn't", container2.ID)
  416. }
  417. // Simulate a crash/manual quit of dockerd: process dies, states stays 'Running'
  418. cStdin, _ := container2.StdinPipe()
  419. cStdin.Close()
  420. if err := container2.WaitTimeout(2 * time.Second); err != nil {
  421. t.Fatal(err)
  422. }
  423. container2.State.Running = true
  424. container2.ToDisk()
  425. if len(runtime1.List()) != 2 {
  426. t.Errorf("Expected 2 container, %v found", len(runtime1.List()))
  427. }
  428. if err := container1.Run(); err != nil {
  429. t.Fatal(err)
  430. }
  431. if !container2.State.Running {
  432. t.Fatalf("Container %v should appear as running but isn't", container2.ID)
  433. }
  434. // Here are are simulating a docker restart - that is, reloading all containers
  435. // from scratch
  436. runtime1.config.AutoRestart = false
  437. runtime2, err := NewRuntimeFromDirectory(runtime1.config)
  438. if err != nil {
  439. t.Fatal(err)
  440. }
  441. defer nuke(runtime2)
  442. if len(runtime2.List()) != 2 {
  443. t.Errorf("Expected 2 container, %v found", len(runtime2.List()))
  444. }
  445. runningCount := 0
  446. for _, c := range runtime2.List() {
  447. if c.State.Running {
  448. t.Errorf("Running container found: %v (%v)", c.ID, c.Path)
  449. runningCount++
  450. }
  451. }
  452. if runningCount != 0 {
  453. t.Fatalf("Expected 0 container alive, %d found", runningCount)
  454. }
  455. container3 := runtime2.Get(container1.ID)
  456. if container3 == nil {
  457. t.Fatal("Unable to Get container")
  458. }
  459. if err := container3.Run(); err != nil {
  460. t.Fatal(err)
  461. }
  462. container2.State.Running = false
  463. }
  464. func TestReloadContainerLinks(t *testing.T) {
  465. runtime1 := mkRuntime(t)
  466. defer nuke(runtime1)
  467. // Create a container with one instance of docker
  468. container1, _, _ := mkContainer(runtime1, []string{"_", "ls", "-al"}, t)
  469. defer runtime1.Destroy(container1)
  470. // Create a second container meant to be killed
  471. container2, _, _ := mkContainer(runtime1, []string{"-i", "_", "/bin/cat"}, t)
  472. defer runtime1.Destroy(container2)
  473. // Start the container non blocking
  474. hostConfig := &HostConfig{}
  475. if err := container2.Start(hostConfig); err != nil {
  476. t.Fatal(err)
  477. }
  478. h1 := &HostConfig{}
  479. // Add a link to container 2
  480. h1.Links = []string{utils.TruncateID(container2.ID) + ":first"}
  481. if err := container1.Start(h1); err != nil {
  482. t.Fatal(err)
  483. }
  484. if !container2.State.Running {
  485. t.Fatalf("Container %v should appear as running but isn't", container2.ID)
  486. }
  487. if !container1.State.Running {
  488. t.Fatalf("Container %s should appear as running bu isn't", container1.ID)
  489. }
  490. if len(runtime1.List()) != 2 {
  491. t.Errorf("Expected 2 container, %v found", len(runtime1.List()))
  492. }
  493. if !container2.State.Running {
  494. t.Fatalf("Container %v should appear as running but isn't", container2.ID)
  495. }
  496. // Here are are simulating a docker restart - that is, reloading all containers
  497. // from scratch
  498. runtime1.config.AutoRestart = true
  499. runtime2, err := NewRuntimeFromDirectory(runtime1.config)
  500. if err != nil {
  501. t.Fatal(err)
  502. }
  503. defer nuke(runtime2)
  504. if len(runtime2.List()) != 2 {
  505. t.Errorf("Expected 2 container, %v found", len(runtime2.List()))
  506. }
  507. runningCount := 0
  508. for _, c := range runtime2.List() {
  509. if c.State.Running {
  510. t.Logf("Running container found: %v (%v)", c.ID, c.Path)
  511. runningCount++
  512. }
  513. }
  514. if runningCount != 2 {
  515. t.Fatalf("Expected 2 container alive, %d found", runningCount)
  516. }
  517. // Make sure container 2 ( the child of container 1 ) was registered and started first
  518. // with the runtime
  519. first := runtime2.containers.Front()
  520. if first.Value.(*Container).ID != container2.ID {
  521. t.Fatalf("Container 2 %s should be registered first in the runtime", container2.ID)
  522. }
  523. t.Logf("Number of links: %d", runtime2.containerGraph.Refs("engine"))
  524. // Verify that the link is still registered in the runtime
  525. entity := runtime2.containerGraph.Get(fmt.Sprintf("/%s", container1.ID))
  526. if entity == nil {
  527. t.Fatal("Entity should not be nil")
  528. }
  529. }
  530. func TestDefaultContainerName(t *testing.T) {
  531. runtime := mkRuntime(t)
  532. defer nuke(runtime)
  533. srv := &Server{runtime: runtime}
  534. config, _, _, err := ParseRun([]string{GetTestImage(runtime).ID, "echo test"}, nil)
  535. if err != nil {
  536. t.Fatal(err)
  537. }
  538. shortId, _, err := srv.ContainerCreate(config)
  539. if err != nil {
  540. t.Fatal(err)
  541. }
  542. container := runtime.Get(shortId)
  543. containerID := container.ID
  544. paths := runtime.containerGraph.RefPaths(containerID)
  545. if paths == nil || len(paths) == 0 {
  546. t.Fatalf("Could not find edges for %s", containerID)
  547. }
  548. edge := paths[0]
  549. if edge.ParentID != "0" {
  550. t.Fatalf("Expected engine got %s", edge.ParentID)
  551. }
  552. if edge.EntityID != containerID {
  553. t.Fatalf("Expected %s got %s", containerID, edge.EntityID)
  554. }
  555. if edge.Name != containerID {
  556. t.Fatalf("Expected %s got %s", containerID, edge.Name)
  557. }
  558. }
  559. func TestDefaultContainerRename(t *testing.T) {
  560. runtime := mkRuntime(t)
  561. defer nuke(runtime)
  562. srv := &Server{runtime: runtime}
  563. config, _, _, err := ParseRun([]string{GetTestImage(runtime).ID, "echo test"}, nil)
  564. if err != nil {
  565. t.Fatal(err)
  566. }
  567. shortId, _, err := srv.ContainerCreate(config)
  568. if err != nil {
  569. t.Fatal(err)
  570. }
  571. container := runtime.Get(shortId)
  572. containerID := container.ID
  573. if err := runtime.RenameLink(fmt.Sprintf("/%s", containerID), "/webapp"); err != nil {
  574. t.Fatal(err)
  575. }
  576. webapp, err := runtime.GetByName("/webapp")
  577. if err != nil {
  578. t.Fatal(err)
  579. }
  580. if webapp.ID != container.ID {
  581. t.Fatalf("Expect webapp id to match container id: %s != %s", webapp.ID, container.ID)
  582. }
  583. }
  584. func TestLinkChildContainer(t *testing.T) {
  585. runtime := mkRuntime(t)
  586. defer nuke(runtime)
  587. srv := &Server{runtime: runtime}
  588. config, _, _, err := ParseRun([]string{GetTestImage(runtime).ID, "echo test"}, nil)
  589. if err != nil {
  590. t.Fatal(err)
  591. }
  592. shortId, _, err := srv.ContainerCreate(config)
  593. if err != nil {
  594. t.Fatal(err)
  595. }
  596. container := runtime.Get(shortId)
  597. if err := runtime.RenameLink(fmt.Sprintf("/%s", container.ID), "/webapp"); err != nil {
  598. t.Fatal(err)
  599. }
  600. webapp, err := runtime.GetByName("/webapp")
  601. if err != nil {
  602. t.Fatal(err)
  603. }
  604. if webapp.ID != container.ID {
  605. t.Fatalf("Expect webapp id to match container id: %s != %s", webapp.ID, container.ID)
  606. }
  607. config, _, _, err = ParseRun([]string{GetTestImage(runtime).ID, "echo test"}, nil)
  608. if err != nil {
  609. t.Fatal(err)
  610. }
  611. shortId, _, err = srv.ContainerCreate(config)
  612. if err != nil {
  613. t.Fatal(err)
  614. }
  615. childContainer := runtime.Get(shortId)
  616. if err := runtime.RenameLink(fmt.Sprintf("/%s", childContainer.ID), "/db"); err != nil {
  617. t.Fatal(err)
  618. }
  619. if err := runtime.Link("/webapp", "/db", "db"); err != nil {
  620. t.Fatal(err)
  621. }
  622. // Get the child by it's new name
  623. db, err := runtime.GetByName("/webapp/db")
  624. if err != nil {
  625. t.Fatal(err)
  626. }
  627. if db.ID != childContainer.ID {
  628. t.Fatalf("Expect db id to match container id: %s != %s", db.ID, childContainer.ID)
  629. }
  630. }
  631. func TestGetAllChildren(t *testing.T) {
  632. runtime := mkRuntime(t)
  633. defer nuke(runtime)
  634. srv := &Server{runtime: runtime}
  635. config, _, _, err := ParseRun([]string{GetTestImage(runtime).ID, "echo test"}, nil)
  636. if err != nil {
  637. t.Fatal(err)
  638. }
  639. shortId, _, err := srv.ContainerCreate(config)
  640. if err != nil {
  641. t.Fatal(err)
  642. }
  643. container := runtime.Get(shortId)
  644. if err := runtime.RenameLink(fmt.Sprintf("/%s", container.ID), "/webapp"); err != nil {
  645. t.Fatal(err)
  646. }
  647. webapp, err := runtime.GetByName("/webapp")
  648. if err != nil {
  649. t.Fatal(err)
  650. }
  651. if webapp.ID != container.ID {
  652. t.Fatalf("Expect webapp id to match container id: %s != %s", webapp.ID, container.ID)
  653. }
  654. config, _, _, err = ParseRun([]string{GetTestImage(runtime).ID, "echo test"}, nil)
  655. if err != nil {
  656. t.Fatal(err)
  657. }
  658. shortId, _, err = srv.ContainerCreate(config)
  659. if err != nil {
  660. t.Fatal(err)
  661. }
  662. childContainer := runtime.Get(shortId)
  663. if err := runtime.RenameLink(fmt.Sprintf("/%s", childContainer.ID), "/db"); err != nil {
  664. t.Fatal(err)
  665. }
  666. if err := runtime.Link("/webapp", "/db", "db"); err != nil {
  667. t.Fatal(err)
  668. }
  669. children, err := runtime.Children("/webapp")
  670. if err != nil {
  671. t.Fatal(err)
  672. }
  673. if children == nil {
  674. t.Fatal("Children should not be nil")
  675. }
  676. if len(children) == 0 {
  677. t.Fatal("Children should not be empty")
  678. }
  679. for key, value := range children {
  680. if key != "/webapp/db" {
  681. t.Fatalf("Expected /webapp/db got %s", key)
  682. }
  683. if value.ID != childContainer.ID {
  684. t.Fatalf("Expected id %s got %s", childContainer.ID, value.ID)
  685. }
  686. }
  687. }