mirror of
https://github.com/stakater/Reloader.git
synced 2026-05-17 14:16:39 +00:00
Add e2e parallel
Signed-off-by: faizanahmad055 <faizan.ahmad55@outlook.com>
This commit is contained in:
@@ -3,6 +3,7 @@ package utils
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
rolloutsclient "github.com/argoproj/argo-rollouts/pkg/client/clientset/versioned"
|
||||
"github.com/onsi/ginkgo/v2"
|
||||
@@ -31,6 +32,71 @@ type TestEnvironment struct {
|
||||
ProjectDir string
|
||||
}
|
||||
|
||||
// SharedEnvData is passed from process 1 to all other processes via
|
||||
// SynchronizedBeforeSuite. It carries the namespace and release name that
|
||||
// process 1 created so the other processes can reuse them.
|
||||
type SharedEnvData struct {
|
||||
Namespace string `json:"namespace"`
|
||||
ReleaseName string `json:"releaseName"`
|
||||
}
|
||||
|
||||
// SetupSharedTestEnvironment creates a TestEnvironment that connects to an
|
||||
// already-provisioned namespace and Helm release. It builds Kubernetes clients
|
||||
// but does NOT create a new namespace or deploy Reloader. Use this in the
|
||||
// allProcsBody of SynchronizedBeforeSuite so that processes 2-N can share the
|
||||
// single Reloader instance that process 1 deployed.
|
||||
func SetupSharedTestEnvironment(ctx context.Context, namespace, releaseName string) (*TestEnvironment, error) {
|
||||
childCtx, cancel := context.WithCancel(ctx)
|
||||
env := &TestEnvironment{
|
||||
Ctx: childCtx,
|
||||
Cancel: cancel,
|
||||
TestImage: GetTestImage(),
|
||||
Namespace: namespace,
|
||||
ReleaseName: releaseName,
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
env.ProjectDir, err = GetProjectDir()
|
||||
if err != nil {
|
||||
cancel()
|
||||
return nil, fmt.Errorf("getting project directory: %w", err)
|
||||
}
|
||||
|
||||
kubeconfig := GetKubeconfig()
|
||||
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return nil, fmt.Errorf("building config from kubeconfig: %w", err)
|
||||
}
|
||||
env.RestConfig = config
|
||||
|
||||
env.KubeClient, err = kubernetes.NewForConfig(config)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return nil, fmt.Errorf("creating kubernetes client: %w", err)
|
||||
}
|
||||
|
||||
env.DiscoveryClient, err = discovery.NewDiscoveryClientForConfig(config)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return nil, fmt.Errorf("creating discovery client: %w", err)
|
||||
}
|
||||
|
||||
// Optional clients — failures are non-fatal.
|
||||
if env.CSIClient, err = csiclient.NewForConfig(config); err != nil {
|
||||
env.CSIClient = nil
|
||||
}
|
||||
if env.RolloutsClient, err = rolloutsclient.NewForConfig(config); err != nil {
|
||||
env.RolloutsClient = nil
|
||||
}
|
||||
if env.OpenShiftClient, err = openshiftclient.NewForConfig(config); err != nil {
|
||||
env.OpenShiftClient = nil
|
||||
}
|
||||
|
||||
return env, nil
|
||||
}
|
||||
|
||||
// SetupTestEnvironment creates a new test environment with kubernetes clients.
|
||||
// It creates a unique namespace with the given prefix. The returned env.Cancel must be
|
||||
// called (e.g., in AfterSuite) to release the child context after env.Cleanup() completes.
|
||||
@@ -112,15 +178,22 @@ func SetupTestEnvironment(ctx context.Context, namespacePrefix string) (*TestEnv
|
||||
}
|
||||
|
||||
// Cleanup cleans up the test environment resources.
|
||||
// It uses a fresh context so it can run safely even after the suite context
|
||||
// has been cancelled by SynchronizedAfterSuite.
|
||||
func (e *TestEnvironment) Cleanup() error {
|
||||
if e.Namespace == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Use a fresh context with a generous timeout so cleanup works even
|
||||
// after the per-process context (e.Ctx) has been cancelled.
|
||||
cleanupCtx, cleanupCancel := context.WithTimeout(context.Background(), 3*time.Minute)
|
||||
defer cleanupCancel()
|
||||
|
||||
ginkgo.GinkgoWriter.Printf("Cleaning up test namespace: %s\n", e.Namespace)
|
||||
ginkgo.GinkgoWriter.Printf("Cleaning up Helm release: %s\n", e.ReleaseName)
|
||||
|
||||
logs, err := GetPodLogs(e.Ctx, e.KubeClient, e.Namespace, ReloaderPodSelector(e.ReleaseName))
|
||||
logs, err := GetPodLogs(cleanupCtx, e.KubeClient, e.Namespace, ReloaderPodSelector(e.ReleaseName))
|
||||
if err == nil && logs != "" {
|
||||
ginkgo.GinkgoWriter.Println("Reloader logs:")
|
||||
ginkgo.GinkgoWriter.Println(logs)
|
||||
@@ -128,7 +201,7 @@ func (e *TestEnvironment) Cleanup() error {
|
||||
|
||||
_ = UndeployReloader(e.Namespace, e.ReleaseName)
|
||||
|
||||
if err := DeleteNamespace(e.Ctx, e.KubeClient, e.Namespace); err != nil {
|
||||
if err := DeleteNamespace(cleanupCtx, e.KubeClient, e.Namespace); err != nil {
|
||||
return fmt.Errorf("deleting namespace: %w", err)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user