server_test.go 3.4 KB

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