Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/go_modules/aws-sdk-6e839e1985
Browse files Browse the repository at this point in the history
  • Loading branch information
elgohr authored Dec 6, 2023
2 parents 958a9ff + e1915e8 commit fb04067
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
15 changes: 10 additions & 5 deletions localstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func (i *Instance) buildLocalImage(ctx context.Context) error {
}

func (i *Instance) mapPorts(ctx context.Context, services []Service, containerId string, try int) error {
if try > 5 {
if try > 10 {
return errors.New("localstack: could not get port from container")
}
startedContainer, err := i.cli.ContainerInspect(ctx, containerId)
Expand All @@ -384,7 +384,7 @@ func (i *Instance) mapPorts(ctx context.Context, services []Service, containerId
if i.fixedPort {
bindings := ports[nat.Port(FixedPort.Port)]
if len(bindings) == 0 {
time.Sleep(time.Second)
time.Sleep(300 * time.Millisecond)
return i.mapPorts(ctx, services, containerId, try+1)
}
i.portMappingMutex.Lock()
Expand All @@ -395,10 +395,15 @@ func (i *Instance) mapPorts(ctx context.Context, services []Service, containerId
i.portMappingMutex.Lock()
defer i.portMappingMutex.Unlock()
for service := range AvailableServices {
bindings := ports[nat.Port(service.Port)]
if len(bindings) == 0 {
time.Sleep(300 * time.Millisecond)
return i.mapPorts(ctx, services, containerId, try+1)
}
if hasFilteredServices && containsService(services, service) {
i.portMapping[service] = "localhost:" + ports[nat.Port(service.Port)][0].HostPort
i.portMapping[service] = "localhost:" + bindings[0].HostPort
} else if !hasFilteredServices {
i.portMapping[service] = "localhost:" + ports[nat.Port(service.Port)][0].HostPort
i.portMapping[service] = "localhost:" + bindings[0].HostPort
}
}
}
Expand All @@ -420,7 +425,7 @@ func (i *Instance) stop() error {
}

func (i *Instance) waitToBeAvailable(ctx context.Context) error {
ticker := time.NewTicker(time.Second)
ticker := time.NewTicker(300 * time.Millisecond)
defer ticker.Stop()
for {
select {
Expand Down
40 changes: 21 additions & 19 deletions localstack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestMain(m *testing.M) {
}

func TestWithLogger(t *testing.T) {
for _, s := range []struct {
for _, scenario := range []struct {
name string
level log.Level
expect func(t require.TestingT, object interface{}, msgAndArgs ...interface{})
Expand All @@ -65,7 +65,9 @@ func TestWithLogger(t *testing.T) {
expect: require.Empty,
},
} {
s := scenario
t.Run(s.name, func(t *testing.T) {
t.Parallel()
buf := &concurrentWriter{buf: &bytes.Buffer{}}
logger := log.New()
logger.SetLevel(s.level)
Expand Down Expand Up @@ -112,16 +114,14 @@ func TestWithTimeoutAfterStartup(t *testing.T) {
cli.NegotiateAPIVersion(ctx)
require.Eventually(t, func() bool {
containers, err := cli.ContainerList(ctx, types.ContainerListOptions{})
if !assert.NoError(t, err) {
return false
}
require.NoError(t, err)
for _, c := range containers {
if c.Image == "go-localstack" {
return false
}
}
return true
}, time.Minute, time.Second, "image is still running but should be terminated")
}, time.Minute, 200*time.Millisecond, "image is still running but should be terminated")
}

func TestWithLabels(t *testing.T) {
Expand Down Expand Up @@ -345,9 +345,9 @@ func TestInstanceWithVersions(t *testing.T) {

func TestInstanceWithBadDockerEnvironment(t *testing.T) {
urlIfSet := os.Getenv("DOCKER_URL")
defer func() {
t.Cleanup(func() {
require.NoError(t, os.Setenv("DOCKER_URL", urlIfSet))
}()
})

require.NoError(t, os.Setenv("DOCKER_URL", "what-is-this-thing:///var/run/not-a-valid-docker.sock"))

Expand Down Expand Up @@ -400,10 +400,10 @@ func TestWithClientFromEnv(t *testing.T) {
},
} {
t.Run(s.name, func(t *testing.T) {
defer func() {
t.Cleanup(func() {
require.NoError(t, os.Unsetenv("DOCKER_HOST"))
require.NoError(t, os.Unsetenv("DOCKER_API_VERSION"))
}()
})
s.given(t)
opt, err := localstack.WithClientFromEnv()
s.expectOpt(t, opt, err)
Expand Down Expand Up @@ -447,8 +447,8 @@ func checkAddress(t *testing.T, val string) {
}

func atLeastOneContainerMatchesLabels(labels map[string]string, containers []types.Container) bool {
for _, container := range containers {
if matchesLabels(labels, container) {
for _, c := range containers {
if matchesLabels(labels, c) {
return true
}
}
Expand Down Expand Up @@ -476,17 +476,19 @@ func clean() error {
}
cli.NegotiateAPIVersion(ctx)

if list, err := cli.ContainerList(ctx, types.ContainerListOptions{All: true}); err == nil {
for _, l := range list {
if err := cli.ContainerStop(ctx, l.ID, container.StopOptions{Timeout: &timeout}); err != nil {
log.Println(err)
}
}
} else {
list, err := cli.ContainerList(ctx, types.ContainerListOptions{All: true})
if err != nil {
return err
}

for _, l := range list {
if err := cli.ContainerStop(ctx, l.ID, container.StopOptions{Timeout: &timeout}); err != nil {
log.Println(err)
}
}

if _, err := cli.ContainersPrune(ctx, filters.Args{}); err != nil {
return err
log.Println(err)
}
return nil
}
Expand Down

0 comments on commit fb04067

Please sign in to comment.