Skip to content

Commit 3e71596

Browse files
committed
policy-test: init test runner
1 parent dc4af5e commit 3e71596

13 files changed

Lines changed: 1266 additions & 2 deletions

File tree

cli/cmd/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func runGenerate(cmd *cobra.Command, args []string) error {
225225
return nil
226226
})
227227
}
228-
if err := manipulateInitdata(fileMap, initdataManipulators...); err != nil {
228+
if err := ManipulateInitdata(fileMap, initdataManipulators...); err != nil {
229229
return fmt.Errorf("manipulate initdata: %w", err)
230230
}
231231

cli/cmd/policies.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import (
1717
applymetav1 "k8s.io/client-go/applyconfigurations/meta/v1"
1818
)
1919

20-
func manipulateInitdata(fileMap map[string][]*unstructured.Unstructured, manipulators ...func(*initdata.Initdata) error) error {
20+
// ManipulateInitdata applies the given manipulators to the initdata of all workloads in the given file map.
21+
func ManipulateInitdata(fileMap map[string][]*unstructured.Unstructured, manipulators ...func(*initdata.Initdata) error) error {
2122
return mapContrastWorkloads(fileMap, func(res any, path string, _ int) (resource any, retErr error) {
2223
return kuberesource.MapPodSpecWithMeta(res, func(meta *applymetav1.ObjectMetaApplyConfiguration, spec *applycorev1.PodSpecApplyConfiguration) (*applymetav1.ObjectMetaApplyConfiguration, *applycorev1.PodSpecApplyConfiguration) {
2324
if meta == nil {

go.work

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use (
66
./imagepuller
77
./imagestore
88
./initdata-processor
9+
./policy-test
910
./service-mesh
1011
./tools/debugshell
1112
./tools/fifo

justfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,13 @@ lint:
554554
unit:
555555
CGO_ENABLED=1 go test -tags=contrast_unstable_api -v -race ./...
556556
557+
# Run the policy test suite.
558+
policy: initializer
559+
#!/usr/bin/env bash
560+
set -euo pipefail
561+
echo "Running policy tests..."
562+
nix run -L .#base.policy-test -- --image-replacements ./{{ workspace_dir }}/just.containerlookup
563+
557564
# Check links.
558565
check-links config="external":
559566
nix run .#base.nixpkgs.lychee -- --config tools/lychee/config-{{ config }}.toml .
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2026 Edgeless Systems GmbH
2+
# SPDX-License-Identifier: BUSL-1.1
3+
4+
{
5+
lib,
6+
buildGoModule,
7+
contrast,
8+
}:
9+
10+
buildGoModule (_finalAttrs: {
11+
pname = "policy-test";
12+
version = builtins.readFile ../../../version.txt;
13+
14+
inherit (contrast.cli)
15+
preConfigure
16+
postConfigure
17+
;
18+
19+
src =
20+
let
21+
inherit (lib) fileset path;
22+
root = ../../../.;
23+
in
24+
fileset.toSource {
25+
inherit root;
26+
fileset = fileset.unions [
27+
(path.append root "go.mod")
28+
(path.append root "go.sum")
29+
(fileset.difference (path.append root "policy-test") (path.append root "policy-test/testdata"))
30+
(path.append root "cli")
31+
(path.append root "internal")
32+
(path.append root "sdk")
33+
(path.append root "apitypes")
34+
];
35+
};
36+
37+
proxyVendor = true;
38+
vendorHash = "sha256-Cob7OieQcJBSc4L4j9X2vi0AYaPU4JdB9Jt8QhIJSk4=";
39+
40+
# The preConfigure and postConfigure hooks from the cli package work on the repository root, so setting
41+
# either sourceRoot or modRoot would break the build. Instead, cd into the subdir here.
42+
preBuild = ''
43+
cd policy-test
44+
'';
45+
46+
env.CGO_ENABLED = 0;
47+
dontFixup = true;
48+
49+
ldflags = [
50+
"-s"
51+
];
52+
53+
tags = [ "contrast_unstable_api" ];
54+
55+
meta = lib.contrast.ourMeta { mainProgram = "policy-test"; };
56+
})

packages/scripts.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
nix-update --version=skip --flake legacyPackages.x86_64-linux.base.imagepuller-benchmark
6969
echo "Updating vendorHash of kernelconfig package" >&2
7070
nix-update --version=skip --flake legacyPackages.x86_64-linux.base.kernelconfig
71+
echo "Updating vendorHash of policy-test package" >&2
72+
nix-update --version=skip --flake legacyPackages.x86_64-linux.base.policy-test
7173
echo "Updating src hash of kata.release-tarball" >&2
7274
./packages/by-name/kata/release-tarball/update.sh
7375

policy-test/assets/pod.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: test
5+
labels:
6+
# Contrast generate expects at least one Coordinator policy hash
7+
contrast.edgeless.systems/pod-role: coordinator
8+
spec:
9+
containers:
10+
- name: test
11+
image: busybox@sha256:dc2d74b28e4cf8984fa52af1f39bc7c3d9c73760b41a74d629f5d11b1ab28616
12+
command: ["sh", "-c", "sleep inf"]
13+
restartPolicy: Always
14+
runtimeClassName: contrast-cc

policy-test/go.mod

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
module github.com/edgelesssys/contrast/policy-test
2+
3+
go 1.25.6
4+
5+
replace github.com/edgelesssys/contrast => ../
6+
7+
replace github.com/google/go-tdx-guest => github.com/edgelesssys/go-tdx-guest v0.0.0-20260625102850-ea481d3db249
8+
9+
replace github.com/google/go-sev-guest => github.com/edgelesssys/go-sev-guest v0.0.0-20260729130516-c98bf131aac5
10+
11+
require (
12+
github.com/edgelesssys/contrast v1.22.0
13+
github.com/evanphx/json-patch/v5 v5.9.11
14+
github.com/open-policy-agent/opa v1.18.2
15+
github.com/spf13/cobra v1.10.2
16+
k8s.io/apimachinery v0.35.2
17+
)
18+
19+
require (
20+
filippo.io/nistec v0.0.4 // indirect
21+
github.com/agnivade/levenshtein v1.2.1 // indirect
22+
github.com/beorn7/perks v1.0.1 // indirect
23+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
24+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
25+
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.1 // indirect
26+
github.com/distribution/reference v0.6.0 // indirect
27+
github.com/emicklei/go-restful/v3 v3.13.0 // indirect
28+
github.com/fxamacker/cbor/v2 v2.9.2 // indirect
29+
github.com/go-logr/logr v1.4.3 // indirect
30+
github.com/go-openapi/jsonpointer v0.23.1 // indirect
31+
github.com/go-openapi/jsonreference v0.21.6 // indirect
32+
github.com/go-openapi/swag v0.26.1 // indirect
33+
github.com/go-openapi/swag/cmdutils v0.26.1 // indirect
34+
github.com/go-openapi/swag/conv v0.26.1 // indirect
35+
github.com/go-openapi/swag/fileutils v0.26.1 // indirect
36+
github.com/go-openapi/swag/jsonname v0.26.1 // indirect
37+
github.com/go-openapi/swag/jsonutils v0.26.1 // indirect
38+
github.com/go-openapi/swag/loading v0.26.1 // indirect
39+
github.com/go-openapi/swag/mangling v0.26.1 // indirect
40+
github.com/go-openapi/swag/netutils v0.26.1 // indirect
41+
github.com/go-openapi/swag/stringutils v0.26.1 // indirect
42+
github.com/go-openapi/swag/typeutils v0.26.1 // indirect
43+
github.com/go-openapi/swag/yamlutils v0.26.1 // indirect
44+
github.com/gobwas/glob v0.2.3 // indirect
45+
github.com/goccy/go-json v0.10.6 // indirect
46+
github.com/google/gnostic-models v0.7.1 // indirect
47+
github.com/google/go-containerregistry v0.21.2 // indirect
48+
github.com/google/go-sev-guest v0.14.2-0.20251119154202-af1c107a648f // indirect
49+
github.com/google/go-tdx-guest v0.3.2-0.20260104162950-32866d7a678f // indirect
50+
github.com/google/logger v1.1.2 // indirect
51+
github.com/google/uuid v1.6.0 // indirect
52+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
53+
github.com/json-iterator/go v1.1.12 // indirect
54+
github.com/lestrrat-go/blackmagic v1.0.4 // indirect
55+
github.com/lestrrat-go/dsig v1.2.1 // indirect
56+
github.com/lestrrat-go/dsig-secp256k1 v1.0.0 // indirect
57+
github.com/lestrrat-go/httpcc v1.0.1 // indirect
58+
github.com/lestrrat-go/httprc/v3 v3.0.5 // indirect
59+
github.com/lestrrat-go/jwx/v3 v3.1.1 // indirect
60+
github.com/lestrrat-go/option/v2 v2.0.0 // indirect
61+
github.com/mdlayher/socket v0.6.1 // indirect
62+
github.com/mdlayher/vsock v1.2.1 // indirect
63+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
64+
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
65+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
66+
github.com/opencontainers/go-digest v1.0.0 // indirect
67+
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
68+
github.com/prometheus/client_golang v1.23.2 // indirect
69+
github.com/prometheus/client_model v0.6.2 // indirect
70+
github.com/prometheus/common v0.67.5 // indirect
71+
github.com/prometheus/procfs v0.20.1 // indirect
72+
github.com/rcrowley/go-metrics v0.0.0-20250401214520-65e299d6c5c9 // indirect
73+
github.com/regclient/regclient v0.11.5 // indirect
74+
github.com/segmentio/asm v1.2.1 // indirect
75+
github.com/sirupsen/logrus v1.9.4 // indirect
76+
github.com/spf13/afero v1.15.0 // indirect
77+
github.com/spf13/pflag v1.0.10 // indirect
78+
github.com/tchap/go-patricia/v2 v2.3.3 // indirect
79+
github.com/valyala/fastjson v1.6.10 // indirect
80+
github.com/vektah/gqlparser/v2 v2.5.34 // indirect
81+
github.com/x448/float16 v0.8.4 // indirect
82+
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
83+
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
84+
github.com/yashtewari/glob-intersection v0.2.0 // indirect
85+
go.uber.org/multierr v1.11.0 // indirect
86+
go.yaml.in/yaml/v2 v2.4.4 // indirect
87+
go.yaml.in/yaml/v3 v3.0.4 // indirect
88+
golang.org/x/crypto v0.53.0 // indirect
89+
golang.org/x/exp v0.0.0-20260218203240-3dfff04db8fa // indirect
90+
golang.org/x/net v0.56.0 // indirect
91+
golang.org/x/oauth2 v0.36.0 // indirect
92+
golang.org/x/sync v0.21.0 // indirect
93+
golang.org/x/sys v0.46.0 // indirect
94+
golang.org/x/term v0.44.0 // indirect
95+
golang.org/x/text v0.39.0 // indirect
96+
golang.org/x/time v0.15.0 // indirect
97+
google.golang.org/genproto/googleapis/rpc v0.0.0-20260610212136-7ab31c22f7ad // indirect
98+
google.golang.org/grpc v1.82.1 // indirect
99+
google.golang.org/protobuf v1.36.11 // indirect
100+
gopkg.in/evanphx/json-patch.v4 v4.13.0 // indirect
101+
gopkg.in/inf.v0 v0.9.1 // indirect
102+
gopkg.in/yaml.v3 v3.0.1 // indirect
103+
k8s.io/api v0.35.2 // indirect
104+
k8s.io/client-go v0.35.2 // indirect
105+
k8s.io/klog/v2 v2.140.0 // indirect
106+
k8s.io/kube-openapi v0.0.0-20260603220949-865597e52e25 // indirect
107+
k8s.io/utils v0.0.0-20260210185600-b8788abfbbc2 // indirect
108+
sigs.k8s.io/json v0.0.0-20250730193827-2d320260d730 // indirect
109+
sigs.k8s.io/randfill v1.0.0 // indirect
110+
sigs.k8s.io/structured-merge-diff/v6 v6.4.0 // indirect
111+
sigs.k8s.io/yaml v1.6.0 // indirect
112+
)

0 commit comments

Comments
 (0)