...

Source file src/edge-infra.dev/pkg/lib/promassert/filter_test.go

Documentation: edge-infra.dev/pkg/lib/promassert

     1  //nolint:dupl
     2  package promassert_test
     3  
     4  import (
     5  	"testing"
     6  
     7  	"edge-infra.dev/pkg/lib/promassert"
     8  )
     9  
    10  type filterWith struct {
    11  	labels map[string]string
    12  	value  float64
    13  }
    14  
    15  func TestFilterWith(t *testing.T) {
    16  	var mets = []promassert.ParsedMetrics{
    17  		promassert.Counter(cv1name),
    18  		promassert.Gauge(gv1name),
    19  	}
    20  	for _, unfiltered := range mets {
    21  		var filters = []filterWith{
    22  			filterWith{
    23  				labels: map[string]string{"foo": "hello"},
    24  				value:  cv1value1 + cv1value3,
    25  			},
    26  			filterWith{
    27  				labels: map[string]string{"bar": "world"},
    28  				value:  cv1value1 + cv1value2,
    29  			},
    30  			filterWith{
    31  				labels: map[string]string{"baz": "bit"},
    32  				value:  cv1value1 + cv1value2 + cv1value3,
    33  			},
    34  			filterWith{
    35  				labels: map[string]string{"foo": "hello", "baz": "bit"},
    36  				value:  cv1value1 + cv1value3,
    37  			},
    38  			filterWith{
    39  				labels: map[string]string{"bar": "world", "baz": "bit"},
    40  				value:  cv1value1 + cv1value2,
    41  			},
    42  			filterWith{
    43  				labels: map[string]string{"foo": "hello", "bar": "world", "baz": "bit"},
    44  				value:  cv1value1,
    45  			},
    46  			filterWith{
    47  				labels: map[string]string{"foo": "goodbye", "bar": "world", "baz": "bit"},
    48  				value:  cv1value2,
    49  			},
    50  			filterWith{
    51  				labels: map[string]string{"foo": "hello", "bar": "goodbye", "baz": "bit"},
    52  				value:  cv1value3,
    53  			},
    54  			filterWith{
    55  				labels: map[string]string{"foo": "dog", "bar": "cat", "baz": "frog"},
    56  				value:  cv1value4,
    57  			},
    58  		}
    59  		for fi, f := range filters {
    60  			p := unfiltered.With(f.labels)
    61  			var happyResults = []bool{
    62  				p.Exists(t),
    63  				p.Equals(t, f.value),
    64  				p.NotEquals(t, f.value+1),
    65  				p.GreaterThan(t, f.value-1),
    66  				p.GreaterThanOrEquals(t, f.value),
    67  				p.GreaterThanOrEquals(t, f.value-1),
    68  				p.LessThan(t, f.value+1),
    69  				p.LessThanOrEquals(t, f.value),
    70  				p.LessThanOrEquals(t, f.value+1),
    71  			}
    72  
    73  			// Ensure all the happy tests returned true.
    74  			for i, result := range happyResults {
    75  				if !result {
    76  					t.Errorf("Happy assertion %d for filter %d should have returned true", i, fi)
    77  				}
    78  			}
    79  
    80  			var sadResults = []bool{
    81  				p.NotExists(nil),
    82  				p.Equals(nil, f.value+1),
    83  				p.NotEquals(nil, f.value),
    84  				p.GreaterThan(nil, f.value),
    85  				p.GreaterThan(nil, f.value+1),
    86  				p.GreaterThanOrEquals(nil, f.value+1),
    87  				p.LessThan(nil, f.value),
    88  				p.LessThan(nil, f.value-1),
    89  				p.LessThanOrEquals(nil, f.value-1),
    90  				p.IsNaN(nil),
    91  				p.IsInf(nil, 0),
    92  			}
    93  
    94  			for i, result := range sadResults {
    95  				if result {
    96  					t.Errorf("Sad assertion %d for filter %d should have returned false", i, fi)
    97  				}
    98  			}
    99  		}
   100  	}
   101  }
   102  
   103  type filterWithValues struct {
   104  	labels map[string][]string
   105  	value  float64
   106  }
   107  
   108  func TestFilterWithValues(t *testing.T) {
   109  	var mets = []promassert.ParsedMetrics{
   110  		promassert.Counter(cv1name),
   111  		promassert.Gauge(gv1name),
   112  	}
   113  	for _, unfiltered := range mets {
   114  		var filters = []filterWithValues{
   115  			filterWithValues{
   116  				labels: map[string][]string{"foo": []string{"hello"}},
   117  				value:  cv1value1 + cv1value3,
   118  			},
   119  			filterWithValues{
   120  				labels: map[string][]string{"bar": []string{"world"}},
   121  				value:  cv1value1 + cv1value2,
   122  			},
   123  			filterWithValues{
   124  				labels: map[string][]string{"baz": []string{"bit"}},
   125  				value:  cv1value1 + cv1value2 + cv1value3,
   126  			},
   127  			filterWithValues{
   128  				labels: map[string][]string{
   129  					"foo": []string{"hello"},
   130  					"bar": []string{"world"},
   131  					"baz": []string{"bit"},
   132  				},
   133  				value: cv1value1,
   134  			},
   135  			filterWithValues{
   136  				labels: map[string][]string{
   137  					"foo": []string{"hello", "goodbye"},
   138  					"bar": []string{"world", "goodbye"},
   139  					"baz": []string{"bit"},
   140  				},
   141  				value: cv1value1 + cv1value2 + cv1value3,
   142  			},
   143  			// even though "frog" exists, the other labels "dog" and "cat" are not present, so the
   144  			// last metric is not included in the value.
   145  			filterWithValues{
   146  				labels: map[string][]string{
   147  					"foo": []string{"hello", "goodbye"},
   148  					"bar": []string{"world", "goodbye"},
   149  					"baz": []string{"bit", "frog"},
   150  				},
   151  				value: cv1value1 + cv1value2 + cv1value3,
   152  			},
   153  			filterWithValues{
   154  				labels: map[string][]string{
   155  					"foo": []string{"hello", "goodbye", "dog"},
   156  					"bar": []string{"world", "goodbye", "cat"},
   157  					"baz": []string{"bit", "frog"},
   158  				},
   159  				value: cv1value1 + cv1value2 + cv1value3 + cv1value4,
   160  			},
   161  			filterWithValues{
   162  				labels: map[string][]string{
   163  					"foo": []string{"hello", "dog"},
   164  					"bar": []string{"world", "goodbye", "cat"},
   165  					"baz": []string{"bit", "frog"},
   166  				},
   167  				value: cv1value1 + cv1value3 + cv1value4,
   168  			},
   169  			filterWithValues{
   170  				labels: map[string][]string{
   171  					"foo": []string{"goodbye", "dog"},
   172  					"bar": []string{"world", "cat"},
   173  					"baz": []string{"bit", "frog"},
   174  				},
   175  				value: cv1value2 + cv1value4,
   176  			},
   177  		}
   178  		for fi, f := range filters {
   179  			p := unfiltered.WithValues(f.labels)
   180  			var happyResults = []bool{
   181  				p.Exists(t),
   182  				p.Equals(t, f.value),
   183  				p.NotEquals(t, f.value+1),
   184  				p.GreaterThan(t, f.value-1),
   185  				p.GreaterThanOrEquals(t, f.value),
   186  				p.GreaterThanOrEquals(t, f.value-1),
   187  				p.LessThan(t, f.value+1),
   188  				p.LessThanOrEquals(t, f.value),
   189  				p.LessThanOrEquals(t, f.value+1),
   190  			}
   191  
   192  			// Ensure all the happy tests returned true.
   193  			for i, result := range happyResults {
   194  				if !result {
   195  					t.Errorf("Happy assertion %d for filter %d should have returned true", i, fi)
   196  				}
   197  			}
   198  
   199  			var sadResults = []bool{
   200  				p.NotExists(nil),
   201  				p.Equals(nil, f.value+1),
   202  				p.NotEquals(nil, f.value),
   203  				p.GreaterThan(nil, f.value),
   204  				p.GreaterThan(nil, f.value+1),
   205  				p.GreaterThanOrEquals(nil, f.value+1),
   206  				p.LessThan(nil, f.value),
   207  				p.LessThan(nil, f.value-1),
   208  				p.LessThanOrEquals(nil, f.value-1),
   209  				p.IsNaN(nil),
   210  				p.IsInf(nil, 0),
   211  			}
   212  
   213  			for i, result := range sadResults {
   214  				if result {
   215  					t.Errorf("Sad assertion %d for filter %d should have returned false", i, fi)
   216  				}
   217  			}
   218  		}
   219  	}
   220  }
   221  

View as plain text