Skip to content

Commit

Permalink
e2e: refactor shared flags to contrasttest package
Browse files Browse the repository at this point in the history
  • Loading branch information
3u13r committed Dec 6, 2024
1 parent 09168b2 commit 58fd490
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 82 deletions.
18 changes: 5 additions & 13 deletions e2e/aks-runtime/aks_runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,12 @@ 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)
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
55 changes: 46 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,25 @@ 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
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.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 +69,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 +396,29 @@ 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))

// Below we join all parts with a dash already, the user usually provides the
// suffix with a dash, so we remove any leading dash from the suffix.
namespaceSuffix = strings.TrimPrefix(namespaceSuffix, "-")

// Append the suffix
namespaceParts = append(namespaceParts, namespaceSuffix)

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

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

0 comments on commit 58fd490

Please sign in to comment.