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 committed Oct 31, 2018
1 parent 4005cef commit cecbf5f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 8 deletions.
11 changes: 5 additions & 6 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
"docker.io/go-docker"
"docker.io/go-docker/api/types"
"docker.io/go-docker/api/types/container"
"docker.io/go-docker/api/types/network"
"github.com/docker/go-connections/nat"
"github.com/it-chain/iLogger"
"github.com/it-chain/tesseract"
"docker.io/go-docker/api/types/network"
)

func CreateContainer(config tesseract.ContainerConfig) (container.ContainerCreateCreatedBody, error) {
Expand Down Expand Up @@ -52,12 +52,12 @@ func CreateContainer(config tesseract.ContainerConfig) (container.ContainerCreat
networkName = config.Network.Name
endpointSetting := make(map[string]*network.EndpointSettings)
endpointSetting[config.Network.Name] = &network.EndpointSettings{
IPAddress: config.ContainerIp,
IPAddress: config.ContainerIp,
}
networkConfig = &network.NetworkingConfig{
EndpointsConfig: endpointSetting,
}
}else{
} else {
exposedPort = nat.PortSet{
nat.Port(config.Port + "/tcp"): struct{}{},
}
Expand Down Expand Up @@ -87,7 +87,6 @@ func CreateContainer(config tesseract.ContainerConfig) (container.ContainerCreat
AttachStdout: true,
AttachStderr: true,
ExposedPorts: exposedPort,

}, &container.HostConfig{
CapAdd: []string{"SYS_ADMIN"},
PortBindings: portBinding,
Expand Down Expand Up @@ -332,8 +331,8 @@ func FindNetworkByName(name string) (tesseract.Network, error) {
for _, net := range networkList {
if net.Name == name {
return tesseract.Network{
ID: net.ID,
Name: net.Name,
ID: net.ID,
Name: net.Name,
}, nil
}
}
Expand Down
3 changes: 1 addition & 2 deletions docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ func TestCreateContainerWithVolume(t *testing.T) {
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",
},
},
)
Expand All @@ -124,7 +123,7 @@ func TestCreateContainerWithVolume(t *testing.T) {
panic(err)
}

err = docker.StartContainer(res)
_, err = docker.StartContainer(res)
if err != nil {
panic(err)
}
Expand Down
69 changes: 69 additions & 0 deletions test/volume/volume-test.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"
"path"
"time"

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

func main() {

volume := os.Args[1]
GOPATH := os.Getenv("GOPATH")

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: "50001",
StartCmd: []string{"go", "run", path.Join(GOPATH, "/tesseract/mock/test-volume/main.go")},
Network: nil,
Mount: volume + ":" + "/go/src",
},
)

if err != nil {
panic(err)
}

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

time.Sleep(30 * time.Second)

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

0 comments on commit cecbf5f

Please sign in to comment.