Skip to content

Commit a57320f

Browse files
false200glours
authored andcommitted
Fix up attach filtering
Signed-off-by: CodeLoopdroid <214800619+CodeLoopdroid@users.noreply.github.com>
1 parent 46d75d0 commit a57320f

2 files changed

Lines changed: 118 additions & 4 deletions

File tree

pkg/compose/up.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
246246
}
247247

248248
monitor.withListener(func(event api.ContainerEvent) {
249-
if event.Type != api.ContainerEventStarted {
250-
return
251-
}
252-
if slices.Contains(attached, event.ID) && !event.Restarting {
249+
if !shouldFollowStartEvent(event, attached, options.Start.AttachTo) {
253250
return
254251
}
255252
eg.Go(func() error {
@@ -302,3 +299,16 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
302299
}
303300
return err
304301
}
302+
303+
func shouldFollowStartEvent(event api.ContainerEvent, attached []string, attachTo []string) bool {
304+
if event.Type != api.ContainerEventStarted {
305+
return false
306+
}
307+
if len(attachTo) > 0 && !slices.Contains(attachTo, event.Service) {
308+
return false
309+
}
310+
if slices.Contains(attached, event.ID) && !event.Restarting {
311+
return false
312+
}
313+
return true
314+
}

pkg/compose/up_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package compose
18+
19+
import (
20+
"testing"
21+
22+
"gotest.tools/v3/assert"
23+
24+
"github.com/docker/compose/v5/pkg/api"
25+
)
26+
27+
func TestShouldFollowStartEvent(t *testing.T) {
28+
tests := []struct {
29+
name string
30+
event api.ContainerEvent
31+
attached []string
32+
attachTo []string
33+
want bool
34+
}{
35+
{
36+
name: "ignores non-start events",
37+
event: api.ContainerEvent{
38+
Type: api.ContainerEventExited,
39+
Service: "validator",
40+
},
41+
attachTo: []string{"validator"},
42+
want: false,
43+
},
44+
{
45+
name: "ignores services outside explicit attach selection",
46+
event: api.ContainerEvent{
47+
Type: api.ContainerEventStarted,
48+
Service: "event-bus-validator",
49+
ID: "event-bus-validator-1",
50+
},
51+
attachTo: []string{"validator"},
52+
want: false,
53+
},
54+
{
55+
name: "ignores containers already attached unless restarting",
56+
event: api.ContainerEvent{
57+
Type: api.ContainerEventStarted,
58+
Service: "validator",
59+
ID: "validator-1",
60+
},
61+
attached: []string{"validator-1"},
62+
attachTo: []string{"validator"},
63+
want: false,
64+
},
65+
{
66+
name: "follows restarts for attached service",
67+
event: api.ContainerEvent{
68+
Type: api.ContainerEventStarted,
69+
Service: "validator",
70+
ID: "validator-1",
71+
Restarting: true,
72+
},
73+
attached: []string{"validator-1"},
74+
attachTo: []string{"validator"},
75+
want: true,
76+
},
77+
{
78+
name: "follows selected service when not already attached",
79+
event: api.ContainerEvent{
80+
Type: api.ContainerEventStarted,
81+
Service: "validator",
82+
ID: "validator-2",
83+
},
84+
attachTo: []string{"validator"},
85+
want: true,
86+
},
87+
{
88+
name: "follows service when no explicit attach filter exists",
89+
event: api.ContainerEvent{
90+
Type: api.ContainerEventStarted,
91+
Service: "validator",
92+
ID: "validator-2",
93+
},
94+
want: true,
95+
},
96+
}
97+
98+
for _, tt := range tests {
99+
t.Run(tt.name, func(t *testing.T) {
100+
got := shouldFollowStartEvent(tt.event, tt.attached, tt.attachTo)
101+
assert.Equal(t, got, tt.want)
102+
})
103+
}
104+
}

0 commit comments

Comments
 (0)