Skip to content

Commit

Permalink
MQE: enforce consistent formatting for test files (#10774)
Browse files Browse the repository at this point in the history
* Initial version: convert indentation to two spaces

* Strip trailing whitespace

* Ensure lines are indented where appropriate

* Rename script, allow formatting multiple files at once

* Add Make target

* Make script work on macOS and Linux

* Ensure load, eval and clear commands are not indented.

* Add license header

* Remove consecutive blank lines
  • Loading branch information
charleskorn authored Mar 3, 2025
1 parent e2aa729 commit acf266f
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 220 deletions.
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ images: ## Print all image names.
PROTO_DEFS := $(shell find . $(DONT_FIND) -type f -name '*.proto' -print)
PROTO_GOS := $(patsubst %.proto,%.pb.go,$(PROTO_DEFS))

PROMQL_TESTS := $(shell find pkg/streamingpromql/testdata/ours pkg/streamingpromql/testdata/ours-only $(DONT_FIND) -type f -path '*.test' -print)

# Generating OTLP translation code is automated.
OTLP_GOS := $(shell find ./pkg/distributor/otlp/ -type f -name '*_generated.go' -print)

Expand All @@ -239,7 +241,7 @@ all: $(UPTODATE_FILES)
test: protos
test-with-race: protos
mod-check: protos
lint: lint-gh-action lint-packaging-scripts protos
lint: lint-gh-action lint-packaging-scripts protos check-promql-tests
mimir-build-image/$(UPTODATE): mimir-build-image/*

# All the boiler plate for building golang follows:
Expand Down Expand Up @@ -270,7 +272,7 @@ GOVOLUMES= -v mimir-go-cache:/go/cache \
# Mount local ssh credentials to be able to clone private repos when doing `mod-check`
SSHVOLUME= -v ~/.ssh/:/root/.ssh:$(CONTAINER_MOUNT_OPTIONS)

exes $(EXES) $(EXES_RACE) protos $(PROTO_GOS) lint lint-gh-action lint-packaging-scripts test test-with-race cover shell mod-check check-protos doc format dist build-mixin format-mixin check-mixin-tests license check-license conftest-fmt check-conftest-fmt helm-conftest-test helm-conftest-quick-test conftest-verify check-helm-tests build-helm-tests print-go-version: fetch-build-image
exes $(EXES) $(EXES_RACE) protos $(PROTO_GOS) lint lint-gh-action lint-packaging-scripts test test-with-race cover shell mod-check check-protos doc format dist build-mixin format-mixin check-mixin-tests license check-license conftest-fmt check-conftest-fmt helm-conftest-test helm-conftest-quick-test conftest-verify check-helm-tests build-helm-tests print-go-version format-promql-tests check-promql-tests: fetch-build-image
@echo ">>>> Entering build container: $@"
$(SUDO) time docker run --rm $(TTY) -i $(SSHVOLUME) $(GOVOLUMES) $(BUILD_IMAGE) GOOS=$(GOOS) GOARCH=$(GOARCH) BINARY_SUFFIX=$(BINARY_SUFFIX) $@;

Expand Down Expand Up @@ -496,6 +498,12 @@ check-protos: ## Check the protobuf files are up to date.
check-protos: clean-protos protos
@./tools/find-diff-or-untracked.sh $(PROTO_GOS) || (echo "Please rebuild protobuf code by running 'check-protos'" && false)

format-promql-tests:
@./tools/format-promql-test.sh $(PROMQL_TESTS)

check-promql-tests: format-promql-tests
@./tools/find-diff-or-untracked.sh $(PROMQL_TESTS) || (echo "Please format PromQL test files by running 'format-promql-tests'" && false)

%.md : %.template
go run ./tools/doc-generator $< > $@

Expand Down
106 changes: 53 additions & 53 deletions pkg/streamingpromql/testdata/ours/aggregators.test
Original file line number Diff line number Diff line change
Expand Up @@ -128,44 +128,44 @@ clear

# Test native histogram aggregations
load 1m
single_histogram{label="value"} {{schema:0 sum:2 count:4 buckets:[1 2 1]}} {{sum:2 count:4 buckets:[1 2 1]}}
single_histogram{label="value2"} {{schema:1 sum:5 count:5 buckets:[1 3 1]}} {{schema:0 sum:2 count:4 buckets:[1 2 1]}} {{schema:0 sum:4 count:6 buckets:[2 2 2]}}
single_histogram{label="value2", another="value"} {{schema:1 sum:10 count:9 buckets:[3 3 3]}} {{schema:2 sum:2 count:4 buckets:[1 2 1]}}
single_histogram{label="value"} {{schema:0 sum:2 count:4 buckets:[1 2 1]}} {{sum:2 count:4 buckets:[1 2 1]}}
single_histogram{label="value2"} {{schema:1 sum:5 count:5 buckets:[1 3 1]}} {{schema:0 sum:2 count:4 buckets:[1 2 1]}} {{schema:0 sum:4 count:6 buckets:[2 2 2]}}
single_histogram{label="value2", another="value"} {{schema:1 sum:10 count:9 buckets:[3 3 3]}} {{schema:2 sum:2 count:4 buckets:[1 2 1]}}

eval instant at 0m sum(single_histogram)
{} {{schema:0 sum:17 count:18 buckets:[5 12 1]}}
{} {{schema:0 sum:17 count:18 buckets:[5 12 1]}}

eval instant at 0m avg(single_histogram)
{} {{schema:0 sum:5.666666666666667 count:6 buckets:[1.6666666666666667 4 0.33333333333333337]}}
{} {{schema:0 sum:5.666666666666667 count:6 buckets:[1.6666666666666667 4 0.33333333333333337]}}

eval instant at 0m sum by (label) (single_histogram)
{label="value"} {{schema:0 count:4 sum:2 buckets:[1 2 1]}}
{label="value2"} {{schema:1 count:14 sum:15 buckets:[4 6 4]}}
{label="value"} {{schema:0 count:4 sum:2 buckets:[1 2 1]}}
{label="value2"} {{schema:1 count:14 sum:15 buckets:[4 6 4]}}

eval instant at 0m avg by (label) (single_histogram)
{label="value"} {{count:4 sum:2 buckets:[1 2 1]}}
{label="value2"} {{schema:1 count:7 sum:7.5 buckets:[2 3 2]}}
{label="value"} {{count:4 sum:2 buckets:[1 2 1]}}
{label="value2"} {{schema:1 count:7 sum:7.5 buckets:[2 3 2]}}

# Test aggregation over a range with lookback behaviour.
eval range from 0 to 3m step 1m sum(single_histogram)
{} {{schema:0 sum:17 count:18 buckets:[5 12 1]}} {{schema:0 sum:6 count:12 buckets:[3 7 2]}} {{schema:0 sum:8 count:14 buckets:[4 7 3]}} {{schema:0 sum:8 count:14 buckets:[4 7 3]}}
{} {{schema:0 sum:17 count:18 buckets:[5 12 1]}} {{schema:0 sum:6 count:12 buckets:[3 7 2]}} {{schema:0 sum:8 count:14 buckets:[4 7 3]}} {{schema:0 sum:8 count:14 buckets:[4 7 3]}}

eval range from 2m to 3m step 1m sum(single_histogram)
{} {{schema:0 sum:8 count:14 buckets:[4 7 3]}} {{schema:0 sum:8 count:14 buckets:[4 7 3]}}
{} {{schema:0 sum:8 count:14 buckets:[4 7 3]}} {{schema:0 sum:8 count:14 buckets:[4 7 3]}}

eval range from 0 to 3m step 1m avg(single_histogram)
{} {{schema:0 sum:5.666666666666667 count:6 buckets:[1.6666666666666667 4 0.33333333333333337]}} {{schema:0 sum:2 count:4 buckets:[1 2.333333333333333 0.6666666666666666]}} {{schema:0 sum:2.6666666666666665 count:4.666666666666667 buckets:[1.3333333333333333 2.333333333333333 1]}} {{schema:0 sum:2.6666666666666665 count:4.666666666666667 buckets:[1.3333333333333333 2.333333333333333 1]}}
{} {{schema:0 sum:5.666666666666667 count:6 buckets:[1.6666666666666667 4 0.33333333333333337]}} {{schema:0 sum:2 count:4 buckets:[1 2.333333333333333 0.6666666666666666]}} {{schema:0 sum:2.6666666666666665 count:4.666666666666667 buckets:[1.3333333333333333 2.333333333333333 1]}} {{schema:0 sum:2.6666666666666665 count:4.666666666666667 buckets:[1.3333333333333333 2.333333333333333 1]}}

eval range from 2m to 3m step 1m avg(single_histogram)
{} {{schema:0 sum:2.6666666666666665 count:4.666666666666667 buckets:[1.3333333333333333 2.333333333333333 1]}} {{schema:0 sum:2.6666666666666665 count:4.666666666666667 buckets:[1.3333333333333333 2.333333333333333 1]}}
{} {{schema:0 sum:2.6666666666666665 count:4.666666666666667 buckets:[1.3333333333333333 2.333333333333333 1]}} {{schema:0 sum:2.6666666666666665 count:4.666666666666667 buckets:[1.3333333333333333 2.333333333333333 1]}}

clear

# Test a mix of float and histogram values
load 1m
single_histogram{label="value"} 0 1 {{schema:0 sum:2 count:4 buckets:[1 2 1]}} {{sum:2 count:4 buckets:[1 2 1]}} 2
single_histogram{label="value2"} 0 2 {{schema:1 sum:5 count:5 buckets:[1 3 1]}} {{schema:0 sum:2 count:4 buckets:[1 2 1]}} 4
single_histogram{label="value2", another="value"} 0 3 {{schema:1 sum:10 count:9 buckets:[3 3 3]}} {{schema:2 sum:2 count:4 buckets:[1 2 1]}} 6
single_histogram{label="value"} 0 1 {{schema:0 sum:2 count:4 buckets:[1 2 1]}} {{sum:2 count:4 buckets:[1 2 1]}} 2
single_histogram{label="value2"} 0 2 {{schema:1 sum:5 count:5 buckets:[1 3 1]}} {{schema:0 sum:2 count:4 buckets:[1 2 1]}} 4
single_histogram{label="value2", another="value"} 0 3 {{schema:1 sum:10 count:9 buckets:[3 3 3]}} {{schema:2 sum:2 count:4 buckets:[1 2 1]}} 6

# TODO: Verify if these tests are correct. At the moment they match promql engine.
# See: https://github.com/prometheus/prometheus/issues/14172
Expand All @@ -181,34 +181,34 @@ load 1m

# What both engines return
eval range from 0 to 4m step 1m sum by (label) (single_histogram)
{label="value"} 0 1 1 1 2
{label="value2"} 0 5 5 5 10
{label="value"} 0 1 1 1 2
{label="value2"} 0 5 5 5 10

eval range from 0 to 4m step 1m avg by (label) (single_histogram)
{label="value"} 0 1 1 1 2
{label="value2"} 0 2.5 2.5 2.5 5
{label="value"} 0 1 1 1 2
{label="value2"} 0 2.5 2.5 2.5 5

clear

# Test a mix of float and histogram values at the same point
load 1m
single_histogram{label="value"} 0 1 {{schema:0 sum:2 count:4 buckets:[1 2 1]}} {{sum:2 count:4 buckets:[1 2 1]}} 2 {{sum:2 count:4 buckets:[1 2 1]}}
single_histogram{label="value2"} 0 {{schema:1 sum:5 count:5 buckets:[1 3 1]}} {{schema:0 sum:2 count:4 buckets:[1 2 1]}} 4 6 {{sum:2 count:4 buckets:[1 2 1]}}
single_histogram{label="value"} 0 1 {{schema:0 sum:2 count:4 buckets:[1 2 1]}} {{sum:2 count:4 buckets:[1 2 1]}} 2 {{sum:2 count:4 buckets:[1 2 1]}}
single_histogram{label="value2"} 0 {{schema:1 sum:5 count:5 buckets:[1 3 1]}} {{schema:0 sum:2 count:4 buckets:[1 2 1]}} 4 6 {{sum:2 count:4 buckets:[1 2 1]}}

# If a float is present, the histogram is ignored.
# If a float comes after a histogram, a lookback'd float is used instead of the histogram (see: https://github.com/prometheus/prometheus/issues/14172)
eval range from 0 to 5m step 1m sum(single_histogram)
{} 0 1 1 5 8 {{sum:4 count:8 buckets:[2 4 2]}}
{} 0 1 1 5 8 {{sum:4 count:8 buckets:[2 4 2]}}

eval range from 0 to 5m step 1m avg(single_histogram)
{} 0 0.5 0.5 2.5 4 {{sum:2 count:4 buckets:[1 2 1]}}
{} 0 0.5 0.5 2.5 4 {{sum:2 count:4 buckets:[1 2 1]}}

clear

# Test a mix of float and histogram values at the same point
load 1m
single_histogram{label="value"} 3
single_histogram{label="value2"} {{sum:5 count:5 buckets:[1 3 1]}}
single_histogram{label="value"} 3
single_histogram{label="value2"} {{sum:5 count:5 buckets:[1 3 1]}}

# "If either operator has to aggregate a mix of histogram samples and float samples, the corresponding vector element is removed from the output vector entirely."
eval_warn instant at 1m sum(single_histogram)
Expand All @@ -219,8 +219,8 @@ clear

# Test a mix of float and histogram values at the same point
load 1m
single_histogram{label="value"} {{sum:5 count:5 buckets:[1 3 1]}}
single_histogram{label="value2"} 3
single_histogram{label="value"} {{sum:5 count:5 buckets:[1 3 1]}}
single_histogram{label="value2"} 3

# "If either operator has to aggregate a mix of histogram samples and float samples, the corresponding vector element is removed from the output vector entirely."
eval_warn instant at 1m sum(single_histogram)
Expand All @@ -232,9 +232,9 @@ clear
# Test a mix of float and histogram values at the same point, where after adding 2 conflicting series and removing a point,
# that a third series doesn't add the point back in.
load 1m
single_histogram{label="value"} 1
single_histogram{label="value2"} {{sum:5 count:5 buckets:[1 3 1]}}
single_histogram{label="value3"} 3
single_histogram{label="value"} 1
single_histogram{label="value2"} {{sum:5 count:5 buckets:[1 3 1]}}
single_histogram{label="value3"} 3

# "If either operator has to aggregate a mix of histogram samples and float samples, the corresponding vector element is removed from the output vector entirely."
eval_warn instant at 1m sum(single_histogram)
Expand All @@ -245,16 +245,16 @@ clear

# Test min/max aggregation with histograms and a mix of histogram+float values
load 1m
series{label="value"} -10 -10 -10 10 -10 {{schema:1 sum:10 count:5 buckets:[1 3 1]}}
series{label="value2"} 0 -9 {{schema:1 sum:5 count:5 buckets:[1 3 1]}} {{schema:1 sum:5 count:5 buckets:[1 3 1]}} {{schema:1 sum:5 count:5 buckets:[1 3 1]}} {{schema:1 sum:5 count:5 buckets:[1 3 1]}}
series{label="value3"} -20 -9 -9 {{schema:1 sum:5 count:5 buckets:[1 3 1]}} {{schema:1 sum:5 count:5 buckets:[1 3 1]}} {{schema:1 sum:5 count:5 buckets:[1 3 1]}}
histogram_only_series {{schema:1 sum:5 count:5 buckets:[1 3 1]}} {{schema:1 sum:5 count:5 buckets:[1 3 1]}} {{schema:1 sum:5 count:5 buckets:[1 3 1]}}
series{label="value"} -10 -10 -10 10 -10 {{schema:1 sum:10 count:5 buckets:[1 3 1]}}
series{label="value2"} 0 -9 {{schema:1 sum:5 count:5 buckets:[1 3 1]}} {{schema:1 sum:5 count:5 buckets:[1 3 1]}} {{schema:1 sum:5 count:5 buckets:[1 3 1]}} {{schema:1 sum:5 count:5 buckets:[1 3 1]}}
series{label="value3"} -20 -9 -9 {{schema:1 sum:5 count:5 buckets:[1 3 1]}} {{schema:1 sum:5 count:5 buckets:[1 3 1]}} {{schema:1 sum:5 count:5 buckets:[1 3 1]}}
histogram_only_series {{schema:1 sum:5 count:5 buckets:[1 3 1]}} {{schema:1 sum:5 count:5 buckets:[1 3 1]}} {{schema:1 sum:5 count:5 buckets:[1 3 1]}}

eval_info range from 0 to 5m step 1m max(series)
{} 0 -9 -9 10 -10 _
{} 0 -9 -9 10 -10 _

eval_info range from 0 to 5m step 1m min(series)
{} -20 -10 -10 10 -10 _
{} -20 -10 -10 10 -10 _

eval_info range from 0 to 2m step 1m max(histogram_only_series)

Expand All @@ -264,33 +264,33 @@ clear

# Test min/max with NaN
load 1m
mixed_series{type="float", label="value"} NaN
mixed_series{type="float", label="value2"} -5
mixed_series{type="float", label="value3"} 10
mixed_series{type="histogram", label="value"} {{schema:1 sum:5 count:5 buckets:[1 3 1]}}
mixed_series{type="histogram", label="value2"} NaN
mixed_series{type="float", label="value"} NaN
mixed_series{type="float", label="value2"} -5
mixed_series{type="float", label="value3"} 10
mixed_series{type="histogram", label="value"} {{schema:1 sum:5 count:5 buckets:[1 3 1]}}
mixed_series{type="histogram", label="value2"} NaN
mixed_series{type="onlyNaN"} NaN

eval_info instant at 1m min by (type) (mixed_series)
{type="float"} -5
{type="histogram"} NaN
{type="float"} -5
{type="histogram"} NaN
{type="onlyNaN"} NaN

eval_info instant at 1m max by (type) (mixed_series)
{type="float"} 10
{type="histogram"} NaN
{type="float"} 10
{type="histogram"} NaN
{type="onlyNaN"} NaN

clear

# Test native histogram compaction
load 1m
single_histogram{label="value"} {{schema:1 sum:5 count:5 buckets:[1 3 1 4 0 2]}}
single_histogram{label="value2"} {{schema:1 sum:5 count:5 buckets:[1 3 1]}}
single_histogram{label="value"} {{schema:1 sum:5 count:5 buckets:[1 3 1 4 0 2]}}
single_histogram{label="value2"} {{schema:1 sum:5 count:5 buckets:[1 3 1]}}

# Compaction will happen in this sum. It will fail without it.
eval instant at 1m sum(single_histogram)
{} {{schema:1 count:10 sum:10 buckets:[2 6 2 4 0 2]}}
{} {{schema:1 count:10 sum:10 buckets:[2 6 2 4 0 2]}}

clear

Expand All @@ -307,8 +307,8 @@ eval range from 0m to 10m step 5m sum(native_histogram)
clear

load 1m
series{label="a"} {{schema:5 sum:15 count:10 buckets:[3 2 5]}} {{schema:5 sum:20 count:15 buckets:[4 5 6]}} {{schema:5 sum:25 count:20 buckets:[5 7 8]}} {{schema:5 sum:30 count:25 buckets:[6 9 10]}} {{schema:5 sum:35 count:30 buckets:[7 10 13]}}
series{label="b"} {{schema:4 sum:12 count:8 buckets:[2 3 3]}} {{schema:4 sum:14 count:9 buckets:[3 3 3]}} _ {{schema:4 sum:16 count:10 buckets:[3 4 3]}} {{schema:4 sum:18 count:11 buckets:[4 4 3]}}
series{label="a"} {{schema:5 sum:15 count:10 buckets:[3 2 5]}} {{schema:5 sum:20 count:15 buckets:[4 5 6]}} {{schema:5 sum:25 count:20 buckets:[5 7 8]}} {{schema:5 sum:30 count:25 buckets:[6 9 10]}} {{schema:5 sum:35 count:30 buckets:[7 10 13]}}
series{label="b"} {{schema:4 sum:12 count:8 buckets:[2 3 3]}} {{schema:4 sum:14 count:9 buckets:[3 3 3]}} _ {{schema:4 sum:16 count:10 buckets:[3 4 3]}} {{schema:4 sum:18 count:11 buckets:[4 4 3]}}

eval range from 0m to 4m step 1m avg(series)
{} {{schema:4 count:9 sum:13.5 buckets:[2.5 5 1.5]}} {{schema:4 count:12 sum:17 buckets:[3.5 7 1.5]}} {{schema:4 count:14.5 sum:19.5 buckets:[4 9 1.5]}} {{schema:4 count:17.5 sum:23 buckets:[4.5 11.5 1.5]}} {{schema:4 count:20.5 sum:26.5 buckets:[5.5 13.5 1.5]}}
Expand All @@ -317,8 +317,8 @@ clear

# Make sure count and group don't emit a 0 when there are no values
load 1m
series{label="a"} _ 9
series{label="b"} _ 9
series{label="a"} _ 9
series{label="b"} _ 9

eval range from 0m to 1m step 1m count(series)
{} _ 2
Expand Down
26 changes: 13 additions & 13 deletions pkg/streamingpromql/testdata/ours/functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -301,18 +301,18 @@ clear
# Otherwise they fail both engines.

load 5m
metric_total{env="1"} 0 60 120
another_metric_total{env="1"} 60 120 180
metric_total{env="1"} 0 60 120
another_metric_total{env="1"} 60 120 180

# Allows relabeling (to-be-dropped) __name__ via label_join.
eval_fail instant at 10m label_join(rate({env="1"}[10m]), "my_name", "_", "__name__")
expected_fail_message vector cannot contain metrics with the same labelset
expected_fail_message vector cannot contain metrics with the same labelset
# {my_name="metric_total", env="1"} 0.2
# {my_name="another_metric_total", env="1"} 0.2

# Allows preserving __name__ via label_join.
eval_fail instant at 10m label_join(rate({env="1"}[10m]), "__name__", "_", "__name__", "env")
expected_fail_message vector cannot contain metrics with the same labelset
expected_fail_message vector cannot contain metrics with the same labelset
# metric_total_1{env="1"} 0.2
# another_metric_total_1{env="1"} 0.2

Expand Down Expand Up @@ -507,13 +507,13 @@ eval range from 0 to 60m step 5m clamp_max(series, scalar(maxes))

# clamp ignores any histograms
eval range from 0 to 15m step 5m clamp(mixed_metric, 2, 5)
{} _ 2 2 3
{} _ 2 2 3

eval range from 0 to 15m step 5m clamp_min(mixed_metric, 2)
{} _ 2 2 3
{} _ 2 2 3

eval range from 0 to 15m step 5m clamp_max(mixed_metric, 2)
{} _ 1 2 2
{} _ 1 2 2

clear

Expand Down Expand Up @@ -543,7 +543,7 @@ eval range from 0 to 24m step 6m round(series, scalar(toNearest))

# round ignores any histograms
eval range from 0 to 18m step 6m round(mixed_metric)
{} _ 1 2 3
{} _ 1 2 3

clear

Expand Down Expand Up @@ -751,7 +751,7 @@ clear

# Test time-related functions.
load 5m
histogram_sample {{schema:0 sum:1 count:1}}
histogram_sample {{schema:0 sum:1 count:1}}

eval range from 0 to 7m step 1m year()
{} 1970 1970 1970 1970 1970 1970 1970 1970
Expand Down Expand Up @@ -1079,9 +1079,9 @@ load 6m
series{case="0a"} _x11
series{case="0b"} _x11
series{case="0"} 0x11
series{case="a"} _ 1 _ 1 _ 1 _ 1 _ 1 _
series{case="b"} _ _ 2 _ 2 _ 2 _ 2 _ 2
series{case="c"} _ _ _ 3 _ _ _ 3 _ _ _
series{case="a"} _ 1 _ 1 _ 1 _ 1 _ 1 _
series{case="b"} _ _ 2 _ 2 _ 2 _ 2 _ 2
series{case="c"} _ _ _ 3 _ _ _ 3 _ _ _

eval instant at 0m absent(series)
# expected no result
Expand Down Expand Up @@ -1146,6 +1146,6 @@ eval range from 0s to 66m step 6m absent(histo{case="a"})
{case="a"} 1

eval range from 0s to 66m step 6m absent(histo{case=~"(0|a)"})
{} 1
{} 1

clear
Loading

0 comments on commit acf266f

Please sign in to comment.