|
| 1 | +/* |
| 2 | + Copyright 2026 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 e2e |
| 18 | + |
| 19 | +// This file is the scenario vocabulary: the complete set of observables a |
| 20 | +// step can expect. It is meant to be read — and reviewed — as a whole; see |
| 21 | +// SCENARIO.md for the rules governing its growth. In short: checks observe |
| 22 | +// real state (containers, labels, image manifests), are generic (no |
| 23 | +// test-specific logic), and are named after the observable they assert. |
| 24 | + |
| 25 | +import ( |
| 26 | + "fmt" |
| 27 | + "slices" |
| 28 | + "strings" |
| 29 | + "time" |
| 30 | + |
| 31 | + "gotest.tools/v3/icmd" |
| 32 | +) |
| 33 | + |
| 34 | +// CheckContext gives a check access to the step's result and to the project |
| 35 | +// state captured before and after the step. |
| 36 | +type CheckContext struct { |
| 37 | + scenario *Scenario |
| 38 | + result *icmd.Result |
| 39 | + prev snapshot |
| 40 | + curr snapshot |
| 41 | +} |
| 42 | + |
| 43 | +// refresh re-observes the project state, so subsequent checks of the same |
| 44 | +// step see the latest state rather than the one captured right after the |
| 45 | +// command returned. |
| 46 | +func (ctx *CheckContext) refresh() { |
| 47 | + s := ctx.scenario |
| 48 | + ctx.curr = s.snapshot() |
| 49 | + s.snaps[len(s.snaps)-1] = ctx.curr |
| 50 | +} |
| 51 | + |
| 52 | +// Check is a named observable expected to hold after a step. |
| 53 | +type Check struct { |
| 54 | + name string |
| 55 | + fn func(*CheckContext) error |
| 56 | +} |
| 57 | + |
| 58 | +// OutputContains expects the command output to contain a string. Prefer |
| 59 | +// state-based checks; use this when the CLI's reported decision is itself |
| 60 | +// the observable. |
| 61 | +func OutputContains(sub string) Check { |
| 62 | + return Check{ |
| 63 | + name: fmt.Sprintf("output contains %q", sub), |
| 64 | + fn: func(ctx *CheckContext) error { |
| 65 | + if !strings.Contains(ctx.result.Combined(), sub) { |
| 66 | + return fmt.Errorf("not found in output") |
| 67 | + } |
| 68 | + return nil |
| 69 | + }, |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// OutputNotContains expects the command output not to contain a string. |
| 74 | +func OutputNotContains(sub string) Check { |
| 75 | + return Check{ |
| 76 | + name: fmt.Sprintf("output does not contain %q", sub), |
| 77 | + fn: func(ctx *CheckContext) error { |
| 78 | + if strings.Contains(ctx.result.Combined(), sub) { |
| 79 | + return fmt.Errorf("found in output") |
| 80 | + } |
| 81 | + return nil |
| 82 | + }, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// Eventually retries a state-based check until it holds or the timeout |
| 87 | +// expires, re-observing the project state between attempts. Output-based |
| 88 | +// checks are not meaningful here: the step's output never changes. |
| 89 | +func Eventually(check Check, timeout time.Duration) Check { |
| 90 | + return Check{ |
| 91 | + name: fmt.Sprintf("%s within %s", check.name, timeout), |
| 92 | + fn: func(ctx *CheckContext) error { |
| 93 | + deadline := time.Now().Add(timeout) |
| 94 | + for { |
| 95 | + err := check.fn(ctx) |
| 96 | + if err == nil { |
| 97 | + return nil |
| 98 | + } |
| 99 | + if time.Now().After(deadline) { |
| 100 | + return err |
| 101 | + } |
| 102 | + time.Sleep(500 * time.Millisecond) |
| 103 | + ctx.refresh() |
| 104 | + } |
| 105 | + }, |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// ServiceState expects every container of the service to be in the given |
| 110 | +// state (running, exited, restarting, …). |
| 111 | +func ServiceState(service, state string) Check { |
| 112 | + return Check{ |
| 113 | + name: fmt.Sprintf("service %q is %s", service, state), |
| 114 | + fn: func(ctx *CheckContext) error { |
| 115 | + containers := ctx.curr[service] |
| 116 | + if len(containers) == 0 { |
| 117 | + return fmt.Errorf("service has no container") |
| 118 | + } |
| 119 | + for _, c := range containers { |
| 120 | + if c.State != state { |
| 121 | + return fmt.Errorf("container %s is %s", c.Name, c.State) |
| 122 | + } |
| 123 | + } |
| 124 | + return nil |
| 125 | + }, |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +// NotRecreated expects the services' containers to be exactly the ones that |
| 130 | +// existed before the step (same container IDs). |
| 131 | +func NotRecreated(services ...string) Check { |
| 132 | + return Check{ |
| 133 | + name: fmt.Sprintf("services %s not recreated", strings.Join(services, ", ")), |
| 134 | + fn: func(ctx *CheckContext) error { |
| 135 | + for _, service := range services { |
| 136 | + before, after := containerIDs(ctx.prev[service]), containerIDs(ctx.curr[service]) |
| 137 | + if len(before) == 0 { |
| 138 | + return fmt.Errorf("service %q had no container before the step", service) |
| 139 | + } |
| 140 | + if !slices.Equal(before, after) { |
| 141 | + return fmt.Errorf("service %q containers changed: %v -> %v", service, before, after) |
| 142 | + } |
| 143 | + } |
| 144 | + return nil |
| 145 | + }, |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func containerIDs(containers []containerState) []string { |
| 150 | + ids := make([]string, 0, len(containers)) |
| 151 | + for _, c := range containers { |
| 152 | + ids = append(ids, c.ID) |
| 153 | + } |
| 154 | + slices.Sort(ids) |
| 155 | + return ids |
| 156 | +} |
| 157 | + |
| 158 | +// LabelSet expects every container of the service to carry a non-empty label. |
| 159 | +func LabelSet(service, key string) Check { |
| 160 | + return Check{ |
| 161 | + name: fmt.Sprintf("service %q has label %q set", service, key), |
| 162 | + fn: func(ctx *CheckContext) error { |
| 163 | + containers := ctx.curr[service] |
| 164 | + if len(containers) == 0 { |
| 165 | + return fmt.Errorf("service has no container") |
| 166 | + } |
| 167 | + for _, c := range containers { |
| 168 | + if c.Labels[key] == "" { |
| 169 | + return fmt.Errorf("label empty on container %s", c.Name) |
| 170 | + } |
| 171 | + } |
| 172 | + return nil |
| 173 | + }, |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +// LabelsDistinct expects the given services to carry pairwise-distinct values |
| 178 | +// for a label. |
| 179 | +func LabelsDistinct(key string, services ...string) Check { |
| 180 | + return Check{ |
| 181 | + name: fmt.Sprintf("services %s have distinct %q labels", strings.Join(services, ", "), key), |
| 182 | + fn: func(ctx *CheckContext) error { |
| 183 | + seen := map[string]string{} |
| 184 | + for _, service := range services { |
| 185 | + containers := ctx.curr[service] |
| 186 | + if len(containers) == 0 { |
| 187 | + return fmt.Errorf("service %q has no container", service) |
| 188 | + } |
| 189 | + value := containers[0].Labels[key] |
| 190 | + if other, dup := seen[value]; dup { |
| 191 | + return fmt.Errorf("services %q and %q share label value %q", other, service, value) |
| 192 | + } |
| 193 | + seen[value] = service |
| 194 | + } |
| 195 | + return nil |
| 196 | + }, |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +// LabelUnchanged expects a service's label to have the same value as before |
| 201 | +// the step. |
| 202 | +func LabelUnchanged(service, key string) Check { |
| 203 | + return Check{ |
| 204 | + name: fmt.Sprintf("service %q label %q unchanged", service, key), |
| 205 | + fn: func(ctx *CheckContext) error { |
| 206 | + before, after := ctx.prev[service], ctx.curr[service] |
| 207 | + if len(before) == 0 || len(after) == 0 { |
| 208 | + return fmt.Errorf("service has no container to compare") |
| 209 | + } |
| 210 | + if before[0].Labels[key] != after[0].Labels[key] { |
| 211 | + return fmt.Errorf("label changed: %q -> %q", before[0].Labels[key], after[0].Labels[key]) |
| 212 | + } |
| 213 | + return nil |
| 214 | + }, |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | +// RunsOnPlatform expects the service's container to have been created for |
| 219 | +// the given platform (from its image manifest descriptor). |
| 220 | +func RunsOnPlatform(service, platform string) Check { |
| 221 | + return Check{ |
| 222 | + name: fmt.Sprintf("service %q container created for platform %s", service, platform), |
| 223 | + fn: func(ctx *CheckContext) error { |
| 224 | + containers := ctx.curr[service] |
| 225 | + if len(containers) == 0 { |
| 226 | + return fmt.Errorf("service has no container") |
| 227 | + } |
| 228 | + res := icmd.RunCmd(ctx.scenario.cli.NewDockerCmd(ctx.scenario.t, "inspect", "--format", |
| 229 | + "{{.ImageManifestDescriptor.Platform.OS}}/{{.ImageManifestDescriptor.Platform.Architecture}}", |
| 230 | + containers[0].ID)) |
| 231 | + if res.ExitCode != 0 { |
| 232 | + return fmt.Errorf("inspect failed: %s", res.Combined()) |
| 233 | + } |
| 234 | + if actual := strings.TrimSpace(res.Stdout()); actual != platform { |
| 235 | + return fmt.Errorf("container platform is %s", actual) |
| 236 | + } |
| 237 | + return nil |
| 238 | + }, |
| 239 | + } |
| 240 | +} |
0 commit comments