compose/pkg/api/create_options_test.go
Guillaume Lours ed25575984 fix(up): keep handling Ctrl+C once the log monitor has ended
Once every attached container is gone the monitor ends the session, and
with it the event loop that handles Ctrl+C: later interruptions are lost.

During --wait that left the stack running on exit 130: end the wait
without canceling, and stop the stack from there when the loop is gone.
During the start phase, blocked on dependencies of unattached services,
the signal was swallowed and the command hung: keep the session open.

Add tests for both and document that --wait leaves the services up.
Keep an api test valid now that Wait is accepted together with Attach.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Guillaume Lours <glours@users.noreply.github.com>
2026-11-17 09:35:33 +01:00

87 lines
3.3 KiB
Go

/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package api
import (
"errors"
"testing"
"gotest.tools/v3/assert"
)
func TestCreateOptionsValidate(t *testing.T) {
tests := []struct {
name string
options CreateOptions
// invalid lists the options expected to be rejected, empty when valid
invalid []string
}{
{name: "zero value is valid", options: CreateOptions{}},
{name: "diverged", options: CreateOptions{Recreate: RecreateDiverged, RecreateDependencies: RecreateDiverged}},
{name: "force", options: CreateOptions{Recreate: RecreateForce, RecreateDependencies: RecreateForce}},
{name: "never", options: CreateOptions{Recreate: RecreateNever, RecreateDependencies: RecreateNever}},
{name: "unknown recreate", options: CreateOptions{Recreate: "forced"}, invalid: []string{"Recreate"}},
{name: "unknown dependencies recreate", options: CreateOptions{RecreateDependencies: "always"}, invalid: []string{"RecreateDependencies"}},
{name: "case matters", options: CreateOptions{Recreate: "Force"}, invalid: []string{"Recreate"}},
{name: "both unknown", options: CreateOptions{Recreate: "x", RecreateDependencies: "y"}, invalid: []string{"Recreate", "RecreateDependencies"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.options.Validate()
if len(tt.invalid) == 0 {
assert.NilError(t, err)
return
}
assert.Assert(t, errors.Is(err, ErrInvalidOption), "got %v", err)
assert.Assert(t, IsErrInvalidOption(err))
// every invalid option is reported, whether the error is a single
// *InvalidOptionError or several of them joined
var reported []string
var walk func(error)
walk = func(e error) {
if joined, ok := e.(interface{ Unwrap() []error }); ok {
for _, inner := range joined.Unwrap() {
walk(inner)
}
return
}
var invalid *InvalidOptionError
if errors.As(e, &invalid) {
reported = append(reported, invalid.Option)
}
}
walk(err)
assert.DeepEqual(t, reported, tt.invalid)
})
}
}
func TestInvalidOptionErrorMessage(t *testing.T) {
err := CreateOptions{Recreate: "forced"}.Validate()
assert.Error(t, err, `invalid option (Recreate): "forced" is not one of diverged, force, never`)
}
func TestUpOptionsValidateReportsInvalidCreateOptions(t *testing.T) {
err := UpOptions{Create: CreateOptions{Recreate: "forced"}}.Validate()
assert.Assert(t, errors.Is(err, ErrInvalidOption), "got %v", err)
// invalid values and incompatible combinations are reported together
err = UpOptions{Create: CreateOptions{Recreate: "forced"}, Start: StartOptions{Wait: true, Attach: nopConsumer{}, Watch: true}}.Validate()
assert.Assert(t, errors.Is(err, ErrInvalidOption), "got %v", err)
assert.Assert(t, errors.Is(err, ErrIncompatibleOptions), "got %v", err)
}