@@ -2,16 +2,17 @@ package controllers
22
33import (
44 "fmt"
5+ "net/http/httptest"
6+ "net/url"
7+ "testing"
8+
59 "github.com/summerwind/actions-runner-controller/api/v1alpha1"
610 "github.com/summerwind/actions-runner-controller/github"
711 "github.com/summerwind/actions-runner-controller/github/fake"
812 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
913 "k8s.io/apimachinery/pkg/runtime"
1014 clientgoscheme "k8s.io/client-go/kubernetes/scheme"
11- "net/http/httptest"
12- "net/url"
1315 "sigs.k8s.io/controller-runtime/pkg/log/zap"
14- "testing"
1516)
1617
1718func newGithubClient (server * httptest.Server ) * github.Client {
@@ -44,9 +45,11 @@ func TestDetermineDesiredReplicas_RepositoryRunner(t *testing.T) {
4445 sReplicas * int
4546 sTime * metav1.Time
4647 workflowRuns string
48+ workflowJobs map [int ]string
4749 want int
4850 err string
4951 }{
52+ // Legacy functionality
5053 // 3 demanded, max at 3
5154 {
5255 repo : "test/valid" ,
@@ -122,6 +125,21 @@ func TestDetermineDesiredReplicas_RepositoryRunner(t *testing.T) {
122125 workflowRuns : `{"total_count": 4, "workflow_runs":[{"status":"in_progress"}, {"status":"in_progress"}, {"status":"in_progress"}, {"status":"completed"}]}"` ,
123126 want : 3 ,
124127 },
128+
129+ // Job-level autoscaling
130+ // 5 requested from 3 workflows
131+ {
132+ repo : "test/valid" ,
133+ min : intPtr (2 ),
134+ max : intPtr (10 ),
135+ workflowRuns : `{"total_count": 4, "workflow_runs":[{"id": 1, "status":"queued"}, {"id": 2, "status":"in_progress"}, {"id": 3, "status":"in_progress"}, {"status":"completed"}]}"` ,
136+ workflowJobs : map [int ]string {
137+ 1 : `{"jobs": [{"status":"queued"}, {"status":"queued"}]}` ,
138+ 2 : `{"jobs": [{"status": "in_progress"}, {"status":"completed"}]}` ,
139+ 3 : `{"jobs": [{"status": "in_progress"}, {"status":"queued"}]}` ,
140+ },
141+ want : 5 ,
142+ },
125143 }
126144
127145 for i := range testcases {
@@ -136,7 +154,7 @@ func TestDetermineDesiredReplicas_RepositoryRunner(t *testing.T) {
136154 _ = v1alpha1 .AddToScheme (scheme )
137155
138156 t .Run (fmt .Sprintf ("case %d" , i ), func (t * testing.T ) {
139- server := fake .NewServer (fake .WithListRepositoryWorkflowRunsResponse (200 , tc .workflowRuns ))
157+ server := fake .NewServer (fake .WithListRepositoryWorkflowRunsResponse (200 , tc .workflowRuns ), fake . WithListWorkflowJobsResponse ( 200 , tc . workflowJobs ) )
140158 defer server .Close ()
141159 client := newGithubClient (server )
142160
@@ -211,6 +229,7 @@ func TestDetermineDesiredReplicas_OrganizationalRunner(t *testing.T) {
211229 sReplicas * int
212230 sTime * metav1.Time
213231 workflowRuns string
232+ workflowJobs map [int ]string
214233 want int
215234 err string
216235 }{
@@ -316,6 +335,22 @@ func TestDetermineDesiredReplicas_OrganizationalRunner(t *testing.T) {
316335 workflowRuns : `{"total_count": 2, "workflow_runs":[{"status":"in_progress"}, {"status":"completed"}]}"` ,
317336 err : "validating autoscaling metrics: spec.autoscaling.metrics[].repositoryNames is required and must have one more more entries for organizational runner deployment" ,
318337 },
338+
339+ // Job-level autoscaling
340+ // 5 requested from 3 workflows
341+ {
342+ org : "test" ,
343+ repos : []string {"valid" },
344+ min : intPtr (2 ),
345+ max : intPtr (10 ),
346+ workflowRuns : `{"total_count": 4, "workflow_runs":[{"id": 1, "status":"queued"}, {"id": 2, "status":"in_progress"}, {"id": 3, "status":"in_progress"}, {"status":"completed"}]}"` ,
347+ workflowJobs : map [int ]string {
348+ 1 : `{"jobs": [{"status":"queued"}, {"status":"queued"}]}` ,
349+ 2 : `{"jobs": [{"status": "in_progress"}, {"status":"completed"}]}` ,
350+ 3 : `{"jobs": [{"status": "in_progress"}, {"status":"queued"}]}` ,
351+ },
352+ want : 5 ,
353+ },
319354 }
320355
321356 for i := range testcases {
@@ -330,7 +365,7 @@ func TestDetermineDesiredReplicas_OrganizationalRunner(t *testing.T) {
330365 _ = v1alpha1 .AddToScheme (scheme )
331366
332367 t .Run (fmt .Sprintf ("case %d" , i ), func (t * testing.T ) {
333- server := fake .NewServer (fake .WithListRepositoryWorkflowRunsResponse (200 , tc .workflowRuns ))
368+ server := fake .NewServer (fake .WithListRepositoryWorkflowRunsResponse (200 , tc .workflowRuns ), fake . WithListWorkflowJobsResponse ( 200 , tc . workflowJobs ) )
334369 defer server .Close ()
335370 client := newGithubClient (server )
336371
0 commit comments