runtime_test.go 9.5 KB

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