utils_test.go 9.5 KB

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