github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/util/strutil/strconv_test.go (about)

     1  // Copyright 2016 The Prometheus Authors
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package strutil
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/stretchr/testify/require"
    20  )
    21  
    22  type linkTest struct {
    23  	expression        string
    24  	expectedGraphLink string
    25  	expectedTableLink string
    26  }
    27  
    28  var linkTests = []linkTest{
    29  	{
    30  		"sum(incoming_http_requests_total) by (system)",
    31  		"/graph?g0.expr=sum%28incoming_http_requests_total%29+by+%28system%29&g0.tab=0",
    32  		"/graph?g0.expr=sum%28incoming_http_requests_total%29+by+%28system%29&g0.tab=1",
    33  	},
    34  	{
    35  		"sum(incoming_http_requests_total{system=\"trackmetadata\"})",
    36  		"/graph?g0.expr=sum%28incoming_http_requests_total%7Bsystem%3D%22trackmetadata%22%7D%29&g0.tab=0",
    37  		"/graph?g0.expr=sum%28incoming_http_requests_total%7Bsystem%3D%22trackmetadata%22%7D%29&g0.tab=1",
    38  	},
    39  }
    40  
    41  func TestLink(t *testing.T) {
    42  	for _, tt := range linkTests {
    43  		graphLink := GraphLinkForExpression(tt.expression)
    44  		require.Equal(t, tt.expectedGraphLink, graphLink,
    45  			"GraphLinkForExpression failed for expression (%#q)", tt.expression)
    46  
    47  		tableLink := TableLinkForExpression(tt.expression)
    48  		require.Equal(t, tt.expectedTableLink, tableLink,
    49  			"TableLinkForExpression failed for expression (%#q)", tt.expression)
    50  	}
    51  }
    52  
    53  func TestSanitizeLabelName(t *testing.T) {
    54  	actual := SanitizeLabelName("fooClientLABEL")
    55  	expected := "fooClientLABEL"
    56  	require.Equal(t, expected, actual, "SanitizeLabelName failed for label (%s)", expected)
    57  
    58  	actual = SanitizeLabelName("barClient.LABEL$$##")
    59  	expected = "barClient_LABEL____"
    60  	require.Equal(t, expected, actual, "SanitizeLabelName failed for label (%s)", expected)
    61  }