github.com/google/cadvisor@v0.49.1/container/containerd/errdefs/grpc_test.go (about)

     1  // Copyright 2017 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  /*
    15     Copyright The containerd Authors.
    16  
    17     Licensed under the Apache License, Version 2.0 (the "License");
    18     you may not use this file except in compliance with the License.
    19     You may obtain a copy of the License at
    20  
    21         http://www.apache.org/licenses/LICENSE-2.0
    22  
    23     Unless required by applicable law or agreed to in writing, software
    24     distributed under the License is distributed on an "AS IS" BASIS,
    25     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    26     See the License for the specific language governing permissions and
    27     limitations under the License.
    28  */
    29  
    30  package errdefs
    31  
    32  import (
    33  	"context"
    34  	"testing"
    35  
    36  	"google.golang.org/grpc/codes"
    37  	"google.golang.org/grpc/status"
    38  
    39  	"github.com/pkg/errors"
    40  )
    41  
    42  func TestGRPCRoundTrip(t *testing.T) {
    43  	errShouldLeaveAlone := errors.New("unknown to package")
    44  
    45  	for _, testcase := range []struct {
    46  		input error
    47  		cause error
    48  		str   string
    49  	}{
    50  		{
    51  			input: ErrAlreadyExists,
    52  			cause: ErrAlreadyExists,
    53  		},
    54  		{
    55  			input: ErrNotFound,
    56  			cause: ErrNotFound,
    57  		},
    58  		{
    59  			input: errors.Wrapf(ErrFailedPrecondition, "test test test"),
    60  			cause: ErrFailedPrecondition,
    61  			str:   "test test test: failed precondition",
    62  		},
    63  		{
    64  			input: status.Errorf(codes.Unavailable, "should be not available"),
    65  			cause: ErrUnavailable,
    66  			str:   "should be not available: unavailable",
    67  		},
    68  		{
    69  			input: errShouldLeaveAlone,
    70  			cause: ErrUnknown,
    71  			str:   errShouldLeaveAlone.Error() + ": " + ErrUnknown.Error(),
    72  		},
    73  		{
    74  			input: context.Canceled,
    75  			cause: context.Canceled,
    76  			str:   "context canceled",
    77  		},
    78  		{
    79  			input: errors.Wrapf(context.Canceled, "this is a test cancel"),
    80  			cause: context.Canceled,
    81  			str:   "this is a test cancel: context canceled",
    82  		},
    83  		{
    84  			input: context.DeadlineExceeded,
    85  			cause: context.DeadlineExceeded,
    86  			str:   "context deadline exceeded",
    87  		},
    88  		{
    89  			input: errors.Wrapf(context.DeadlineExceeded, "this is a test deadline exceeded"),
    90  			cause: context.DeadlineExceeded,
    91  			str:   "this is a test deadline exceeded: context deadline exceeded",
    92  		},
    93  	} {
    94  		t.Run(testcase.input.Error(), func(t *testing.T) {
    95  			t.Logf("input: %v", testcase.input)
    96  			gerr := ToGRPC(testcase.input)
    97  			t.Logf("grpc: %v", gerr)
    98  			ferr := FromGRPC(gerr)
    99  			t.Logf("recovered: %v", ferr)
   100  
   101  			if errors.Cause(ferr) != testcase.cause {
   102  				t.Fatalf("unexpected cause: %v != %v", errors.Cause(ferr), testcase.cause)
   103  			}
   104  			if !errors.Is(ferr, testcase.cause) {
   105  				t.Fatalf("unexpected cause: !errors.Is(%v, %v)", ferr, testcase.cause)
   106  			}
   107  
   108  			expected := testcase.str
   109  			if expected == "" {
   110  				expected = testcase.cause.Error()
   111  			}
   112  			if ferr.Error() != expected {
   113  				t.Fatalf("unexpected string: %q != %q", ferr.Error(), expected)
   114  			}
   115  		})
   116  	}
   117  
   118  }