utils_test.go 8.8 KB

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