Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 939b9a3

Browse files
committed
Handled empty target context name when deciding to mount bindings
Testing with local and tcp hosts Signed-off-by: Nick Adcock <[email protected]>
1 parent 5168e5b commit 939b9a3

File tree

32 files changed

+156
-59
lines changed

32 files changed

+156
-59
lines changed

Gopkg.lock

Lines changed: 10 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ required = ["github.com/wadey/gocovmerge"]
4444

4545
[[override]]
4646
name = "github.com/deislabs/duffle"
47-
source = "github.com/simonferquel/duffle"
48-
branch = "custom-container-config"
47+
branch = "master"
4948

5049
[[constraint]]
5150
name = "github.com/sirupsen/logrus"

e2e/commands_test.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,15 @@ func TestBundle(t *testing.T) {
276276
assert.Assert(t, fs.Equal(tmpDir.Join("simple.dockerapp"), manifest))
277277
}
278278

279-
func TestDockerAppLifecycle(t *testing.T) {
279+
func TestDockerAppLifecycleWithBindMounts(t *testing.T) {
280+
testDockerAppLifecycle(t, true)
281+
}
282+
283+
func TestDockerAppLifecycleWithoutBindMounts(t *testing.T) {
284+
testDockerAppLifecycle(t, false)
285+
}
286+
287+
func testDockerAppLifecycle(t *testing.T, useBindMount bool) {
280288
cmd, cleanup := dockerCli.createTestCmd()
281289
defer cleanup()
282290

@@ -301,13 +309,18 @@ func TestDockerAppLifecycle(t *testing.T) {
301309
cmd.Command = dockerCli.Command("context", "create", "swarm-context", "--docker", fmt.Sprintf(`"host=tcp://%s"`, swarm.GetAddress(t)), "--default-stack-orchestrator", "swarm")
302310
icmd.RunCmd(cmd).Assert(t, icmd.Success)
303311

304-
// When creating a context on a Windows host we cannot use
305-
// the unix socket but it's needed inside the invocation image.
306-
// The workaround is to create a context with an empty host.
307-
// This host will default to the unix socket inside the
308-
// invocation image
309-
cmd.Command = dockerCli.Command("context", "create", "swarm-target-context", "--docker", "host=", "--default-stack-orchestrator", "swarm")
310-
icmd.RunCmd(cmd).Assert(t, icmd.Success)
312+
if useBindMount {
313+
// When creating a context on a Windows host we cannot use
314+
// the unix socket but it's needed inside the invocation image.
315+
// The workaround is to create a context with an empty host.
316+
// This host will default to the unix socket inside the
317+
// invocation image
318+
cmd.Command = dockerCli.Command("context", "create", "swarm-target-context", "--docker", "host=", "--default-stack-orchestrator", "swarm")
319+
icmd.RunCmd(cmd).Assert(t, icmd.Success)
320+
} else {
321+
cmd.Command = dockerCli.Command("context", "create", "swarm-target-context", "--docker", fmt.Sprintf(`"host=tcp://%s"`, swarm.GetPrivateAddress(t)), "--default-stack-orchestrator", "swarm")
322+
icmd.RunCmd(cmd).Assert(t, icmd.Success)
323+
}
311324

312325
// Initialize the swarm
313326
cmd.Env = append(cmd.Env, "DOCKER_CONTEXT=swarm-context")

e2e/helper_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,13 @@ func (c *Container) GetAddress(t *testing.T) string {
9797
c.address = fmt.Sprintf("127.0.0.1:%v", strings.Trim(strings.Split(result.Stdout(), ":")[1], " \r\n"))
9898
return c.address
9999
}
100+
101+
// GetPrivateAddress returns the host:port this container listens on
102+
func (c *Container) GetPrivateAddress(t *testing.T) string {
103+
container := c.parentContainer
104+
if container == "" {
105+
container = c.container
106+
}
107+
result := icmd.RunCommand(dockerCli.path, "inspect", container, "-f", "{{.NetworkSettings.IPAddress}}").Assert(t, icmd.Success)
108+
return fmt.Sprintf("%s:%d", strings.TrimSpace(result.Stdout()), c.privatePort)
109+
}

e2e/testdata/simple-bundle.json.golden

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,39 @@
1717
"invocationImages": [
1818
{
1919
"imageType": "docker",
20-
"image": "simple:1.1.0-beta1-invoc"
20+
"image": "simple:1.1.0-beta1-invoc",
21+
"platform": {}
2122
}
2223
],
2324
"images": {
2425
"api": {
2526
"imageType": "docker",
2627
"image": "python:3.6",
28+
"platform": {},
2729
"description": "python:3.6",
2830
"refs": null
2931
},
3032
"db": {
3133
"imageType": "docker",
3234
"image": "postgres:9.3",
35+
"platform": {},
3336
"description": "postgres:9.3",
3437
"refs": null
3538
},
3639
"web": {
3740
"imageType": "docker",
3841
"image": "nginx:latest",
42+
"platform": {},
3943
"description": "nginx:latest",
4044
"refs": null
4145
}
4246
},
4347
"actions": {
4448
"com.docker.app.inspect": {
45-
"Modifies": false
49+
"modifies": false
4650
},
4751
"com.docker.app.status": {
48-
"Modifies": false
52+
"modifies": false
4953
}
5054
},
5155
"parameters": {

internal/commands/cnab.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package commands
22

33
import (
4-
"github.com/docker/cli/cli/context/docker"
54
"fmt"
65
"io/ioutil"
76
"os"
@@ -17,6 +16,8 @@ import (
1716
"github.com/docker/app/internal/packager"
1817
bundlestore "github.com/docker/app/internal/store"
1918
"github.com/docker/cli/cli/command"
19+
"github.com/docker/cli/cli/context"
20+
"github.com/docker/cli/cli/context/docker"
2021
"github.com/docker/cli/cli/context/store"
2122
"github.com/docker/distribution/reference"
2223
"github.com/docker/docker/api/types/container"
@@ -179,29 +180,36 @@ func resolveBundle(dockerCli command.Cli, name string, pullRef bool, insecureReg
179180
return nil, fmt.Errorf("could not resolve bundle %q", name)
180181
}
181182

182-
func requiresBindMount(targetContextName string, targetOrchestrator string, dockerCli command.Cli) (bool, error){
183-
if targetOrchestrator == "kubernetes"{
183+
func requiresBindMount(targetContextName string, targetOrchestrator string, dockerCli command.Cli) (bool, error) {
184+
if targetOrchestrator == "kubernetes" {
184185
return false, nil
185186
}
187+
188+
// TODO:smarter handling of default context required
189+
if targetContextName == "" {
190+
return true, nil
191+
}
192+
186193
ctxMeta, err := dockerCli.ContextStore().GetContextMetadata(targetContextName)
187-
if err != nil{
194+
if err != nil {
188195
return false, err
189196
}
190197
dockerCtx, err := command.GetDockerContext(ctxMeta)
191-
if err != nil{
198+
if err != nil {
192199
return false, err
193200
}
194-
if dockerCtx.StackOrchestrator == command.OrchestratorKubernetes{
201+
if dockerCtx.StackOrchestrator == command.OrchestratorKubernetes {
195202
return false, nil
196203
}
197204
dockerEndpoint, err := docker.EndpointFromContext(ctxMeta)
198-
if err != nil{
205+
if err != nil {
199206
return false, err
200207
}
208+
209+
return isDockerHostLocal(dockerEndpoint), nil
210+
}
211+
212+
func isDockerHostLocal(dockerEndpoint context.EndpointMetaBase) bool {
201213
host := dockerEndpoint.Host
202-
switch host{
203-
case "", "unix:///var/run/docker.sock", "npipe:////./pipe/docker_engine":
204-
return true, nil
205-
}
206-
return false, nil
207-
}
214+
return host == "" || host == "unix:///var/run/docker.sock" || host == "npipe:////./pipe/docker_engine"
215+
}

internal/commands/cnab_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package commands
2+
3+
import (
4+
"testing"
5+
6+
"github.com/docker/cli/cli/command"
7+
"github.com/docker/cli/cli/context"
8+
"gotest.tools/assert"
9+
)
10+
11+
func TestRequiresBindMount(t *testing.T) {
12+
dockerCli, err := command.NewDockerCli()
13+
assert.NilError(t, err)
14+
15+
testCases := []struct {
16+
name string
17+
targetContextName string
18+
targetOrchestrator string
19+
expectedResult bool
20+
expectedError string
21+
}{
22+
{
23+
name: "kubernetes-orchestrator",
24+
targetContextName: "target-context",
25+
targetOrchestrator: "kubernetes",
26+
expectedResult: false,
27+
expectedError: "",
28+
},
29+
{
30+
name: "no-context",
31+
targetContextName: "",
32+
targetOrchestrator: "swarm",
33+
expectedResult: true,
34+
expectedError: "",
35+
},
36+
}
37+
38+
for _, testCase := range testCases {
39+
t.Run(testCase.name, func(t *testing.T) {
40+
result, err := requiresBindMount(testCase.targetContextName, testCase.targetOrchestrator, dockerCli)
41+
if testCase.expectedError == "" {
42+
assert.NilError(t, err)
43+
} else {
44+
assert.Error(t, err, testCase.expectedError)
45+
}
46+
assert.Equal(t, testCase.expectedResult, result)
47+
})
48+
}
49+
}
50+
51+
func TestIsDockerHostLocal(t *testing.T) {
52+
testCases := []struct {
53+
name string
54+
host string
55+
expected bool
56+
}{
57+
{
58+
name: "not-local",
59+
host: "tcp://not.local.host",
60+
expected: false,
61+
},
62+
{
63+
name: "no-endpoint",
64+
host: "",
65+
expected: true,
66+
},
67+
{
68+
name: "docker-sock",
69+
host: "unix:///var/run/docker.sock",
70+
expected: true,
71+
},
72+
{
73+
name: "named-pipe",
74+
host: "npipe:////./pipe/docker_engine",
75+
expected: true,
76+
},
77+
}
78+
79+
for _, testCase := range testCases {
80+
t.Run(testCase.name, func(t *testing.T) {
81+
dockerEndpoint := context.EndpointMetaBase{Host: testCase.host}
82+
assert.Equal(t, testCase.expected, isDockerHostLocal(dockerEndpoint))
83+
})
84+
}
85+
}

vendor/github.com/coreos/etcd/Documentation/README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

vendor/github.com/coreos/etcd/cmd/etcd

Lines changed: 0 additions & 1 deletion
This file was deleted.

vendor/github.com/coreos/etcd/cmd/etcdctl

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)