feat: add promtool validation for golden files#8216
feat: add promtool validation for golden files#8216simonpasquier merged 4 commits intoprometheus-operator:mainfrom
Conversation
Hi @Arpit529Srivastava 👋🏻 Thank you for the PR, I will take a look at this today Since some of the work is done in #7381 can you add @heliapb as |
sure. |
87dcc59 to
94d92d1
Compare
|
Can we add this to the Makefile so the check is also performed locally? E.g. running |
|
@simonpasquier , i'll add a |
.github/workflows/promtool.yaml
Outdated
| push: | ||
| branches: | ||
| - 'release-*' | ||
| - 'master' |
There was a problem hiding this comment.
nit: let's remove master branch as we don't use it
pkg/prometheus/promtool_test.go
Outdated
| } | ||
|
|
||
| for _, file := range goldenFiles { | ||
| t.Run(filepath.Base(file), func(t *testing.T) { |
There was a problem hiding this comment.
Since each golden file validation is independent, lets add t.Parallel() after the Run()
pkg/prometheus/promtool_test.go
Outdated
| return | ||
| } | ||
|
|
||
| tmpFile, err := os.CreateTemp("", "promtool-*.yaml") |
There was a problem hiding this comment.
Instead of manually doing this we can use testing package's TempDir which will do autoclean up as well
tmpDir := t.TempDir()
tmpFile := filepath.Join(tmpDir, "config.yaml")
if err := os.WriteFile(tmpFile, content, 0644); err != nil {
t.Fatalf("failed to write temp file: %v", err)
}
cmd := exec.Command("promtool", "check", "config", tmpFile)
output, err := cmd.CombinedOutput()
if err != nil {
t.Errorf("promtool validation failed:\n%s", string(output))
}
pkg/prometheus/promtool_test.go
Outdated
| } | ||
|
|
||
| // Skip partial configs that are not complete Prometheus configs. | ||
| if !bytes.HasPrefix(bytes.TrimSpace(content), []byte("global:")) { |
There was a problem hiding this comment.
t.Skipf already causes test to skip
| if !bytes.HasPrefix(bytes.TrimSpace(content), []byte("global:")) { | |
| if !bytes.HasPrefix(bytes.TrimSpace(content), []byte("global:")) { | |
| t.Skipf("skipping partial config: %s", file) | |
| } |
.github/workflows/promtool.yaml
Outdated
| check-latest: true | ||
| - name: Install promtool | ||
| run: | | ||
| PROMETHEUS_VERSION="3.0.0" |
There was a problem hiding this comment.
We may need a step to extract latest from compatibility matrix
94d92d1 to
54ed4a2
Compare
54ed4a2 to
7aa477d
Compare
|
@slashpai made the requested changes. PTAL |
.github/workflows/promtool.yaml
Outdated
| check-latest: true | ||
| - name: Install promtool | ||
| run: | | ||
| # Extract latest Prometheus version from compatibility matrix |
There was a problem hiding this comment.
The steps in this could go as a Makefile target something say test-goldenfiles so we can utilize it locally also and in actions can call the make command directly
There was a problem hiding this comment.
done with the changes.
7aa477d to
4e1d149
Compare
|
@slashpai PTAL |
Makefile
Outdated
|
|
||
| .PHONY: test-goldenfiles | ||
| test-goldenfiles: | ||
| @command -v promtool >/dev/null 2>&1 || { \ |
There was a problem hiding this comment.
I would follow the same pattern we have for other "tools" and add github.com/prometheus/prometheus/cmd/promtool to scripts/tools.go.
Makefile
Outdated
| sudo mv "prometheus-$${VERSION_NUM}.linux-amd64/promtool" /usr/local/bin/; \ | ||
| rm -rf prometheus-*; \ | ||
| } | ||
| go test -v -tags=promtool ./pkg/prometheus/ -run TestPromtoolGoldenFiles |
There was a problem hiding this comment.
can we simply run promtool check config pkg/prometheus/testdata/*?
There was a problem hiding this comment.
i’ve added the test-goldenfiles makefile target and updated tools.go as requested. about removing promtool_test.go: running promtool on all files fails for partial configs. i can drop the test file and instead use grep in the makefile to only run it on valid configs (starting with global:). does that sound better than keeping the test?
There was a problem hiding this comment.
What if we add a marker comment to the golden file to signal that it's not a Prometheus config file?
#skip-promtool-verification
Another option is to move the few problematic files to a sub-directory under testdata/.
We also have a few tests calling generateK8SSDConfig() which could be updated to call GenerateServerConfiguration() instead.
|
@slashpai @simonpasquier |
let me know if anything else needs changes. |
Makefile
Outdated
| ./scripts/update-golden-files.sh | ||
|
|
||
| .PHONY: test-goldenfiles | ||
| test-goldenfiles: $(TOOLS_BIN_DIR)/promtool |
Makefile
Outdated
| ./scripts/update-golden-files.sh | ||
|
|
||
| .PHONY: test-goldenfiles | ||
| test-goldenfiles: $(TOOLS_BIN_DIR)/promtool |
There was a problem hiding this comment.
| test-goldenfiles: $(TOOLS_BIN_DIR)/promtool | |
| test-goldenfiles: $(PROMTOOL_BINARY) |
Makefile
Outdated
| .PHONY: test-goldenfiles | ||
| test-goldenfiles: $(TOOLS_BIN_DIR)/promtool | ||
| @echo "Validating golden files with promtool..." | ||
| -@$(TOOLS_BIN_DIR)/promtool check config pkg/prometheus/testdata/*.golden 2>&1 | tee /dev/stderr | grep -c "SUCCESS" | xargs -I {} echo "{} golden files passed promtool validation" |
There was a problem hiding this comment.
| -@$(TOOLS_BIN_DIR)/promtool check config pkg/prometheus/testdata/*.golden 2>&1 | tee /dev/stderr | grep -c "SUCCESS" | xargs -I {} echo "{} golden files passed promtool validation" | |
| -$(PROMTOOL_BINARY) check config pkg/prometheus/testdata/*.golden 2>&1 | tee /dev/stderr | grep -c "SUCCESS" | xargs -I {} echo "{} golden files passed promtool validation" |
.github/workflows/promtool.yaml
Outdated
| jobs: | ||
| changed-files: | ||
| uses: ./.github/workflows/changed-files.yaml | ||
| promtool-check: |
There was a problem hiding this comment.
we can add this as new job check-goldenfiles in existing workflow checks.yaml
|
Can you also add about the new make file target |
Makefile
Outdated
| test-unit-update-golden: | ||
| ./scripts/update-golden-files.sh | ||
|
|
||
| .PHONY: test-goldenfiles |
There was a problem hiding this comment.
Since only testing prometheus golden files here may be name it as test-prometheus-goldenfiles and later when other packages are added create test-goldenfiles as aggregate target
There was a problem hiding this comment.
addressed all the requested changes
|
@slashpai PTAL. |
Makefile
Outdated
| -$(PROMTOOL_BINARY) check config pkg/prometheus/testdata/*.golden 2>&1 | tee /dev/stderr | grep -c "SUCCESS" | xargs -I {} echo "{} golden files passed promtool validation" | ||
| @echo "Note: Some failures are expected for version-specific or intentionally invalid test fixtures" | ||
|
|
||
| $(PROMTOOL_BINARY): scripts/go.mod scripts/go.sum scripts/tools.go |
There was a problem hiding this comment.
nit: do we need this?
I am not sure other tools works without it right?
There was a problem hiding this comment.
you're right - it's not needed.
removed it.
slashpai
left a comment
There was a problem hiding this comment.
lgtm
I would have @simonpasquier also take a look
thanks for the guidance @simonpasquier @slashpai! here’s my plan: bug fixes
test migrations (convert partials to full configs)
final
i’ll start with the |
Signed-off-by: arpit529srivastava <arpitsrivastava529@gmail.com>
cf2ba79 to
505a3fd
Compare
Signed-off-by: arpit529srivastava <arpitsrivastava529@gmail.com>
|
@slashpai @heliapb @simonpasquier when you get a chance, could you do a final review? From my side it looks like the promtool issue is resolved. |
|
Good work on this |
|
@simonpasquier a gentle ping, thanks :) |
TESTING.md
Outdated
| make test-prometheus-goldenfiles | ||
| ``` | ||
|
|
||
| **Note:** Files in `pkg/prometheus/testdata/input/` and `pkg/prometheus/testdata/legacy-versions/` are excluded from validation as they contain configuration fragments or version-specific test data. |
There was a problem hiding this comment.
(nit) maybe have a more generic stance since we have more than these 2 directories (e.g. testdata subdirectories aren't checked because they contain either configuration fragments or legacy configurations).
Makefile
Outdated
| test-prometheus-goldenfiles: $(TOOLS_BIN_DIR)/promtool | ||
| $(TOOLS_BIN_DIR)/promtool check config --syntax-only pkg/prometheus/testdata/*.golden | ||
|
|
||
| $(TOOLS_BIN_DIR)/promtool: $(TOOLS_BIN_DIR) |
There was a problem hiding this comment.
can we use the same mechanism as we have for other tools (e.g. add the command to scripts/tools.go)?
There was a problem hiding this comment.
adding promtool to tools.go will significantly increase the dependency footprint in go.mod (roughly 100+ extra packages). cuz prometheus pulls in a wide range of service discovery clients and integrations for multiple cloud providers like aws, azure, gcp, digital ocean, hetzner and others.
There was a problem hiding this comment.
that's why i was initially hesitant to add it to tool.go and went with the binary download approach instead.
There was a problem hiding this comment.
should i go ahead and add it to tools.go for consistency with the other tools, or would you prefer to stick with the binary download approach? or if i am missing anything 🤔
There was a problem hiding this comment.
the additional dependencies will only show up in tools/go.mod which is acceptable.
There was a problem hiding this comment.
okay, done.
Signed-off-by: arpit529srivastava <arpitsrivastava529@gmail.com>
Signed-off-by: arpit529srivastava <arpitsrivastava529@gmail.com>
|
@slashpai @simonpasquier final review and then ig we are good to go?.. |
) * feat: add promtool validation for golden files Signed-off-by: arpit529srivastava <arpitsrivastava529@gmail.com> * fix: add goldenfiles and testdata to cspell dictionary Signed-off-by: arpit529srivastava <arpitsrivastava529@gmail.com> * fix: clarify note on excluded files in golden file validation Signed-off-by: arpit529srivastava <arpitsrivastava529@gmail.com> * refactor: use tools.go pattern for promtool Signed-off-by: arpit529srivastava <arpitsrivastava529@gmail.com> --------- Signed-off-by: arpit529srivastava <arpitsrivastava529@gmail.com>
Description
This PR adds promtool validation for golden files to catch invalid Prometheus configurations early. currently unit tests only compare generated configs against expected golden files (string comparison) but don't validate if the config is actually valid according to Prometheus schema.
This would have caught bugs like #7196 where
fallback_scrape_protocolwas incorrectly placed in the global config section.Closes: #7201
If you're contributing for the first-time, check our contribution guidelines.
Type of change
What type of changes does your code introduce to the Prometheus operator? Put an
xin the box that apply.CHANGE(fix or feature that would cause existing functionality to not work as expected)FEATURE(non-breaking change which adds functionality)BUGFIX(non-breaking change which fixes an issue)ENHANCEMENT(non-breaking change which improves existing functionality)NONE(if none of the other choices apply. Example, tooling, build system, CI, docs, etc.)Verification
Please check the Prometheus-Operator testing guidelines for recommendations about automated tests.
Changelog entry