utils_test.go 8.7 KB

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