github.com/m3db/m3@v1.5.0/src/metrics/rules/convert_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 rules
    22  
    23  import (
    24  	"strings"
    25  	"testing"
    26  	"time"
    27  
    28  	"github.com/m3db/m3/src/metrics/aggregation"
    29  	"github.com/m3db/m3/src/metrics/generated/proto/aggregationpb"
    30  	"github.com/m3db/m3/src/metrics/generated/proto/policypb"
    31  	"github.com/m3db/m3/src/metrics/policy"
    32  	xtime "github.com/m3db/m3/src/x/time"
    33  
    34  	"github.com/stretchr/testify/require"
    35  )
    36  
    37  func TestToAggregationIDAndStoragePoliciesNilPolicyProto(t *testing.T) {
    38  	policiesProto := []*policypb.Policy{nil}
    39  	_, _, err := toAggregationIDAndStoragePolicies(policiesProto)
    40  	require.Equal(t, errNilPolicyProto, err)
    41  }
    42  
    43  func TestToAggregationIDAndStoragePoliciesNilStoragePolicyProto(t *testing.T) {
    44  	policiesProto := []*policypb.Policy{
    45  		&policypb.Policy{},
    46  	}
    47  	_, _, err := toAggregationIDAndStoragePolicies(policiesProto)
    48  	require.Error(t, err)
    49  }
    50  
    51  func TestToAggregationIDAndStoragePoliciesInvalidStoragePolicyProto(t *testing.T) {
    52  	policiesProto := []*policypb.Policy{
    53  		&policypb.Policy{
    54  			StoragePolicy: &policypb.StoragePolicy{
    55  				Resolution: policypb.Resolution{Precision: 1234},
    56  				Retention:  policypb.Retention{Period: 5678},
    57  			},
    58  		},
    59  	}
    60  	_, _, err := toAggregationIDAndStoragePolicies(policiesProto)
    61  	require.Error(t, err)
    62  }
    63  
    64  func TestToAggregationIDAndStoragePoliciesInvalidAggregationTypes(t *testing.T) {
    65  	policiesProto := []*policypb.Policy{
    66  		&policypb.Policy{
    67  			StoragePolicy: &policypb.StoragePolicy{
    68  				Resolution: policypb.Resolution{
    69  					WindowSize: 10 * time.Second.Nanoseconds(),
    70  					Precision:  time.Second.Nanoseconds(),
    71  				},
    72  				Retention: policypb.Retention{
    73  					Period: 24 * time.Hour.Nanoseconds(),
    74  				},
    75  			},
    76  			AggregationTypes: []aggregationpb.AggregationType{10, 1234567},
    77  		},
    78  	}
    79  	_, _, err := toAggregationIDAndStoragePolicies(policiesProto)
    80  	require.Error(t, err)
    81  }
    82  
    83  func TestToAggregationIDAndStoragePoliciesInconsistentAggregationIDs(t *testing.T) {
    84  	policiesProto := []*policypb.Policy{
    85  		&policypb.Policy{
    86  			StoragePolicy: &policypb.StoragePolicy{
    87  				Resolution: policypb.Resolution{
    88  					WindowSize: 10 * time.Second.Nanoseconds(),
    89  					Precision:  time.Second.Nanoseconds(),
    90  				},
    91  				Retention: policypb.Retention{
    92  					Period: 24 * time.Hour.Nanoseconds(),
    93  				},
    94  			},
    95  			AggregationTypes: []aggregationpb.AggregationType{1},
    96  		},
    97  		&policypb.Policy{
    98  			StoragePolicy: &policypb.StoragePolicy{
    99  				Resolution: policypb.Resolution{
   100  					WindowSize: time.Minute.Nanoseconds(),
   101  					Precision:  time.Minute.Nanoseconds(),
   102  				},
   103  				Retention: policypb.Retention{
   104  					Period: 720 * time.Hour.Nanoseconds(),
   105  				},
   106  			},
   107  			AggregationTypes: []aggregationpb.AggregationType{2},
   108  		},
   109  	}
   110  	_, _, err := toAggregationIDAndStoragePolicies(policiesProto)
   111  	require.Error(t, err)
   112  	require.True(t, strings.Contains(err.Error(), "more than one aggregation ID in legacy policies list proto: ID1=Last, ID2=Min"))
   113  }
   114  
   115  func TestToAggregationIDAndStoragePoliciesDefaultAggregationID(t *testing.T) {
   116  	policiesProto := []*policypb.Policy{
   117  		&policypb.Policy{
   118  			StoragePolicy: &policypb.StoragePolicy{
   119  				Resolution: policypb.Resolution{
   120  					WindowSize: 10 * time.Second.Nanoseconds(),
   121  					Precision:  time.Second.Nanoseconds(),
   122  				},
   123  				Retention: policypb.Retention{
   124  					Period: 24 * time.Hour.Nanoseconds(),
   125  				},
   126  			},
   127  		},
   128  		&policypb.Policy{
   129  			StoragePolicy: &policypb.StoragePolicy{
   130  				Resolution: policypb.Resolution{
   131  					WindowSize: time.Minute.Nanoseconds(),
   132  					Precision:  time.Minute.Nanoseconds(),
   133  				},
   134  				Retention: policypb.Retention{
   135  					Period: 720 * time.Hour.Nanoseconds(),
   136  				},
   137  			},
   138  		},
   139  		&policypb.Policy{
   140  			StoragePolicy: &policypb.StoragePolicy{
   141  				Resolution: policypb.Resolution{
   142  					WindowSize: time.Hour.Nanoseconds(),
   143  					Precision:  time.Hour.Nanoseconds(),
   144  				},
   145  				Retention: policypb.Retention{
   146  					Period: 365 * 24 * time.Hour.Nanoseconds(),
   147  				},
   148  			},
   149  		},
   150  	}
   151  	expectedStoragePolicies := policy.StoragePolicies{
   152  		policy.NewStoragePolicy(10*time.Second, xtime.Second, 24*time.Hour),
   153  		policy.NewStoragePolicy(time.Minute, xtime.Minute, 720*time.Hour),
   154  		policy.NewStoragePolicy(time.Hour, xtime.Hour, 365*24*time.Hour),
   155  	}
   156  	aggregationID, storagePolicies, err := toAggregationIDAndStoragePolicies(policiesProto)
   157  	require.NoError(t, err)
   158  	require.Equal(t, aggregation.DefaultID, aggregationID)
   159  	require.Equal(t, expectedStoragePolicies, storagePolicies)
   160  }
   161  
   162  func TestToAggregationIDAndStoragePoliciesCustomAggregationID(t *testing.T) {
   163  	policiesProto := []*policypb.Policy{
   164  		&policypb.Policy{
   165  			StoragePolicy: &policypb.StoragePolicy{
   166  				Resolution: policypb.Resolution{
   167  					WindowSize: 10 * time.Second.Nanoseconds(),
   168  					Precision:  time.Second.Nanoseconds(),
   169  				},
   170  				Retention: policypb.Retention{
   171  					Period: 24 * time.Hour.Nanoseconds(),
   172  				},
   173  			},
   174  			AggregationTypes: []aggregationpb.AggregationType{1, 2},
   175  		},
   176  		&policypb.Policy{
   177  			StoragePolicy: &policypb.StoragePolicy{
   178  				Resolution: policypb.Resolution{
   179  					WindowSize: time.Minute.Nanoseconds(),
   180  					Precision:  time.Minute.Nanoseconds(),
   181  				},
   182  				Retention: policypb.Retention{
   183  					Period: 720 * time.Hour.Nanoseconds(),
   184  				},
   185  			},
   186  			AggregationTypes: []aggregationpb.AggregationType{1, 2},
   187  		},
   188  		&policypb.Policy{
   189  			StoragePolicy: &policypb.StoragePolicy{
   190  				Resolution: policypb.Resolution{
   191  					WindowSize: time.Hour.Nanoseconds(),
   192  					Precision:  time.Hour.Nanoseconds(),
   193  				},
   194  				Retention: policypb.Retention{
   195  					Period: 365 * 24 * time.Hour.Nanoseconds(),
   196  				},
   197  			},
   198  			AggregationTypes: []aggregationpb.AggregationType{1, 2},
   199  		},
   200  	}
   201  	expectedStoragePolicies := policy.StoragePolicies{
   202  		policy.NewStoragePolicy(10*time.Second, xtime.Second, 24*time.Hour),
   203  		policy.NewStoragePolicy(time.Minute, xtime.Minute, 720*time.Hour),
   204  		policy.NewStoragePolicy(time.Hour, xtime.Hour, 365*24*time.Hour),
   205  	}
   206  	aggregationID, storagePolicies, err := toAggregationIDAndStoragePolicies(policiesProto)
   207  	require.NoError(t, err)
   208  	require.Equal(t, aggregation.MustCompressTypes(aggregation.Last, aggregation.Min), aggregationID)
   209  	require.Equal(t, expectedStoragePolicies, storagePolicies)
   210  }