...

Source file src/github.com/linkerd/linkerd2/testutil/prommatch/prommatch_test.go

Documentation: github.com/linkerd/linkerd2/testutil/prommatch

     1  package prommatch
     2  
     3  import (
     4  	_ "embed"
     5  	"regexp"
     6  	"testing"
     7  )
     8  
     9  //go:embed testdata/sampleMetrics.txt
    10  var sampleMetrics string
    11  
    12  func TestMatchingString(t *testing.T) {
    13  	tests := []struct {
    14  		name    string
    15  		matcher *Matcher
    16  		result  bool
    17  	}{
    18  		{
    19  			name:    "match by name",
    20  			matcher: NewMatcher("process_cpu_seconds_total"),
    21  			result:  true,
    22  		},
    23  		{
    24  			name:    "match by name (no results)",
    25  			matcher: NewMatcher("process_cpu_seconds_total_xxx"),
    26  			result:  false,
    27  		},
    28  		{
    29  			name: "match by label",
    30  			matcher: NewMatcher(
    31  				"inbound_http_authz_allow_total",
    32  				Labels{
    33  					"authz_kind": Equals("default"),
    34  				}),
    35  			result: true,
    36  		},
    37  		{
    38  			name: "match by label (no results, no such value)",
    39  			matcher: NewMatcher(
    40  				"inbound_http_authz_allow_total",
    41  				Labels{
    42  					"authz_kind": Equals("default_xxx"),
    43  				}),
    44  			result: false,
    45  		},
    46  		{
    47  			name: "match by label (no results, no such labels)",
    48  			matcher: NewMatcher(
    49  				"inbound_http_authz_allow_total",
    50  				Labels{
    51  					"authz_kind_xxx": Equals("default"),
    52  				}),
    53  			result: false,
    54  		},
    55  		{
    56  			name: "match by label regex and name",
    57  			matcher: NewMatcher(
    58  				"control_response_latency_ms_sum",
    59  				Labels{
    60  					"addr": Like(regexp.MustCompile(`linkerd-identity-headless.linkerd.svc.cluster.local:[\d]{4}`)),
    61  				}),
    62  			result: true,
    63  		},
    64  		{
    65  			name: "match by label regex and name (no results)",
    66  			matcher: NewMatcher(
    67  				"control_response_latency_ms_sum",
    68  				Labels{
    69  					"addr": Like(regexp.MustCompile(`linkerd-identity-headless.linkerd.svc.cluster.local:[\d]{5}`)),
    70  				}),
    71  			result: false,
    72  		},
    73  		{
    74  			name: "match histogram bucket",
    75  			matcher: NewMatcher(
    76  				"control_response_latency_ms_bucket",
    77  				Labels{
    78  					"le": Equals("2"),
    79  				}),
    80  			result: true,
    81  		},
    82  		{
    83  			name: "match histogram bucket",
    84  			matcher: NewMatcher(
    85  				"control_response_latency_ms_bucket",
    86  				Labels{
    87  					"le": Equals("2"),
    88  				},
    89  				HasPositiveValue()),
    90  			result: true,
    91  		},
    92  		{
    93  			name: "match by value (no results)",
    94  			matcher: NewMatcher(
    95  				"control_response_latency_ms_bucket",
    96  				Labels{
    97  					"le": Equals("0"),
    98  				},
    99  				HasPositiveValue(),
   100  			),
   101  			result: false,
   102  		},
   103  	}
   104  
   105  	for _, tc := range tests {
   106  		t.Run(tc.name, func(t *testing.T) {
   107  			ok, err := tc.matcher.HasMatchInString(sampleMetrics)
   108  			if err != nil {
   109  				t.Fatalf("Unexpected error: %s", err)
   110  			}
   111  			if ok != tc.result {
   112  				t.Fatalf("Expected %v, got %v", tc.result, ok)
   113  
   114  			}
   115  		})
   116  	}
   117  }
   118  

View as plain text