diff --git a/pkg/app/pipectl/cmd/application/BUILD.bazel b/pkg/app/pipectl/cmd/application/BUILD.bazel index 1bab402103..7b2a3bb383 100644 --- a/pkg/app/pipectl/cmd/application/BUILD.bazel +++ b/pkg/app/pipectl/cmd/application/BUILD.bazel @@ -1,4 +1,4 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( name = "go_default_library", @@ -17,14 +17,3 @@ go_library( "@com_github_spf13_cobra//:go_default_library", ], ) - -go_test( - name = "go_default_test", - size = "small", - srcs = ["sync_test.go"], - embed = [":go_default_library"], - deps = [ - "//pkg/model:go_default_library", - "@com_github_stretchr_testify//assert:go_default_library", - ], -) diff --git a/pkg/app/pipectl/cmd/application/sync.go b/pkg/app/pipectl/cmd/application/sync.go index bf9678cb28..069d8224ee 100644 --- a/pkg/app/pipectl/cmd/application/sync.go +++ b/pkg/app/pipectl/cmd/application/sync.go @@ -31,7 +31,7 @@ type sync struct { root *command appID string - status []string + statuses []string checkInterval time.Duration timeout time.Duration } @@ -49,7 +49,7 @@ func newSyncCommand(root *command) *cobra.Command { } cmd.Flags().StringVar(&c.appID, "app-id", c.appID, "The application ID.") - cmd.Flags().StringSliceVar(&c.status, "wait-status", c.status, fmt.Sprintf("The list of waiting statuses. Empty means returning immediately after triggered. (%s)", strings.Join(availableStatuses(), "|"))) + cmd.Flags().StringSliceVar(&c.statuses, "wait-status", c.statuses, fmt.Sprintf("The list of waiting statuses. Empty means returning immediately after triggered. (%s)", strings.Join(model.DeploymentStatusStrings(), "|"))) cmd.Flags().DurationVar(&c.checkInterval, "check-interval", c.checkInterval, "The interval of checking the requested command.") cmd.Flags().DurationVar(&c.timeout, "timeout", c.timeout, "Maximum execution time.") @@ -59,7 +59,7 @@ func newSyncCommand(root *command) *cobra.Command { } func (c *sync) run(ctx context.Context, t cli.Telemetry) error { - statuses, err := makeStatuses(c.status) + statuses, err := model.DeploymentStatusesFromStrings(c.statuses) if err != nil { return fmt.Errorf("invalid deployment status: %w", err) } @@ -92,23 +92,3 @@ func (c *sync) run(ctx context.Context, t cli.Telemetry) error { t.Logger, ) } - -func makeStatuses(statuses []string) ([]model.DeploymentStatus, error) { - out := make([]model.DeploymentStatus, 0, len(statuses)) - for _, s := range statuses { - status, ok := model.DeploymentStatus_value["DEPLOYMENT_"+s] - if !ok { - return nil, fmt.Errorf("bad status %s", s) - } - out = append(out, model.DeploymentStatus(status)) - } - return out, nil -} - -func availableStatuses() []string { - out := make([]string, 0, len(model.DeploymentStatus_value)) - for s := range model.DeploymentStatus_value { - out = append(out, strings.TrimPrefix(s, "DEPLOYMENT_")) - } - return out -} diff --git a/pkg/app/pipectl/cmd/application/sync_test.go b/pkg/app/pipectl/cmd/application/sync_test.go deleted file mode 100644 index b11e4375f6..0000000000 --- a/pkg/app/pipectl/cmd/application/sync_test.go +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright 2020 The PipeCD 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 application - -import ( - "testing" - - "github.com/stretchr/testify/assert" - - "github.com/pipe-cd/pipe/pkg/model" -) - -func TestMakeStatuses(t *testing.T) { - testcases := []struct { - name string - statuses []string - expected []model.DeploymentStatus - expectedErr bool - }{ - { - name: "empty", - expected: []model.DeploymentStatus{}, - }, - { - name: "has an invalid status", - statuses: []string{"SUCCESS", "INVALID"}, - expectedErr: true, - }, - { - name: "ok", - statuses: []string{"SUCCESS", "PLANNED"}, - expected: []model.DeploymentStatus{ - model.DeploymentStatus_DEPLOYMENT_SUCCESS, - model.DeploymentStatus_DEPLOYMENT_PLANNED, - }, - }, - } - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - statuses, err := makeStatuses(tc.statuses) - assert.Equal(t, tc.expected, statuses) - assert.Equal(t, tc.expectedErr, err != nil) - }) - } -} - -func TestAvailableStatuses(t *testing.T) { - statuses := availableStatuses() - assert.True(t, len(statuses) > 0) -} diff --git a/pkg/app/pipectl/cmd/deployment/BUILD.bazel b/pkg/app/pipectl/cmd/deployment/BUILD.bazel index 4d8ee5b973..981b693c42 100644 --- a/pkg/app/pipectl/cmd/deployment/BUILD.bazel +++ b/pkg/app/pipectl/cmd/deployment/BUILD.bazel @@ -1,4 +1,4 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( name = "go_default_library", @@ -15,14 +15,3 @@ go_library( "@com_github_spf13_cobra//:go_default_library", ], ) - -go_test( - name = "go_default_test", - size = "small", - srcs = ["waitstatus_test.go"], - embed = [":go_default_library"], - deps = [ - "//pkg/model:go_default_library", - "@com_github_stretchr_testify//assert:go_default_library", - ], -) diff --git a/pkg/app/pipectl/cmd/deployment/waitstatus.go b/pkg/app/pipectl/cmd/deployment/waitstatus.go index 0b072ddaa1..a5956b7c64 100644 --- a/pkg/app/pipectl/cmd/deployment/waitstatus.go +++ b/pkg/app/pipectl/cmd/deployment/waitstatus.go @@ -31,7 +31,7 @@ type waitStatus struct { root *command deploymentID string - status []string + statuses []string checkInterval time.Duration timeout time.Duration } @@ -49,7 +49,7 @@ func newWaitStatusCommand(root *command) *cobra.Command { } cmd.Flags().StringVar(&c.deploymentID, "deployment-id", c.deploymentID, "The deployment ID.") - cmd.Flags().StringSliceVar(&c.status, "status", c.status, fmt.Sprintf("The list of waiting statuses. (%s)", strings.Join(availableStatuses(), "|"))) + cmd.Flags().StringSliceVar(&c.statuses, "status", c.statuses, fmt.Sprintf("The list of waiting statuses. (%s)", strings.Join(model.DeploymentStatusStrings(), "|"))) cmd.Flags().DurationVar(&c.checkInterval, "check-interval", c.checkInterval, "The interval of checking the deployment status.") cmd.Flags().DurationVar(&c.timeout, "timeout", c.timeout, "Maximum execution time.") @@ -60,7 +60,7 @@ func newWaitStatusCommand(root *command) *cobra.Command { } func (c *waitStatus) run(ctx context.Context, t cli.Telemetry) error { - statuses, err := makeStatuses(c.status) + statuses, err := model.DeploymentStatusesFromStrings(c.statuses) if err != nil { return fmt.Errorf("invalid deployment status: %w", err) } @@ -81,23 +81,3 @@ func (c *waitStatus) run(ctx context.Context, t cli.Telemetry) error { t.Logger, ) } - -func makeStatuses(statuses []string) ([]model.DeploymentStatus, error) { - out := make([]model.DeploymentStatus, 0, len(statuses)) - for _, s := range statuses { - status, ok := model.DeploymentStatus_value["DEPLOYMENT_"+s] - if !ok { - return nil, fmt.Errorf("bad status %s", s) - } - out = append(out, model.DeploymentStatus(status)) - } - return out, nil -} - -func availableStatuses() []string { - out := make([]string, 0, len(model.DeploymentStatus_value)) - for s := range model.DeploymentStatus_value { - out = append(out, strings.TrimPrefix(s, "DEPLOYMENT_")) - } - return out -} diff --git a/pkg/app/pipectl/cmd/deployment/waitstatus_test.go b/pkg/app/pipectl/cmd/deployment/waitstatus_test.go deleted file mode 100644 index 59a1fa1217..0000000000 --- a/pkg/app/pipectl/cmd/deployment/waitstatus_test.go +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright 2020 The PipeCD 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 deployment - -import ( - "testing" - - "github.com/stretchr/testify/assert" - - "github.com/pipe-cd/pipe/pkg/model" -) - -func TestMakeStatuses(t *testing.T) { - testcases := []struct { - name string - statuses []string - expected []model.DeploymentStatus - expectedErr bool - }{ - { - name: "empty", - expected: []model.DeploymentStatus{}, - }, - { - name: "has an invalid status", - statuses: []string{"SUCCESS", "INVALID"}, - expectedErr: true, - }, - { - name: "ok", - statuses: []string{"SUCCESS", "PLANNED"}, - expected: []model.DeploymentStatus{ - model.DeploymentStatus_DEPLOYMENT_SUCCESS, - model.DeploymentStatus_DEPLOYMENT_PLANNED, - }, - }, - } - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - statuses, err := makeStatuses(tc.statuses) - assert.Equal(t, tc.expected, statuses) - assert.Equal(t, tc.expectedErr, err != nil) - }) - } -} - -func TestAvailableStatuses(t *testing.T) { - statuses := availableStatuses() - assert.True(t, len(statuses) > 0) -} diff --git a/pkg/model/deployment.go b/pkg/model/deployment.go index c84209d178..c12a70d6c1 100644 --- a/pkg/model/deployment.go +++ b/pkg/model/deployment.go @@ -15,6 +15,8 @@ package model import ( + "fmt" + "google.golang.org/protobuf/proto" ) @@ -166,3 +168,25 @@ func (d *Deployment) FindRollbackStage() (*PipelineStage, bool) { } return nil, false } + +// DeploymentStatusesFromStrings converts a list of strings to list of DeploymentStatus. +func DeploymentStatusesFromStrings(statuses []string) ([]DeploymentStatus, error) { + out := make([]DeploymentStatus, 0, len(statuses)) + for _, s := range statuses { + status, ok := DeploymentStatus_value[s] + if !ok { + return nil, fmt.Errorf("invalid status %s", s) + } + out = append(out, DeploymentStatus(status)) + } + return out, nil +} + +// DeploymentStatusStrings returns a list of available deployment statuses in string. +func DeploymentStatusStrings() []string { + out := make([]string, 0, len(DeploymentStatus_value)) + for s := range DeploymentStatus_value { + out = append(out, s) + } + return out +}