diff --git a/CHANGELOG.md b/CHANGELOG.md index 07e43b8f9..5065008a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ ### Bug Fixes: +- fix(service-version): use FASTLY_SERVICE_ID and fastly.toml fallbacks when validating a service version. - fix(service-version): support autoclone when staging a service version. ([#1850](https://github.com/fastly/cli/pull/1850)) - fix(logging): the `placement` flag for all loggging commands can now be reset back to `null` by setting it's value to `""` when it was previously set to another value ([#1855](https://github.com/fastly/cli/pull/1855)) - fix(profile): profiles can now be created and updated with service-limited tokens, which cannot access `/current_user` ([#1856](https://github.com/fastly/cli/pull/1856)) diff --git a/pkg/commands/service/version/serviceversion_test.go b/pkg/commands/service/version/serviceversion_test.go index f25f0e16b..db926ff1b 100644 --- a/pkg/commands/service/version/serviceversion_test.go +++ b/pkg/commands/service/version/serviceversion_test.go @@ -10,6 +10,7 @@ import ( root "github.com/fastly/cli/pkg/commands/service" sub "github.com/fastly/cli/pkg/commands/service/version" + "github.com/fastly/cli/pkg/global" "github.com/fastly/cli/pkg/mock" "github.com/fastly/cli/pkg/testutil" ) @@ -564,15 +565,49 @@ func lockVersionError(_ context.Context, _ *fastly.LockVersionInput) (*fastly.Ve func TestVersionValidate(t *testing.T) { scenarios := []testutil.CLIScenario{ { - Name: "validate missing --service-id flag", + Name: "validate missing service ID", Args: "--version 1", - WantError: "error parsing arguments: required flag --service-id not provided", + EnvVars: map[string]string{"FASTLY_SERVICE_ID": ""}, + WantError: "error reading service: no service ID found", }, { Name: "validate missing --version flag", Args: "--service-id 123", WantError: "error parsing arguments: required flag --version not provided", }, + { + Name: "validate service ID from environment", + Args: "--version latest", + EnvVars: map[string]string{"FASTLY_SERVICE_ID": "env-service"}, + API: &mock.API{ + ListVersionsFn: testutil.ListVersions, + ValidateVersionFn: validateVersionForService("env-service", 4), + }, + WantOutput: "Service env-service version 4 is valid", + }, + { + Name: "validate service ID from manifest", + Args: "--version 1", + EnvVars: map[string]string{"FASTLY_SERVICE_ID": ""}, + Setup: func(_ *testing.T, _ *testutil.CLIScenario, opts *global.Data) { + opts.Manifest.File.ServiceID = "manifest-service" + }, + API: &mock.API{ + GetVersionFn: testutil.GetVersion, + ValidateVersionFn: validateVersionForService("manifest-service", 1), + }, + WantOutput: "Service manifest-service version 1 is valid", + }, + { + Name: "validate explicit service ID overrides environment", + Args: "--service-id flag-service --version 1", + EnvVars: map[string]string{"FASTLY_SERVICE_ID": "env-service"}, + API: &mock.API{ + GetVersionFn: testutil.GetVersion, + ValidateVersionFn: validateVersionForService("flag-service", 1), + }, + WantOutput: "Service flag-service version 1 is valid", + }, { Name: "validate successful - valid version without message", Args: "--service-id 123 --version 1", @@ -647,6 +682,19 @@ func validateVersionValid(message string) func(context.Context, *fastly.Validate } } +func validateVersionForService(serviceID string, serviceVersion int) func(context.Context, *fastly.ValidateVersionInput) (bool, string, error) { + return func(_ context.Context, input *fastly.ValidateVersionInput) (bool, string, error) { + if input.ServiceID != serviceID || input.ServiceVersion != serviceVersion { + return false, "", fmt.Errorf( + "unexpected validate input: service ID %q, version %d", + input.ServiceID, + input.ServiceVersion, + ) + } + return true, "", nil + } +} + func validateVersionInvalid(message string) func(context.Context, *fastly.ValidateVersionInput) (bool, string, error) { return func(_ context.Context, _ *fastly.ValidateVersionInput) (bool, string, error) { return false, message, nil diff --git a/pkg/commands/service/version/validate.go b/pkg/commands/service/version/validate.go index bcbfed55b..63e394cdd 100644 --- a/pkg/commands/service/version/validate.go +++ b/pkg/commands/service/version/validate.go @@ -35,7 +35,6 @@ func NewValidateCommand(parent argparser.Registerer, g *global.Data) *ValidateCo Description: argparser.FlagServiceIDDesc, Dst: &g.Manifest.Flag.ServiceID, Short: 's', - Required: true, }) c.RegisterFlag(argparser.StringFlagOpts{ Name: argparser.FlagVersionName,