Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: suffix ci k8s namespaces with "-ci" #1055

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ jobs:
--image-replacements workspace/just.containerlookup \
--namespace-file workspace/e2e.namespace \
--platform ${{ inputs.platform }} \
--skip-undeploy="${{ inputs.skip-undeploy && 'true' || 'false' }}"
--skip-undeploy="${{ inputs.skip-undeploy && 'true' || 'false' }}" \
Copy link
Contributor

Choose a reason for hiding this comment

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

fyi, @miampf is removing --skip-undeploy in #1046.

--namespace-suffix="-ci"
- name: Download logs
if: always()
run: |
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/e2e_aks_runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ jobs:
--image-replacements workspace/just.containerlookup \
--namespace-file workspace/e2e.namespace \
--platform AKS-CLH-SNP \
--skip-undeploy="false"
--skip-undeploy="false" \
--namespace-suffix="-ci"
- name: Download logs
if: always()
run: |
Expand Down
20 changes: 6 additions & 14 deletions e2e/aks-runtime/aks_runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,16 @@ import (

const testContainer = "testcontainer"

var (
imageReplacementsFile, namespaceFile, _platformStr string
skipUndeploy bool
)

func TestAKSRuntime(t *testing.T) {
require := require.New(t)

workdir := t.TempDir()

f, err := os.Open(imageReplacementsFile)
f, err := os.Open(contrasttest.Flags.ImageReplacementsFile)
require.NoError(err)
imageReplacements, err := kuberesource.ImageReplacementsFromFile(f)
require.NoError(err)
namespace := contrasttest.MakeNamespace(t)
namespace := contrasttest.MakeNamespace(t, contrasttest.Flags.NamespaceSuffix)

// Log versions
kataPolicyGenV, err := az.KataPolicyGenVersion()
Expand All @@ -59,8 +54,8 @@ func TestAKSRuntime(t *testing.T) {
err = c.Apply(ctx, ns...)
cancel()
require.NoError(err)
if namespaceFile != "" {
require.NoError(os.WriteFile(namespaceFile, []byte(namespace), 0o644))
if contrasttest.Flags.NamespaceFile != "" {
require.NoError(os.WriteFile(contrasttest.Flags.NamespaceFile, []byte(namespace), 0o644))
}

// simple deployment that logs the kernel version and then sleeps
Expand Down Expand Up @@ -110,7 +105,7 @@ func TestAKSRuntime(t *testing.T) {
require.NoError(c.WaitFor(ctx, kubeclient.Ready, kubeclient.Deployment{}, namespace, testContainer))

t.Cleanup(func() {
if skipUndeploy {
if contrasttest.Flags.SkipUndeploy {
return
}

Expand All @@ -134,10 +129,7 @@ func TestAKSRuntime(t *testing.T) {
}

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")
contrasttest.RegisterFlags()
flag.Parse()

os.Exit(m.Run())
Expand Down
14 changes: 3 additions & 11 deletions e2e/genpolicy/genpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,18 @@ import (
"github.com/stretchr/testify/require"
)

var (
imageReplacementsFile, namespaceFile, platformStr string
skipUndeploy bool
)

// TestGenpolicy runs regression tests for generated policies.
func TestGenpolicy(t *testing.T) {
testCases := kuberesource.GenpolicyRegressionTests()

platform, err := platforms.FromString(platformStr)
platform, err := platforms.FromString(contrasttest.Flags.PlatformStr)
require.NoError(t, err)
runtimeHandler, err := manifest.RuntimeHandler(platform)
require.NoError(t, err)

for name, deploy := range testCases {
t.Run(name, func(t *testing.T) {
ct := contrasttest.New(t, imageReplacementsFile, namespaceFile, platform, skipUndeploy)
ct := contrasttest.New(t)

ct.Init(t, kuberesource.PatchRuntimeHandlers([]any{deploy}, runtimeHandler))

Expand Down Expand Up @@ -72,10 +67,7 @@ func TestGenpolicy(t *testing.T) {
}

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")
contrasttest.RegisterFlags()
flag.Parse()

os.Exit(m.Run())
Expand Down
14 changes: 3 additions & 11 deletions e2e/getdents/getdents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,10 @@ const (
getdent = "getdents-tester"
)

var (
imageReplacementsFile, namespaceFile, platformStr string
skipUndeploy bool
)

func TestGetDEnts(t *testing.T) {
platform, err := platforms.FromString(platformStr)
platform, err := platforms.FromString(contrasttest.Flags.PlatformStr)
require.NoError(t, err)
ct := contrasttest.New(t, imageReplacementsFile, namespaceFile, platform, skipUndeploy)
ct := contrasttest.New(t)

runtimeHandler, err := manifest.RuntimeHandler(platform)
require.NoError(t, err)
Expand Down Expand Up @@ -89,10 +84,7 @@ func TestGetDEnts(t *testing.T) {
}

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")
contrasttest.RegisterFlags()
flag.Parse()

os.Exit(m.Run())
Expand Down
50 changes: 41 additions & 9 deletions e2e/internal/contrasttest/contrasttest.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"context"
"crypto/rand"
"crypto/x509"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io"
"os"
Expand All @@ -32,6 +32,27 @@ import (
"github.com/stretchr/testify/require"
)

// Flags contains the parsed Flags for the test.
var Flags testFlags

// testFlags contains the flags for the test.
type testFlags struct {
PlatformStr string
ImageReplacementsFile string
NamespaceFile string
NamespaceSuffix string
SkipUndeploy bool
}

// RegisterFlags registers the flags that are shared between all tests.
func RegisterFlags() {
flag.StringVar(&Flags.ImageReplacementsFile, "image-replacements", "", "path to image replacements file")
flag.StringVar(&Flags.NamespaceFile, "namespace-file", "", "file to store the namespace in")
flag.StringVar(&Flags.NamespaceSuffix, "namespace-suffix", "", "suffix to append to the namespace")
flag.StringVar(&Flags.PlatformStr, "platform", "", "Deployment platform")
flag.BoolVar(&Flags.SkipUndeploy, "skip-undeploy", false, "Skip undeploying the test namespace")
}

// ContrastTest is the Contrast test helper struct.
type ContrastTest struct {
// inputs, usually filled by New()
Expand All @@ -50,14 +71,17 @@ type ContrastTest struct {
}

// New creates a new contrasttest.T object bound to the given test.
func New(t *testing.T, imageReplacements, namespaceFile string, platform platforms.Platform, skipUndeploy bool) *ContrastTest {
func New(t *testing.T) *ContrastTest {
platform, err := platforms.FromString(Flags.PlatformStr)
require.NoError(t, err)

return &ContrastTest{
Namespace: MakeNamespace(t),
Namespace: MakeNamespace(t, Flags.NamespaceSuffix),
WorkDir: t.TempDir(),
ImageReplacementsFile: imageReplacements,
ImageReplacementsFile: Flags.ImageReplacementsFile,
Platform: platform,
NamespaceFile: namespaceFile,
SkipUndeploy: skipUndeploy,
NamespaceFile: Flags.NamespaceFile,
SkipUndeploy: Flags.SkipUndeploy,
Kubeclient: kubeclient.NewForTest(t),
}
}
Expand Down Expand Up @@ -374,14 +398,22 @@ func (ct *ContrastTest) FactorPlatformTimeout(timeout time.Duration) time.Durati
}

// MakeNamespace creates a namespace string using a given *testing.T.
func MakeNamespace(t *testing.T) string {
buf := make([]byte, 4)
func MakeNamespace(t *testing.T, namespaceSuffix string) string {
var namespaceParts []string

// First part(s) are consist of all valid characters in the lower case test name.
re := regexp.MustCompile("[a-z0-9-]+")
namespaceParts = append(namespaceParts, re.FindAllString(strings.ToLower(t.Name()), -1)...)

// Append some randomness
buf := make([]byte, 4)
n, err := rand.Reader.Read(buf)
require.NoError(t, err)
require.Equal(t, 4, n)

return strings.Join(append(re.FindAllString(strings.ToLower(t.Name()), -1), hex.EncodeToString(buf)), "-")
namespaceParts = append(namespaceParts, fmt.Sprintf("%x", buf))

return strings.Join(namespaceParts, "-") + namespaceSuffix
}

func toPtr[T any](t T) *T {
Expand Down
14 changes: 3 additions & 11 deletions e2e/openssl/openssl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,11 @@ const (
rootCAFile = "coordinator-root-ca.pem"
)

var (
imageReplacementsFile, namespaceFile, platformStr string
skipUndeploy bool
)

// TestOpenSSL runs e2e tests on the example OpenSSL deployment.
func TestOpenSSL(t *testing.T) {
platform, err := platforms.FromString(platformStr)
platform, err := platforms.FromString(contrasttest.Flags.PlatformStr)
require.NoError(t, err)
ct := contrasttest.New(t, imageReplacementsFile, namespaceFile, platform, skipUndeploy)
ct := contrasttest.New(t)

runtimeHandler, err := manifest.RuntimeHandler(platform)
require.NoError(t, err)
Expand Down Expand Up @@ -245,10 +240,7 @@ func TestOpenSSL(t *testing.T) {
}

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")
contrasttest.RegisterFlags()
flag.Parse()

os.Exit(m.Run())
Expand Down
5 changes: 2 additions & 3 deletions e2e/policy/deterministic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import (

func TestDeterminsticPolicyGeneration(t *testing.T) {
require := require.New(t)
platform, err := platforms.FromString(platformStr)
platform, err := platforms.FromString(contrasttest.Flags.PlatformStr)
require.NoError(err)
skipUndeploy := true // doesn't matter, because we don't deploy
ct := contrasttest.New(t, imageReplacementsFile, namespaceFile, platform, skipUndeploy)
ct := contrasttest.New(t)

// create K8s resources
runtimeHandler, err := manifest.RuntimeHandler(platform)
Expand Down
14 changes: 3 additions & 11 deletions e2e/policy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,10 @@ const (
coordinatorPod = "coordinator-0"
)

var (
imageReplacementsFile, namespaceFile, platformStr string
skipUndeploy bool
)

func TestPolicy(t *testing.T) {
platform, err := platforms.FromString(platformStr)
platform, err := platforms.FromString(contrasttest.Flags.PlatformStr)
require.NoError(t, err)
ct := contrasttest.New(t, imageReplacementsFile, namespaceFile, platform, skipUndeploy)
ct := contrasttest.New(t)

runtimeHandler, err := manifest.RuntimeHandler(platform)
require.NoError(t, err)
Expand Down Expand Up @@ -197,10 +192,7 @@ func TestPolicy(t *testing.T) {
}

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")
contrasttest.RegisterFlags()
flag.Parse()

os.Exit(m.Run())
Expand Down
16 changes: 3 additions & 13 deletions e2e/regression/regression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,18 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var (
imageReplacementsFile, namespaceFile, platformStr string
_skipUndeploy bool // just here for interoptability, ignored in this test
)

func TestRegression(t *testing.T) {
yamlDir := "./e2e/regression/testdata/"
files, err := os.ReadDir(yamlDir)
require.NoError(t, err)

platform, err := platforms.FromString(platformStr)
platform, err := platforms.FromString(contrasttest.Flags.PlatformStr)
require.NoError(t, err)

runtimeHandler, err := manifest.RuntimeHandler(platform)
require.NoError(t, err)

ct := contrasttest.New(t, imageReplacementsFile, namespaceFile, platform, false)
ct := contrasttest.New(t)

// Initially just deploy the coordinator bundle

Expand Down Expand Up @@ -97,12 +92,7 @@ func TestRegression(t *testing.T) {
}

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

// ignored and just here for interoptability, we always undeploy to save resources
flag.BoolVar(&_skipUndeploy, "skip-undeploy", false, "skip undeploy step in the test")
contrasttest.RegisterFlags()
flag.Parse()

os.Exit(m.Run())
Expand Down
14 changes: 3 additions & 11 deletions e2e/servicemesh/servicemesh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,11 @@ import (
"github.com/stretchr/testify/require"
)

var (
imageReplacementsFile, namespaceFile, platformStr string
skipUndeploy bool
)

// TestIngressEgress tests that the ingress and egress proxies work as configured.
func TestIngressEgress(t *testing.T) {
platform, err := platforms.FromString(platformStr)
platform, err := platforms.FromString(contrasttest.Flags.PlatformStr)
require.NoError(t, err)
ct := contrasttest.New(t, imageReplacementsFile, namespaceFile, platform, skipUndeploy)
ct := contrasttest.New(t)

runtimeHandler, err := manifest.RuntimeHandler(platform)
require.NoError(t, err)
Expand Down Expand Up @@ -153,10 +148,7 @@ func TestIngressEgress(t *testing.T) {
}

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")
contrasttest.RegisterFlags()
flag.Parse()

os.Exit(m.Run())
Expand Down
Loading
Loading