Skip to content

Commit

Permalink
test: Improve e2e test setup error reporting
Browse files Browse the repository at this point in the history
- collect required environment variable checks into one place
- report all missing required env-vars instead of just the first failure
  • Loading branch information
aThorp96 committed Feb 17, 2025
1 parent a0fc668 commit b41ff42
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 72 deletions.
9 changes: 4 additions & 5 deletions test/pkg/bitbucketcloud/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/bitbucketcloud"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/options"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/repository"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/setup"
"gotest.tools/v3/assert"
)

Expand All @@ -22,12 +23,10 @@ func Setup(ctx context.Context) (*params.Run, options.E2E, bitbucketcloud.Provid
bitbucketWSOwner := os.Getenv("TEST_BITBUCKET_CLOUD_E2E_REPOSITORY")
bitbucketCloudAPIURL := os.Getenv("TEST_BITBUCKET_CLOUD_API_URL")

for _, value := range []string{
if err := setup.RequireEnvs(
"BITBUCKET_CLOUD_TOKEN", "BITBUCKET_CLOUD_E2E_REPOSITORY", "BITBUCKET_CLOUD_API_URL",
} {
if env := os.Getenv("TEST_" + value); env == "" {
return nil, options.E2E{}, bitbucketcloud.Provider{}, fmt.Errorf("\"TEST_%s\" env variable is required, skipping", value)
}
); err != nil {
return nil, options.E2E{}, bitbucketcloud.Provider{}, err
}

split := strings.Split(bitbucketWSOwner, "/")
Expand Down
10 changes: 4 additions & 6 deletions test/pkg/bitbucketserver/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bitbucketserver

import (
"context"
"fmt"
"net/http"
"os"
"strings"
Expand All @@ -12,6 +11,7 @@ import (
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/options"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/repository"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/setup"

"github.com/jenkins-x/go-scm/scm"
"github.com/jenkins-x/go-scm/scm/driver/stash"
Expand All @@ -25,12 +25,10 @@ func Setup(ctx context.Context) (context.Context, *params.Run, options.E2E, *scm
bitbucketWSOwner := os.Getenv("TEST_BITBUCKET_SERVER_E2E_REPOSITORY")
bitbucketServerAPIURL := os.Getenv("TEST_BITBUCKET_SERVER_API_URL")

for _, value := range []string{
if err := setup.RequireEnvs(
"BITBUCKET_SERVER_USER", "BITBUCKET_SERVER_TOKEN", "BITBUCKET_SERVER_E2E_REPOSITORY", "BITBUCKET_SERVER_API_URL", "BITBUCKET_SERVER_WEBHOOK_SECRET",
} {
if env := os.Getenv("TEST_" + value); env == "" {
return ctx, nil, options.E2E{}, nil, fmt.Errorf("\"TEST_%s\" env variable is required, skipping", value)
}
); err != nil {
return ctx, nil, options.E2E{}, nil, err
}

split := strings.Split(bitbucketWSOwner, "/")
Expand Down
25 changes: 12 additions & 13 deletions test/pkg/gitea/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/gitea"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/options"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/repository"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/setup"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -38,23 +39,21 @@ func CreateProvider(ctx context.Context, giteaURL, user, password string) (gitea
}

func Setup(ctx context.Context) (*params.Run, options.E2E, gitea.Provider, error) {
if err := setup.RequireEnvs(
"TEST_EL_URL",
"TEST_GITEA_API_URL",
"TEST_GITEA_PASSWORD",
"TEST_GITEA_REPO_OWNER",
"TEST_EL_WEBHOOK_SECRET",
"TEST_GITEA_SMEEURL",
); err != nil {
return nil, options.E2E{}, gitea.Provider{}, err
}

giteaURL := os.Getenv("TEST_GITEA_API_URL")
giteaPassword := os.Getenv("TEST_GITEA_PASSWORD")
giteaRepoOwner := os.Getenv("TEST_GITEA_REPO_OWNER")

for _, value := range []string{
"EL_URL",
"GITEA_API_URL",
"GITEA_PASSWORD",
"GITEA_REPO_OWNER",
"EL_WEBHOOK_SECRET",
"GITEA_SMEEURL",
} {
if env := os.Getenv("TEST_" + value); env == "" {
return nil, options.E2E{}, gitea.Provider{}, fmt.Errorf("\"TEST_%s\" env variable is required, cannot continue", value)
}
}

var split []string
if giteaURL == "" || giteaPassword == "" || giteaRepoOwner == "" {
return nil, options.E2E{}, gitea.Provider{}, fmt.Errorf("TEST_GITEA_API_URL TEST_GITEA_PASSWORD TEST_GITEA_REPO_OWNER need to be set")
Expand Down
51 changes: 20 additions & 31 deletions test/pkg/github/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,44 @@ import (
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/github"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/cctx"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/options"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/setup"
)

func Setup(ctx context.Context, onSecondController, viaDirectWebhook bool) (context.Context, *params.Run, options.E2E, *github.Provider, error) {
if err := setup.RequireEnvs(
"TEST_EL_URL",
"TEST_GITHUB_API_URL",
"TEST_GITHUB_TOKEN",
"TEST_GITHUB_REPO_OWNER_GITHUBAPP",
"TEST_EL_WEBHOOK_SECRET",
); err != nil {
return ctx, nil, options.E2E{}, github.New(), err
}

githubToken := ""
githubURL := os.Getenv("TEST_GITHUB_API_URL")
githubRepoOwnerGithubApp := os.Getenv("TEST_GITHUB_REPO_OWNER_GITHUBAPP")
githubRepoOwnerDirectWebhook := os.Getenv("TEST_GITHUB_REPO_OWNER_WEBHOOK")

// EL_URL mean CONTROLLER URL, it's called el_url because a long time ago pac was based on trigger
for _, value := range []string{
"EL_URL",
"GITHUB_API_URL",
"GITHUB_TOKEN",
"GITHUB_REPO_OWNER_GITHUBAPP",
"EL_WEBHOOK_SECRET",
} {
if env := os.Getenv("TEST_" + value); env == "" {
return ctx, nil, options.E2E{}, github.New(), fmt.Errorf("\"TEST_%s\" env variable is required, cannot continue", value)
}
}
controllerURL := os.Getenv("TEST_EL_URL")

if onSecondController {
for _, value := range []string{
if err := setup.RequireEnvs(
"TEST_GITHUB_SECOND_API_URL",
"TEST_GITHUB_SECOND_REPO_OWNER_GITHUBAPP",
"TEST_GITHUB_SECOND_TOKEN",
"TEST_GITHUB_SECOND_EL_URL",
} {
if env := os.Getenv(value); env == "" {
return ctx, nil, options.E2E{}, github.New(), fmt.Errorf("\"%s\" env variable is required for testing a second controller, cannot continue", value)
}
); err != nil {
return ctx, nil, options.E2E{}, github.New(), err
}
}
controllerURL := os.Getenv("TEST_EL_URL")
if controllerURL == "" {
return ctx, nil, options.E2E{}, github.New(), fmt.Errorf("TEST_EL_URL variable is required, cannot continue")
}

var split []string
if !viaDirectWebhook {
if githubURL == "" || githubRepoOwnerGithubApp == "" {
return ctx, nil, options.E2E{}, github.New(), fmt.Errorf("TEST_GITHUB_API_URL TEST_GITHUB_REPO_OWNER_GITHUBAPP need to be set")
}
split = strings.Split(githubRepoOwnerGithubApp, "/")
}
if viaDirectWebhook {
githubToken = os.Getenv("TEST_GITHUB_TOKEN")
if githubURL == "" || githubToken == "" || githubRepoOwnerDirectWebhook == "" {
return ctx, nil, options.E2E{}, github.New(), fmt.Errorf("TEST_GITHUB_API_URL TEST_GITHUB_TOKEN TEST_GITHUB_REPO_OWNER_WEBHOOK need to be set")
}
split = strings.Split(githubRepoOwnerDirectWebhook, "/")
}
if onSecondController {
Expand Down Expand Up @@ -89,14 +78,14 @@ func Setup(ctx context.Context, onSecondController, viaDirectWebhook bool) (cont
return ctx, nil, options.E2E{}, github.New(), err
}

envGithubRepoInstallationID := os.Getenv("TEST_GITHUB_REPO_INSTALLATION_ID")
if envGithubRepoInstallationID == "" {
return ctx, nil, options.E2E{}, github.New(), fmt.Errorf("TEST_GITHUB_REPO_INSTALLATION_ID need to be set")
envGithubRepoInstallationID, err := setup.GetRequiredEnv("TEST_GITHUB_REPO_INSTALLATION_ID")
if err != nil {
return ctx, nil, options.E2E{}, github.New(), err
}
// convert to int64 githubRepoInstallationID
githubRepoInstallationID, err := strconv.ParseInt(envGithubRepoInstallationID, 10, 64)
if err != nil {
return ctx, nil, options.E2E{}, github.New(), fmt.Errorf("TEST_GITHUB_REPO_INSTALLATION_ID need to be set")
return ctx, nil, options.E2E{}, github.New(), fmt.Errorf("TEST_GITHUB_REPO_INSTALLATION_ID env variable must be an integer but got '%s'", envGithubRepoInstallationID)
}
ns := info.GetNS(ctx)
githubToken, err = gprovider.GetAppToken(ctx, run.Clients.Kube, githubURL, githubRepoInstallationID, ns)
Expand Down
28 changes: 11 additions & 17 deletions test/pkg/gitlab/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,30 @@ import (
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/gitlab"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/options"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/repository"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/setup"
gitlab2 "gitlab.com/gitlab-org/api/client-go"
"gotest.tools/v3/assert"
)

func Setup(ctx context.Context) (*params.Run, options.E2E, gitlab.Provider, error) {
if err := setup.RequireEnvs(
"TEST_GITLAB_API_URL",
"TEST_GITLAB_TOKEN",
"TEST_GITLAB_PROJECT_ID",
"TEST_EL_WEBHOOK_SECRET",
"TEST_EL_URL",
); err != nil {
return nil, options.E2E{}, gitlab.Provider{}, err
}
gitlabURL := os.Getenv("TEST_GITLAB_API_URL")
gitlabToken := os.Getenv("TEST_GITLAB_TOKEN")
sgitlabPid := os.Getenv("TEST_GITLAB_PROJECT_ID")
gitlabPid, err := strconv.Atoi(sgitlabPid)
if err != nil {
return nil, options.E2E{}, gitlab.Provider{}, err
}

for _, value := range []string{
"API_URL", "TOKEN", "PROJECT_ID",
} {
if env := os.Getenv("TEST_GITLAB_" + value); env == "" {
return nil, options.E2E{}, gitlab.Provider{}, fmt.Errorf("\"TEST_%s\" env variable is required, cannot continue", value)
}
}

gitlabWebhookSecret := os.Getenv("TEST_EL_WEBHOOK_SECRET")
if gitlabWebhookSecret == "" {
return nil, options.E2E{}, gitlab.Provider{}, fmt.Errorf("TEST_EL_WEBHOOK_SECRET env variable is required, cannot continue")
return nil, options.E2E{}, gitlab.Provider{}, fmt.Errorf("TEST_GITLAB_PROJECT_ID env variable must be an integer, found '%v': %w", gitlabPid, err)
}

controllerURL := os.Getenv("TEST_EL_URL")
if controllerURL == "" {
return nil, options.E2E{}, gitlab.Provider{}, fmt.Errorf("TEST_EL_URL variable is required, cannot continue")
}

run := params.New()
if err := run.Clients.NewClients(ctx, &run.Info); err != nil {
Expand Down
28 changes: 28 additions & 0 deletions test/pkg/setup/setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package setup

import (
"fmt"
"os"
)

func RequireEnvs(keys ...string) error {
missingEnvs := []string{}
for _, key := range keys {
_, err := GetRequiredEnv(key)
if err != nil {
missingEnvs = append(missingEnvs, key)
}
}
if len(missingEnvs) > 0 {
return fmt.Errorf("missing required env variable(s): %v, cannot continue", missingEnvs)
}
return nil
}

func GetRequiredEnv(key string) (string, error) {
env := os.Getenv(key)
if env == "" {
return "", fmt.Errorf("\"%s\" env variable is required, cannot continue", key)
}
return env, nil
}

0 comments on commit b41ff42

Please sign in to comment.