548 lines
		
	
	
		
			18 KiB
		
	
	
	
		
			Go
		
	
	
	
			
		
		
	
	
			548 lines
		
	
	
		
			18 KiB
		
	
	
	
		
			Go
		
	
	
	
| package controllers
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"fmt"
 | |
| 	"github.com/google/go-github/v33/github"
 | |
| 	github3 "github.com/google/go-github/v33/github"
 | |
| 	github2 "github.com/summerwind/actions-runner-controller/github"
 | |
| 	"k8s.io/apimachinery/pkg/runtime"
 | |
| 	"net/http"
 | |
| 	"net/http/httptest"
 | |
| 	"time"
 | |
| 
 | |
| 	"github.com/summerwind/actions-runner-controller/github/fake"
 | |
| 
 | |
| 	corev1 "k8s.io/api/core/v1"
 | |
| 	"k8s.io/apimachinery/pkg/types"
 | |
| 	"k8s.io/client-go/kubernetes/scheme"
 | |
| 	ctrl "sigs.k8s.io/controller-runtime"
 | |
| 	logf "sigs.k8s.io/controller-runtime/pkg/log"
 | |
| 
 | |
| 	. "github.com/onsi/ginkgo"
 | |
| 	. "github.com/onsi/gomega"
 | |
| 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 | |
| 	"sigs.k8s.io/controller-runtime/pkg/client"
 | |
| 
 | |
| 	actionsv1alpha1 "github.com/summerwind/actions-runner-controller/api/v1alpha1"
 | |
| )
 | |
| 
 | |
| type testEnvironment struct {
 | |
| 	Namespace *corev1.Namespace
 | |
| 	Responses *fake.FixedResponses
 | |
| 
 | |
| 	webhookServer    *httptest.Server
 | |
| 	ghClient         *github2.Client
 | |
| 	fakeRunnerList   *fake.RunnersList
 | |
| 	fakeGithubServer *httptest.Server
 | |
| }
 | |
| 
 | |
| var (
 | |
| 	workflowRunsFor3Replicas             = `{"total_count": 5, "workflow_runs":[{"status":"queued"}, {"status":"queued"}, {"status":"in_progress"}, {"status":"in_progress"}, {"status":"completed"}]}"`
 | |
| 	workflowRunsFor3Replicas_queued      = `{"total_count": 2, "workflow_runs":[{"status":"queued"}, {"status":"queued"}]}"`
 | |
| 	workflowRunsFor3Replicas_in_progress = `{"total_count": 1, "workflow_runs":[{"status":"in_progress"}]}"`
 | |
| 	workflowRunsFor1Replicas             = `{"total_count": 6, "workflow_runs":[{"status":"queued"}, {"status":"completed"}, {"status":"completed"}, {"status":"completed"}, {"status":"completed"}]}"`
 | |
| 	workflowRunsFor1Replicas_queued      = `{"total_count": 1, "workflow_runs":[{"status":"queued"}]}"`
 | |
| 	workflowRunsFor1Replicas_in_progress = `{"total_count": 0, "workflow_runs":[]}"`
 | |
| )
 | |
| 
 | |
| // SetupIntegrationTest will set up a testing environment.
 | |
| // This includes:
 | |
| // * creating a Namespace to be used during the test
 | |
| // * starting all the reconcilers
 | |
| // * stopping all the reconcilers after the test ends
 | |
| // Call this function at the start of each of your tests.
 | |
| func SetupIntegrationTest(ctx context.Context) *testEnvironment {
 | |
| 	var stopCh chan struct{}
 | |
| 	ns := &corev1.Namespace{}
 | |
| 
 | |
| 	env := &testEnvironment{
 | |
| 		Namespace:     ns,
 | |
| 		webhookServer: nil,
 | |
| 		ghClient:      nil,
 | |
| 	}
 | |
| 
 | |
| 	BeforeEach(func() {
 | |
| 		stopCh = make(chan struct{})
 | |
| 		*ns = corev1.Namespace{
 | |
| 			ObjectMeta: metav1.ObjectMeta{Name: "testns-" + randStringRunes(5)},
 | |
| 		}
 | |
| 
 | |
| 		err := k8sClient.Create(ctx, ns)
 | |
| 		Expect(err).NotTo(HaveOccurred(), "failed to create test namespace")
 | |
| 
 | |
| 		mgr, err := ctrl.NewManager(cfg, ctrl.Options{})
 | |
| 		Expect(err).NotTo(HaveOccurred(), "failed to create manager")
 | |
| 
 | |
| 		responses := &fake.FixedResponses{}
 | |
| 		responses.ListRunners = fake.DefaultListRunnersHandler()
 | |
| 		responses.ListRepositoryWorkflowRuns = &fake.Handler{
 | |
| 			Status: 200,
 | |
| 			Body:   workflowRunsFor3Replicas,
 | |
| 			Statuses: map[string]string{
 | |
| 				"queued":      workflowRunsFor3Replicas_queued,
 | |
| 				"in_progress": workflowRunsFor3Replicas_in_progress,
 | |
| 			},
 | |
| 		}
 | |
| 		fakeRunnerList := fake.NewRunnersList()
 | |
| 		responses.ListRunners = fakeRunnerList.HandleList()
 | |
| 		fakeGithubServer := fake.NewServer(fake.WithFixedResponses(responses))
 | |
| 
 | |
| 		env.Responses = responses
 | |
| 		env.fakeRunnerList = fakeRunnerList
 | |
| 		env.fakeGithubServer = fakeGithubServer
 | |
| 		env.ghClient = newGithubClient(fakeGithubServer)
 | |
| 
 | |
| 		controllerName := func(name string) string {
 | |
| 			return fmt.Sprintf("%s%s", ns.Name, name)
 | |
| 		}
 | |
| 
 | |
| 		replicasetController := &RunnerReplicaSetReconciler{
 | |
| 			Client:       mgr.GetClient(),
 | |
| 			Scheme:       scheme.Scheme,
 | |
| 			Log:          logf.Log,
 | |
| 			Recorder:     mgr.GetEventRecorderFor("runnerreplicaset-controller"),
 | |
| 			GitHubClient: env.ghClient,
 | |
| 			Name:         controllerName("runnerreplicaset"),
 | |
| 		}
 | |
| 		err = replicasetController.SetupWithManager(mgr)
 | |
| 		Expect(err).NotTo(HaveOccurred(), "failed to setup controller")
 | |
| 
 | |
| 		deploymentsController := &RunnerDeploymentReconciler{
 | |
| 			Client:   mgr.GetClient(),
 | |
| 			Scheme:   scheme.Scheme,
 | |
| 			Log:      logf.Log,
 | |
| 			Recorder: mgr.GetEventRecorderFor("runnerdeployment-controller"),
 | |
| 			Name:     controllerName("runnnerdeployment"),
 | |
| 		}
 | |
| 		err = deploymentsController.SetupWithManager(mgr)
 | |
| 		Expect(err).NotTo(HaveOccurred(), "failed to setup controller")
 | |
| 
 | |
| 		autoscalerController := &HorizontalRunnerAutoscalerReconciler{
 | |
| 			Client:        mgr.GetClient(),
 | |
| 			Scheme:        scheme.Scheme,
 | |
| 			Log:           logf.Log,
 | |
| 			GitHubClient:  env.ghClient,
 | |
| 			Recorder:      mgr.GetEventRecorderFor("horizontalrunnerautoscaler-controller"),
 | |
| 			CacheDuration: 1 * time.Second,
 | |
| 			Name:          controllerName("horizontalrunnerautoscaler"),
 | |
| 		}
 | |
| 		err = autoscalerController.SetupWithManager(mgr)
 | |
| 		Expect(err).NotTo(HaveOccurred(), "failed to setup controller")
 | |
| 
 | |
| 		autoscalerWebhook := &HorizontalRunnerAutoscalerGitHubWebhook{
 | |
| 			Client:   mgr.GetClient(),
 | |
| 			Scheme:   scheme.Scheme,
 | |
| 			Log:      logf.Log,
 | |
| 			Recorder: mgr.GetEventRecorderFor("horizontalrunnerautoscaler-controller"),
 | |
| 			Name:     controllerName("horizontalrunnerautoscalergithubwebhook"),
 | |
| 		}
 | |
| 		err = autoscalerWebhook.SetupWithManager(mgr)
 | |
| 		Expect(err).NotTo(HaveOccurred(), "failed to setup autoscaler webhook")
 | |
| 
 | |
| 		mux := http.NewServeMux()
 | |
| 		mux.HandleFunc("/", autoscalerWebhook.Handle)
 | |
| 
 | |
| 		env.webhookServer = httptest.NewServer(mux)
 | |
| 
 | |
| 		go func() {
 | |
| 			defer GinkgoRecover()
 | |
| 
 | |
| 			err := mgr.Start(stopCh)
 | |
| 			Expect(err).NotTo(HaveOccurred(), "failed to start manager")
 | |
| 		}()
 | |
| 	})
 | |
| 
 | |
| 	AfterEach(func() {
 | |
| 		close(stopCh)
 | |
| 
 | |
| 		env.fakeGithubServer.Close()
 | |
| 		env.webhookServer.Close()
 | |
| 
 | |
| 		err := k8sClient.Delete(ctx, ns)
 | |
| 		Expect(err).NotTo(HaveOccurred(), "failed to delete test namespace")
 | |
| 	})
 | |
| 
 | |
| 	return env
 | |
| }
 | |
| 
 | |
| var _ = Context("INTEGRATION: Inside of a new namespace", func() {
 | |
| 	ctx := context.TODO()
 | |
| 	env := SetupIntegrationTest(ctx)
 | |
| 	ns := env.Namespace
 | |
| 
 | |
| 	Describe("when no existing resources exist", func() {
 | |
| 
 | |
| 		It("should create and scale runners on pull_request event", func() {
 | |
| 			name := "example-runnerdeploy"
 | |
| 
 | |
| 			{
 | |
| 				rd := &actionsv1alpha1.RunnerDeployment{
 | |
| 					ObjectMeta: metav1.ObjectMeta{
 | |
| 						Name:      name,
 | |
| 						Namespace: ns.Name,
 | |
| 					},
 | |
| 					Spec: actionsv1alpha1.RunnerDeploymentSpec{
 | |
| 						Replicas: intPtr(1),
 | |
| 						Template: actionsv1alpha1.RunnerTemplate{
 | |
| 							Spec: actionsv1alpha1.RunnerSpec{
 | |
| 								Repository: "test/valid",
 | |
| 								Image:      "bar",
 | |
| 								Group:      "baz",
 | |
| 								Env: []corev1.EnvVar{
 | |
| 									{Name: "FOO", Value: "FOOVALUE"},
 | |
| 								},
 | |
| 							},
 | |
| 						},
 | |
| 					},
 | |
| 				}
 | |
| 
 | |
| 				ExpectCreate(ctx, rd, "test RunnerDeployment")
 | |
| 				ExpectRunnerSetsCountEventuallyEquals(ctx, ns.Name, 1)
 | |
| 				ExpectRunnerSetsManagedReplicasCountEventuallyEquals(ctx, ns.Name, 1)
 | |
| 			}
 | |
| 
 | |
| 			{
 | |
| 				ExpectRunnerDeploymentEventuallyUpdates(ctx, ns.Name, name, func(rd *actionsv1alpha1.RunnerDeployment) {
 | |
| 					rd.Spec.Replicas = intPtr(2)
 | |
| 				})
 | |
| 				ExpectRunnerSetsCountEventuallyEquals(ctx, ns.Name, 1)
 | |
| 				ExpectRunnerSetsManagedReplicasCountEventuallyEquals(ctx, ns.Name, 2)
 | |
| 			}
 | |
| 
 | |
| 			// Scale-up to 3 replicas
 | |
| 			{
 | |
| 				hra := &actionsv1alpha1.HorizontalRunnerAutoscaler{
 | |
| 					ObjectMeta: metav1.ObjectMeta{
 | |
| 						Name:      name,
 | |
| 						Namespace: ns.Name,
 | |
| 					},
 | |
| 					Spec: actionsv1alpha1.HorizontalRunnerAutoscalerSpec{
 | |
| 						ScaleTargetRef: actionsv1alpha1.ScaleTargetRef{
 | |
| 							Name: name,
 | |
| 						},
 | |
| 						MinReplicas:                       intPtr(1),
 | |
| 						MaxReplicas:                       intPtr(3),
 | |
| 						ScaleDownDelaySecondsAfterScaleUp: intPtr(1),
 | |
| 						Metrics:                           nil,
 | |
| 						ScaleUpTriggers: []actionsv1alpha1.ScaleUpTrigger{
 | |
| 							{
 | |
| 								GitHubEvent: &actionsv1alpha1.GitHubEventScaleUpTriggerSpec{
 | |
| 									PullRequest: &actionsv1alpha1.PullRequestSpec{
 | |
| 										Types:    []string{"created"},
 | |
| 										Branches: []string{"main"},
 | |
| 									},
 | |
| 								},
 | |
| 								Amount:   1,
 | |
| 								Duration: metav1.Duration{Duration: time.Minute},
 | |
| 							},
 | |
| 						},
 | |
| 					},
 | |
| 				}
 | |
| 
 | |
| 				ExpectCreate(ctx, hra, "test HorizontalRunnerAutoscaler")
 | |
| 
 | |
| 				ExpectRunnerSetsCountEventuallyEquals(ctx, ns.Name, 1)
 | |
| 				ExpectRunnerSetsManagedReplicasCountEventuallyEquals(ctx, ns.Name, 3)
 | |
| 			}
 | |
| 
 | |
| 			{
 | |
| 				var runnerList actionsv1alpha1.RunnerList
 | |
| 
 | |
| 				err := k8sClient.List(ctx, &runnerList, client.InNamespace(ns.Name))
 | |
| 				if err != nil {
 | |
| 					logf.Log.Error(err, "list runners")
 | |
| 				}
 | |
| 
 | |
| 				for i, r := range runnerList.Items {
 | |
| 					env.fakeRunnerList.Add(&github3.Runner{
 | |
| 						ID:     github.Int64(int64(i)),
 | |
| 						Name:   github.String(r.Name),
 | |
| 						OS:     github.String("linux"),
 | |
| 						Status: github.String("online"),
 | |
| 						Busy:   github.Bool(false),
 | |
| 					})
 | |
| 				}
 | |
| 
 | |
| 				rs, err := env.ghClient.ListRunners(context.Background(), "", "", "test/valid")
 | |
| 				Expect(err).NotTo(HaveOccurred(), "verifying list fake runners response")
 | |
| 				Expect(len(rs)).To(Equal(3), "count of fake list runners")
 | |
| 			}
 | |
| 
 | |
| 			// Scale-down to 1 replica
 | |
| 			{
 | |
| 				time.Sleep(time.Second)
 | |
| 
 | |
| 				env.Responses.ListRepositoryWorkflowRuns.Body = workflowRunsFor1Replicas
 | |
| 				env.Responses.ListRepositoryWorkflowRuns.Statuses["queued"] = workflowRunsFor1Replicas_queued
 | |
| 				env.Responses.ListRepositoryWorkflowRuns.Statuses["in_progress"] = workflowRunsFor1Replicas_in_progress
 | |
| 
 | |
| 				var hra actionsv1alpha1.HorizontalRunnerAutoscaler
 | |
| 
 | |
| 				err := k8sClient.Get(ctx, types.NamespacedName{Namespace: ns.Name, Name: name}, &hra)
 | |
| 
 | |
| 				Expect(err).NotTo(HaveOccurred(), "failed to get test HorizontalRunnerAutoscaler resource")
 | |
| 
 | |
| 				hra.Annotations = map[string]string{
 | |
| 					"force-update": "1",
 | |
| 				}
 | |
| 
 | |
| 				err = k8sClient.Update(ctx, &hra)
 | |
| 
 | |
| 				Expect(err).NotTo(HaveOccurred(), "failed to get test HorizontalRunnerAutoscaler resource")
 | |
| 
 | |
| 				ExpectRunnerSetsManagedReplicasCountEventuallyEquals(ctx, ns.Name, 1, "runners after HRA force update for scale-down")
 | |
| 			}
 | |
| 
 | |
| 			// Scale-up to 2 replicas on first pull_request create webhook event
 | |
| 			{
 | |
| 				env.SendPullRequestEvent("test", "valid", "main", "created")
 | |
| 				ExpectRunnerSetsCountEventuallyEquals(ctx, ns.Name, 1, "runner sets after webhook")
 | |
| 				ExpectRunnerSetsManagedReplicasCountEventuallyEquals(ctx, ns.Name, 2, "runners after first webhook event")
 | |
| 			}
 | |
| 
 | |
| 			// Scale-up to 3 replicas on second pull_request create webhook event
 | |
| 			{
 | |
| 				env.SendPullRequestEvent("test", "valid", "main", "created")
 | |
| 				ExpectRunnerSetsManagedReplicasCountEventuallyEquals(ctx, ns.Name, 3, "runners after second webhook event")
 | |
| 			}
 | |
| 		})
 | |
| 
 | |
| 		It("should create and scale runners on check_run event", func() {
 | |
| 			name := "example-runnerdeploy"
 | |
| 
 | |
| 			{
 | |
| 				rd := &actionsv1alpha1.RunnerDeployment{
 | |
| 					ObjectMeta: metav1.ObjectMeta{
 | |
| 						Name:      name,
 | |
| 						Namespace: ns.Name,
 | |
| 					},
 | |
| 					Spec: actionsv1alpha1.RunnerDeploymentSpec{
 | |
| 						Replicas: intPtr(1),
 | |
| 						Template: actionsv1alpha1.RunnerTemplate{
 | |
| 							Spec: actionsv1alpha1.RunnerSpec{
 | |
| 								Repository: "test/valid",
 | |
| 								Image:      "bar",
 | |
| 								Group:      "baz",
 | |
| 								Env: []corev1.EnvVar{
 | |
| 									{Name: "FOO", Value: "FOOVALUE"},
 | |
| 								},
 | |
| 							},
 | |
| 						},
 | |
| 					},
 | |
| 				}
 | |
| 
 | |
| 				ExpectCreate(ctx, rd, "test RunnerDeployment")
 | |
| 				ExpectRunnerSetsCountEventuallyEquals(ctx, ns.Name, 1)
 | |
| 				ExpectRunnerSetsManagedReplicasCountEventuallyEquals(ctx, ns.Name, 1)
 | |
| 			}
 | |
| 
 | |
| 			{
 | |
| 				env.ExpectRegisteredNumberCountEventuallyEquals(1, "count of fake list runners")
 | |
| 			}
 | |
| 
 | |
| 			// Scale-up to 3 replicas by the default TotalNumberOfQueuedAndInProgressWorkflowRuns-based scaling
 | |
| 			// See workflowRunsFor3Replicas_queued and workflowRunsFor3Replicas_in_progress for GitHub List-Runners API responses
 | |
| 			// used while testing.
 | |
| 			{
 | |
| 				hra := &actionsv1alpha1.HorizontalRunnerAutoscaler{
 | |
| 					ObjectMeta: metav1.ObjectMeta{
 | |
| 						Name:      name,
 | |
| 						Namespace: ns.Name,
 | |
| 					},
 | |
| 					Spec: actionsv1alpha1.HorizontalRunnerAutoscalerSpec{
 | |
| 						ScaleTargetRef: actionsv1alpha1.ScaleTargetRef{
 | |
| 							Name: name,
 | |
| 						},
 | |
| 						MinReplicas:                       intPtr(1),
 | |
| 						MaxReplicas:                       intPtr(5),
 | |
| 						ScaleDownDelaySecondsAfterScaleUp: intPtr(1),
 | |
| 						Metrics:                           nil,
 | |
| 						ScaleUpTriggers: []actionsv1alpha1.ScaleUpTrigger{
 | |
| 							{
 | |
| 								GitHubEvent: &actionsv1alpha1.GitHubEventScaleUpTriggerSpec{
 | |
| 									CheckRun: &actionsv1alpha1.CheckRunSpec{
 | |
| 										Types:  []string{"created"},
 | |
| 										Status: "pending",
 | |
| 									},
 | |
| 								},
 | |
| 								Amount:   1,
 | |
| 								Duration: metav1.Duration{Duration: time.Minute},
 | |
| 							},
 | |
| 						},
 | |
| 					},
 | |
| 				}
 | |
| 
 | |
| 				ExpectCreate(ctx, hra, "test HorizontalRunnerAutoscaler")
 | |
| 
 | |
| 				ExpectRunnerSetsCountEventuallyEquals(ctx, ns.Name, 1)
 | |
| 				ExpectRunnerSetsManagedReplicasCountEventuallyEquals(ctx, ns.Name, 3)
 | |
| 			}
 | |
| 
 | |
| 			{
 | |
| 				env.ExpectRegisteredNumberCountEventuallyEquals(3, "count of fake list runners")
 | |
| 			}
 | |
| 
 | |
| 			// Scale-up to 4 replicas on first check_run create webhook event
 | |
| 			{
 | |
| 				env.SendCheckRunEvent("test", "valid", "pending", "created")
 | |
| 				ExpectRunnerSetsCountEventuallyEquals(ctx, ns.Name, 1, "runner sets after webhook")
 | |
| 				ExpectRunnerSetsManagedReplicasCountEventuallyEquals(ctx, ns.Name, 4, "runners after first webhook event")
 | |
| 			}
 | |
| 
 | |
| 			{
 | |
| 				env.ExpectRegisteredNumberCountEventuallyEquals(4, "count of fake list runners")
 | |
| 			}
 | |
| 
 | |
| 			// Scale-up to 5 replicas on second check_run create webhook event
 | |
| 			{
 | |
| 				env.SendCheckRunEvent("test", "valid", "pending", "created")
 | |
| 				ExpectRunnerSetsManagedReplicasCountEventuallyEquals(ctx, ns.Name, 5, "runners after second webhook event")
 | |
| 			}
 | |
| 
 | |
| 			env.ExpectRegisteredNumberCountEventuallyEquals(5, "count of fake list runners")
 | |
| 		})
 | |
| 	})
 | |
| })
 | |
| 
 | |
| func (env *testEnvironment) ExpectRegisteredNumberCountEventuallyEquals(want int, optionalDescriptions ...interface{}) {
 | |
| 	EventuallyWithOffset(
 | |
| 		1,
 | |
| 		func() int {
 | |
| 			env.SyncRunnerRegistrations()
 | |
| 
 | |
| 			rs, err := env.ghClient.ListRunners(context.Background(), "", "", "test/valid")
 | |
| 			Expect(err).NotTo(HaveOccurred(), "verifying list fake runners response")
 | |
| 
 | |
| 			return len(rs)
 | |
| 		},
 | |
| 		time.Second*1, time.Millisecond*500).Should(Equal(want), optionalDescriptions...)
 | |
| }
 | |
| 
 | |
| func (env *testEnvironment) SendPullRequestEvent(org, repo, branch, action string) {
 | |
| 	resp, err := sendWebhook(env.webhookServer, "pull_request", &github.PullRequestEvent{
 | |
| 		PullRequest: &github.PullRequest{
 | |
| 			Base: &github.PullRequestBranch{
 | |
| 				Ref: github.String(branch),
 | |
| 			},
 | |
| 		},
 | |
| 		Repo: &github.Repository{
 | |
| 			Name: github.String(repo),
 | |
| 			Organization: &github.Organization{
 | |
| 				Name: github.String(org),
 | |
| 			},
 | |
| 		},
 | |
| 		Action: github.String(action),
 | |
| 	})
 | |
| 
 | |
| 	ExpectWithOffset(1, err).NotTo(HaveOccurred(), "failed to send pull_request event")
 | |
| 
 | |
| 	ExpectWithOffset(1, resp.StatusCode).To(Equal(200))
 | |
| }
 | |
| 
 | |
| func (env *testEnvironment) SendCheckRunEvent(org, repo, status, action string) {
 | |
| 	resp, err := sendWebhook(env.webhookServer, "check_run", &github.CheckRunEvent{
 | |
| 		CheckRun: &github.CheckRun{
 | |
| 			Status: github.String(status),
 | |
| 		},
 | |
| 		Org: &github.Organization{
 | |
| 			Login: github.String(org),
 | |
| 		},
 | |
| 		Repo: &github.Repository{
 | |
| 			Name: github.String(repo),
 | |
| 		},
 | |
| 		Action: github.String(action),
 | |
| 	})
 | |
| 
 | |
| 	ExpectWithOffset(1, err).NotTo(HaveOccurred(), "failed to send check_run event")
 | |
| 
 | |
| 	ExpectWithOffset(1, resp.StatusCode).To(Equal(200))
 | |
| }
 | |
| 
 | |
| func (env *testEnvironment) SyncRunnerRegistrations() {
 | |
| 	var runnerList actionsv1alpha1.RunnerList
 | |
| 
 | |
| 	err := k8sClient.List(context.TODO(), &runnerList, client.InNamespace(env.Namespace.Name))
 | |
| 	if err != nil {
 | |
| 		logf.Log.Error(err, "list runners")
 | |
| 	}
 | |
| 
 | |
| 	for i, r := range runnerList.Items {
 | |
| 		env.fakeRunnerList.Add(&github3.Runner{
 | |
| 			ID:     github.Int64(int64(i)),
 | |
| 			Name:   github.String(r.Name),
 | |
| 			OS:     github.String("linux"),
 | |
| 			Status: github.String("online"),
 | |
| 			Busy:   github.Bool(false),
 | |
| 		})
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func ExpectCreate(ctx context.Context, rd runtime.Object, s string) {
 | |
| 	err := k8sClient.Create(ctx, rd)
 | |
| 
 | |
| 	ExpectWithOffset(1, err).NotTo(HaveOccurred(), fmt.Sprintf("failed to create %s resource", s))
 | |
| }
 | |
| 
 | |
| func ExpectRunnerDeploymentEventuallyUpdates(ctx context.Context, ns string, name string, f func(rd *actionsv1alpha1.RunnerDeployment)) {
 | |
| 	// We wrap the update in the Eventually block to avoid the below error that occurs due to concurrent modification
 | |
| 	// made by the controller to update .Status.AvailableReplicas and .Status.ReadyReplicas
 | |
| 	//   Operation cannot be fulfilled on runnersets.actions.summerwind.dev "example-runnerset": the object has been modified; please apply your changes to the latest version and try again
 | |
| 	EventuallyWithOffset(
 | |
| 		1,
 | |
| 		func() error {
 | |
| 			var rd actionsv1alpha1.RunnerDeployment
 | |
| 
 | |
| 			err := k8sClient.Get(ctx, types.NamespacedName{Namespace: ns, Name: name}, &rd)
 | |
| 
 | |
| 			Expect(err).NotTo(HaveOccurred(), "failed to get test RunnerDeployment resource")
 | |
| 
 | |
| 			f(&rd)
 | |
| 
 | |
| 			return k8sClient.Update(ctx, &rd)
 | |
| 		},
 | |
| 		time.Second*1, time.Millisecond*500).Should(BeNil())
 | |
| }
 | |
| 
 | |
| func ExpectRunnerSetsCountEventuallyEquals(ctx context.Context, ns string, count int, optionalDescription ...interface{}) {
 | |
| 	runnerSets := actionsv1alpha1.RunnerReplicaSetList{Items: []actionsv1alpha1.RunnerReplicaSet{}}
 | |
| 
 | |
| 	EventuallyWithOffset(
 | |
| 		1,
 | |
| 		func() int {
 | |
| 			err := k8sClient.List(ctx, &runnerSets, client.InNamespace(ns))
 | |
| 			if err != nil {
 | |
| 				logf.Log.Error(err, "list runner sets")
 | |
| 			}
 | |
| 
 | |
| 			return len(runnerSets.Items)
 | |
| 		},
 | |
| 		time.Second*10, time.Millisecond*500).Should(BeEquivalentTo(count), optionalDescription...)
 | |
| }
 | |
| 
 | |
| func ExpectRunnerSetsManagedReplicasCountEventuallyEquals(ctx context.Context, ns string, count int, optionalDescription ...interface{}) {
 | |
| 	runnerSets := actionsv1alpha1.RunnerReplicaSetList{Items: []actionsv1alpha1.RunnerReplicaSet{}}
 | |
| 
 | |
| 	EventuallyWithOffset(
 | |
| 		1,
 | |
| 		func() int {
 | |
| 			err := k8sClient.List(ctx, &runnerSets, client.InNamespace(ns))
 | |
| 			if err != nil {
 | |
| 				logf.Log.Error(err, "list runner sets")
 | |
| 			}
 | |
| 
 | |
| 			if len(runnerSets.Items) == 0 {
 | |
| 				logf.Log.Info("No runnerreplicasets exist yet")
 | |
| 				return -1
 | |
| 			}
 | |
| 
 | |
| 			if len(runnerSets.Items) != 1 {
 | |
| 				logf.Log.Info("Too many runnerreplicasets exist", "runnerSets", runnerSets)
 | |
| 				return -1
 | |
| 			}
 | |
| 
 | |
| 			return *runnerSets.Items[0].Spec.Replicas
 | |
| 		},
 | |
| 		time.Second*5, time.Millisecond*500).Should(BeEquivalentTo(count), optionalDescription...)
 | |
| }
 |