agones.dev/agones@v1.54.0/pkg/util/runtime/runtime_test.go (about)

     1  // Copyright 2019 Google LLC 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  // Package runtime handles runtime errors
    16  // Wraps and reconfigures functionality in apimachinery/pkg/runtime
    17  package runtime
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  	"k8s.io/apimachinery/pkg/util/runtime"
    26  )
    27  
    28  func TestHandleError(t *testing.T) {
    29  	old := runtime.ErrorHandlers
    30  	defer func() { runtime.ErrorHandlers = old }()
    31  	var result error
    32  	runtime.ErrorHandlers = []runtime.ErrorHandler{
    33  		func(_ context.Context, err error, _ string, _ ...interface{}) {
    34  			result = err
    35  		},
    36  	}
    37  	HandleError(nil, nil)
    38  	assert.Nil(t, result, "No Errors for now")
    39  
    40  	err := fmt.Errorf("test")
    41  	// test nil logger
    42  	logger := NewLoggerWithSource("test")
    43  	HandleError(logger.WithError(err), err)
    44  	if result != err {
    45  		t.Errorf("did not receive custom handler")
    46  	}
    47  }