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