MCPcopy
hub / github.com/open-policy-agent/gatekeeper / TestRun_WithGatherStats

Function TestRun_WithGatherStats

pkg/gator/bench/bench_test.go:361–427  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

359}
360
361func TestRun_WithGatherStats(t *testing.T) {
362 tmpDir := t.TempDir()
363
364 // Write template
365 templateFile := filepath.Join(tmpDir, "template.yaml")
366 err := os.WriteFile(templateFile, []byte(`
367apiVersion: templates.gatekeeper.sh/v1
368kind: ConstraintTemplate
369metadata:
370 name: k8srequiredlabels
371spec:
372 crd:
373 spec:
374 names:
375 kind: K8sRequiredLabels
376 targets:
377 - target: admission.k8s.gatekeeper.sh
378 rego: |
379 package k8srequiredlabels
380 violation[{"msg": msg}] {
381 msg := "test"
382 }
383`), 0o600)
384 if err != nil {
385 t.Fatalf("failed to write template file: %v", err)
386 }
387
388 // Write constraint
389 constraintFile := filepath.Join(tmpDir, "constraint.yaml")
390 err = os.WriteFile(constraintFile, []byte(`
391apiVersion: constraints.gatekeeper.sh/v1beta1
392kind: K8sRequiredLabels
393metadata:
394 name: require-team-label
395`), 0o600)
396 if err != nil {
397 t.Fatalf("failed to write constraint file: %v", err)
398 }
399
400 // Write object
401 objectFile := filepath.Join(tmpDir, "pod.yaml")
402 err = os.WriteFile(objectFile, []byte(`
403apiVersion: v1
404kind: Pod
405metadata:
406 name: test-pod
407`), 0o600)
408 if err != nil {
409 t.Fatalf("failed to write object file: %v", err)
410 }
411
412 // Run with GatherStats enabled
413 results, err := Run(&Opts{
414 Filenames: []string{tmpDir},
415 Iterations: 2,
416 Warmup: 0,
417 Engine: EngineRego,
418 GatherStats: true,

Callers

nothing calls this directly

Calls 1

RunFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…