mirror of
https://github.com/agent-substrate/substrate.git
synced 2026-10-02 03:24:42 +08:00
118 lines
3.0 KiB
Go
118 lines
3.0 KiB
Go
// Copyright 2026 Google LLC
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package e2e
|
|
|
|
import (
|
|
"context"
|
|
goflag "flag"
|
|
"fmt"
|
|
"os"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/spf13/pflag"
|
|
)
|
|
|
|
var (
|
|
RunE2E bool
|
|
KubeConfig string
|
|
KubeContext string
|
|
StorageClass string
|
|
)
|
|
|
|
var (
|
|
suiteCleanupsMu sync.Mutex
|
|
suiteCleanups []func()
|
|
)
|
|
|
|
func RegisterSuiteCleanup(fn func()) {
|
|
suiteCleanupsMu.Lock()
|
|
defer suiteCleanupsMu.Unlock()
|
|
suiteCleanups = append(suiteCleanups, fn)
|
|
}
|
|
|
|
func runSuiteCleanups() {
|
|
suiteCleanupsMu.Lock()
|
|
fns := suiteCleanups
|
|
suiteCleanups = nil
|
|
suiteCleanupsMu.Unlock()
|
|
|
|
for i := len(fns) - 1; i >= 0; i-- {
|
|
fns[i]()
|
|
}
|
|
}
|
|
|
|
func bindFlags() {
|
|
pflag.BoolVar(&RunE2E, "e2e", false, "run e2e tests")
|
|
pflag.BoolVar(&NoColor, "no-color", false, "disable colors in output")
|
|
pflag.StringVar(&KubeConfig, "kube-config", "", "Location of the kubeconfig")
|
|
pflag.StringVar(&KubeContext, "kube-context", "", "Kubernetes context to use")
|
|
pflag.StringVar(&StorageClass, "storage-class", "csi-nfs-sc", "StorageClass to use for external volume tests")
|
|
}
|
|
|
|
// RunTestMain should be used to run your e2e test suite.
|
|
func RunTestMain(m *testing.M) int {
|
|
bindFlags()
|
|
pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
|
|
pflag.Parse()
|
|
if err := pflag.ParseSkippedFlags(os.Args[1:], goflag.CommandLine); err != nil {
|
|
fmt.Printf("Failed to parse Go test flags: %v\n", err)
|
|
return 1
|
|
}
|
|
|
|
if !RunE2E {
|
|
fmt.Println(Colorf(`
|
|
<yellow>This is an e2e test suite and does not run by default.
|
|
Run with "go test ./internal/e2e/... -args --e2e"</yellow>`))
|
|
fmt.Println()
|
|
return 0
|
|
}
|
|
|
|
return runAndCleanup(m)
|
|
}
|
|
|
|
func runAndCleanup(m *testing.M) int {
|
|
ctx := context.Background()
|
|
clients, err := NewClients(ctx)
|
|
if err != nil {
|
|
fmt.Printf("Failed to initialize E2E clients: %v\n", err)
|
|
return 1
|
|
}
|
|
sharedClients = clients
|
|
defer func() {
|
|
sharedClients.Close()
|
|
sharedClients = nil
|
|
}()
|
|
|
|
if err := PreflightChecks(); err != nil {
|
|
fmt.Println(Colorf("<red>Preflight checks FAILED: %v</red>\n", err))
|
|
return 1
|
|
}
|
|
|
|
// Namespaces are deleted only on success. A failed run keeps them: the actor
|
|
// lives in a worker pod there, and its ateom logs (for a micro-VM worker, the
|
|
// guest's console tail too) are the only record of why it failed. Deleting the
|
|
// namespace takes those pods with it before anyone — a developer or CI's
|
|
// post-failure log dump — can read them.
|
|
code := m.Run()
|
|
runSuiteCleanups()
|
|
if code != 0 {
|
|
RetainNamespaces()
|
|
return code
|
|
}
|
|
CleanupNamespaces()
|
|
return code
|
|
}
|