utils_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. if err := utils.CopyDirectory(unitTestStoreBase, root); err != nil {
  59. return nil, err
  60. }
  61. config := &DaemonConfig{
  62. GraphPath: 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, *HostConfig, 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, 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, nil, err
  124. }
  125. return c, hostConfig, nil
  126. }
  127. // Create a test container, start it, wait for it to complete, destroy it,
  128. // and return its standard output as a string.
  129. // The image name (eg. the XXX in []string{"-i", "-t", "XXX", "bash"}, is dynamically replaced by the current test image.
  130. // If t is not nil, call t.Fatal() at the first error. Otherwise return errors normally.
  131. func runContainer(r *Runtime, args []string, t *testing.T) (output string, err error) {
  132. defer func() {
  133. if err != nil && t != nil {
  134. t.Fatal(err)
  135. }
  136. }()
  137. container, hostConfig, err := mkContainer(r, args, t)
  138. if err != nil {
  139. return "", err
  140. }
  141. defer r.Destroy(container)
  142. stdout, err := container.StdoutPipe()
  143. if err != nil {
  144. return "", err
  145. }
  146. defer stdout.Close()
  147. if err := container.Start(hostConfig); err != nil {
  148. return "", err
  149. }
  150. container.Wait()
  151. data, err := ioutil.ReadAll(stdout)
  152. if err != nil {
  153. return "", err
  154. }
  155. output = string(data)
  156. return
  157. }
  158. func TestCompareConfig(t *testing.T) {
  159. volumes1 := make(map[string]struct{})
  160. volumes1["/test1"] = struct{}{}
  161. config1 := Config{
  162. Dns: []string{"1.1.1.1", "2.2.2.2"},
  163. PortSpecs: []string{"1111:1111", "2222:2222"},
  164. Env: []string{"VAR1=1", "VAR2=2"},
  165. VolumesFrom: "11111111",
  166. Volumes: volumes1,
  167. }
  168. config2 := Config{
  169. Dns: []string{"0.0.0.0", "2.2.2.2"},
  170. PortSpecs: []string{"1111:1111", "2222:2222"},
  171. Env: []string{"VAR1=1", "VAR2=2"},
  172. VolumesFrom: "11111111",
  173. Volumes: volumes1,
  174. }
  175. config3 := Config{
  176. Dns: []string{"1.1.1.1", "2.2.2.2"},
  177. PortSpecs: []string{"0000:0000", "2222:2222"},
  178. Env: []string{"VAR1=1", "VAR2=2"},
  179. VolumesFrom: "11111111",
  180. Volumes: volumes1,
  181. }
  182. config4 := Config{
  183. Dns: []string{"1.1.1.1", "2.2.2.2"},
  184. PortSpecs: []string{"0000:0000", "2222:2222"},
  185. Env: []string{"VAR1=1", "VAR2=2"},
  186. VolumesFrom: "22222222",
  187. Volumes: volumes1,
  188. }
  189. volumes2 := make(map[string]struct{})
  190. volumes2["/test2"] = struct{}{}
  191. config5 := Config{
  192. Dns: []string{"1.1.1.1", "2.2.2.2"},
  193. PortSpecs: []string{"0000:0000", "2222:2222"},
  194. Env: []string{"VAR1=1", "VAR2=2"},
  195. VolumesFrom: "11111111",
  196. Volumes: volumes2,
  197. }
  198. if CompareConfig(&config1, &config2) {
  199. t.Fatalf("CompareConfig should return false, Dns are different")
  200. }
  201. if CompareConfig(&config1, &config3) {
  202. t.Fatalf("CompareConfig should return false, PortSpecs are different")
  203. }
  204. if CompareConfig(&config1, &config4) {
  205. t.Fatalf("CompareConfig should return false, VolumesFrom are different")
  206. }
  207. if CompareConfig(&config1, &config5) {
  208. t.Fatalf("CompareConfig should return false, Volumes are different")
  209. }
  210. if !CompareConfig(&config1, &config1) {
  211. t.Fatalf("CompareConfig should return true")
  212. }
  213. }
  214. func TestMergeConfig(t *testing.T) {
  215. volumesImage := make(map[string]struct{})
  216. volumesImage["/test1"] = struct{}{}
  217. volumesImage["/test2"] = struct{}{}
  218. configImage := &Config{
  219. Dns: []string{"1.1.1.1", "2.2.2.2"},
  220. PortSpecs: []string{"1111:1111", "2222:2222"},
  221. Env: []string{"VAR1=1", "VAR2=2"},
  222. VolumesFrom: "1111",
  223. Volumes: volumesImage,
  224. }
  225. volumesUser := make(map[string]struct{})
  226. volumesUser["/test3"] = struct{}{}
  227. configUser := &Config{
  228. Dns: []string{"3.3.3.3"},
  229. PortSpecs: []string{"3333:2222", "3333:3333"},
  230. Env: []string{"VAR2=3", "VAR3=3"},
  231. Volumes: volumesUser,
  232. }
  233. MergeConfig(configUser, configImage)
  234. if len(configUser.Dns) != 3 {
  235. t.Fatalf("Expected 3 dns, 1.1.1.1, 2.2.2.2 and 3.3.3.3, found %d", len(configUser.Dns))
  236. }
  237. for _, dns := range configUser.Dns {
  238. if dns != "1.1.1.1" && dns != "2.2.2.2" && dns != "3.3.3.3" {
  239. t.Fatalf("Expected 1.1.1.1 or 2.2.2.2 or 3.3.3.3, found %s", dns)
  240. }
  241. }
  242. if len(configUser.ExposedPorts) != 3 {
  243. t.Fatalf("Expected 3 portSpecs, 1111, 2222 and 3333, found %d", len(configUser.PortSpecs))
  244. }
  245. for portSpecs := range configUser.ExposedPorts {
  246. if portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" {
  247. t.Fatalf("Expected 1111 or 2222 or 3333, found %s", portSpecs)
  248. }
  249. }
  250. if len(configUser.Env) != 3 {
  251. t.Fatalf("Expected 3 env var, VAR1=1, VAR2=3 and VAR3=3, found %d", len(configUser.Env))
  252. }
  253. for _, env := range configUser.Env {
  254. if env != "VAR1=1" && env != "VAR2=3" && env != "VAR3=3" {
  255. t.Fatalf("Expected VAR1=1 or VAR2=3 or VAR3=3, found %s", env)
  256. }
  257. }
  258. if len(configUser.Volumes) != 3 {
  259. t.Fatalf("Expected 3 volumes, /test1, /test2 and /test3, found %d", len(configUser.Volumes))
  260. }
  261. for v := range configUser.Volumes {
  262. if v != "/test1" && v != "/test2" && v != "/test3" {
  263. t.Fatalf("Expected /test1 or /test2 or /test3, found %s", v)
  264. }
  265. }
  266. if configUser.VolumesFrom != "1111" {
  267. t.Fatalf("Expected VolumesFrom to be 1111, found %s", configUser.VolumesFrom)
  268. }
  269. }
  270. func TestParseLxcConfOpt(t *testing.T) {
  271. opts := []string{"lxc.utsname=docker", "lxc.utsname = docker "}
  272. for _, o := range opts {
  273. k, v, err := parseLxcOpt(o)
  274. if err != nil {
  275. t.FailNow()
  276. }
  277. if k != "lxc.utsname" {
  278. t.Fail()
  279. }
  280. if v != "docker" {
  281. t.Fail()
  282. }
  283. }
  284. }
  285. func TestParseNetworkOptsPrivateOnly(t *testing.T) {
  286. ports, bindings, err := parsePortSpecs([]string{"192.168.1.100::80"})
  287. if err != nil {
  288. t.Fatal(err)
  289. }
  290. if len(ports) != 1 {
  291. t.Logf("Expected 1 got %d", len(ports))
  292. t.FailNow()
  293. }
  294. if len(bindings) != 1 {
  295. t.Logf("Expected 1 got %d", len(bindings))
  296. t.FailNow()
  297. }
  298. for k := range ports {
  299. if k.Proto() != "tcp" {
  300. t.Logf("Expected tcp got %s", k.Proto())
  301. t.Fail()
  302. }
  303. if k.Port() != "80" {
  304. t.Logf("Expected 80 got %s", k.Port())
  305. t.Fail()
  306. }
  307. b, exists := bindings[k]
  308. if !exists {
  309. t.Log("Binding does not exist")
  310. t.FailNow()
  311. }
  312. if len(b) != 1 {
  313. t.Logf("Expected 1 got %d", len(b))
  314. t.FailNow()
  315. }
  316. s := b[0]
  317. if s.HostPort != "" {
  318. t.Logf("Expected \"\" got %s", s.HostPort)
  319. t.Fail()
  320. }
  321. if s.HostIp != "192.168.1.100" {
  322. t.Fail()
  323. }
  324. }
  325. }
  326. func TestParseNetworkOptsPublic(t *testing.T) {
  327. ports, bindings, err := parsePortSpecs([]string{"192.168.1.100:8080:80"})
  328. if err != nil {
  329. t.Fatal(err)
  330. }
  331. if len(ports) != 1 {
  332. t.Logf("Expected 1 got %d", len(ports))
  333. t.FailNow()
  334. }
  335. if len(bindings) != 1 {
  336. t.Logf("Expected 1 got %d", len(bindings))
  337. t.FailNow()
  338. }
  339. for k := range ports {
  340. if k.Proto() != "tcp" {
  341. t.Logf("Expected tcp got %s", k.Proto())
  342. t.Fail()
  343. }
  344. if k.Port() != "80" {
  345. t.Logf("Expected 80 got %s", k.Port())
  346. t.Fail()
  347. }
  348. b, exists := bindings[k]
  349. if !exists {
  350. t.Log("Binding does not exist")
  351. t.FailNow()
  352. }
  353. if len(b) != 1 {
  354. t.Logf("Expected 1 got %d", len(b))
  355. t.FailNow()
  356. }
  357. s := b[0]
  358. if s.HostPort != "8080" {
  359. t.Logf("Expected 8080 got %s", s.HostPort)
  360. t.Fail()
  361. }
  362. if s.HostIp != "192.168.1.100" {
  363. t.Fail()
  364. }
  365. }
  366. }
  367. func TestParseNetworkOptsUdp(t *testing.T) {
  368. ports, bindings, err := parsePortSpecs([]string{"192.168.1.100::6000/udp"})
  369. if err != nil {
  370. t.Fatal(err)
  371. }
  372. if len(ports) != 1 {
  373. t.Logf("Expected 1 got %d", len(ports))
  374. t.FailNow()
  375. }
  376. if len(bindings) != 1 {
  377. t.Logf("Expected 1 got %d", len(bindings))
  378. t.FailNow()
  379. }
  380. for k := range ports {
  381. if k.Proto() != "udp" {
  382. t.Logf("Expected udp got %s", k.Proto())
  383. t.Fail()
  384. }
  385. if k.Port() != "6000" {
  386. t.Logf("Expected 6000 got %s", k.Port())
  387. t.Fail()
  388. }
  389. b, exists := bindings[k]
  390. if !exists {
  391. t.Log("Binding does not exist")
  392. t.FailNow()
  393. }
  394. if len(b) != 1 {
  395. t.Logf("Expected 1 got %d", len(b))
  396. t.FailNow()
  397. }
  398. s := b[0]
  399. if s.HostPort != "" {
  400. t.Logf("Expected \"\" got %s", s.HostPort)
  401. t.Fail()
  402. }
  403. if s.HostIp != "192.168.1.100" {
  404. t.Fail()
  405. }
  406. }
  407. }