server_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package docker
  2. import (
  3. "testing"
  4. )
  5. func TestContainerTagImageDelete(t *testing.T) {
  6. runtime, err := newTestRuntime()
  7. if err != nil {
  8. t.Fatal(err)
  9. }
  10. defer nuke(runtime)
  11. srv := &Server{runtime: runtime}
  12. initialImages, err := srv.Images(false, "")
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. if err := srv.runtime.repositories.Set("utest", "tag1", unitTestImageName, false); err != nil {
  17. t.Fatal(err)
  18. }
  19. if err := srv.runtime.repositories.Set("utest/docker", "tag2", unitTestImageName, false); err != nil {
  20. t.Fatal(err)
  21. }
  22. images, err := srv.Images(false, "")
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. if len(images) != len(initialImages)+2 {
  27. t.Errorf("Expected %d images, %d found", len(initialImages)+2, len(images))
  28. }
  29. if _, err := srv.ImageDelete("utest/docker:tag2", true); err != nil {
  30. t.Fatal(err)
  31. }
  32. images, err = srv.Images(false, "")
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. if len(images) != len(initialImages)+1 {
  37. t.Errorf("Expected %d images, %d found", len(initialImages)+1, len(images))
  38. }
  39. if _, err := srv.ImageDelete("utest:tag1", true); err != nil {
  40. t.Fatal(err)
  41. }
  42. images, err = srv.Images(false, "")
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. if len(images) != len(initialImages) {
  47. t.Errorf("Expected %d image, %d found", len(initialImages), len(images))
  48. }
  49. }
  50. func TestCreateRm(t *testing.T) {
  51. runtime, err := newTestRuntime()
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. defer nuke(runtime)
  56. srv := &Server{runtime: runtime}
  57. config, _, _, err := ParseRun([]string{GetTestImage(runtime).ID, "echo test"}, nil)
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. id, err := srv.ContainerCreate(config)
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. if len(runtime.List()) != 1 {
  66. t.Errorf("Expected 1 container, %v found", len(runtime.List()))
  67. }
  68. if err = srv.ContainerDestroy(id, true); err != nil {
  69. t.Fatal(err)
  70. }
  71. if len(runtime.List()) != 0 {
  72. t.Errorf("Expected 0 container, %v found", len(runtime.List()))
  73. }
  74. }
  75. func TestCreateStartRestartStopStartKillRm(t *testing.T) {
  76. runtime, err := newTestRuntime()
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. defer nuke(runtime)
  81. srv := &Server{runtime: runtime}
  82. config, hostConfig, _, err := ParseRun([]string{GetTestImage(runtime).ID, "/bin/cat"}, nil)
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. id, err := srv.ContainerCreate(config)
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. if len(runtime.List()) != 1 {
  91. t.Errorf("Expected 1 container, %v found", len(runtime.List()))
  92. }
  93. err = srv.ContainerStart(id, hostConfig)
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. err = srv.ContainerRestart(id, 1)
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. err = srv.ContainerStop(id, 1)
  102. if err != nil {
  103. t.Fatal(err)
  104. }
  105. err = srv.ContainerStart(id, hostConfig)
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. err = srv.ContainerKill(id)
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. // FIXME: this failed once with a race condition ("Unable to remove filesystem for xxx: directory not empty")
  114. if err = srv.ContainerDestroy(id, true); err != nil {
  115. t.Fatal(err)
  116. }
  117. if len(runtime.List()) != 0 {
  118. t.Errorf("Expected 0 container, %v found", len(runtime.List()))
  119. }
  120. }
  121. func TestRunWithTooLowMemoryLimit(t *testing.T) {
  122. runtime, err := newTestRuntime()
  123. srv := &Server{runtime: runtime}
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. defer nuke(runtime)
  128. // Try to create a container with a memory limit of 1 byte less than the minimum allowed limit.
  129. _, err = srv.ContainerCreate(
  130. &Config{
  131. Image: GetTestImage(runtime).ID,
  132. Memory: 524287,
  133. CpuShares: 1000,
  134. Cmd: []string{"/bin/cat"},
  135. },
  136. )
  137. if err == nil {
  138. t.Errorf("Memory limit is smaller than the allowed limit. Container creation should've failed!")
  139. }
  140. }