|
| 1 | +// Copyright 2020 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 langserver |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "testing" |
| 19 | + "time" |
| 20 | + |
| 21 | + v1 "github.com/prometheus/client_golang/api/prometheus/v1" |
| 22 | + "github.com/prometheus/common/model" |
| 23 | + promql "github.com/prometheus/prometheus/promql/parser" |
| 24 | + |
| 25 | + "github.com/prometheus-community/promql-langserver/internal/vendored/go-tools/lsp/protocol" |
| 26 | + "github.com/prometheus-community/promql-langserver/langserver/cache" |
| 27 | +) |
| 28 | + |
| 29 | +// This is a mock object which serves as a test helper |
| 30 | +type MockMetadataService struct { |
| 31 | + metadata map[string][]v1.Metadata |
| 32 | + labelNames []string |
| 33 | + labelValues []model.LabelValue |
| 34 | +} |
| 35 | + |
| 36 | +func (m *MockMetadataService) MetricMetadata(ctx context.Context, metric string) (v1.Metadata, error) { |
| 37 | + return m.metadata[metric][0], nil |
| 38 | +} |
| 39 | + |
| 40 | +func (m *MockMetadataService) AllMetricMetadata(ctx context.Context) (map[string][]v1.Metadata, error) { |
| 41 | + return m.metadata, nil |
| 42 | +} |
| 43 | + |
| 44 | +func (m *MockMetadataService) LabelNames(ctx context.Context, metricName string, startTime time.Time, endTime time.Time) ([]string, error) { |
| 45 | + return m.labelNames, nil |
| 46 | +} |
| 47 | + |
| 48 | +func (m *MockMetadataService) LabelValues(ctx context.Context, label string, startTime time.Time, endTime time.Time) ([]model.LabelValue, error) { |
| 49 | + return m.labelValues, nil |
| 50 | +} |
| 51 | + |
| 52 | +func (m *MockMetadataService) ChangeDataSource(prometheusURL string) error { |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +func (m *MockMetadataService) GetURL() string { |
| 57 | + return "testhost:9090" |
| 58 | +} |
| 59 | + |
| 60 | +func TestMetricNameCompletion(t *testing.T) { |
| 61 | + s := &server{ |
| 62 | + metadataService: &MockMetadataService{ |
| 63 | + metadata: map[string][]v1.Metadata{ |
| 64 | + "a1": { |
| 65 | + v1.Metadata{ |
| 66 | + Type: v1.MetricTypeCounter, |
| 67 | + Help: "For completion test", |
| 68 | + Unit: "bytes", |
| 69 | + }, |
| 70 | + }, |
| 71 | + "b2": { |
| 72 | + v1.Metadata{ |
| 73 | + Type: v1.MetricTypeGauge, |
| 74 | + Help: "Not selected", |
| 75 | + Unit: "seconds", |
| 76 | + }, |
| 77 | + }, |
| 78 | + "c3": { |
| 79 | + v1.Metadata{ |
| 80 | + Type: v1.MetricTypeUnknown, |
| 81 | + Help: "Not selected", |
| 82 | + Unit: "ratio", |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + dc := &cache.DocumentCache{} |
| 90 | + dc.Init() |
| 91 | + doc, err := dc.AddDocument( |
| 92 | + context.Background(), |
| 93 | + &protocol.TextDocumentItem{ |
| 94 | + URI: "test.promql", |
| 95 | + LanguageID: "promql", |
| 96 | + Version: 0, |
| 97 | + Text: "", |
| 98 | + }) |
| 99 | + |
| 100 | + if err != nil { |
| 101 | + t.Fatalf("Error occurred when adding document: %s", err) |
| 102 | + } |
| 103 | + |
| 104 | + //TODO add compiled quries via dc.compileQuery |
| 105 | + |
| 106 | + l := &cache.Location{ |
| 107 | + Doc: doc, |
| 108 | + Query: &cache.CompiledQuery{ |
| 109 | + Pos: 0, |
| 110 | + }, |
| 111 | + Node: &promql.Item{}, |
| 112 | + } |
| 113 | + items := new([]protocol.CompletionItem) |
| 114 | + err = s.completeMetricName(context.Background(), items, l, "a") |
| 115 | + |
| 116 | + if err != nil { |
| 117 | + t.Errorf("Error occurred when calling completeMetricName: %s.\n", err) |
| 118 | + } |
| 119 | + |
| 120 | + if len(*items) != 1 { |
| 121 | + t.Errorf("Expected to have %d items, got: %d.\n", 1, len(*items)) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func TestLabelNameCompletion(t *testing.T) { |
| 126 | + s := &server{ |
| 127 | + metadataService: &MockMetadataService{ |
| 128 | + labelNames: []string{ |
| 129 | + "a1", |
| 130 | + "a2", |
| 131 | + "b3", |
| 132 | + "c4", |
| 133 | + }, |
| 134 | + }, |
| 135 | + } |
| 136 | + |
| 137 | + dc := &cache.DocumentCache{} |
| 138 | + dc.Init() |
| 139 | + doc, err := dc.AddDocument( |
| 140 | + context.Background(), |
| 141 | + &protocol.TextDocumentItem{ |
| 142 | + URI: "test.promql", |
| 143 | + LanguageID: "promql", |
| 144 | + Version: 0, |
| 145 | + Text: "", |
| 146 | + }) |
| 147 | + |
| 148 | + if err != nil { |
| 149 | + t.Fatalf("Error occurred when adding document: %s", err) |
| 150 | + } |
| 151 | + |
| 152 | + l := &cache.Location{ |
| 153 | + Doc: doc, |
| 154 | + Query: &cache.CompiledQuery{ |
| 155 | + Pos: 0, |
| 156 | + }, |
| 157 | + Node: &promql.Item{ |
| 158 | + Val: "a", |
| 159 | + }, |
| 160 | + } |
| 161 | + items := new([]protocol.CompletionItem) |
| 162 | + err = s.completeLabel(context.Background(), items, l, nil) |
| 163 | + |
| 164 | + if err != nil { |
| 165 | + t.Errorf("Error occurred when calling completeLabel: %s.\n", err) |
| 166 | + } |
| 167 | + |
| 168 | + if len(*items) != 2 { |
| 169 | + t.Errorf("Expected to have %d items, got: %d.\n", 2, len(*items)) |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +func TestFunctionNameCompletion(t *testing.T) { |
| 174 | + s := &server{ |
| 175 | + metadataService: &MockMetadataService{}, |
| 176 | + } |
| 177 | + |
| 178 | + dc := &cache.DocumentCache{} |
| 179 | + dc.Init() |
| 180 | + doc, err := dc.AddDocument( |
| 181 | + context.Background(), |
| 182 | + &protocol.TextDocumentItem{ |
| 183 | + URI: "test.promql", |
| 184 | + LanguageID: "promql", |
| 185 | + Version: 0, |
| 186 | + Text: "", |
| 187 | + }) |
| 188 | + |
| 189 | + if err != nil { |
| 190 | + t.Fatalf("Error occurred when adding document: %s", err) |
| 191 | + } |
| 192 | + |
| 193 | + l := &cache.Location{ |
| 194 | + Doc: doc, |
| 195 | + Query: &cache.CompiledQuery{ |
| 196 | + Pos: 0, |
| 197 | + }, |
| 198 | + Node: &promql.Item{}, |
| 199 | + } |
| 200 | + items := new([]protocol.CompletionItem) |
| 201 | + name := "std" |
| 202 | + // expect to match 5 functions: |
| 203 | + // - sort_desc |
| 204 | + // - stddev_over_time |
| 205 | + // - stdvar_over_time |
| 206 | + // - stddev |
| 207 | + // - stdvar |
| 208 | + err = s.completeFunctionName(items, l, name) |
| 209 | + |
| 210 | + if err != nil { |
| 211 | + t.Errorf("Error occurred when calling completeFunctionName: %s.\n", err) |
| 212 | + } |
| 213 | + |
| 214 | + if len(*items) != 5 { |
| 215 | + results := make([]string, len(*items)) |
| 216 | + for i, it := range *items { |
| 217 | + results[i] = it.Label |
| 218 | + } |
| 219 | + t.Errorf("Expected to have %d items, got: %d, are: %v.\n", 5, len(*items), results) |
| 220 | + } |
| 221 | +} |
0 commit comments