Skip to content

Commit

Permalink
Create test icode to check docker volume mount inside docker
Browse files Browse the repository at this point in the history
  • Loading branch information
junbeomlee authored and hea9549 committed Oct 27, 2018
1 parent 43a8d6b commit 559f728
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ install:
- dep ensure

script:
- bash ./travis/run.sh.sh
- bash ./travis/run.sh
2 changes: 1 addition & 1 deletion container.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type ContainerConfig struct {

Network *Network // docker network option. if you don't use, nil

Mount string // [Mount-src-path or volume name]:[Mount-dist-path] ex) /go/src/github.com/it-chain/learn-icode:/icode
Mount []string // [Mount-src-path or volume name]:[Mount-dist-path] ex) /go/src/github.com/it-chain/learn-icode:/icode

//Volume *Volume // docker volume option.
//
Expand Down
17 changes: 12 additions & 5 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ func CreateContainer(config tesseract.ContainerConfig) (container.ContainerCreat
}, &container.HostConfig{
CapAdd: []string{"SYS_ADMIN"},
PortBindings: portBinding,
Binds: []string{
config.Mount,
},
NetworkMode: container.NetworkMode(networkName),
Binds: config.Mount,
NetworkMode: container.NetworkMode(networkName),
}, nil, containerName)

if err != nil {
Expand Down Expand Up @@ -257,8 +255,9 @@ func CreateVolume(name string) (tesseract.Volume, error) {
if err != nil {
return tesseract.Volume{}, err
}

if !isVolumeEmpty(vol) {
return tesseract.Volume{}, errors.New(fmt.Sprintf("volume [name: %s] already exist", vol.Name))
return tesseract.NewVolume(vol.CreatedAt, vol.Driver, vol.Mountpoint, vol.Name, vol.Options), nil
}

res, err := cli.VolumeCreate(ctx, convToVolumesCreateBody(name))
Expand All @@ -269,6 +268,14 @@ func CreateVolume(name string) (tesseract.Volume, error) {
return tesseract.NewVolume(res.CreatedAt, res.Driver, res.Mountpoint, res.Name, res.Options), nil
}

func RemoveVolume(name string, force bool) error {
ctx := context.Background()
cli, _ := docker.NewEnvClient()
defer cli.Close()

return cli.VolumeRemove(ctx, name, force)
}

func CreateNetwork(name string) (tesseract.Network, error) {
ctx := context.Background()
cli, _ := docker.NewEnvClient()
Expand Down
92 changes: 50 additions & 42 deletions docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func TestCreateContainer(t *testing.T) {
Tag: "1.9",
}

docker.CreateVolume("testVolume")
GOPATH := os.Getenv("GOPATH")
// when
res, err := docker.CreateContainer(tesseract.ContainerConfig{
Expand All @@ -73,7 +72,9 @@ func TestCreateContainer(t *testing.T) {
Port: "50001",
StartCmd: []string{"go", "run", path.Join("/go/src", "icode_1", "icode.go"), "-p", "50001"},
Network: nil,
Mount: path.Join(GOPATH, "src/github.com/it-chain/tesseract/mock") + ":" + "/go/src",
Mount: []string{
path.Join(GOPATH, "src/github.com/it-chain/tesseract/mock") + ":" + "/go/src",
},
},
)

Expand All @@ -89,45 +90,53 @@ func TestCreateContainer(t *testing.T) {
assert.Equal(t, "/container_mock", containerName)
}

//func TestCreateContainerWithVolume(t *testing.T) {
// defer setup(t, removeAllContainers)()
//
// testGolangImg := tesseract.ContainerImage{
// Name: "golang",
// Tag: "1.9",
// }
//
// v, err := docker.CreateVolume("it-chain-default-volume")
// assert.NoError(t, err)
// n, err := docker.CreateNetwork("it-chain-default-network")
// assert.NoError(t, err)
//
// // when
// res, err := docker.CreateContainer(
// tesseract.ContainerConfig{
// Name: "container_mock",
// ContainerImage: testGolangImg,
// IP: "127.0.0.1",
// Port: "50001",
// StartCmd: []string{"go", "run.sh", "icode_1/icode.go", "-p", "50001"},
// Network: &n,
// Mount: v.Name + ":" + "/go/src",
// },
// )
//
// assert.NoError(t, err)
//
// err = docker.StartContainer(res)
// assert.NoError(t, err)
//
// time.Sleep(10 * time.Second)
//
// // when
// containerName, err := getContainerName(res.ID)
// // then
// assert.NoError(t, err)
// assert.Equal(t, "/container_mock", containerName)
//}
func TestCreateContainerWithVolume(t *testing.T) {
docker.RemoveVolume("it-chain-default-volume", true)
defer setup(t, removeAllContainers)()

v, err := docker.CreateVolume("it-chain-default-volume")
assert.NoError(t, err)

GOPATH := os.Getenv("GOPATH")
GolangImg := tesseract.ContainerImage{
Name: "teamit/itchain-test",
Tag: "latest",
}

// when
res, err := docker.CreateContainer(
tesseract.ContainerConfig{
Name: "container_volume_test",
ContainerImage: GolangImg,
IP: "127.0.0.1",
Port: "50001",
StartCmd: []string{"cp", "-r", "/go/src/github.com/it-chain/tesseract/", "/volume/", "&&", "go", "run", "/go/src/github.com/it-chain/tesseract/test/volume/main.go", v.Name},
Network: nil,
Mount: []string{
v.Name + ":" + "/volume/",
path.Join(GOPATH, "src/github.com/it-chain/tesseract") + ":" + "/go/src/github.com/it-chain/tesseract",
"/var/run/docker.sock:/var/run/docker.sock",
},
},
)

if err != nil {
panic(err)
}

err = docker.StartContainer(res)
if err != nil {
panic(err)
}

time.Sleep(60 * time.Second)

// when
containerName, err := getContainerName(res.ID)
// then
assert.NoError(t, err)
assert.Equal(t, "/container_volume_test", containerName)
}

//func TestCreateContainer_WhenSameNamedContainerExist_RandomGenerateName(t *testing.T) {
// defer setup(t, removeAllContainers)()
Expand Down Expand Up @@ -348,7 +357,6 @@ func connect(ipAddress string, port string, timeout time.Duration) error {
c := make(chan *rpc.ClientStream, 1)

go func() {

ticker := time.NewTicker(2 * time.Second)
for _ = range ticker.C {
client, err := rpc.NewClientStream(ipAddress + ":" + port)
Expand Down
24 changes: 24 additions & 0 deletions mock/test-volume/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2018 It-chain
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package main

import "os"

func main() {
err := os.Mkdir("/go/src/test", os.FileMode(777))
panic(err)
}
69 changes: 69 additions & 0 deletions test/volume/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2018 It-chain
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package main

import (
"os"
"time"

"github.com/it-chain/tesseract"
"github.com/it-chain/tesseract/docker"
)

func main() {

volume := os.Args[1]
if volume == "" {
panic("Volume name is missing")
}

testGolangImg := tesseract.ContainerImage{
Name: "golang",
Tag: "1.9",
}

// when
res, err := docker.CreateContainer(
tesseract.ContainerConfig{
Name: "container_mock",
ContainerImage: testGolangImg,
IP: "127.0.0.1",
Port: "50002",
//StartCmd: []string{"go", "run", "/go/src/github.com/it-chain/tesseract/mock/test-volume/main.go"},
StartCmd: []string{"sleep", "1000"},
Network: nil,
Mount: []string{
volume + ":" + "/go/src",
},
},
)

if err != nil {
panic(err)
}

err = docker.StartContainer(res)
if err != nil {
panic(err)
}

time.Sleep(60 * time.Second)

if _, err := os.Stat("/go/src/test"); os.IsNotExist(err) {
panic("fail to bind volume")
}
}

0 comments on commit 559f728

Please sign in to comment.