github.com/m3db/m3@v1.5.0/src/query/storage/error_behavior_test.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package storage
    22  
    23  import (
    24  	"errors"
    25  	"fmt"
    26  	"testing"
    27  
    28  	xtest "github.com/m3db/m3/src/x/test"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  	"github.com/stretchr/testify/require"
    32  	yaml "gopkg.in/yaml.v2"
    33  )
    34  
    35  func TestParseErrorBehavior(t *testing.T) {
    36  	b, err := ParseErrorBehavior("fail")
    37  	assert.NoError(t, err)
    38  	assert.Equal(t, BehaviorFail, b)
    39  
    40  	b, err = ParseErrorBehavior("warn")
    41  	assert.NoError(t, err)
    42  	assert.Equal(t, BehaviorWarn, b)
    43  
    44  	_, err = ParseErrorBehavior(BehaviorContainer.String())
    45  	assert.Error(t, err)
    46  
    47  	_, err = ParseErrorBehavior("blah")
    48  	assert.Error(t, err)
    49  
    50  }
    51  
    52  func TestErrorBehaviorUnmarshalYAML(t *testing.T) {
    53  	type config struct {
    54  		Type ErrorBehavior `yaml:"type"`
    55  	}
    56  
    57  	valid := []ErrorBehavior{BehaviorFail, BehaviorWarn}
    58  	for _, value := range valid {
    59  		str := fmt.Sprintf("type: %s\n", value.String())
    60  		var cfg config
    61  		require.NoError(t, yaml.Unmarshal([]byte(str), &cfg))
    62  		assert.Equal(t, value, cfg.Type)
    63  	}
    64  
    65  	container := fmt.Sprintf("type: %s\n", BehaviorContainer.String())
    66  	var cfg config
    67  	require.Error(t, yaml.Unmarshal([]byte(container), &cfg))
    68  	require.Error(t, yaml.Unmarshal([]byte("type: not_a_known_type\n"), &cfg))
    69  }
    70  
    71  func TestWarnError(t *testing.T) {
    72  	ctrl := xtest.NewController(t)
    73  	defer ctrl.Finish()
    74  
    75  	store := NewMockStorage(ctrl)
    76  
    77  	err := errors.New("e")
    78  	warnErr := warnError{inner: errors.New("f")}
    79  
    80  	// NB: this shouldn't go to store.
    81  	warn, wrapped := IsWarning(store, warnErr)
    82  	assert.True(t, warn)
    83  	_, ok := wrapped.(warnError)
    84  	assert.True(t, ok)
    85  
    86  	store.EXPECT().ErrorBehavior().Return(BehaviorFail).Times(1)
    87  	warn, wrapped = IsWarning(store, err)
    88  	assert.False(t, warn)
    89  	_, ok = wrapped.(warnError)
    90  	assert.False(t, ok)
    91  
    92  	store.EXPECT().ErrorBehavior().Return(BehaviorWarn).Times(1)
    93  	warn, wrapped = IsWarning(store, err)
    94  	assert.True(t, warn)
    95  	_, ok = wrapped.(warnError)
    96  	assert.True(t, ok)
    97  }