utils_test.go 11 KB

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