|
| 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