Skip to content

feat: add promtool validation for golden files#8216

Merged
simonpasquier merged 4 commits intoprometheus-operator:mainfrom
Arpit529Srivastava:fix/promtool-7201
Feb 2, 2026
Merged

feat: add promtool validation for golden files#8216
simonpasquier merged 4 commits intoprometheus-operator:mainfrom
Arpit529Srivastava:fix/promtool-7201

Conversation

@Arpit529Srivastava
Copy link
Contributor

@Arpit529Srivastava Arpit529Srivastava commented Dec 26, 2025

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_protocol was 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 x in 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.

# Run locally (requires promtool installed)
go test -v -tags=promtool ./pkg/prometheus/ -run TestPromtoolGoldenFiles

Changelog entry

[FEATURE] Add promtool validation for golden files to catch invalid Prometheus configurations early. (#7201)


@Arpit529Srivastava Arpit529Srivastava requested a review from a team as a code owner December 26, 2025 22:05
@Arpit529Srivastava
Copy link
Contributor Author

cc: @slashpai
the ci failure for promtool is expected as it is catching the issue. some golden files are test fixtures with intentionally invalid data.
should i skip known test fixtures or should these golden files be fixed?

@slashpai
Copy link
Contributor

slashpai commented Jan 5, 2026

cc: @slashpai the ci failure for promtool is expected as it is catching the issue. some golden files are test fixtures with intentionally invalid data. should i skip known test fixtures or should these golden files be fixed?

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 Co-Authored By to the PR commit?

@Arpit529Srivastava
Copy link
Contributor Author

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 Co-Authored By to the PR commit?

sure.

@simonpasquier
Copy link
Contributor

Can we add this to the Makefile so the check is also performed locally? E.g. running make tests-unit or make test-long should have a dependency which verifies the validity of the golden files.

@Arpit529Srivastava
Copy link
Contributor Author

@simonpasquier , i'll add a test-promtool target. should it be a dependency of test-long or test-unit?

push:
branches:
- 'release-*'
- 'master'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: let's remove master branch as we don't use it

}

for _, file := range goldenFiles {
t.Run(filepath.Base(file), func(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since each golden file validation is independent, lets add t.Parallel() after the Run()

return
}

tmpFile, err := os.CreateTemp("", "promtool-*.yaml")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))
	}

}

// Skip partial configs that are not complete Prometheus configs.
if !bytes.HasPrefix(bytes.TrimSpace(content), []byte("global:")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t.Skipf already causes test to skip

Suggested change
if !bytes.HasPrefix(bytes.TrimSpace(content), []byte("global:")) {
if !bytes.HasPrefix(bytes.TrimSpace(content), []byte("global:")) {
t.Skipf("skipping partial config: %s", file)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

check-latest: true
- name: Install promtool
run: |
PROMETHEUS_VERSION="3.0.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may need a step to extract latest from compatibility matrix

DefaultPrometheusVersion = PrometheusCompatibilityMatrix[len(PrometheusCompatibilityMatrix)-1]

@Arpit529Srivastava
Copy link
Contributor Author

@slashpai made the requested changes. PTAL
let me know if i miss anything.
Thank you.

check-latest: true
- name: Install promtool
run: |
# Extract latest Prometheus version from compatibility matrix
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done with the changes.

@Arpit529Srivastava
Copy link
Contributor Author

@slashpai PTAL

Makefile Outdated

.PHONY: test-goldenfiles
test-goldenfiles:
@command -v promtool >/dev/null 2>&1 || { \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we simply run promtool check config pkg/prometheus/testdata/*?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Arpit529Srivastava
Copy link
Contributor Author

@slashpai @simonpasquier
using skip markers breaks existing golden tests and fixing the tests would require significant refactoring to make partial configs valid. moving the partial configs is the cleanest approach. i’ll move the 12 partial config files to pkg/prometheus/testdata/partial/ and update their references in promcfg_test.go.
thank u

@Arpit529Srivastava
Copy link
Contributor Author

@slashpai @simonpasquier

  • added promtool to scripts/tools.go
  • replaced the test with promtool check config in the Makefile
  • moved partial configs to testdata/partial/

let me know if anything else needs changes.
thank you.

Makefile Outdated
./scripts/update-golden-files.sh

.PHONY: test-goldenfiles
test-goldenfiles: $(TOOLS_BIN_DIR)/promtool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets add PROMTOOL_BINARY=$(TOOLS_BIN_DIR)/promtool after

PROMLINTER_BINARY=$(TOOLS_BIN_DIR)/promlinter
to follow approach as other tools and include it in
TOOLING=$(CONTROLLER_GEN_BINARY) $(GOBINDATA_BINARY) $(JB_BINARY) $(GOJSONTOYAML_BINARY) $(JSONNET_BINARY) $(JSONNETFMT_BINARY) $(SHELLCHECK_BINARY) $(PROMLINTER_BINARY) $(GOLANGCILINTER_BINARY) $(MDOX_BINARY) $(API_DOC_GEN_BINARY) $(GOLANGCIKUBEAPILINTER_BINARY)

Makefile Outdated
./scripts/update-golden-files.sh

.PHONY: test-goldenfiles
test-goldenfiles: $(TOOLS_BIN_DIR)/promtool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
-@$(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"

jobs:
changed-files:
uses: ./.github/workflows/changed-files.yaml
promtool-check:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can add this as new job check-goldenfiles in existing workflow checks.yaml

@slashpai
Copy link
Contributor

slashpai commented Jan 9, 2026

Can you also add about the new make file target make test-goldenfiles in TESTING.md explaining its use and partial configs as well.

Makefile Outdated
test-unit-update-golden:
./scripts/update-golden-files.sh

.PHONY: test-goldenfiles
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed all the requested changes

@Arpit529Srivastava
Copy link
Contributor Author

@slashpai PTAL.
Thank you for addressing.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: do we need this?

I am not sure other tools works without it right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right - it's not needed.
removed it.

slashpai
slashpai previously approved these changes Jan 9, 2026
Copy link
Contributor

@slashpai slashpai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

I would have @simonpasquier also take a look

@Arpit529Srivastava
Copy link
Contributor Author

I'd prefer to enforce strict validation first (as I wrote earlier I already spotted a bug about gce_sd_configs). To avoid the PR growing too much, I'd be ok if you fix any issue found by promtool in separate PRs which we can merge before this one.

For partials, I'd also recommend that we update the existing tests to build full configurations which could be passed to promtool. You can also submit individual PRs to migrate the tests first.

Again thanks for the work, I think that it's very valuable!

thanks for the guidance @simonpasquier @slashpai!

here’s my plan:

bug fixes

  • pr1: fix the gce_sd_configgce_sd_configs bug

test migrations (convert partials to full configs)

  • pr2: alertmanager config tests (~10 files)
  • pr3: sd config tests (k8s, azure, etc.) (~30 files)
  • pr4: remotewrite config tests (~10 files)
  • pr5: servicemonitor / podmonitor tests (~15 files)
  • pr6: remaining tests (~15 files)

final

  • this pr: enable strict validation once the above are merged

i’ll start with the gce_sd_configs fix and send it shortly.

Signed-off-by: arpit529srivastava <arpitsrivastava529@gmail.com>
Signed-off-by: arpit529srivastava <arpitsrivastava529@gmail.com>
@Arpit529Srivastava
Copy link
Contributor Author

@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.
thanks :)

@slashpai
Copy link
Contributor

Good work on this
lgtm

slashpai
slashpai previously approved these changes Jan 29, 2026
@Arpit529Srivastava
Copy link
Contributor Author

@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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use the same mechanism as we have for other tools (e.g. add the command to scripts/tools.go)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's why i was initially hesitant to add it to tool.go and went with the binary download approach instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the additional dependencies will only show up in tools/go.mod which is acceptable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, done.

Signed-off-by: arpit529srivastava <arpitsrivastava529@gmail.com>
Signed-off-by: arpit529srivastava <arpitsrivastava529@gmail.com>
@pull-request-size pull-request-size bot added size/L and removed size/M labels Jan 30, 2026
@Arpit529Srivastava
Copy link
Contributor Author

@slashpai @simonpasquier final review and then ig we are good to go?..
thanks for reviewing :)

Copy link
Contributor

@simonpasquier simonpasquier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@simonpasquier simonpasquier merged commit 47c8b93 into prometheus-operator:main Feb 2, 2026
23 checks passed
@Arpit529Srivastava Arpit529Srivastava deleted the fix/promtool-7201 branch February 2, 2026 15:17
nutmos pushed a commit to nutmos/prometheus-operator that referenced this pull request Feb 14, 2026
)

* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Explore the possibility of using promtool in unit tests

3 participants