-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
e2e: Add waiting mechanism and correct event watching/listing to default policy test Structure changes to deploy.go
- Loading branch information
Showing
3 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright 2024 Edgeless Systems GmbH | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
//go:build e2e | ||
|
||
package nopolicy | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/edgelesssys/contrast/e2e/internal/contrasttest" | ||
"github.com/edgelesssys/contrast/e2e/internal/kubeclient" | ||
"github.com/edgelesssys/contrast/internal/kuberesource" | ||
"github.com/edgelesssys/contrast/internal/manifest" | ||
"github.com/edgelesssys/contrast/internal/platforms" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const ( | ||
// Persistent pod identifier of StatefulSet Coordinator is used. | ||
coordinatorPod = "coordinator-0" | ||
coordinator = "coordinator" | ||
) | ||
|
||
// Namespace the tests are executed in. | ||
var ( | ||
imageReplacementsFile, namespaceFile, platformStr string | ||
skipUndeploy bool | ||
) | ||
|
||
// TestNoPolicy runs e2e test on the example of coordinator pod of the OpenSSL deployment, to ensure that container without policy annotation (default policy) do not start. | ||
func TestNoPolicy(t *testing.T) { | ||
platform, err := platforms.FromString(platformStr) | ||
require.NoError(t, err) | ||
ct := contrasttest.New(t, imageReplacementsFile, namespaceFile, platform, skipUndeploy) | ||
runtimeHandler, err := manifest.RuntimeHandler(platform) | ||
require.NoError(t, err) | ||
|
||
// Deployment resources are limited to Coordinator only. | ||
resources := kuberesource.CoordinatorBundle() | ||
resources = kuberesource.PatchRuntimeHandlers(resources, runtimeHandler) | ||
resources = kuberesource.AddPortForwarders(resources) | ||
ct.Init(t, resources) | ||
|
||
// Apply deployment using default policies | ||
require.True(t, t.Run("apply", ct.Apply), "Kubernetes resources need to be applied for subsequent tests") | ||
|
||
t.Run("check containers without policy annotation do not start", func(t *testing.T) { | ||
require := require.New(t) | ||
ctx, cancel := context.WithTimeout(context.Background(), ct.FactorPlatformTimeout(2*time.Minute)) | ||
defer cancel() | ||
|
||
c := kubeclient.NewForTest(t) | ||
|
||
t.Logf("Cwd: %s", ct.WorkDir) | ||
t.Log("Waiting to ensure container start up failed") | ||
|
||
// waiting for 3 ocurrences of the event | ||
for range 3 { | ||
err := c.WaitForEvent(ctx, kubeclient.StartingBlocked, kubeclient.Pod{}, ct.Namespace, coordinatorPod) | ||
require.NoError(err) | ||
} | ||
t.Log("Restarting container") | ||
|
||
require.NoError(c.Restart(ctx, kubeclient.Deployment{}, ct.Namespace, coordinator)) | ||
t.Log("Waiting to ensure container start up failed") | ||
|
||
for range 3 { | ||
err := c.WaitForEvent(ctx, kubeclient.StartingBlocked, kubeclient.Pod{}, ct.Namespace, coordinatorPod) | ||
require.NoError(err) | ||
} | ||
}) | ||
} | ||
|
||
func TestMain(m *testing.M) { | ||
flag.StringVar(&imageReplacementsFile, "image-replacements", "", "path to image replacements file") | ||
flag.StringVar(&namespaceFile, "namespace-file", "", "file to store the namespace in") | ||
flag.StringVar(&platformStr, "platform", "", "Deployment platform") | ||
flag.BoolVar(&skipUndeploy, "skip-undeploy", false, "skip undeploy step in the test") | ||
flag.Parse() | ||
|
||
os.Exit(m.Run()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters