diff --git a/test/e2e/bundle_test.go b/test/e2e/bundle_test.go index 840fd0f..c29fbaa 100644 --- a/test/e2e/bundle_test.go +++ b/test/e2e/bundle_test.go @@ -91,7 +91,7 @@ var _ = Describe("Bundle", Label("bundle"), Ordered, func() { out, err := utils.OperatorSdkRun("run", "bundle", "--namespace", testNamespaces[0].Name, "--install-mode", "OwnNamespace", - fmt.Sprintf("--skip-tls-verify=%v", registryInsecure), + fmt.Sprintf("--skip-tls-verify=%v", utils.IsRegistryInsecure()), bundleImage) Expect(err).NotTo(HaveOccurred()) _, _ = fmt.Fprint(GinkgoWriter, out) @@ -129,7 +129,7 @@ var _ = Describe("Bundle", Label("bundle"), Ordered, func() { out, err := utils.OperatorSdkRun("run", "bundle", "--namespace", testNamespaces[0].Name, "--install-mode", fmt.Sprintf("SingleNamespace=%s", testNamespaces[1].Name), - fmt.Sprintf("--skip-tls-verify=%v", registryInsecure), + fmt.Sprintf("--skip-tls-verify=%v", utils.IsRegistryInsecure()), bundleImage) Expect(err).NotTo(HaveOccurred()) _, _ = fmt.Fprint(GinkgoWriter, out) @@ -169,7 +169,7 @@ var _ = Describe("Bundle", Label("bundle"), Ordered, func() { out, err := utils.OperatorSdkRun("run", "bundle", "--namespace", testNamespaces[0].Name, "--install-mode", fmt.Sprintf("MultiNamespace=%s,%s", testNamespaces[1].Name, testNamespaces[2].Name), - fmt.Sprintf("--skip-tls-verify=%v", registryInsecure), + fmt.Sprintf("--skip-tls-verify=%v", utils.IsRegistryInsecure()), bundleImage) Expect(err).NotTo(HaveOccurred()) _, _ = fmt.Fprint(GinkgoWriter, out) @@ -209,7 +209,7 @@ var _ = Describe("Bundle", Label("bundle"), Ordered, func() { out, err := utils.OperatorSdkRun("run", "bundle", "--namespace", testNamespaces[0].Name, "--install-mode", fmt.Sprintf("SingleNamespace=%s", testNamespaces[1].Name), - fmt.Sprintf("--skip-tls-verify=%v", registryInsecure), + fmt.Sprintf("--skip-tls-verify=%v", utils.IsRegistryInsecure()), bundleImage) Expect(err).NotTo(HaveOccurred()) _, _ = fmt.Fprint(GinkgoWriter, out) @@ -218,7 +218,7 @@ var _ = Describe("Bundle", Label("bundle"), Ordered, func() { out, err = utils.OperatorSdkRun("run", "bundle", "--namespace", testNamespaces[2].Name, "--install-mode", fmt.Sprintf("SingleNamespace=%s", testNamespaces[3].Name), - fmt.Sprintf("--skip-tls-verify=%v", registryInsecure), + fmt.Sprintf("--skip-tls-verify=%v", utils.IsRegistryInsecure()), bundleImage) Expect(err).NotTo(HaveOccurred()) _, _ = fmt.Fprint(GinkgoWriter, out) @@ -266,7 +266,7 @@ var _ = Describe("Bundle", Label("bundle"), Ordered, func() { out, err := utils.OperatorSdkRun("run", "bundle", "--namespace", testNamespaces[0].Name, "--install-mode", "AllNamespaces", - fmt.Sprintf("--skip-tls-verify=%v", registryInsecure), + fmt.Sprintf("--skip-tls-verify=%v", utils.IsRegistryInsecure()), bundleImage) Expect(err).NotTo(HaveOccurred()) _, _ = fmt.Fprint(GinkgoWriter, out) @@ -363,11 +363,7 @@ func createNamespaceAndDeployFunction() TestNamespace { DeferCleanup(os.RemoveAll, repoDir) // Deploy function - out, err := utils.RunFunc("deploy", - "--namespace", ns, - "--path", repoDir, - "--registry", registry, - fmt.Sprintf("--registry-insecure=%t", registryInsecure)) + out, err := utils.RunFuncDeploy(repoDir, utils.WithNamespace(ns)) Expect(err).NotTo(HaveOccurred()) _, _ = fmt.Fprint(GinkgoWriter, out) diff --git a/test/e2e/e2e_suite_test.go b/test/e2e/e2e_suite_test.go index a6ebafd..31d786b 100644 --- a/test/e2e/e2e_suite_test.go +++ b/test/e2e/e2e_suite_test.go @@ -19,8 +19,6 @@ package e2e import ( "context" "fmt" - "os" - "strings" "testing" . "github.com/onsi/ginkgo/v2" @@ -37,9 +35,6 @@ var ( k8sClient client.Client ctx context.Context - registry string - registryInsecure bool - repoProvider utils.RepositoryProvider ) @@ -72,17 +67,6 @@ var _ = BeforeSuite(func() { Expect(err).NotTo(HaveOccurred()) Expect(k8sClient).NotTo(BeNil()) - // Setup vars from env - registry = os.Getenv("REGISTRY") - if registry == "" { - registry = "kind-registry:5000" - } - - registryInsecure = false - if sec := os.Getenv("REGISTRY_INSECURE"); strings.ToLower(sec) == "true" { - registryInsecure = true - } - // Initialize repository provider (Gitea) repoProvider, err = utils.NewGiteaClient() Expect(err).NotTo(HaveOccurred()) diff --git a/test/e2e/func_deploy_test.go b/test/e2e/func_deploy_test.go index 542c0e7..95ab51d 100644 --- a/test/e2e/func_deploy_test.go +++ b/test/e2e/func_deploy_test.go @@ -154,11 +154,7 @@ var _ = Describe("Operator", func() { DeferCleanup(cleanupNamespaces, functionNamespace) // Deploy function using func CLI - out, err := utils.RunFunc("deploy", - "--namespace", functionNamespace, - "--path", repoDir, - "--registry", registry, - fmt.Sprintf("--registry-insecure=%t", registryInsecure)) + out, err := utils.RunFuncDeploy(repoDir, utils.WithNamespace(functionNamespace)) Expect(err).NotTo(HaveOccurred()) _, _ = fmt.Fprint(GinkgoWriter, out) @@ -223,7 +219,12 @@ var _ = Describe("Operator", func() { DeferCleanup(cleanup) // Initialize repository with function code - repoDir, err = utils.InitializeRepoWithFunctionInSubDir(repoURL, subPath, username, password, "go") + repoDir, err = utils.InitializeRepoWithFunction( + repoURL, + username, + password, + "go", + utils.WithSubDir(subPath)) Expect(err).NotTo(HaveOccurred()) DeferCleanup(os.RemoveAll, repoDir) @@ -234,11 +235,7 @@ var _ = Describe("Operator", func() { functionDir := filepath.Join(repoDir, subPath) // Deploy function using func CLI - out, err := utils.RunFunc("deploy", - "--namespace", functionNamespace, - "--path", functionDir, - "--registry", registry, - fmt.Sprintf("--registry-insecure=%t", registryInsecure)) + out, err := utils.RunFuncDeploy(functionDir, utils.WithNamespace(functionNamespace)) Expect(err).NotTo(HaveOccurred()) _, _ = fmt.Fprint(GinkgoWriter, out) @@ -378,11 +375,7 @@ var _ = Describe("Operator", func() { DeferCleanup(cleanupNamespaces, functionNamespace) // Deploy function using func CLI - out, err := utils.RunFunc("deploy", - "--namespace", functionNamespace, - "--path", repoDir, - "--registry", registry, - fmt.Sprintf("--registry-insecure=%t", registryInsecure)) + out, err := utils.RunFuncDeploy(repoDir, utils.WithNamespace(functionNamespace)) Expect(err).NotTo(HaveOccurred()) _, _ = fmt.Fprint(GinkgoWriter, out) diff --git a/test/e2e/func_middleware_update_test.go b/test/e2e/func_middleware_update_test.go index cc2cf18..9b96af7 100644 --- a/test/e2e/func_middleware_update_test.go +++ b/test/e2e/func_middleware_update_test.go @@ -66,7 +66,12 @@ var _ = Describe("Middleware Update", func() { // Initialize repository with function code using OLD func CLI version // v1.20.1 has no middleware-version label and uses instance-compatible templates oldFuncVersion := "v1.20.1" - repoDir, err = utils.InitializeRepoWithFunctionVersion(repoURL, ".", username, password, "go", oldFuncVersion) + repoDir, err = utils.InitializeRepoWithFunction( + repoURL, + username, + password, + "go", + utils.WithCliVersion(oldFuncVersion)) Expect(err).NotTo(HaveOccurred()) DeferCleanup(os.RemoveAll, repoDir) @@ -74,8 +79,8 @@ var _ = Describe("Middleware Update", func() { out, err := utils.RunFuncWithVersion(oldFuncVersion, "deploy", "--namespace", functionNamespace, "--path", repoDir, - "--registry", registry, - fmt.Sprintf("--registry-insecure=%t", registryInsecure)) + "--registry", utils.Registry(), + fmt.Sprintf("--registry-insecure=%t", utils.IsRegistryInsecure())) Expect(err).NotTo(HaveOccurred()) _, _ = fmt.Fprint(GinkgoWriter, out) diff --git a/test/utils/func.go b/test/utils/func.go index 8fbaa95..d592b85 100644 --- a/test/utils/func.go +++ b/test/utils/func.go @@ -33,6 +33,39 @@ func RunFunc(command string, args ...string) (string, error) { return Run(cmd) } +// RunFuncDeploy runs func deploy +func RunFuncDeploy(functionDir string, optFns ...FuncDeployOption) (string, error) { + opts := &FuncDeployOptions{ + // defaults + Registry: Registry(), + RegistryInsecure: IsRegistryInsecure(), + } + + for _, optFn := range optFns { + optFn(opts) + } + + args := []string{ + "--path", functionDir, + "--registry", opts.Registry, + fmt.Sprintf("--registry-insecure=%t", opts.RegistryInsecure), + } + + if opts.Namespace != "" { + args = append(args, "--namespace", opts.Namespace) + } + + if opts.Builder != "" { + args = append(args, "--builder", opts.Builder) + } + + if opts.Deployer != "" { + args = append(args, "--deployer", opts.Deployer) + } + + return RunFunc("deploy", args...) +} + // RunFuncWithVersion executes the func CLI with a specific version // It downloads and caches the version if not already present func RunFuncWithVersion(version string, command string, args ...string) (string, error) { @@ -46,6 +79,34 @@ func RunFuncWithVersion(version string, command string, args ...string) (string, return Run(cmd) } +type FuncDeployOptions struct { + Registry string + RegistryInsecure bool + Namespace string + Builder string + Deployer string +} + +type FuncDeployOption func(*FuncDeployOptions) + +func WithNamespace(namespace string) FuncDeployOption { + return func(opts *FuncDeployOptions) { + opts.Namespace = namespace + } +} + +func WithBuilder(builder string) FuncDeployOption { + return func(o *FuncDeployOptions) { + o.Builder = builder + } +} + +func WithDeployer(deployer string) FuncDeployOption { + return func(o *FuncDeployOptions) { + o.Deployer = deployer + } +} + // ensureFuncVersion ensures the specified func version is available and returns its path func ensureFuncVersion(version string) (string, error) { projectDir, err := GetProjectDir() diff --git a/test/utils/git.go b/test/utils/git.go index 6e4f4c1..1931498 100644 --- a/test/utils/git.go +++ b/test/utils/git.go @@ -32,33 +32,45 @@ func buildAuthURL(repoURL, username, password string) string { fmt.Sprintf("http://%s:%s@", username, password), 1) } -// InitializeRepoWithFunction creates a function project and pushes it to the Gitea repo -func InitializeRepoWithFunction(repoURL, username, password, language string) (string, error) { - return InitializeRepoWithFunctionVersion(repoURL, ".", username, password, language, "") +type RepoOptions struct { + SubDir string + CliVersion string } -// InitializeRepoWithFunctionInSubDir creates a function project in a repos subdirectory and pushes it to the Gitea repo -func InitializeRepoWithFunctionInSubDir(repoURL, subDir, username, password, language string) (string, error) { - return InitializeRepoWithFunctionVersion(repoURL, subDir, username, password, language, "") +type RepoOption func(*RepoOptions) + +func WithSubDir(subDir string) RepoOption { + return func(o *RepoOptions) { + o.SubDir = subDir + } +} +func WithCliVersion(cliVersion string) RepoOption { + return func(o *RepoOptions) { + o.CliVersion = cliVersion + } } -// InitializeRepoWithFunctionVersion creates a function project with a specific func CLI version -// If version is empty, uses the current func CLI -func InitializeRepoWithFunctionVersion(repoURL, subdir, username, password, language, version string) (string, error) { +// InitializeRepoWithFunction creates a function project and pushes it to the Gitea repo +func InitializeRepoWithFunction(repoURL, username, password, language string, optFns ...RepoOption) (string, error) { + opts := &RepoOptions{} + for _, fn := range optFns { + fn(opts) + } + repoDir := fmt.Sprintf("%s/func-test-%s", os.TempDir(), rand.String(10)) // Build authenticated URL authURL := buildAuthURL(repoURL, username, password) - functionPath := filepath.Join(repoDir, subdir) + functionPath := filepath.Join(repoDir, opts.SubDir) // Initialize function (func init creates the directory) - if version == "" { + if opts.CliVersion == "" { if _, err := RunFunc("init", "-l", language, functionPath); err != nil { return "", fmt.Errorf("failed to init function: %w", err) } } else { - if _, err := RunFuncWithVersion(version, "init", "-l", language, functionPath); err != nil { + if _, err := RunFuncWithVersion(opts.CliVersion, "init", "-l", language, functionPath); err != nil { return "", fmt.Errorf("failed to init function: %w", err) } } diff --git a/test/utils/registry.go b/test/utils/registry.go new file mode 100644 index 0000000..59952df --- /dev/null +++ b/test/utils/registry.go @@ -0,0 +1,41 @@ +package utils + +import ( + "os" + "strings" + "sync" +) + +var ( + registry string + registryInsecure bool +) + +var registryOnce sync.Once + +// Registry returns the registry used for the e2e tests +func Registry() string { + registryOnce.Do(func() { + // Setup vars from env + registry = os.Getenv("REGISTRY") + if registry == "" { + registry = "kind-registry:5000" + } + }) + + return registry +} + +var registryInsecureOnce sync.Once + +// IsRegistryInsecure returns if the registry for the e2e tests is insecure (no TLS verify) +func IsRegistryInsecure() bool { + registryInsecureOnce.Do(func() { + registryInsecure = false + if sec := os.Getenv("REGISTRY_INSECURE"); strings.ToLower(sec) == "true" { + registryInsecure = true + } + }) + + return registryInsecure +}