compose/pkg/api/create_options_test.go
Nicolas De Loof b3c39f17a6
Some checks are pending
ci / validate (lint) (push) Waiting to run
ci / validate (relay-lint) (push) Waiting to run
ci / validate (relay-test) (push) Waiting to run
ci / validate (validate-docs) (push) Waiting to run
ci / validate (validate-go-mod) (push) Waiting to run
ci / validate (validate-headers) (push) Waiting to run
ci / validate (validate-mocks) (push) Waiting to run
ci / binary (push) Waiting to run
ci / binary-finalize (push) Blocked by required conditions
ci / bin-image-test (push) Waiting to run
ci / relay-image-test (push) Waiting to run
ci / test (push) Waiting to run
ci / e2e (plugin, oldstable, graphdriver) (push) Waiting to run
ci / e2e (standalone, oldstable, graphdriver) (push) Waiting to run
ci / e2e (plugin, stable, containerd) (push) Waiting to run
ci / e2e (plugin, stable, graphdriver) (push) Waiting to run
ci / e2e (standalone, stable, containerd) (push) Waiting to run
ci / e2e (standalone, stable, graphdriver) (push) Waiting to run
ci / e2e (plugin, test, graphdriver, jobs extension) (push) Waiting to run
ci / coverage (push) Blocked by required conditions
ci / ci-ok (push) Blocked by required conditions
ci / release (push) Blocked by required conditions
merge / bin-image-prepare (push) Waiting to run
merge / bin-image (push) Blocked by required conditions
merge / relay-image (push) Waiting to run
merge / module-image (push) Waiting to run
Scorecards supply-chain security / Scorecards analysis (push) Waiting to run
zizmor / zizmor (push) Waiting to run
fix(api): list every accepted value in InvalidOptionError
DownOptions.Validate omitted the empty ImagePruneNone value from the
allowed list, so the error did not tell callers that "" is accepted.
Quote the allowed values so an empty one stays readable, fall back to a
generic message when no closed set is given, and word the Logs Index
error properly when no service is selected.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
2026-11-27 10:12:06 +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)
}