123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- package docker
- import (
- "fmt"
- "strings"
- "testing"
- "github.com/rancherio/os/config"
- "github.com/rancherio/rancher-compose/librcompose/project"
- "github.com/stretchr/testify/require"
- dockerClient "github.com/fsouza/go-dockerclient"
- "os"
- )
- func testDockerHost(t *testing.T) {
- assert := require.New(t)
- assert.Equal(os.Getenv("DOCKER_HOST"), config.DOCKER_HOST)
- }
- func TestHash(t *testing.T) {
- assert := require.New(t)
- hash := getHash(&config.ContainerConfig{
- Id: "id",
- Cmd: "1 2 3",
- })
- hash2 := getHash(&config.ContainerConfig{
- Id: "id2",
- Cmd: "1 2 3",
- })
- hash3 := getHash(&config.ContainerConfig{
- Id: "id3",
- Cmd: "1 2 3 4",
- })
- assert.Equal("d601444333c7fb4cb955bcca36c5ed59b6fa8c3f", hash, "")
- assert.NotEqual(hash, hash2, "")
- assert.NotEqual(hash2, hash3, "")
- assert.NotEqual(hash, hash3, "")
- }
- func TestHash2(t *testing.T) {
- assert := require.New(t)
- cfg := &config.ContainerConfig{
- Id: "docker-volumes",
- Cmd: "",
- MigrateVolumes: false,
- ReloadConfig: false,
- CreateOnly: true,
- Service: &project.ServiceConfig{
- CapAdd: nil,
- CapDrop: nil,
- CpuShares: 0,
- Command: project.NewCommand(),
- Detach: "",
- Dns: project.NewStringorslice(),
- DnsSearch: project.NewStringorslice(),
- DomainName: "",
- Entrypoint: project.NewCommand(),
- EnvFile: project.NewStringorslice(),
- Environment: project.NewMaporEqualSlice([]string{}),
- Hostname: "",
- Image: "state",
- Labels: project.NewSliceorMap(map[string]string{
- "io.rancher.os.createonly": "true",
- "io.rancher.os.scope": "system"}),
- Links: project.NewMaporColonSlice(nil),
- LogDriver: "json-file",
- MemLimit: 0,
- Name: "",
- Net: "none",
- Pid: "",
- Ipc: "",
- Ports: nil,
- Privileged: true,
- Restart: "",
- ReadOnly: true,
- StdinOpen: false,
- Tty: false,
- User: "",
- Volumes: []string{
- "/var/lib/docker:/var/lib/docker",
- "/var/lib/rancher/conf:/var/lib/rancher/conf",
- "/var/lib/system-docker:/var/lib/system-docker"},
- VolumesFrom: nil,
- WorkingDir: "",
- Expose: nil,
- ExternalLinks: nil},
- }
- for i := 0; i < 1000; i++ {
- assert.Equal(getHash(cfg), getHash(cfg), fmt.Sprintf("Failed at iteration: %v", i))
- }
- }
- func TestBool2String(t *testing.T) {
- assert := require.New(t)
- assert.Equal("true", fmt.Sprint(true), "")
- }
- func TestParse(t *testing.T) {
- assert := require.New(t)
- cfg := &config.ContainerConfig{
- Cmd: "--name c1 " +
- "-d " +
- "--rm " +
- "--privileged " +
- "test/image " +
- "arg1 " +
- "arg2 ",
- }
- c := NewContainer("", cfg).Parse()
- assert.NoError(c.Err, "")
- assert.Equal(cfg.Id, "c1", "Id doesn't match")
- assert.Equal(c.Name, "c1", "Name doesn't match")
- assert.True(c.remove, "Remove doesn't match")
- assert.True(c.detach, "Detach doesn't match")
- assert.Equal(c.Config.Cmd.Len(), 2, "Args doesn't match")
- assert.Equal(c.Config.Cmd.Slice()[0], "arg1", "Arg1 doesn't match")
- assert.Equal(c.Config.Cmd.Slice()[1], "arg2", "Arg2 doesn't match")
- assert.True(c.HostConfig.Privileged, "Privileged doesn't match")
- }
- func TestIdFromName(t *testing.T) {
- assert := require.New(t)
- cfg := &config.ContainerConfig{
- Cmd: "--name foo -v /test busybox echo hi",
- }
- assert.Equal("", cfg.Id)
- NewContainer(config.DOCKER_HOST, cfg)
- assert.Equal("foo", cfg.Id)
- }
- func testMigrateVolumes(t *testing.T) {
- assert := require.New(t)
- c := NewContainer(config.DOCKER_HOST, &config.ContainerConfig{
- Cmd: "--name foo -v /test busybox echo hi",
- }).Parse().Start().Lookup()
- assert.NoError(c.Err, "")
- test_path, ok := c.Container.Volumes["/test"]
- assert.True(ok, "")
- c2 := NewContainer(config.DOCKER_HOST, &config.ContainerConfig{
- MigrateVolumes: true,
- Cmd: "--name foo -v /test2 busybox echo hi",
- }).Parse().Start().Lookup()
- assert.NoError(c2.Err, "")
- assert.True(c2.Container != nil)
- _, ok = c2.Container.Volumes["/test2"]
- assert.True(ok, "")
- assert.Equal(test_path, c2.Container.Volumes["/test"])
- c.Delete()
- c2.Delete()
- }
- func testRollback(t *testing.T) {
- assert := require.New(t)
- c := NewContainer(config.DOCKER_HOST, &config.ContainerConfig{
- Cmd: "--name rollback busybox echo hi",
- }).Parse().Start().Lookup()
- assert.NoError(c.Err, "")
- assert.Equal("rollback", c.Container.Name)
- c2 := NewContainer(config.DOCKER_HOST, &config.ContainerConfig{
- Cmd: "--name rollback busybox echo bye",
- }).Parse().Start().Lookup()
- assert.Equal("rollback", c2.Container.Name)
- assert.NoError(c2.Err, "")
- assert.NotEqual(c.Container.ID, c2.Container.ID)
- c3 := NewContainer(config.DOCKER_HOST, &config.ContainerConfig{
- Cmd: "--name rollback busybox echo hi",
- }).Parse().Start().Lookup()
- assert.NoError(c3.Err, "")
- assert.Equal(c.Container.ID, c3.Container.ID)
- assert.Equal("rollback", c3.Container.Name)
- c2.Reset().Lookup()
- assert.NoError(c2.Err, "")
- assert.True(strings.HasPrefix(c2.Container.Name, "rollback-"))
- c.Delete()
- c2.Delete()
- }
- func testStart(t *testing.T) {
- assert := require.New(t)
- c := NewContainer(config.DOCKER_HOST, &config.ContainerConfig{
- Cmd: "--pid=host --privileged --rm busybox echo hi",
- }).Parse().Start().Lookup()
- assert.NoError(c.Err, "")
- assert.True(c.HostConfig.Privileged, "")
- assert.True(c.Container.HostConfig.Privileged, "")
- assert.Equal("host", c.Container.HostConfig.PidMode, "")
- c.Delete()
- }
- func testLookup(t *testing.T) {
- assert := require.New(t)
- cfg := &config.ContainerConfig{
- Cmd: "--rm busybox echo hi",
- }
- c := NewContainer(config.DOCKER_HOST, cfg).Parse().Start()
- cfg2 := &config.ContainerConfig{
- Cmd: "--rm busybox echo hi2",
- }
- c2 := NewContainer(config.DOCKER_HOST, cfg2).Parse().Start()
- assert.NoError(c.Err, "")
- assert.NoError(c2.Err, "")
- c1Lookup := NewContainer(config.DOCKER_HOST, cfg).Lookup()
- c2Lookup := NewContainer(config.DOCKER_HOST, cfg2).Lookup()
- assert.NoError(c1Lookup.Err, "")
- assert.NoError(c2Lookup.Err, "")
- assert.Equal(c.Container.ID, c1Lookup.Container.ID, "")
- assert.Equal(c2.Container.ID, c2Lookup.Container.ID, "")
- c.Delete()
- c2.Delete()
- }
- func testDelete(t *testing.T) {
- assert := require.New(t)
- c := NewContainer(config.DOCKER_HOST, &config.ContainerConfig{
- Cmd: "--rm busybox echo hi",
- }).Parse()
- assert.False(c.Exists())
- assert.NoError(c.Err, "")
- c.Start()
- assert.NoError(c.Err, "")
- c.Reset()
- assert.NoError(c.Err, "")
- assert.True(c.Exists())
- assert.NoError(c.Err, "")
- c.Delete()
- assert.NoError(c.Err, "")
- c.Reset()
- assert.False(c.Exists())
- assert.NoError(c.Err, "")
- }
- func testDockerClientNames(t *testing.T) {
- assert := require.New(t)
- client, err := dockerClient.NewClient(config.DOCKER_HOST)
- assert.NoError(err, "")
- c, err := client.CreateContainer(dockerClient.CreateContainerOptions{
- Name: "foo",
- Config: &dockerClient.Config{
- Image: "ubuntu",
- },
- })
- assert.NoError(err, "")
- assert.Equal("foo", c.Name)
- c2, err := client.InspectContainer(c.ID)
- assert.NoError(err, "")
- assert.Equal("/foo", c2.Name)
- c2, err = inspect(client, c.ID)
- assert.NoError(err, "")
- assert.Equal("foo", c2.Name)
- client.RemoveContainer(dockerClient.RemoveContainerOptions{
- ID: c2.ID,
- Force: true,
- })
- }
|