compose/pkg/api/validate_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

72 lines
2.7 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 TestLogOptionsValidate(t *testing.T) {
assert.NilError(t, LogOptions{}.Validate())
assert.NilError(t, LogOptions{Services: []string{"web", "db"}}.Validate())
assert.NilError(t, LogOptions{Services: []string{"web"}, Index: 2}.Validate())
for _, services := range [][]string{nil, {"web", "db"}} {
err := LogOptions{Services: services, Index: 1}.Validate()
assert.Assert(t, errors.Is(err, ErrIncompatibleOptions), "services %v: got %v", services, err)
var conflict *IncompatibleOptionsError
assert.Assert(t, errors.As(err, &conflict))
assert.DeepEqual(t, conflict.Options, []string{"Index", "Services"})
}
}
func TestInvalidOptionErrorWithoutAllowedValues(t *testing.T) {
err := &InvalidOptionError{Option: "X", Value: "v"}
assert.Assert(t, errors.Is(err, ErrInvalidOption))
assert.Error(t, err, `invalid option (X): "v" is not a valid value`)
}
func TestEventsOptionsValidate(t *testing.T) {
assert.NilError(t, EventsOptions{Consumer: func(Event) error { return nil }}.Validate())
err := EventsOptions{}.Validate()
assert.Assert(t, errors.Is(err, ErrInvalidOption), "got %v", err)
assert.Error(t, err, "invalid option (Consumer): a consumer of the events is required")
}
func TestPublishOptionsValidate(t *testing.T) {
for _, version := range []OCIVersion{"", OCIVersion1_0, OCIVersion1_1} {
assert.NilError(t, PublishOptions{OCIVersion: version}.Validate(), "version %q", version)
}
err := PublishOptions{OCIVersion: "1.2"}.Validate()
assert.Assert(t, errors.Is(err, ErrInvalidOption), "got %v", err)
assert.Error(t, err, `invalid option (OCIVersion): "1.2" is not one of "1.0", "1.1"`)
}
func TestDownOptionsValidate(t *testing.T) {
for _, mode := range []ImagePruneMode{ImagePruneNone, ImagePruneLocal, ImagePruneAll} {
assert.NilError(t, DownOptions{Images: mode}.Validate(), "mode %q", mode)
}
err := DownOptions{Images: "bogus"}.Validate()
assert.Assert(t, errors.Is(err, ErrInvalidOption), "got %v", err)
assert.Error(t, err, `invalid option (Images): "bogus" is not one of "", "local", "all"`)
}