utils_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. package docker
  2. import (
  3. "fmt"
  4. "github.com/dotcloud/docker/utils"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "path"
  9. "runtime"
  10. "strings"
  11. "testing"
  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. if err := utils.CopyDirectory(unitTestStoreBase, root); err != nil {
  59. return nil, err
  60. }
  61. config := &DaemonConfig{
  62. Root: root,
  63. AutoRestart: false,
  64. }
  65. runtime, err = NewRuntimeFromDirectory(config)
  66. if err != nil {
  67. return nil, err
  68. }
  69. runtime.UpdateCapabilities(true)
  70. return runtime, nil
  71. }
  72. // Write `content` to the file at path `dst`, creating it if necessary,
  73. // as well as any missing directories.
  74. // The file is truncated if it already exists.
  75. // Call t.Fatal() at the first error.
  76. func writeFile(dst, content string, t *testing.T) {
  77. // Create subdirectories if necessary
  78. if err := os.MkdirAll(path.Dir(dst), 0700); err != nil && !os.IsExist(err) {
  79. t.Fatal(err)
  80. }
  81. f, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700)
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. // Write content (truncate if it exists)
  86. if _, err := io.Copy(f, strings.NewReader(content)); err != nil {
  87. t.Fatal(err)
  88. }
  89. }
  90. // Return the contents of file at path `src`.
  91. // Call t.Fatal() at the first error (including if the file doesn't exist)
  92. func readFile(src string, t *testing.T) (content string) {
  93. f, err := os.Open(src)
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. data, err := ioutil.ReadAll(f)
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. return string(data)
  102. }
  103. // Create a test container from the given runtime `r` and run arguments `args`.
  104. // If the image name is "_", (eg. []string{"-i", "-t", "_", "bash"}, it is
  105. // dynamically replaced by the current test image.
  106. // The caller is responsible for destroying the container.
  107. // Call t.Fatal() at the first error.
  108. func mkContainer(r *Runtime, args []string, t *testing.T) (*Container, error) {
  109. config, hostConfig, _, err := ParseRun(args, nil)
  110. defer func() {
  111. if err != nil && t != nil {
  112. t.Fatal(err)
  113. }
  114. }()
  115. if err != nil {
  116. return nil, err
  117. }
  118. if config.Image == "_" {
  119. config.Image = GetTestImage(r).ID
  120. }
  121. c, _, err := r.Create(config, "")
  122. if err != nil {
  123. return nil, err
  124. }
  125. c.hostConfig = hostConfig
  126. return c, nil
  127. }
  128. // Create a test container, start it, wait for it to complete, destroy it,
  129. // and return its standard output as a string.
  130. // The image name (eg. the XXX in []string{"-i", "-t", "XXX", "bash"}, is dynamically replaced by the current test image.
  131. // If t is not nil, call t.Fatal() at the first error. Otherwise return errors normally.
  132. func runContainer(r *Runtime, args []string, t *testing.T) (output string, err error) {
  133. defer func() {
  134. if err != nil && t != nil {
  135. t.Fatal(err)
  136. }
  137. }()
  138. container, err := mkContainer(r, args, t)
  139. if err != nil {
  140. return "", err
  141. }
  142. defer r.Destroy(container)
  143. stdout, err := container.StdoutPipe()
  144. if err != nil {
  145. return "", err
  146. }
  147. defer stdout.Close()
  148. if err := container.Start(); err != nil {
  149. return "", err
  150. }
  151. container.Wait()
  152. data, err := ioutil.ReadAll(stdout)
  153. if err != nil {
  154. return "", err
  155. }
  156. output = string(data)
  157. return
  158. }
  159. func TestCompareConfig(t *testing.T) {
  160. volumes1 := make(map[string]struct{})
  161. volumes1["/test1"] = struct{}{}
  162. config1 := Config{
  163. Dns: []string{"1.1.1.1", "2.2.2.2"},
  164. PortSpecs: []string{"1111:1111", "2222:2222"},
  165. Env: []string{"VAR1=1", "VAR2=2"},
  166. VolumesFrom: "11111111",
  167. Volumes: volumes1,
  168. }
  169. config2 := Config{
  170. Dns: []string{"0.0.0.0", "2.2.2.2"},
  171. PortSpecs: []string{"1111:1111", "2222:2222"},
  172. Env: []string{"VAR1=1", "VAR2=2"},
  173. VolumesFrom: "11111111",
  174. Volumes: volumes1,
  175. }
  176. config3 := Config{
  177. Dns: []string{"1.1.1.1", "2.2.2.2"},
  178. PortSpecs: []string{"0000:0000", "2222:2222"},
  179. Env: []string{"VAR1=1", "VAR2=2"},
  180. VolumesFrom: "11111111",
  181. Volumes: volumes1,
  182. }
  183. config4 := Config{
  184. Dns: []string{"1.1.1.1", "2.2.2.2"},
  185. PortSpecs: []string{"0000:0000", "2222:2222"},
  186. Env: []string{"VAR1=1", "VAR2=2"},
  187. VolumesFrom: "22222222",
  188. Volumes: volumes1,
  189. }
  190. volumes2 := make(map[string]struct{})
  191. volumes2["/test2"] = struct{}{}
  192. config5 := Config{
  193. Dns: []string{"1.1.1.1", "2.2.2.2"},
  194. PortSpecs: []string{"0000:0000", "2222:2222"},
  195. Env: []string{"VAR1=1", "VAR2=2"},
  196. VolumesFrom: "11111111",
  197. Volumes: volumes2,
  198. }
  199. if CompareConfig(&config1, &config2) {
  200. t.Fatalf("CompareConfig should return false, Dns are different")
  201. }
  202. if CompareConfig(&config1, &config3) {
  203. t.Fatalf("CompareConfig should return false, PortSpecs are different")
  204. }
  205. if CompareConfig(&config1, &config4) {
  206. t.Fatalf("CompareConfig should return false, VolumesFrom are different")
  207. }
  208. if CompareConfig(&config1, &config5) {
  209. t.Fatalf("CompareConfig should return false, Volumes are different")
  210. }
  211. if !CompareConfig(&config1, &config1) {
  212. t.Fatalf("CompareConfig should return true")
  213. }
  214. }
  215. func TestMergeConfig(t *testing.T) {
  216. volumesImage := make(map[string]struct{})
  217. volumesImage["/test1"] = struct{}{}
  218. volumesImage["/test2"] = struct{}{}
  219. configImage := &Config{
  220. Dns: []string{"1.1.1.1", "2.2.2.2"},
  221. PortSpecs: []string{"1111:1111", "2222:2222"},
  222. Env: []string{"VAR1=1", "VAR2=2"},
  223. VolumesFrom: "1111",
  224. Volumes: volumesImage,
  225. }
  226. volumesUser := make(map[string]struct{})
  227. volumesUser["/test3"] = struct{}{}
  228. configUser := &Config{
  229. Dns: []string{"3.3.3.3"},
  230. PortSpecs: []string{"3333:2222", "3333:3333"},
  231. Env: []string{"VAR2=3", "VAR3=3"},
  232. Volumes: volumesUser,
  233. }
  234. if err := MergeConfig(configUser, configImage); err != nil {
  235. t.Error(err)
  236. }
  237. if len(configUser.Dns) != 3 {
  238. t.Fatalf("Expected 3 dns, 1.1.1.1, 2.2.2.2 and 3.3.3.3, found %d", len(configUser.Dns))
  239. }
  240. for _, dns := range configUser.Dns {
  241. if dns != "1.1.1.1" && dns != "2.2.2.2" && dns != "3.3.3.3" {
  242. t.Fatalf("Expected 1.1.1.1 or 2.2.2.2 or 3.3.3.3, found %s", dns)
  243. }
  244. }
  245. if len(configUser.ExposedPorts) != 3 {
  246. t.Fatalf("Expected 3 ExposedPorts, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts))
  247. }
  248. for portSpecs := range configUser.ExposedPorts {
  249. if portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" {
  250. t.Fatalf("Expected 1111 or 2222 or 3333, found %s", portSpecs)
  251. }
  252. }
  253. if len(configUser.Env) != 3 {
  254. t.Fatalf("Expected 3 env var, VAR1=1, VAR2=3 and VAR3=3, found %d", len(configUser.Env))
  255. }
  256. for _, env := range configUser.Env {
  257. if env != "VAR1=1" && env != "VAR2=3" && env != "VAR3=3" {
  258. t.Fatalf("Expected VAR1=1 or VAR2=3 or VAR3=3, found %s", env)
  259. }
  260. }
  261. if len(configUser.Volumes) != 3 {
  262. t.Fatalf("Expected 3 volumes, /test1, /test2 and /test3, found %d", len(configUser.Volumes))
  263. }
  264. for v := range configUser.Volumes {
  265. if v != "/test1" && v != "/test2" && v != "/test3" {
  266. t.Fatalf("Expected /test1 or /test2 or /test3, found %s", v)
  267. }
  268. }
  269. if configUser.VolumesFrom != "1111" {
  270. t.Fatalf("Expected VolumesFrom to be 1111, found %s", configUser.VolumesFrom)
  271. }
  272. ports, _, err := parsePortSpecs([]string{"0000"})
  273. if err != nil {
  274. t.Error(err)
  275. }
  276. configImage2 := &Config{
  277. ExposedPorts: ports,
  278. }
  279. if err := MergeConfig(configUser, configImage2); err != nil {
  280. t.Error(err)
  281. }
  282. if len(configUser.ExposedPorts) != 4 {
  283. t.Fatalf("Expected 4 ExposedPorts, 0000, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts))
  284. }
  285. for portSpecs := range configUser.ExposedPorts {
  286. if portSpecs.Port() != "0000" && portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" {
  287. t.Fatalf("Expected 0000 or 1111 or 2222 or 3333, found %s", portSpecs)
  288. }
  289. }
  290. }
  291. func TestParseLxcConfOpt(t *testing.T) {
  292. opts := []string{"lxc.utsname=docker", "lxc.utsname = docker "}
  293. for _, o := range opts {
  294. k, v, err := parseLxcOpt(o)
  295. if err != nil {
  296. t.FailNow()
  297. }
  298. if k != "lxc.utsname" {
  299. t.Fail()
  300. }
  301. if v != "docker" {
  302. t.Fail()
  303. }
  304. }
  305. }
  306. func TestParseNetworkOptsPrivateOnly(t *testing.T) {
  307. ports, bindings, err := parsePortSpecs([]string{"192.168.1.100::80"})
  308. if err != nil {
  309. t.Fatal(err)
  310. }
  311. if len(ports) != 1 {
  312. t.Logf("Expected 1 got %d", len(ports))
  313. t.FailNow()
  314. }
  315. if len(bindings) != 1 {
  316. t.Logf("Expected 1 got %d", len(bindings))
  317. t.FailNow()
  318. }
  319. for k := range ports {
  320. if k.Proto() != "tcp" {
  321. t.Logf("Expected tcp got %s", k.Proto())
  322. t.Fail()
  323. }
  324. if k.Port() != "80" {
  325. t.Logf("Expected 80 got %s", k.Port())
  326. t.Fail()
  327. }
  328. b, exists := bindings[k]
  329. if !exists {
  330. t.Log("Binding does not exist")
  331. t.FailNow()
  332. }
  333. if len(b) != 1 {
  334. t.Logf("Expected 1 got %d", len(b))
  335. t.FailNow()
  336. }
  337. s := b[0]
  338. if s.HostPort != "" {
  339. t.Logf("Expected \"\" got %s", s.HostPort)
  340. t.Fail()
  341. }
  342. if s.HostIp != "192.168.1.100" {
  343. t.Fail()
  344. }
  345. }
  346. }
  347. func TestParseNetworkOptsPublic(t *testing.T) {
  348. ports, bindings, err := parsePortSpecs([]string{"192.168.1.100:8080:80"})
  349. if err != nil {
  350. t.Fatal(err)
  351. }
  352. if len(ports) != 1 {
  353. t.Logf("Expected 1 got %d", len(ports))
  354. t.FailNow()
  355. }
  356. if len(bindings) != 1 {
  357. t.Logf("Expected 1 got %d", len(bindings))
  358. t.FailNow()
  359. }
  360. for k := range ports {
  361. if k.Proto() != "tcp" {
  362. t.Logf("Expected tcp got %s", k.Proto())
  363. t.Fail()
  364. }
  365. if k.Port() != "80" {
  366. t.Logf("Expected 80 got %s", k.Port())
  367. t.Fail()
  368. }
  369. b, exists := bindings[k]
  370. if !exists {
  371. t.Log("Binding does not exist")
  372. t.FailNow()
  373. }
  374. if len(b) != 1 {
  375. t.Logf("Expected 1 got %d", len(b))
  376. t.FailNow()
  377. }
  378. s := b[0]
  379. if s.HostPort != "8080" {
  380. t.Logf("Expected 8080 got %s", s.HostPort)
  381. t.Fail()
  382. }
  383. if s.HostIp != "192.168.1.100" {
  384. t.Fail()
  385. }
  386. }
  387. }
  388. func TestParseNetworkOptsUdp(t *testing.T) {
  389. ports, bindings, err := parsePortSpecs([]string{"192.168.1.100::6000/udp"})
  390. if err != nil {
  391. t.Fatal(err)
  392. }
  393. if len(ports) != 1 {
  394. t.Logf("Expected 1 got %d", len(ports))
  395. t.FailNow()
  396. }
  397. if len(bindings) != 1 {
  398. t.Logf("Expected 1 got %d", len(bindings))
  399. t.FailNow()
  400. }
  401. for k := range ports {
  402. if k.Proto() != "udp" {
  403. t.Logf("Expected udp got %s", k.Proto())
  404. t.Fail()
  405. }
  406. if k.Port() != "6000" {
  407. t.Logf("Expected 6000 got %s", k.Port())
  408. t.Fail()
  409. }
  410. b, exists := bindings[k]
  411. if !exists {
  412. t.Log("Binding does not exist")
  413. t.FailNow()
  414. }
  415. if len(b) != 1 {
  416. t.Logf("Expected 1 got %d", len(b))
  417. t.FailNow()
  418. }
  419. s := b[0]
  420. if s.HostPort != "" {
  421. t.Logf("Expected \"\" got %s", s.HostPort)
  422. t.Fail()
  423. }
  424. if s.HostIp != "192.168.1.100" {
  425. t.Fail()
  426. }
  427. }
  428. }