github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/metrics/policy/drop_policy_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 policy
    22  
    23  import (
    24  	"fmt"
    25  	"math"
    26  	"testing"
    27  
    28  	"github.com/m3db/m3/src/x/test/testmarshal"
    29  	"github.com/stretchr/testify/assert"
    30  	"github.com/stretchr/testify/require"
    31  	"gopkg.in/yaml.v2"
    32  )
    33  
    34  func TestDropPolicyIsDefault(t *testing.T) {
    35  	assert.True(t, DefaultDropPolicy.IsDefault())
    36  }
    37  
    38  func TestDropPolicyString(t *testing.T) {
    39  	for _, policy := range validDropPolicies {
    40  		switch policy {
    41  		case DropNone:
    42  			assert.Equal(t, "drop_none", policy.String())
    43  		case DropMust:
    44  			assert.Equal(t, "drop_must", policy.String())
    45  		case DropIfOnlyMatch:
    46  			assert.Equal(t, "drop_if_only_match", policy.String())
    47  		default:
    48  			assert.Fail(t, fmt.Sprintf("unknown policy: value=%d, string=%s",
    49  				int(policy), policy.String()))
    50  		}
    51  	}
    52  }
    53  
    54  func TestDropPolicyIsValid(t *testing.T) {
    55  	for _, policy := range validDropPolicies {
    56  		assert.True(t, policy.IsValid())
    57  	}
    58  	assert.False(t, DropPolicy(math.MaxUint64).IsValid())
    59  }
    60  
    61  func TestDropPolicyMarshalling(t *testing.T) {
    62  	inputs := []struct {
    63  		str      string
    64  		expected DropPolicy
    65  	}{
    66  		{
    67  			str:      "",
    68  			expected: DropNone,
    69  		},
    70  		{
    71  			str:      DropNone.String(),
    72  			expected: DropNone,
    73  		},
    74  		{
    75  			str:      DropMust.String(),
    76  			expected: DropMust,
    77  		},
    78  		{
    79  			str:      DropIfOnlyMatch.String(),
    80  			expected: DropIfOnlyMatch,
    81  		},
    82  	}
    83  
    84  	examples := make([]DropPolicy, 0, len(inputs))
    85  	for _, p := range inputs {
    86  		examples = append(examples, p.expected)
    87  	}
    88  
    89  	t.Run("roundtrips", func(t *testing.T) {
    90  		testmarshal.TestMarshalersRoundtrip(t, examples, []testmarshal.Marshaler{testmarshal.YAMLMarshaler, testmarshal.JSONMarshaler, testmarshal.TextMarshaler})
    91  	})
    92  
    93  	t.Run("yaml/unmarshals", func(t *testing.T) {
    94  		for _, input := range inputs {
    95  			testmarshal.Require(t, testmarshal.AssertUnmarshals(t, testmarshal.YAMLMarshaler, input.expected, []byte(input.str)))
    96  		}
    97  	})
    98  }
    99  
   100  func TestDropPolicyUnmarshalYAMLErrors(t *testing.T) {
   101  	inputs := []string{
   102  		"drop_musty",
   103  		"drop_unknown",
   104  	}
   105  	for _, input := range inputs {
   106  		var p DropPolicy
   107  		require.Error(t, yaml.Unmarshal([]byte(input), &p))
   108  	}
   109  }