...

Source file src/github.com/prometheus/common/expfmt/expfmt_test.go

Documentation: github.com/prometheus/common/expfmt

     1  // Copyright 2024 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 expfmt
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/prometheus/common/model"
    20  )
    21  
    22  // Test Format to Escapting Scheme conversion
    23  // Path: expfmt/expfmt_test.go
    24  // Compare this snippet from expfmt/expfmt.go:
    25  func TestToFormatType(t *testing.T) {
    26  	tests := []struct {
    27  		format   Format
    28  		expected FormatType
    29  	}{
    30  		{
    31  			format:   fmtProtoCompact,
    32  			expected: TypeProtoCompact,
    33  		},
    34  		{
    35  			format:   fmtProtoDelim,
    36  			expected: TypeProtoDelim,
    37  		},
    38  		{
    39  			format:   fmtProtoText,
    40  			expected: TypeProtoText,
    41  		},
    42  		{
    43  			format:   fmtOpenMetrics_1_0_0,
    44  			expected: TypeOpenMetrics,
    45  		},
    46  		{
    47  			format:   fmtText,
    48  			expected: TypeTextPlain,
    49  		},
    50  		{
    51  			format:   fmtOpenMetrics_0_0_1,
    52  			expected: TypeOpenMetrics,
    53  		},
    54  		{
    55  			format:   "application/vnd.google.protobuf; proto=BadProtocol; encoding=text",
    56  			expected: TypeUnknown,
    57  		},
    58  		{
    59  			format:   "application/vnd.google.protobuf",
    60  			expected: TypeUnknown,
    61  		},
    62  		{
    63  			format:   "application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily=bad",
    64  			expected: TypeUnknown,
    65  		},
    66  		// encoding missing
    67  		{
    68  			format:   "application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily",
    69  			expected: TypeUnknown,
    70  		},
    71  		// invalid encoding
    72  		{
    73  			format:   "application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=textual",
    74  			expected: TypeUnknown,
    75  		},
    76  		// bad charset, must be utf-8
    77  		{
    78  			format:   "application/openmetrics-text; version=1.0.0; charset=ascii",
    79  			expected: TypeUnknown,
    80  		},
    81  		{
    82  			format:   "text/plain",
    83  			expected: TypeTextPlain,
    84  		},
    85  		{
    86  			format:   "text/plain; version=invalid",
    87  			expected: TypeUnknown,
    88  		},
    89  		{
    90  			format:   "gobbledygook",
    91  			expected: TypeUnknown,
    92  		},
    93  	}
    94  	for _, test := range tests {
    95  		if test.format.FormatType() != test.expected {
    96  			t.Errorf("expected %v got %v", test.expected, test.format.FormatType())
    97  		}
    98  	}
    99  }
   100  
   101  func TestToEscapingScheme(t *testing.T) {
   102  	tests := []struct {
   103  		format   Format
   104  		expected model.EscapingScheme
   105  	}{
   106  		{
   107  			format:   fmtProtoCompact,
   108  			expected: model.ValueEncodingEscaping,
   109  		},
   110  		{
   111  			format:   "application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=underscores",
   112  			expected: model.UnderscoreEscaping,
   113  		},
   114  		{
   115  			format:   "application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=allow-utf-8",
   116  			expected: model.NoEscaping,
   117  		},
   118  		// error returns default
   119  		{
   120  			format:   "application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=invalid",
   121  			expected: model.NameEscapingScheme,
   122  		},
   123  	}
   124  	for _, test := range tests {
   125  		if test.format.ToEscapingScheme() != test.expected {
   126  			t.Errorf("expected %v got %v", test.expected, test.format.ToEscapingScheme())
   127  		}
   128  	}
   129  }
   130  

View as plain text