Skip to content

Commit

Permalink
feat(scheduler): opt-in scheduling on PR merge
Browse files Browse the repository at this point in the history
  • Loading branch information
danilo-gemoli committed Apr 2, 2024
1 parent d9f7891 commit 71a71ae
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion prow/plugins/trigger/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func handlePE(c Client, pe github.PushEvent) error {
labels[k] = v
}
labels[github.EventGUID] = pe.GUID
pj := pjutil.NewProwJob(pjutil.PostsubmitSpec(j, refs), labels, j.Annotations)
pj := pjutil.NewProwJob(pjutil.PostsubmitSpec(j, refs), labels, j.Annotations, pjutil.RequireScheduling(c.Config.Scheduler.Enabled))
c.Logger.WithFields(pjutil.ProwJobFields(&pj)).Info("Creating a new prowjob.")
if err := createWithRetry(context.TODO(), c.ProwJobClient, &pj); err != nil {
return err
Expand Down
77 changes: 77 additions & 0 deletions prow/plugins/trigger/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ limitations under the License.
package trigger

import (
"context"
"testing"

"github.com/sirupsen/logrus"
clienttesting "k8s.io/client-go/testing"

"k8s.io/apimachinery/pkg/api/equality"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/diff"
prowapi "k8s.io/test-infra/prow/apis/prowjobs/v1"
"k8s.io/test-infra/prow/client/clientset/versioned/fake"
Expand Down Expand Up @@ -210,3 +212,78 @@ func TestHandlePE(t *testing.T) {
}
}
}

func TestHandlePEScheduling(t *testing.T) {
job := config.JobBase{Name: "job"}
postsubmits := map[string][]config.Postsubmit{"org/repo": {{JobBase: job}}}

for _, tc := range []struct {
name string
enableScheduling bool
pe github.PushEvent
wantPJState prowapi.ProwJobState
}{
{
name: "Create job in triggered state",
pe: github.PushEvent{
Ref: "refs/heads/master",
Repo: github.Repo{
Owner: github.User{Login: "org"},
Name: "repo",
},
},
wantPJState: prowapi.TriggeredState,
},
{
name: "Create job in scheduling state",
enableScheduling: true,
pe: github.PushEvent{
Ref: "refs/heads/master",
Repo: github.Repo{
Owner: github.User{Login: "org"},
Name: "repo",
},
},
wantPJState: prowapi.SchedulingState,
},
} {
t.Run(tc.name, func(t *testing.T) {
ghClient := fakegithub.NewFakeClient()
fakeProwJobClient := fake.NewSimpleClientset()
c := Client{
GitHubClient: ghClient,
ProwJobClient: fakeProwJobClient.ProwV1().ProwJobs("prowjobs"),
Config: &config.Config{ProwConfig: config.ProwConfig{
ProwJobNamespace: "prowjobs",
Scheduler: config.Scheduler{Enabled: tc.enableScheduling},
}},
Logger: logrus.WithField("plugin", PluginName),
}

c.Config.SetPostsubmits(postsubmits)

err := handlePE(c, tc.pe)
if err != nil {
t.Errorf("test %q: handlePE returned unexpected error %v", tc.name, err)
}

pjs, err := c.ProwJobClient.List(context.TODO(), v1.ListOptions{})
if err != nil {
t.Fatalf("Couldn't get PJs from the fake client: %s", err)
}

if len(pjs.Items) != 1 {
t.Errorf("Expected 1 job but got %d", len(pjs.Items))
}

resultPJ := pjs.Items[0]
if job.Name != resultPJ.Spec.Job {
t.Errorf("Expected job %s but got %s", job.Name, resultPJ.Spec.Job)
}

if tc.wantPJState != resultPJ.Status.State {
t.Errorf("Expected state %s but got %s", tc.wantPJState, resultPJ.Status.State)
}
})
}
}

0 comments on commit 71a71ae

Please sign in to comment.