github.com/m3db/m3@v1.5.0/src/cmd/services/m3coordinator/ingest/carbon/rewrite_test.go (about)

     1  // Copyright (c) 2020 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 ingestcarbon
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/require"
    27  
    28  	"github.com/m3db/m3/src/cmd/services/m3query/config"
    29  )
    30  
    31  func TestCopyAndRewrite(t *testing.T) {
    32  	tests := []struct {
    33  		name     string
    34  		input    string
    35  		expected string
    36  		cfg      *config.CarbonIngesterRewriteConfiguration
    37  	}{
    38  		{
    39  			name:     "bad but no rewrite",
    40  			input:    "foo$$.bar%%.baz@@",
    41  			expected: "foo$$.bar%%.baz@@",
    42  			cfg:      nil,
    43  		},
    44  		{
    45  			name:     "bad but no rewrite cleanup",
    46  			input:    "foo$$.bar%%.baz@@",
    47  			expected: "foo$$.bar%%.baz@@",
    48  			cfg: &config.CarbonIngesterRewriteConfiguration{
    49  				Cleanup: false,
    50  			},
    51  		},
    52  		{
    53  			name:     "good with rewrite cleanup",
    54  			input:    "foo.bar.baz",
    55  			expected: "foo.bar.baz",
    56  			cfg: &config.CarbonIngesterRewriteConfiguration{
    57  				Cleanup: true,
    58  			},
    59  		},
    60  		{
    61  			name:     "bad with rewrite cleanup",
    62  			input:    "foo$$.bar%%.baz@@",
    63  			expected: "foo_.bar_.baz_",
    64  			cfg: &config.CarbonIngesterRewriteConfiguration{
    65  				Cleanup: true,
    66  			},
    67  		},
    68  		{
    69  			name:     "collapse two dots with rewrite cleanup",
    70  			input:    "foo..bar.baz",
    71  			expected: "foo.bar.baz",
    72  			cfg: &config.CarbonIngesterRewriteConfiguration{
    73  				Cleanup: true,
    74  			},
    75  		},
    76  		{
    77  			name:     "collapse three and two dots with rewrite cleanup",
    78  			input:    "foo...bar..baz",
    79  			expected: "foo.bar.baz",
    80  			cfg: &config.CarbonIngesterRewriteConfiguration{
    81  				Cleanup: true,
    82  			},
    83  		},
    84  		{
    85  			name:     "remove leading dot with rewrite cleanup",
    86  			input:    ".foo.bar.baz",
    87  			expected: "foo.bar.baz",
    88  			cfg: &config.CarbonIngesterRewriteConfiguration{
    89  				Cleanup: true,
    90  			},
    91  		},
    92  		{
    93  			name:     "remove multiple leading dots with rewrite cleanup",
    94  			input:    "..foo.bar.baz",
    95  			expected: "foo.bar.baz",
    96  			cfg: &config.CarbonIngesterRewriteConfiguration{
    97  				Cleanup: true,
    98  			},
    99  		},
   100  		{
   101  			name:     "remove trailing dot with rewrite cleanup",
   102  			input:    "foo.bar.baz.",
   103  			expected: "foo.bar.baz",
   104  			cfg: &config.CarbonIngesterRewriteConfiguration{
   105  				Cleanup: true,
   106  			},
   107  		},
   108  		{
   109  			name:     "remove multiple trailing dots with rewrite cleanup",
   110  			input:    "foo.bar.baz..",
   111  			expected: "foo.bar.baz",
   112  			cfg: &config.CarbonIngesterRewriteConfiguration{
   113  				Cleanup: true,
   114  			},
   115  		},
   116  	}
   117  
   118  	for _, test := range tests {
   119  		t.Run(test.name, func(t *testing.T) {
   120  			actual := copyAndRewrite(nil, []byte(test.input), test.cfg)
   121  			require.Equal(t, test.expected, string(actual))
   122  		})
   123  	}
   124  }