Skip to content

Commit e47d348

Browse files
committed
e2e: apply k8s resources from Go
1 parent 4580209 commit e47d348

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

.github/workflows/e2e_openssl.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ jobs:
5555
- name: Build, deploy, contrast generate, contrast set, contrast verify
5656
run: |
5757
just coordinator initializer openssl
58-
just deploy openssl contrast.cli
58+
just generate openssl contrast.cli
5959
- name: Setup Summary
6060
run: |
6161
cat ./workspace/just.namespace | tee -a "${GITHUB_STEP_SUMMARY}"

e2e/openssl/openssl_test.go

+32-5
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import (
1717

1818
"github.com/edgelesssys/contrast/cli/cmd"
1919
"github.com/edgelesssys/contrast/e2e/internal/kubeclient"
20+
"github.com/edgelesssys/contrast/internal/kubeapi"
2021
"github.com/stretchr/testify/assert"
2122
"github.com/stretchr/testify/require"
23+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2224
)
2325

2426
// namespace the tests are executed in.
@@ -36,6 +38,34 @@ func TestOpenSSL(t *testing.T) {
3638
namespace := os.Getenv(namespaceEnv)
3739
require.NotEmpty(t, namespace, "environment variable %q must be set", namespaceEnv)
3840

41+
resources, err := filepath.Glob("./workspace/deployment/*.yml")
42+
require.NoError(t, err)
43+
44+
// TODO(burgerdev): policy hash should come from contrast generate output.
45+
coordinatorPolicyHashBytes, err := os.ReadFile("workspace/coordinator-policy.sha256")
46+
require.NoError(t, err)
47+
coordinatorPolicyHash := string(coordinatorPolicyHashBytes)
48+
require.NotEmpty(t, coordinatorPolicyHash, "expected apply to fill coordinator policy hash")
49+
50+
require.True(t, t.Run("apply", func(t *testing.T) {
51+
require := require.New(t)
52+
53+
var objects []*unstructured.Unstructured
54+
for _, file := range resources {
55+
yaml, err := os.ReadFile(file)
56+
require.NoError(err)
57+
fileObjects, err := kubeapi.UnmarshalUnstructuredK8SResource(yaml)
58+
require.NoError(err)
59+
objects = append(objects, fileObjects...)
60+
}
61+
62+
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
63+
defer cancel()
64+
65+
c := kubeclient.NewForTest(t)
66+
require.NoError(c.Apply(ctx, objects...))
67+
}), "Kubernetes resources need to be applied for subsequent tests")
68+
3969
require.True(t, t.Run("set", func(t *testing.T) {
4070
require := require.New(t)
4171

@@ -48,11 +78,8 @@ func TestOpenSSL(t *testing.T) {
4878
require.NoError(err)
4979
defer cancelPortForward()
5080

51-
resources, err := filepath.Glob("./workspace/deployment/*.yml")
52-
require.NoError(err)
53-
5481
args := []string{
55-
"--coordinator-policy-hash=", // TODO(burgerdev): enable policy checking
82+
"--coordinator-policy-hash", coordinatorPolicyHash,
5683
"--coordinator", coordinator,
5784
"--workspace-dir", "./workspace",
5885
}
@@ -88,7 +115,7 @@ func TestOpenSSL(t *testing.T) {
88115
verify := cmd.NewVerifyCmd()
89116
verify.SetArgs([]string{
90117
"--workspace-dir", workspaceDir,
91-
"--coordinator-policy-hash=", // TODO(burgerdev): enable policy checking
118+
"--coordinator-policy-hash", coordinatorPolicyHash,
92119
"--coordinator", coordinator,
93120
})
94121
verify.SetOut(io.Discard)

internal/kubeapi/kubeapi.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type (
3030
// UnmarshalK8SResources unmarshals a Kubernetes resource into a list of objects that can be
3131
// type casted to a Kubernetes resource.
3232
func UnmarshalK8SResources(data []byte) ([]any, error) {
33-
objs, err := unmarshalUnstructuredK8SResource(data)
33+
objs, err := UnmarshalUnstructuredK8SResource(data)
3434
if err != nil {
3535
return nil, err
3636
}
@@ -77,7 +77,8 @@ func UnmarshalK8SResources(data []byte) ([]any, error) {
7777
return result, nil
7878
}
7979

80-
func unmarshalUnstructuredK8SResource(data []byte) ([]*unstructured.Unstructured, error) {
80+
// UnmarshalUnstructuredK8SResource parses the input YAML into unstructured Kubernetes resources.
81+
func UnmarshalUnstructuredK8SResource(data []byte) ([]*unstructured.Unstructured, error) {
8182
documentsData, err := splitYAML(data)
8283
if err != nil {
8384
return nil, fmt.Errorf("splitting YAML into multiple documents: %w", err)

0 commit comments

Comments
 (0)