Skip to content

Commit

Permalink
Reorganize the GitHub test
Browse files Browse the repository at this point in the history
we now use a struct and function attached to it instead of passing
everything around all the times

this will be more readable and easier to maintain

Signed-off-by: Chmouel Boudjnah <[email protected]>
  • Loading branch information
chmouel authored and savitaashture committed Feb 12, 2024
1 parent efedf88 commit b30bd22
Show file tree
Hide file tree
Showing 21 changed files with 414 additions and 266 deletions.
2 changes: 1 addition & 1 deletion pkg/opscomments/comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func TestSetEventTypeTestPipelineRun(t *testing.T) {
wantTestPr: "prname",
},
{
name: "no-ops-commenttest event type",
name: "test single event type",
comment: "/test prname",
wantType: TestSingleCommentEventType.String(),
wantTestPr: "prname",
Expand Down
3 changes: 2 additions & 1 deletion test/gitea_access_control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,8 @@ func TestGiteaPolicyAllowedOwnerFiles(t *testing.T) {
PollTimeout: twait.DefaultTimeout,
TargetSHA: npr.Head.Sha,
}
assert.NilError(t, twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts))
_, err = twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts)
assert.NilError(t, err)
time.Sleep(5 * time.Second) // “Evil does not sleep. It waits.” - Galadriel

prs, err := topts.ParamsRun.Clients.Tekton.TektonV1().PipelineRuns(topts.TargetNS).List(context.Background(), metav1.ListOptions{})
Expand Down
6 changes: 3 additions & 3 deletions test/gitea_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ my email is a true beauty and like groot, I AM pac`
PollTimeout: twait.DefaultTimeout,
TargetSHA: topts.PullRequest.Head.Sha,
}
err = twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts)
_, err = twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts)
assert.NilError(t, err)

time.Sleep(5 * time.Second)
Expand Down Expand Up @@ -338,7 +338,7 @@ func TestGiteaParamsChangedFilesCEL(t *testing.T) {
PollTimeout: twait.DefaultTimeout,
TargetSHA: topts.PullRequest.Head.Sha,
}
err = twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts)
_, err = twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts)
assert.NilError(t, err)
time.Sleep(5 * time.Second)

Expand Down Expand Up @@ -385,7 +385,7 @@ func TestGiteaParamsChangedFilesCEL(t *testing.T) {
PollTimeout: twait.DefaultTimeout,
TargetSHA: topts.PullRequest.Head.Sha,
}
err = twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts)
_, err = twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts)
assert.NilError(t, err)
time.Sleep(5 * time.Second)

Expand Down
8 changes: 4 additions & 4 deletions test/gitea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func TestGiteaConfigMaxKeepRun(t *testing.T) {
PollTimeout: twait.DefaultTimeout,
TargetSHA: topts.PullRequest.Head.Sha,
}
err := twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts)
_, err := twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts)
assert.NilError(t, err)

time.Sleep(15 * time.Second) // “Evil does not sleep. It waits.” - Galadriel
Expand Down Expand Up @@ -511,7 +511,7 @@ func TestGiteaCancelRun(t *testing.T) {
PollTimeout: twait.DefaultTimeout,
TargetSHA: topts.PullRequest.Head.Sha,
}
err := twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts)
_, err := twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts)
assert.Error(t, err, "pipelinerun has failed")

tgitea.CheckIfPipelineRunsCancelled(t, topts)
Expand Down Expand Up @@ -694,7 +694,7 @@ func TestGiteaPushToTagGreedy(t *testing.T) {
MinNumberStatus: 0,
PollTimeout: twait.DefaultTimeout,
}
err = twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts)
_, err = twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts)
assert.NilError(t, err)
}

Expand Down Expand Up @@ -750,7 +750,7 @@ func TestGiteaClusterTasks(t *testing.T) {
PollTimeout: twait.DefaultTimeout,
TargetSHA: topts.PullRequest.Head.Sha,
}
err = twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts)
_, err = twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts)
assert.NilError(t, err)

topts.CheckForStatus = "success"
Expand Down
32 changes: 16 additions & 16 deletions test/github_config_maxkeepruns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,48 @@ import (

func TestGithubMaxKeepRuns(t *testing.T) {
ctx := context.TODO()
runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, sha := tgithub.RunPullRequest(ctx, t,
"Github MaxKeepRun config",
[]string{"testdata/pipelinerun-max-keep-run-1.yaml"}, false, false)
defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts)
g := &tgithub.PRTest{
Label: "Github MaxKeepRun config",
YamlFiles: []string{"testdata/pipelinerun-max-keep-run-1.yaml"},
}
g.RunPullRequest(ctx, t)
defer g.TearDown(ctx, t)

runcnx.Clients.Log.Infof("Creating /retest in PullRequest")
_, _, err := ghcnx.Client.Issues.CreateComment(ctx,
opts.Organization,
opts.Repo, prNumber,
g.Cnx.Clients.Log.Infof("Creating /retest in PullRequest")
_, _, err := g.Provider.Client.Issues.CreateComment(ctx, g.Options.Organization, g.Options.Repo, g.PRNumber,
&ghlib.IssueComment{Body: ghlib.String("/retest")})
assert.NilError(t, err)

runcnx.Clients.Log.Infof("Wait for the second repository update to be updated")
g.Cnx.Clients.Log.Infof("Wait for the second repository update to be updated")
waitOpts := twait.Opts{
RepoName: targetNS,
Namespace: targetNS,
RepoName: g.TargetNamespace,
Namespace: g.TargetNamespace,
MinNumberStatus: 2,
PollTimeout: twait.DefaultTimeout,
TargetSHA: sha,
TargetSHA: g.SHA,
}
err = twait.UntilRepositoryUpdated(ctx, runcnx.Clients, waitOpts)
_, err = twait.UntilRepositoryUpdated(ctx, g.Cnx.Clients, waitOpts)
assert.NilError(t, err)

count := 0
for {
prs, err := runcnx.Clients.Tekton.TektonV1().PipelineRuns(targetNS).List(ctx, metav1.ListOptions{})
prs, err := g.Cnx.Clients.Tekton.TektonV1().PipelineRuns(g.TargetNamespace).List(ctx, metav1.ListOptions{})
if err == nil && len(prs.Items) == 1 {
if prs.Items[0].GetStatusCondition().GetCondition(apis.ConditionSucceeded).GetReason() == "Running" {
t.Logf("skipping %s since currently running", prs.Items[0].GetName())
continue
}
// making sure secret is not deleted for existing pipelinerun
if secretName, ok := prs.Items[0].GetAnnotations()[keys.GitAuthSecret]; ok {
sData, err := runcnx.Clients.Kube.CoreV1().Secrets(targetNS).Get(ctx, secretName, metav1.GetOptions{})
sData, err := g.Cnx.Clients.Kube.CoreV1().Secrets(g.TargetNamespace).Get(ctx, secretName, metav1.GetOptions{})
assert.NilError(t, err, "Secret should not have been deleted while running pipelinerun")
assert.Assert(t, sData.Name != "")
}
break
}
time.Sleep(10 * time.Second)
if count > 10 {
t.Fatalf("PipelineRun cleanups has not been done, we found %d in %s", len(prs.Items), targetNS)
t.Fatalf("PipelineRun cleanups has not been done, we found %d in %s", len(prs.Items), g.TargetNamespace)
}
count++
}
Expand Down
12 changes: 11 additions & 1 deletion test/github_incoming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,17 @@ func verifyIncomingWebhook(t *testing.T, randomedString string, entries map[stri
runcnx.Clients.Log.Infof("Kicked off on incoming URL: %s", url)
assert.Assert(t, httpResp.StatusCode >= 200 && httpResp.StatusCode < 300)
// to re enable after debugging...
defer tgithub.TearDown(ctx, t, runcnx, ghprovider, -1, targetRefName, randomedString, opts)
g := tgithub.PRTest{
Cnx: runcnx,
Options: opts,
Provider: ghprovider,
TargetNamespace: randomedString,
TargetRefName: targetRefName,
PRNumber: -1,
SHA: sha,
Logger: runcnx.Clients.Log,
}
defer g.TearDown(ctx, t)

sopt := wait.SuccessOpt{
Title: title,
Expand Down
12 changes: 11 additions & 1 deletion test/github_pullrequest_concurrency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,17 @@ func TestGithubPullRequestConcurrency(t *testing.T) {
prNumber, err := tgithub.PRCreate(ctx, runcnx, ghcnx, opts.Organization,
opts.Repo, targetRefName, repoinfo.GetDefaultBranch(), logmsg)
assert.NilError(t, err)
defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts)
g := tgithub.PRTest{
Cnx: runcnx,
Options: opts,
Provider: ghcnx,
TargetNamespace: targetNS,
TargetRefName: targetRefName,
PRNumber: prNumber,
SHA: sha,
Logger: runcnx.Clients.Log,
}
defer g.TearDown(ctx, t)

runcnx.Clients.Log.Info("waiting to let controller process the event")
time.Sleep(5 * time.Second)
Expand Down
37 changes: 20 additions & 17 deletions test/github_pullrequest_oktotest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,25 @@ func TestGithubPullRequestOkToTest(t *testing.T) {
t.Skip("Skipping test since only enabled for nightly")
}
ctx := context.TODO()
runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, sha := tgithub.RunPullRequest(ctx, t,
"Github OkToTest comment", []string{"testdata/pipelinerun.yaml"}, false, false)
defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts)
g := &tgithub.PRTest{
Label: "Github OkToTest comment",
YamlFiles: []string{"testdata/pipelinerun.yaml"},
}
g.RunPullRequest(ctx, t)
defer g.TearDown(ctx, t)

repoinfo, resp, err := ghcnx.Client.Repositories.Get(ctx, opts.Organization, opts.Repo)
repoinfo, resp, err := g.Provider.Client.Repositories.Get(ctx, g.Options.Organization, g.Options.Repo)
assert.NilError(t, err)
if resp != nil && resp.StatusCode == http.StatusNotFound {
t.Errorf("Repository %s not found in %s", opts.Organization, opts.Repo)
t.Errorf("Repository %s not found in %s", g.Options.Organization, g.Options.Repo)
}

runevent := info.Event{
DefaultBranch: repoinfo.GetDefaultBranch(),
Organization: opts.Organization,
Repository: opts.Repo,
Organization: g.Options.Organization,
Repository: g.Options.Repo,
URL: repoinfo.GetHTMLURL(),
Sender: opts.Organization,
Sender: g.Options.Organization,
}

installID, err := strconv.ParseInt(os.Getenv("TEST_GITHUB_REPO_INSTALLATION_ID"), 10, 64)
Expand All @@ -59,7 +62,7 @@ func TestGithubPullRequestOkToTest(t *testing.T) {
PullRequestLinks: &github.PullRequestLinks{
HTMLURL: github.String(fmt.Sprintf("%s/%s/pull/%d",
os.Getenv("TEST_GITHUB_API_URL"),
os.Getenv("TEST_GITHUB_REPO_OWNER"), prNumber)),
os.Getenv("TEST_GITHUB_REPO_OWNER"), g.PRNumber)),
},
},
Repo: &github.Repository{
Expand All @@ -74,7 +77,7 @@ func TestGithubPullRequestOkToTest(t *testing.T) {
}

err = payload.Send(ctx,
runcnx,
g.Cnx,
os.Getenv("TEST_EL_URL"),
os.Getenv("TEST_EL_WEBHOOK_SECRET"),
os.Getenv("TEST_GITHUB_API_URL"),
Expand All @@ -84,19 +87,19 @@ func TestGithubPullRequestOkToTest(t *testing.T) {
)
assert.NilError(t, err)

runcnx.Clients.Log.Infof("Wait for the second repository update to be updated")
g.Cnx.Clients.Log.Infof("Wait for the second repository update to be updated")
waitOpts := twait.Opts{
RepoName: targetNS,
Namespace: targetNS,
RepoName: g.TargetNamespace,
Namespace: g.TargetNamespace,
MinNumberStatus: 1,
PollTimeout: twait.DefaultTimeout,
TargetSHA: sha,
TargetSHA: g.SHA,
}
err = twait.UntilRepositoryUpdated(ctx, runcnx.Clients, waitOpts)
_, err = twait.UntilRepositoryUpdated(ctx, g.Cnx.Clients, waitOpts)
assert.NilError(t, err)

runcnx.Clients.Log.Infof("Check if we have the repository set as succeeded")
repo, err := runcnx.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(targetNS).Get(ctx, targetNS, metav1.GetOptions{})
g.Cnx.Clients.Log.Infof("Check if we have the repository set as succeeded")
repo, err := g.Cnx.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(g.TargetNamespace).Get(ctx, g.TargetNamespace, metav1.GetOptions{})
assert.NilError(t, err)
assert.Assert(t, repo.Status[len(repo.Status)-1].Conditions[0].Status == corev1.ConditionTrue)
}
29 changes: 20 additions & 9 deletions test/github_pullrequest_privaterepository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,37 @@ func TestGithubPullRequestGitClone(t *testing.T) {
t.Skip("Skipping test since only enabled for nightly")
}
ctx := context.Background()
runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, _ := tgithub.RunPullRequest(ctx, t,
"Github Private Repo", []string{"testdata/pipelinerun_git_clone_private.yaml"}, false, false)
defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts)
g := &tgithub.PRTest{
Label: "Github - Private Repo",
YamlFiles: []string{"testdata/pipelinerun_git_clone_private.yaml"},
}
g.RunPullRequest(ctx, t)
defer g.TearDown(ctx, t)
}

func TestGithubSecondPullRequestGitClone(t *testing.T) {
ctx := context.Background()
runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, _ := tgithub.RunPullRequest(ctx, t,
"Github Private Repo", []string{"testdata/pipelinerun_git_clone_private.yaml"}, true, false)
defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts)
g := &tgithub.PRTest{
Label: "Github GHE - Private Repo",
YamlFiles: []string{"testdata/pipelinerun_git_clone_private.yaml"},
SecondController: true,
}
g.RunPullRequest(ctx, t)
defer g.TearDown(ctx, t)
}

func TestGithubPullRequestPrivateRepositoryOnWebhook(t *testing.T) {
if os.Getenv("NIGHTLY_E2E_TEST") != "true" {
t.Skip("Skipping test since only enabled for nightly")
}
ctx := context.Background()
runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, _ := tgithub.RunPullRequest(ctx, t,
"Github Private Repo OnWebhook", []string{"testdata/pipelinerun_git_clone_private.yaml"}, false, true)
defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts)
g := &tgithub.PRTest{
Label: "Github Rerequest",
YamlFiles: []string{"testdata/pipelinerun_git_clone_private.yaml"},
Webhook: true,
}
g.RunPullRequest(ctx, t)
defer g.TearDown(ctx, t)
}

// Local Variables:
Expand Down
Loading

0 comments on commit b30bd22

Please sign in to comment.