github.com/m3db/m3@v1.5.0/src/metrics/rules/view/changes/ruleset_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 changes
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/m3db/m3/src/metrics/rules/view"
    27  	"github.com/m3db/m3/src/metrics/x/bytes"
    28  
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  func TestRuleSetChangeSetsSort(t *testing.T) {
    33  	expected := RuleSetChanges{
    34  		Namespace: "service1",
    35  		MappingRuleChanges: []MappingRuleChange{
    36  			{
    37  				Op: AddOp,
    38  				RuleData: &view.MappingRule{
    39  					Name: "Add1",
    40  				},
    41  			},
    42  			{
    43  				Op: AddOp,
    44  				RuleData: &view.MappingRule{
    45  					Name: "Add2",
    46  				},
    47  			},
    48  			{
    49  				Op:     ChangeOp,
    50  				RuleID: ptr("mrID2"),
    51  				RuleData: &view.MappingRule{
    52  					Name: "change1",
    53  				},
    54  			},
    55  			{
    56  				Op:     ChangeOp,
    57  				RuleID: ptr("mrID3"),
    58  				RuleData: &view.MappingRule{
    59  					Name: "change2",
    60  				},
    61  			},
    62  			{
    63  				Op:     ChangeOp,
    64  				RuleID: ptr("mrID1"),
    65  				RuleData: &view.MappingRule{
    66  					Name: "change3",
    67  				},
    68  			},
    69  		},
    70  		RollupRuleChanges: []RollupRuleChange{
    71  			{
    72  				Op: AddOp,
    73  				RuleData: &view.RollupRule{
    74  					Name: "Add1",
    75  				},
    76  			},
    77  			{
    78  				Op: AddOp,
    79  				RuleData: &view.RollupRule{
    80  					Name: "Add2",
    81  				},
    82  			},
    83  			{
    84  				Op:     ChangeOp,
    85  				RuleID: ptr("rrID2"),
    86  				RuleData: &view.RollupRule{
    87  					Name: "change1",
    88  				},
    89  			},
    90  			{
    91  				Op:     ChangeOp,
    92  				RuleID: ptr("rrID3"),
    93  				RuleData: &view.RollupRule{
    94  					Name: "change2",
    95  				},
    96  			},
    97  			{
    98  				Op:     ChangeOp,
    99  				RuleID: ptr("rrID1"),
   100  				RuleData: &view.RollupRule{
   101  					Name: "change3",
   102  				},
   103  			},
   104  		},
   105  	}
   106  
   107  	ruleSet.Sort()
   108  	require.Equal(t, expected, ruleSet)
   109  	require.Equal(t, expected, ruleSet)
   110  }
   111  
   112  var (
   113  	ruleSet = RuleSetChanges{
   114  		Namespace: "service1",
   115  		MappingRuleChanges: []MappingRuleChange{
   116  			{
   117  				Op:     ChangeOp,
   118  				RuleID: ptr("mrID1"),
   119  				RuleData: &view.MappingRule{
   120  					Name: "change3",
   121  				},
   122  			},
   123  			{
   124  				Op: AddOp,
   125  				RuleData: &view.MappingRule{
   126  					Name: "Add2",
   127  				},
   128  			},
   129  			{
   130  				Op:     ChangeOp,
   131  				RuleID: ptr("mrID2"),
   132  				RuleData: &view.MappingRule{
   133  					Name: "change1",
   134  				},
   135  			},
   136  			{
   137  				Op:     ChangeOp,
   138  				RuleID: ptr("mrID3"),
   139  				RuleData: &view.MappingRule{
   140  					Name: "change2",
   141  				},
   142  			},
   143  			{
   144  				Op: AddOp,
   145  				RuleData: &view.MappingRule{
   146  					Name: "Add1",
   147  				},
   148  			},
   149  		},
   150  		RollupRuleChanges: []RollupRuleChange{
   151  			{
   152  				Op:     ChangeOp,
   153  				RuleID: ptr("rrID1"),
   154  				RuleData: &view.RollupRule{
   155  					Name: "change3",
   156  				},
   157  			},
   158  			{
   159  				Op: AddOp,
   160  				RuleData: &view.RollupRule{
   161  					Name: "Add2",
   162  				},
   163  			},
   164  			{
   165  				Op:     ChangeOp,
   166  				RuleID: ptr("rrID2"),
   167  				RuleData: &view.RollupRule{
   168  					Name: "change1",
   169  				},
   170  			},
   171  			{
   172  				Op:     ChangeOp,
   173  				RuleID: ptr("rrID3"),
   174  				RuleData: &view.RollupRule{
   175  					Name: "change2",
   176  				},
   177  			},
   178  			{
   179  				Op: AddOp,
   180  				RuleData: &view.RollupRule{
   181  					Name: "Add1",
   182  				},
   183  			},
   184  		},
   185  	}
   186  )
   187  
   188  // nolint:unparam
   189  func b(v string) []byte       { return []byte(v) }
   190  func bs(v ...string) [][]byte { return bytes.ArraysFromStringArray(v) }
   191  func ptr(str string) *string  { return &str }