utils_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. MergeConfig(configUser, configImage)
  235. if len(configUser.Dns) != 3 {
  236. t.Fatalf("Expected 3 dns, 1.1.1.1, 2.2.2.2 and 3.3.3.3, found %d", len(configUser.Dns))
  237. }
  238. for _, dns := range configUser.Dns {
  239. if dns != "1.1.1.1" && dns != "2.2.2.2" && dns != "3.3.3.3" {
  240. t.Fatalf("Expected 1.1.1.1 or 2.2.2.2 or 3.3.3.3, found %s", dns)
  241. }
  242. }
  243. if len(configUser.ExposedPorts) != 3 {
  244. t.Fatalf("Expected 3 portSpecs, 1111, 2222 and 3333, found %d", len(configUser.PortSpecs))
  245. }
  246. for portSpecs := range configUser.ExposedPorts {
  247. if portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" {
  248. t.Fatalf("Expected 1111 or 2222 or 3333, found %s", portSpecs)
  249. }
  250. }
  251. if len(configUser.Env) != 3 {
  252. t.Fatalf("Expected 3 env var, VAR1=1, VAR2=3 and VAR3=3, found %d", len(configUser.Env))
  253. }
  254. for _, env := range configUser.Env {
  255. if env != "VAR1=1" && env != "VAR2=3" && env != "VAR3=3" {
  256. t.Fatalf("Expected VAR1=1 or VAR2=3 or VAR3=3, found %s", env)
  257. }
  258. }
  259. if len(configUser.Volumes) != 3 {
  260. t.Fatalf("Expected 3 volumes, /test1, /test2 and /test3, found %d", len(configUser.Volumes))
  261. }
  262. for v := range configUser.Volumes {
  263. if v != "/test1" && v != "/test2" && v != "/test3" {
  264. t.Fatalf("Expected /test1 or /test2 or /test3, found %s", v)
  265. }
  266. }
  267. if configUser.VolumesFrom != "1111" {
  268. t.Fatalf("Expected VolumesFrom to be 1111, found %s", configUser.VolumesFrom)
  269. }
  270. }
  271. func TestParseLxcConfOpt(t *testing.T) {
  272. opts := []string{"lxc.utsname=docker", "lxc.utsname = docker "}
  273. for _, o := range opts {
  274. k, v, err := parseLxcOpt(o)
  275. if err != nil {
  276. t.FailNow()
  277. }
  278. if k != "lxc.utsname" {
  279. t.Fail()
  280. }
  281. if v != "docker" {
  282. t.Fail()
  283. }
  284. }
  285. }
  286. func TestParseNetworkOptsPrivateOnly(t *testing.T) {
  287. ports, bindings, err := parsePortSpecs([]string{"192.168.1.100::80"})
  288. if err != nil {
  289. t.Fatal(err)
  290. }
  291. if len(ports) != 1 {
  292. t.Logf("Expected 1 got %d", len(ports))
  293. t.FailNow()
  294. }
  295. if len(bindings) != 1 {
  296. t.Logf("Expected 1 got %d", len(bindings))
  297. t.FailNow()
  298. }
  299. for k := range ports {
  300. if k.Proto() != "tcp" {
  301. t.Logf("Expected tcp got %s", k.Proto())
  302. t.Fail()
  303. }
  304. if k.Port() != "80" {
  305. t.Logf("Expected 80 got %s", k.Port())
  306. t.Fail()
  307. }
  308. b, exists := bindings[k]
  309. if !exists {
  310. t.Log("Binding does not exist")
  311. t.FailNow()
  312. }
  313. if len(b) != 1 {
  314. t.Logf("Expected 1 got %d", len(b))
  315. t.FailNow()
  316. }
  317. s := b[0]
  318. if s.HostPort != "" {
  319. t.Logf("Expected \"\" got %s", s.HostPort)
  320. t.Fail()
  321. }
  322. if s.HostIp != "192.168.1.100" {
  323. t.Fail()
  324. }
  325. }
  326. }
  327. func TestParseNetworkOptsPublic(t *testing.T) {
  328. ports, bindings, err := parsePortSpecs([]string{"192.168.1.100:8080:80"})
  329. if err != nil {
  330. t.Fatal(err)
  331. }
  332. if len(ports) != 1 {
  333. t.Logf("Expected 1 got %d", len(ports))
  334. t.FailNow()
  335. }
  336. if len(bindings) != 1 {
  337. t.Logf("Expected 1 got %d", len(bindings))
  338. t.FailNow()
  339. }
  340. for k := range ports {
  341. if k.Proto() != "tcp" {
  342. t.Logf("Expected tcp got %s", k.Proto())
  343. t.Fail()
  344. }
  345. if k.Port() != "80" {
  346. t.Logf("Expected 80 got %s", k.Port())
  347. t.Fail()
  348. }
  349. b, exists := bindings[k]
  350. if !exists {
  351. t.Log("Binding does not exist")
  352. t.FailNow()
  353. }
  354. if len(b) != 1 {
  355. t.Logf("Expected 1 got %d", len(b))
  356. t.FailNow()
  357. }
  358. s := b[0]
  359. if s.HostPort != "8080" {
  360. t.Logf("Expected 8080 got %s", s.HostPort)
  361. t.Fail()
  362. }
  363. if s.HostIp != "192.168.1.100" {
  364. t.Fail()
  365. }
  366. }
  367. }
  368. func TestParseNetworkOptsUdp(t *testing.T) {
  369. ports, bindings, err := parsePortSpecs([]string{"192.168.1.100::6000/udp"})
  370. if err != nil {
  371. t.Fatal(err)
  372. }
  373. if len(ports) != 1 {
  374. t.Logf("Expected 1 got %d", len(ports))
  375. t.FailNow()
  376. }
  377. if len(bindings) != 1 {
  378. t.Logf("Expected 1 got %d", len(bindings))
  379. t.FailNow()
  380. }
  381. for k := range ports {
  382. if k.Proto() != "udp" {
  383. t.Logf("Expected udp got %s", k.Proto())
  384. t.Fail()
  385. }
  386. if k.Port() != "6000" {
  387. t.Logf("Expected 6000 got %s", k.Port())
  388. t.Fail()
  389. }
  390. b, exists := bindings[k]
  391. if !exists {
  392. t.Log("Binding does not exist")
  393. t.FailNow()
  394. }
  395. if len(b) != 1 {
  396. t.Logf("Expected 1 got %d", len(b))
  397. t.FailNow()
  398. }
  399. s := b[0]
  400. if s.HostPort != "" {
  401. t.Logf("Expected \"\" got %s", s.HostPort)
  402. t.Fail()
  403. }
  404. if s.HostIp != "192.168.1.100" {
  405. t.Fail()
  406. }
  407. }
  408. }