container_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package docker
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "github.com/rancherio/os/config"
  7. "github.com/rancherio/rancher-compose/librcompose/project"
  8. "github.com/stretchr/testify/require"
  9. dockerClient "github.com/fsouza/go-dockerclient"
  10. "os"
  11. )
  12. func testDockerHost(t *testing.T) {
  13. assert := require.New(t)
  14. assert.Equal(os.Getenv("DOCKER_HOST"), config.DOCKER_HOST)
  15. }
  16. func TestHash(t *testing.T) {
  17. assert := require.New(t)
  18. hash := getHash(&config.ContainerConfig{
  19. Id: "id",
  20. Cmd: "1 2 3",
  21. })
  22. hash2 := getHash(&config.ContainerConfig{
  23. Id: "id2",
  24. Cmd: "1 2 3",
  25. })
  26. hash3 := getHash(&config.ContainerConfig{
  27. Id: "id3",
  28. Cmd: "1 2 3 4",
  29. })
  30. assert.Equal("d601444333c7fb4cb955bcca36c5ed59b6fa8c3f", hash, "")
  31. assert.NotEqual(hash, hash2, "")
  32. assert.NotEqual(hash2, hash3, "")
  33. assert.NotEqual(hash, hash3, "")
  34. }
  35. func TestHash2(t *testing.T) {
  36. assert := require.New(t)
  37. cfg := &config.ContainerConfig{
  38. Id: "docker-volumes",
  39. Cmd: "",
  40. MigrateVolumes: false,
  41. ReloadConfig: false,
  42. CreateOnly: true,
  43. Service: &project.ServiceConfig{
  44. CapAdd: nil,
  45. CapDrop: nil,
  46. CpuShares: 0,
  47. Command: project.NewCommand(),
  48. Detach: "",
  49. Dns: project.NewStringorslice(),
  50. DnsSearch: project.NewStringorslice(),
  51. DomainName: "",
  52. Entrypoint: project.NewCommand(),
  53. EnvFile: project.NewStringorslice(),
  54. Environment: project.NewMaporEqualSlice([]string{}),
  55. Hostname: "",
  56. Image: "state",
  57. Labels: project.NewSliceorMap(map[string]string{
  58. "io.rancher.os.createonly": "true",
  59. "io.rancher.os.scope": "system"}),
  60. Links: project.NewMaporColonSlice(nil),
  61. LogDriver: "json-file",
  62. MemLimit: 0,
  63. Name: "",
  64. Net: "none",
  65. Pid: "",
  66. Ipc: "",
  67. Ports: nil,
  68. Privileged: true,
  69. Restart: "",
  70. ReadOnly: true,
  71. StdinOpen: false,
  72. Tty: false,
  73. User: "",
  74. Volumes: []string{
  75. "/var/lib/docker:/var/lib/docker",
  76. "/var/lib/rancher/conf:/var/lib/rancher/conf",
  77. "/var/lib/system-docker:/var/lib/system-docker"},
  78. VolumesFrom: nil,
  79. WorkingDir: "",
  80. Expose: nil,
  81. ExternalLinks: nil},
  82. }
  83. for i := 0; i < 1000; i++ {
  84. assert.Equal(getHash(cfg), getHash(cfg), fmt.Sprintf("Failed at iteration: %v", i))
  85. }
  86. }
  87. func TestBool2String(t *testing.T) {
  88. assert := require.New(t)
  89. assert.Equal("true", fmt.Sprint(true), "")
  90. }
  91. func TestParse(t *testing.T) {
  92. assert := require.New(t)
  93. cfg := &config.ContainerConfig{
  94. Cmd: "--name c1 " +
  95. "-d " +
  96. "--rm " +
  97. "--privileged " +
  98. "test/image " +
  99. "arg1 " +
  100. "arg2 ",
  101. }
  102. c := NewContainer("", cfg).Parse()
  103. assert.NoError(c.Err, "")
  104. assert.Equal(cfg.Id, "c1", "Id doesn't match")
  105. assert.Equal(c.Name, "c1", "Name doesn't match")
  106. assert.True(c.remove, "Remove doesn't match")
  107. assert.True(c.detach, "Detach doesn't match")
  108. assert.Equal(c.Config.Cmd.Len(), 2, "Args doesn't match")
  109. assert.Equal(c.Config.Cmd.Slice()[0], "arg1", "Arg1 doesn't match")
  110. assert.Equal(c.Config.Cmd.Slice()[1], "arg2", "Arg2 doesn't match")
  111. assert.True(c.HostConfig.Privileged, "Privileged doesn't match")
  112. }
  113. func TestIdFromName(t *testing.T) {
  114. assert := require.New(t)
  115. cfg := &config.ContainerConfig{
  116. Cmd: "--name foo -v /test busybox echo hi",
  117. }
  118. assert.Equal("", cfg.Id)
  119. NewContainer(config.DOCKER_HOST, cfg)
  120. assert.Equal("foo", cfg.Id)
  121. }
  122. func testMigrateVolumes(t *testing.T) {
  123. assert := require.New(t)
  124. c := NewContainer(config.DOCKER_HOST, &config.ContainerConfig{
  125. Cmd: "--name foo -v /test busybox echo hi",
  126. }).Parse().Start().Lookup()
  127. assert.NoError(c.Err, "")
  128. test_path, ok := c.Container.Volumes["/test"]
  129. assert.True(ok, "")
  130. c2 := NewContainer(config.DOCKER_HOST, &config.ContainerConfig{
  131. MigrateVolumes: true,
  132. Cmd: "--name foo -v /test2 busybox echo hi",
  133. }).Parse().Start().Lookup()
  134. assert.NoError(c2.Err, "")
  135. assert.True(c2.Container != nil)
  136. _, ok = c2.Container.Volumes["/test2"]
  137. assert.True(ok, "")
  138. assert.Equal(test_path, c2.Container.Volumes["/test"])
  139. c.Delete()
  140. c2.Delete()
  141. }
  142. func testRollback(t *testing.T) {
  143. assert := require.New(t)
  144. c := NewContainer(config.DOCKER_HOST, &config.ContainerConfig{
  145. Cmd: "--name rollback busybox echo hi",
  146. }).Parse().Start().Lookup()
  147. assert.NoError(c.Err, "")
  148. assert.Equal("rollback", c.Container.Name)
  149. c2 := NewContainer(config.DOCKER_HOST, &config.ContainerConfig{
  150. Cmd: "--name rollback busybox echo bye",
  151. }).Parse().Start().Lookup()
  152. assert.Equal("rollback", c2.Container.Name)
  153. assert.NoError(c2.Err, "")
  154. assert.NotEqual(c.Container.ID, c2.Container.ID)
  155. c3 := NewContainer(config.DOCKER_HOST, &config.ContainerConfig{
  156. Cmd: "--name rollback busybox echo hi",
  157. }).Parse().Start().Lookup()
  158. assert.NoError(c3.Err, "")
  159. assert.Equal(c.Container.ID, c3.Container.ID)
  160. assert.Equal("rollback", c3.Container.Name)
  161. c2.Reset().Lookup()
  162. assert.NoError(c2.Err, "")
  163. assert.True(strings.HasPrefix(c2.Container.Name, "rollback-"))
  164. c.Delete()
  165. c2.Delete()
  166. }
  167. func testStart(t *testing.T) {
  168. assert := require.New(t)
  169. c := NewContainer(config.DOCKER_HOST, &config.ContainerConfig{
  170. Cmd: "--pid=host --privileged --rm busybox echo hi",
  171. }).Parse().Start().Lookup()
  172. assert.NoError(c.Err, "")
  173. assert.True(c.HostConfig.Privileged, "")
  174. assert.True(c.Container.HostConfig.Privileged, "")
  175. assert.Equal("host", c.Container.HostConfig.PidMode, "")
  176. c.Delete()
  177. }
  178. func testLookup(t *testing.T) {
  179. assert := require.New(t)
  180. cfg := &config.ContainerConfig{
  181. Cmd: "--rm busybox echo hi",
  182. }
  183. c := NewContainer(config.DOCKER_HOST, cfg).Parse().Start()
  184. cfg2 := &config.ContainerConfig{
  185. Cmd: "--rm busybox echo hi2",
  186. }
  187. c2 := NewContainer(config.DOCKER_HOST, cfg2).Parse().Start()
  188. assert.NoError(c.Err, "")
  189. assert.NoError(c2.Err, "")
  190. c1Lookup := NewContainer(config.DOCKER_HOST, cfg).Lookup()
  191. c2Lookup := NewContainer(config.DOCKER_HOST, cfg2).Lookup()
  192. assert.NoError(c1Lookup.Err, "")
  193. assert.NoError(c2Lookup.Err, "")
  194. assert.Equal(c.Container.ID, c1Lookup.Container.ID, "")
  195. assert.Equal(c2.Container.ID, c2Lookup.Container.ID, "")
  196. c.Delete()
  197. c2.Delete()
  198. }
  199. func testDelete(t *testing.T) {
  200. assert := require.New(t)
  201. c := NewContainer(config.DOCKER_HOST, &config.ContainerConfig{
  202. Cmd: "--rm busybox echo hi",
  203. }).Parse()
  204. assert.False(c.Exists())
  205. assert.NoError(c.Err, "")
  206. c.Start()
  207. assert.NoError(c.Err, "")
  208. c.Reset()
  209. assert.NoError(c.Err, "")
  210. assert.True(c.Exists())
  211. assert.NoError(c.Err, "")
  212. c.Delete()
  213. assert.NoError(c.Err, "")
  214. c.Reset()
  215. assert.False(c.Exists())
  216. assert.NoError(c.Err, "")
  217. }
  218. func testDockerClientNames(t *testing.T) {
  219. assert := require.New(t)
  220. client, err := dockerClient.NewClient(config.DOCKER_HOST)
  221. assert.NoError(err, "")
  222. c, err := client.CreateContainer(dockerClient.CreateContainerOptions{
  223. Name: "foo",
  224. Config: &dockerClient.Config{
  225. Image: "ubuntu",
  226. },
  227. })
  228. assert.NoError(err, "")
  229. assert.Equal("foo", c.Name)
  230. c2, err := client.InspectContainer(c.ID)
  231. assert.NoError(err, "")
  232. assert.Equal("/foo", c2.Name)
  233. c2, err = inspect(client, c.ID)
  234. assert.NoError(err, "")
  235. assert.Equal("foo", c2.Name)
  236. client.RemoveContainer(dockerClient.RemoveContainerOptions{
  237. ID: c2.ID,
  238. Force: true,
  239. })
  240. }