utils_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. package docker
  2. import (
  3. "github.com/dotcloud/docker/utils"
  4. "io"
  5. "io/ioutil"
  6. "os"
  7. "path"
  8. "strings"
  9. "testing"
  10. )
  11. // This file contains utility functions for docker's unit test suite.
  12. // It has to be named XXX_test.go, apparently, in other to access private functions
  13. // from other XXX_test.go functions.
  14. // Create a temporary runtime suitable for unit testing.
  15. // Call t.Fatal() at the first error.
  16. func mkRuntime(f Fataler) *Runtime {
  17. runtime, err := newTestRuntime()
  18. if err != nil {
  19. f.Fatal(err)
  20. }
  21. return runtime
  22. }
  23. // A common interface to access the Fatal method of
  24. // both testing.B and testing.T.
  25. type Fataler interface {
  26. Fatal(args ...interface{})
  27. }
  28. func newTestRuntime() (runtime *Runtime, err error) {
  29. utils.Debugf("newTestRuntime start")
  30. root, err := ioutil.TempDir("", "docker-test")
  31. defer func() {
  32. utils.Debugf("newTestRuntime: %s", root)
  33. }()
  34. if err != nil {
  35. return nil, err
  36. }
  37. if err := os.Remove(root); err != nil {
  38. return nil, err
  39. }
  40. utils.Debugf("Copying %s to %s", unitTestStoreBase, root)
  41. if err := utils.CopyDirectory(unitTestStoreBase, root); err != nil {
  42. utils.Debugf("ERROR: Copying %s to %s returned %s", unitTestStoreBase, root, err)
  43. return nil, err
  44. }
  45. runtime, err = NewRuntimeFromDirectory(root, false)
  46. if err != nil {
  47. return nil, err
  48. }
  49. runtime.UpdateCapabilities(true)
  50. return runtime, nil
  51. }
  52. // Write `content` to the file at path `dst`, creating it if necessary,
  53. // as well as any missing directories.
  54. // The file is truncated if it already exists.
  55. // Call t.Fatal() at the first error.
  56. func writeFile(dst, content string, t *testing.T) {
  57. // Create subdirectories if necessary
  58. if err := os.MkdirAll(path.Dir(dst), 0700); err != nil && !os.IsExist(err) {
  59. t.Fatal(err)
  60. }
  61. f, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700)
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. // Write content (truncate if it exists)
  66. if _, err := io.Copy(f, strings.NewReader(content)); err != nil {
  67. t.Fatal(err)
  68. }
  69. }
  70. // Return the contents of file at path `src`.
  71. // Call t.Fatal() at the first error (including if the file doesn't exist)
  72. func readFile(src string, t *testing.T) (content string) {
  73. f, err := os.Open(src)
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. data, err := ioutil.ReadAll(f)
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. return string(data)
  82. }
  83. // Create a test container from the given runtime `r` and run arguments `args`.
  84. // If the image name is "_", (eg. []string{"-i", "-t", "_", "bash"}, it is
  85. // dynamically replaced by the current test image.
  86. // The caller is responsible for destroying the container.
  87. // Call t.Fatal() at the first error.
  88. func mkContainer(r *Runtime, args []string, t *testing.T) (*Container, *HostConfig, error) {
  89. config, hostConfig, _, err := ParseRun(args, nil)
  90. defer func() {
  91. if err != nil && t != nil {
  92. t.Fatal(err)
  93. }
  94. }()
  95. if err != nil {
  96. return nil, nil, err
  97. }
  98. if config.Image == "_" {
  99. config.Image = GetTestImage(r).ID
  100. }
  101. c, err := r.Create(config)
  102. if err != nil {
  103. return nil, nil, err
  104. }
  105. return c, hostConfig, nil
  106. }
  107. // Create a test container, start it, wait for it to complete, destroy it,
  108. // and return its standard output as a string.
  109. // The image name (eg. the XXX in []string{"-i", "-t", "XXX", "bash"}, is dynamically replaced by the current test image.
  110. // If t is not nil, call t.Fatal() at the first error. Otherwise return errors normally.
  111. func runContainer(r *Runtime, args []string, t *testing.T) (output string, err error) {
  112. defer func() {
  113. if err != nil && t != nil {
  114. t.Fatal(err)
  115. }
  116. }()
  117. container, hostConfig, err := mkContainer(r, args, t)
  118. if err != nil {
  119. return "", err
  120. }
  121. defer r.Destroy(container)
  122. stdout, err := container.StdoutPipe()
  123. if err != nil {
  124. return "", err
  125. }
  126. defer stdout.Close()
  127. if err := container.Start(hostConfig); err != nil {
  128. return "", err
  129. }
  130. container.Wait()
  131. data, err := ioutil.ReadAll(stdout)
  132. if err != nil {
  133. return "", err
  134. }
  135. output = string(data)
  136. return
  137. }
  138. func TestCompareConfig(t *testing.T) {
  139. volumes1 := make(map[string]struct{})
  140. volumes1["/test1"] = struct{}{}
  141. config1 := Config{
  142. Dns: []string{"1.1.1.1", "2.2.2.2"},
  143. PortSpecs: []string{"1111:1111", "2222:2222"},
  144. Env: []string{"VAR1=1", "VAR2=2"},
  145. VolumesFrom: "11111111",
  146. Volumes: volumes1,
  147. }
  148. config2 := Config{
  149. Dns: []string{"0.0.0.0", "2.2.2.2"},
  150. PortSpecs: []string{"1111:1111", "2222:2222"},
  151. Env: []string{"VAR1=1", "VAR2=2"},
  152. VolumesFrom: "11111111",
  153. Volumes: volumes1,
  154. }
  155. config3 := Config{
  156. Dns: []string{"1.1.1.1", "2.2.2.2"},
  157. PortSpecs: []string{"0000:0000", "2222:2222"},
  158. Env: []string{"VAR1=1", "VAR2=2"},
  159. VolumesFrom: "11111111",
  160. Volumes: volumes1,
  161. }
  162. config4 := Config{
  163. Dns: []string{"1.1.1.1", "2.2.2.2"},
  164. PortSpecs: []string{"0000:0000", "2222:2222"},
  165. Env: []string{"VAR1=1", "VAR2=2"},
  166. VolumesFrom: "22222222",
  167. Volumes: volumes1,
  168. }
  169. volumes2 := make(map[string]struct{})
  170. volumes2["/test2"] = struct{}{}
  171. config5 := Config{
  172. Dns: []string{"1.1.1.1", "2.2.2.2"},
  173. PortSpecs: []string{"0000:0000", "2222:2222"},
  174. Env: []string{"VAR1=1", "VAR2=2"},
  175. VolumesFrom: "11111111",
  176. Volumes: volumes2,
  177. }
  178. if CompareConfig(&config1, &config2) {
  179. t.Fatalf("CompareConfig should return false, Dns are different")
  180. }
  181. if CompareConfig(&config1, &config3) {
  182. t.Fatalf("CompareConfig should return false, PortSpecs are different")
  183. }
  184. if CompareConfig(&config1, &config4) {
  185. t.Fatalf("CompareConfig should return false, VolumesFrom are different")
  186. }
  187. if CompareConfig(&config1, &config5) {
  188. t.Fatalf("CompareConfig should return false, Volumes are different")
  189. }
  190. if !CompareConfig(&config1, &config1) {
  191. t.Fatalf("CompareConfig should return true")
  192. }
  193. }
  194. func TestMergeConfig(t *testing.T) {
  195. volumesImage := make(map[string]struct{})
  196. volumesImage["/test1"] = struct{}{}
  197. volumesImage["/test2"] = struct{}{}
  198. configImage := &Config{
  199. Dns: []string{"1.1.1.1", "2.2.2.2"},
  200. PortSpecs: []string{"1111:1111", "2222:2222"},
  201. Env: []string{"VAR1=1", "VAR2=2"},
  202. VolumesFrom: "1111",
  203. Volumes: volumesImage,
  204. }
  205. volumesUser := make(map[string]struct{})
  206. volumesUser["/test3"] = struct{}{}
  207. configUser := &Config{
  208. Dns: []string{"3.3.3.3"},
  209. PortSpecs: []string{"3333:2222", "3333:3333"},
  210. Env: []string{"VAR2=3", "VAR3=3"},
  211. Volumes: volumesUser,
  212. }
  213. MergeConfig(configUser, configImage)
  214. if len(configUser.Dns) != 3 {
  215. t.Fatalf("Expected 3 dns, 1.1.1.1, 2.2.2.2 and 3.3.3.3, found %d", len(configUser.Dns))
  216. }
  217. for _, dns := range configUser.Dns {
  218. if dns != "1.1.1.1" && dns != "2.2.2.2" && dns != "3.3.3.3" {
  219. t.Fatalf("Expected 1.1.1.1 or 2.2.2.2 or 3.3.3.3, found %s", dns)
  220. }
  221. }
  222. if len(configUser.PortSpecs) != 3 {
  223. t.Fatalf("Expected 3 portSpecs, 1111:1111, 3333:2222 and 3333:3333, found %d", len(configUser.PortSpecs))
  224. }
  225. for _, portSpecs := range configUser.PortSpecs {
  226. if portSpecs != "1111:1111" && portSpecs != "3333:2222" && portSpecs != "3333:3333" {
  227. t.Fatalf("Expected 1111:1111 or 3333:2222 or 3333:3333, found %s", portSpecs)
  228. }
  229. }
  230. if len(configUser.Env) != 3 {
  231. t.Fatalf("Expected 3 env var, VAR1=1, VAR2=3 and VAR3=3, found %d", len(configUser.Env))
  232. }
  233. for _, env := range configUser.Env {
  234. if env != "VAR1=1" && env != "VAR2=3" && env != "VAR3=3" {
  235. t.Fatalf("Expected VAR1=1 or VAR2=3 or VAR3=3, found %s", env)
  236. }
  237. }
  238. if len(configUser.Volumes) != 3 {
  239. t.Fatalf("Expected 3 volumes, /test1, /test2 and /test3, found %d", len(configUser.Volumes))
  240. }
  241. for v := range configUser.Volumes {
  242. if v != "/test1" && v != "/test2" && v != "/test3" {
  243. t.Fatalf("Expected /test1 or /test2 or /test3, found %s", v)
  244. }
  245. }
  246. if configUser.VolumesFrom != "1111" {
  247. t.Fatalf("Expected VolumesFrom to be 1111, found %s", configUser.VolumesFrom)
  248. }
  249. }
  250. func TestMergeConfigPublicPortNotHonored(t *testing.T) {
  251. volumesImage := make(map[string]struct{})
  252. volumesImage["/test1"] = struct{}{}
  253. volumesImage["/test2"] = struct{}{}
  254. configImage := &Config{
  255. Dns: []string{"1.1.1.1", "2.2.2.2"},
  256. PortSpecs: []string{"1111", "2222"},
  257. Env: []string{"VAR1=1", "VAR2=2"},
  258. Volumes: volumesImage,
  259. }
  260. volumesUser := make(map[string]struct{})
  261. volumesUser["/test3"] = struct{}{}
  262. configUser := &Config{
  263. Dns: []string{"3.3.3.3"},
  264. PortSpecs: []string{"1111:3333"},
  265. Env: []string{"VAR2=3", "VAR3=3"},
  266. Volumes: volumesUser,
  267. }
  268. MergeConfig(configUser, configImage)
  269. contains := func(a []string, expect string) bool {
  270. for _, p := range a {
  271. if p == expect {
  272. return true
  273. }
  274. }
  275. return false
  276. }
  277. if !contains(configUser.PortSpecs, "2222") {
  278. t.Logf("Expected '2222' Ports: %v", configUser.PortSpecs)
  279. t.Fail()
  280. }
  281. if !contains(configUser.PortSpecs, "1111:3333") {
  282. t.Logf("Expected '1111:3333' Ports: %v", configUser.PortSpecs)
  283. t.Fail()
  284. }
  285. }
  286. func TestParseLxcConfOpt(t *testing.T) {
  287. opts := []string{"lxc.utsname=docker", "lxc.utsname = docker "}
  288. for _, o := range opts {
  289. k, v, err := parseLxcOpt(o)
  290. if err != nil {
  291. t.FailNow()
  292. }
  293. if k != "lxc.utsname" {
  294. t.Fail()
  295. }
  296. if v != "docker" {
  297. t.Fail()
  298. }
  299. }
  300. }