-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Kenta Kozuka <[email protected]>
- Loading branch information
1 parent
d34fcfb
commit 5984a3d
Showing
1 changed file
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2023 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 grpcapi | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/pipe-cd/pipecd/pkg/datastore" | ||
"github.com/pipe-cd/pipecd/pkg/filestore" | ||
"github.com/stretchr/testify/assert" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func TestGRPCStoreError(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
inputErr error | ||
inputMsg string | ||
expected error | ||
}{ | ||
{ | ||
name: "datastore not found error", | ||
inputErr: datastore.ErrNotFound, | ||
inputMsg: "datastore", | ||
expected: status.Error(codes.NotFound, "Entity was not found to datastore"), | ||
}, | ||
{ | ||
name: "filestore not found error", | ||
inputErr: filestore.ErrNotFound, | ||
inputMsg: "filestore", | ||
expected: status.Error(codes.NotFound, "Entity was not found to filestore"), | ||
}, | ||
{ | ||
name: "stagelogstore not found error", | ||
inputErr: filestore.ErrNotFound, | ||
inputMsg: "stagelogstore", | ||
expected: status.Error(codes.NotFound, "Entity was not found to stagelogstore"), | ||
}, | ||
{ | ||
name: "datastore invalid argument error", | ||
inputErr: datastore.ErrInvalidArgument, | ||
inputMsg: "datastore", | ||
expected: status.Error(codes.InvalidArgument, "Invalid argument to datastore"), | ||
}, | ||
{ | ||
name: "datastore already exists error", | ||
inputErr: datastore.ErrAlreadyExists, | ||
inputMsg: "datastore", | ||
expected: status.Error(codes.AlreadyExists, "Entity already exists to datastore"), | ||
}, | ||
{ | ||
name: "user defined error", | ||
inputErr: datastore.ErrUserDefined, | ||
expected: status.Error(codes.FailedPrecondition, "user defined error"), | ||
}, | ||
{ | ||
name: "user defined error with message", | ||
inputErr: fmt.Errorf("%w: %s", datastore.ErrUserDefined, "test"), | ||
expected: status.Error(codes.FailedPrecondition, "user defined error: test"), | ||
}, | ||
{ | ||
name: "internal error", | ||
inputErr: errors.New("internal error"), | ||
inputMsg: "test", | ||
expected: status.Error(codes.Internal, "Failed to test"), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := gRPCStoreError(tt.inputErr, tt.inputMsg) | ||
assert.Equal(t, tt.expected, err) | ||
}) | ||
} | ||
} |