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

Commit eb657c0

Browse files
committed
Address PR feedback
1 parent 8cb7f8e commit eb657c0

File tree

4 files changed

+64
-72
lines changed

4 files changed

+64
-72
lines changed

ecs-cli/modules/cli/local/network/generate_mock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313

1414
package network
1515

16-
//go:generate /Users/karakuse/go/src/github.com/aws/amazon-ecs-cli/scripts/mockgen.sh github.com/aws/amazon-ecs-cli/ecs-cli/modules/cli/local/network IdempotentLocalEndpointsStarter mock_network/network.go
16+
//go:generate mockgen.sh github.com/aws/amazon-ecs-cli/ecs-cli/modules/cli/local/network LocalEndpointsStarter mock_network/network.go

ecs-cli/modules/cli/local/network/mock_network/network.go

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

ecs-cli/modules/cli/local/network/network.go

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,28 @@ import (
2020
"strings"
2121
"time"
2222

23-
"github.com/docker/docker/client"
24-
23+
"github.com/docker/docker/api/types"
2524
"github.com/docker/docker/api/types/container"
26-
2725
"github.com/docker/docker/api/types/filters"
28-
26+
"github.com/docker/docker/api/types/network"
27+
"github.com/docker/docker/client"
2928
"github.com/sirupsen/logrus"
3029
"golang.org/x/net/context"
31-
32-
"github.com/docker/docker/api/types/network"
33-
34-
"github.com/docker/docker/api/types"
3530
)
3631

37-
// IdempotentLocalEndpointsStarter groups Docker functions to create the ECS local network and
32+
// LocalEndpointsStarter groups Docker functions to create the ECS local network and
3833
// the Local Container Endpoints container if they don't exist.
39-
type IdempotentLocalEndpointsStarter interface {
40-
idempotentNetworkCreator
41-
idempotentContainerStarter
34+
type LocalEndpointsStarter interface {
35+
networkCreator
36+
containerStarter
4237
}
4338

44-
type idempotentNetworkCreator interface {
39+
type networkCreator interface {
4540
NetworkInspect(ctx context.Context, networkID string, options types.NetworkInspectOptions) (types.NetworkResource, error)
4641
NetworkCreate(ctx context.Context, name string, options types.NetworkCreate) (types.NetworkCreateResponse, error)
4742
}
4843

49-
type idempotentContainerStarter interface {
44+
type containerStarter interface {
5045
ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error)
5146
ContainerCreate(ctx context.Context, config *container.Config,
5247
hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig,
@@ -85,26 +80,24 @@ const (
8580
// already exists or the container is already running then the operation is a no-op.
8681
//
8782
// If there is any unexpected errors, we exit the program with a fatal log.
88-
func Setup(docker IdempotentLocalEndpointsStarter) {
89-
setupLocalNetwork(docker)
90-
setupLocalEndpointsContainer(docker)
83+
func Setup(dockerClient LocalEndpointsStarter) {
84+
setupLocalNetwork(dockerClient)
85+
setupLocalEndpointsContainer(dockerClient)
9186
}
9287

93-
// setupLocalNetwork creates the local network if it doesn't already exist.
94-
func setupLocalNetwork(docker idempotentNetworkCreator) {
95-
if localNetworkExists(docker) {
88+
func setupLocalNetwork(dockerClient networkCreator) {
89+
if localNetworkExists(dockerClient) {
9690
logrus.Infof("The network %s already exists", EcsLocalNetworkName)
9791
return
9892
}
99-
createLocalNetwork(docker)
93+
createLocalNetwork(dockerClient)
10094
}
10195

102-
// localNetworkExists returns true if the local network already exists, false otherwise.
103-
func localNetworkExists(docker idempotentNetworkCreator) bool {
96+
func localNetworkExists(dockerClient networkCreator) bool {
10497
ctx, cancel := context.WithTimeout(context.Background(), dockerTimeout)
10598
defer cancel()
10699

107-
_, err := docker.NetworkInspect(ctx, EcsLocalNetworkName, types.NetworkInspectOptions{})
100+
_, err := dockerClient.NetworkInspect(ctx, EcsLocalNetworkName, types.NetworkInspectOptions{})
108101
if err != nil {
109102
if client.IsErrNotFound(err) {
110103
return false
@@ -115,12 +108,11 @@ func localNetworkExists(docker idempotentNetworkCreator) bool {
115108
return true
116109
}
117110

118-
// createLocalNetwork creates the local network.
119-
func createLocalNetwork(docker idempotentNetworkCreator) {
111+
func createLocalNetwork(dockerClient networkCreator) {
120112
ctx, cancel := context.WithTimeout(context.Background(), dockerTimeout)
121113
defer cancel()
122114

123-
resp, err := docker.NetworkCreate(ctx, EcsLocalNetworkName, types.NetworkCreate{
115+
resp, err := dockerClient.NetworkCreate(ctx, EcsLocalNetworkName, types.NetworkCreate{
124116
IPAM: &network.IPAM{
125117
Config: []network.IPAMConfig{
126118
{
@@ -135,21 +127,20 @@ func createLocalNetwork(docker idempotentNetworkCreator) {
135127
logrus.Infof("Created network %s with ID %s", EcsLocalNetworkName, resp.ID)
136128
}
137129

138-
// setupLocalEndpointsContainer creates the Local Endpoints container if one doesn't exist already.
139-
func setupLocalEndpointsContainer(docker idempotentContainerStarter) {
130+
func setupLocalEndpointsContainer(docker containerStarter) {
140131
containerID := createLocalEndpointsContainer(docker)
141132
startContainer(docker, containerID)
142133
}
143134

144135
// createLocalEndpointsContainer returns the ID of the newly created container.
145136
// If the container already exists, returns the ID of the existing container.
146-
func createLocalEndpointsContainer(docker idempotentContainerStarter) string {
137+
func createLocalEndpointsContainer(dockerClient containerStarter) string {
147138
ctx, cancel := context.WithTimeout(context.Background(), dockerTimeout)
148139
defer cancel()
149140

150141
// See First Scenario in https://aws.amazon.com/blogs/compute/a-guide-to-locally-testing-containers-with-amazon-ecs-local-endpoints-and-docker-compose/
151142
// for an explanation of these fields.
152-
resp, err := docker.ContainerCreate(ctx,
143+
resp, err := dockerClient.ContainerCreate(ctx,
153144
&container.Config{
154145
Image: localEndpointsImageName,
155146
Env: []string{
@@ -178,7 +169,7 @@ func createLocalEndpointsContainer(docker idempotentContainerStarter) string {
178169
if err != nil {
179170
if strings.Contains(err.Error(), "Conflict") {
180171
// We already created this container before since there is a name conflict, fetch its ID and return it.
181-
containerID := localEndpointsContainerID(docker)
172+
containerID := localEndpointsContainerID(dockerClient)
182173
logrus.Infof("The %s container already exists with ID %s", localEndpointsContainerName, containerID)
183174
return containerID
184175
}
@@ -189,12 +180,11 @@ func createLocalEndpointsContainer(docker idempotentContainerStarter) string {
189180
return resp.ID
190181
}
191182

192-
// localEndpointsContainerID returns the ID of the Local Endpoints container.
193-
func localEndpointsContainerID(docker idempotentContainerStarter) string {
183+
func localEndpointsContainerID(dockerClient containerStarter) string {
194184
ctx, cancel := context.WithTimeout(context.Background(), dockerTimeout)
195185
defer cancel()
196186

197-
resp, err := docker.ContainerList(ctx, types.ContainerListOptions{
187+
resp, err := dockerClient.ContainerList(ctx, types.ContainerListOptions{
198188
All: true,
199189
Filters: filters.NewArgs(
200190
filters.Arg("name", localEndpointsContainerName),
@@ -203,16 +193,18 @@ func localEndpointsContainerID(docker idempotentContainerStarter) string {
203193
if err != nil {
204194
logrus.Fatalf("Failed to list containers with name %s due to %v", localEndpointsContainerName, err)
205195
}
196+
if len(resp) != 1 {
197+
logrus.Fatalf("Expected to find one container named %s but found %d", localEndpointsContainerName, len(resp))
198+
}
206199
return resp[0].ID
207200
}
208201

209-
// startContainer starts the container with the containerID.
210-
// If the container is already running, Docker does not return an error response.
211-
func startContainer(docker idempotentContainerStarter, containerID string) {
202+
func startContainer(dockerClient containerStarter, containerID string) {
212203
ctx, cancel := context.WithTimeout(context.Background(), dockerTimeout)
213204
defer cancel()
214205

215-
if err := docker.ContainerStart(ctx, containerID, types.ContainerStartOptions{}); err != nil {
206+
// If the container is already running, Docker does not return an error response.
207+
if err := dockerClient.ContainerStart(ctx, containerID, types.ContainerStartOptions{}); err != nil {
216208
logrus.Fatalf("Failed to start container with ID %s due to %v", containerID, err)
217209
}
218210
logrus.Infof("Started container with ID %s", containerID)

ecs-cli/modules/cli/local/network/network_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/cli/local/network/mock_network"
1414
)
1515

16-
type CallsConfigurer func(docker *mock_network.MockIdempotentLocalEndpointsStarter) *mock_network.MockIdempotentLocalEndpointsStarter
16+
type CallsConfigurer func(docker *mock_network.MockLocalEndpointsStarter) *mock_network.MockLocalEndpointsStarter
1717

1818
type notFoundErr struct{}
1919

@@ -33,7 +33,7 @@ func TestSetup(t *testing.T) {
3333
configureCalls CallsConfigurer
3434
}{
3535
"new network and new container": {
36-
configureCalls: func(docker *mock_network.MockIdempotentLocalEndpointsStarter) *mock_network.MockIdempotentLocalEndpointsStarter {
36+
configureCalls: func(docker *mock_network.MockLocalEndpointsStarter) *mock_network.MockLocalEndpointsStarter {
3737
containerID := "1234"
3838
gomock.InOrder(
3939
// We expect to create the network if it doesn't exist already.
@@ -53,7 +53,7 @@ func TestSetup(t *testing.T) {
5353
},
5454
},
5555
"existing network and existing container": {
56-
configureCalls: func(docker *mock_network.MockIdempotentLocalEndpointsStarter) *mock_network.MockIdempotentLocalEndpointsStarter {
56+
configureCalls: func(docker *mock_network.MockLocalEndpointsStarter) *mock_network.MockLocalEndpointsStarter {
5757
networkID := "abcd"
5858
containerID := "1234"
5959
gomock.InOrder(
@@ -87,9 +87,9 @@ func TestSetup(t *testing.T) {
8787
}
8888
}
8989

90-
func newMockLocalNetworkStarter(t *testing.T) *mock_network.MockIdempotentLocalEndpointsStarter {
90+
func newMockLocalNetworkStarter(t *testing.T) *mock_network.MockLocalEndpointsStarter {
9191
ctrl := gomock.NewController(t)
9292
defer ctrl.Finish()
9393

94-
return mock_network.NewMockIdempotentLocalEndpointsStarter(ctrl)
94+
return mock_network.NewMockLocalEndpointsStarter(ctrl)
9595
}

0 commit comments

Comments
 (0)