diff --git a/controllers/actions.github.com/autoscalingrunnerset_controller_test.go b/controllers/actions.github.com/autoscalingrunnerset_controller_test.go index 9ebd2871..f0d191a7 100644 --- a/controllers/actions.github.com/autoscalingrunnerset_controller_test.go +++ b/controllers/actions.github.com/autoscalingrunnerset_controller_test.go @@ -2,25 +2,17 @@ package actionsgithubcom import ( "context" - "io" - "path/filepath" "testing" "time" - corev1 "k8s.io/api/core/v1" - "k8s.io/client-go/kubernetes/scheme" - "k8s.io/client-go/rest" + "k8s.io/apimachinery/pkg/api/errors" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/envtest" logf "sigs.k8s.io/controller-runtime/pkg/log" - "sigs.k8s.io/controller-runtime/pkg/log/zap" // . "github.com/onsi/ginkgo/v2" // . "github.com/onsi/gomega" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/actions/actions-runner-controller/apis/actions.github.com/v1alpha1" - actionsv1alpha1 "github.com/actions/actions-runner-controller/apis/actions.github.com/v1alpha1" "github.com/actions/actions-runner-controller/github/actions/fake" "github.com/rentziass/eventually" "github.com/stretchr/testify/assert" @@ -33,50 +25,11 @@ const ( autoscalingRunnerSetTestGitHubToken = "gh_token" ) -func setupK8s(t *testing.T) (client.Client, *rest.Config) { - logf.SetLogger(zap.New(zap.UseDevMode(true), zap.WriteTo(io.Discard))) - - testEnv := &envtest.Environment{ - CRDDirectoryPaths: []string{filepath.Join("../..", "config", "crd", "bases")}, - } - - // Avoids the following error: - // 2021-03-19T15:14:11.673+0900 ERROR controller-runtime.controller - // Reconciler error {"controller": "testns-tvjzjrunner", "request": - // "testns-gdnyx/example-runnerdeploy-zps4z-j5562", "error": "Pod - // \"example-runnerdeploy-zps4z-j5562\" is invalid: - // [spec.containers[1].image: Required value, - // spec.containers[1].securityContext.privileged: Forbidden: disallowed by - // cluster policy]"} - testEnv.ControlPlane.GetAPIServer().Configure(). - Append("allow-privileged", "true") - - cfg, err := testEnv.Start() - require.NoError(t, err) - require.NotNil(t, cfg) - - err = actionsv1alpha1.AddToScheme(scheme.Scheme) - require.NoError(t, err) - - // +kubebuilder:scaffold:scheme - - k8sClient, err := client.New(cfg, client.Options{Scheme: scheme.Scheme}) - require.NoError(t, err) - require.NotNil(t, k8sClient) - - t.Cleanup(func() { - err := testEnv.Stop() - require.NoError(t, err) - }) - - return k8sClient, cfg -} - func TestAutoscalitRunnerSetReconciler_CreateRunnerScaleSet(t *testing.T) { - k8sClient, cfg := setupK8s(t) + t.Parallel() ctx := context.Background() - autoscalingNS, mgr := createNamespace(t, k8sClient, cfg) - configSecret := createDefaultSecret(t, k8sClient, autoscalingNS.Name) + autoscalingNS, mgr := createNamespace(t) + configSecret := createDefaultSecret(t, autoscalingNS.Name) controller := &AutoscalingRunnerSetReconciler{ Client: mgr.GetClient(), @@ -89,32 +42,7 @@ func TestAutoscalitRunnerSetReconciler_CreateRunnerScaleSet(t *testing.T) { err := controller.SetupWithManager(mgr) require.NoError(t, err, "failed to setup controller") - min := 1 - max := 10 - autoscalingRunnerSet := &v1alpha1.AutoscalingRunnerSet{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-asrs", - Namespace: autoscalingNS.Name, - }, - Spec: v1alpha1.AutoscalingRunnerSetSpec{ - GitHubConfigUrl: "https://github.com/owner/repo", - GitHubConfigSecret: configSecret.Name, - MaxRunners: &max, - MinRunners: &min, - RunnerGroup: "testgroup", - Template: corev1.PodTemplateSpec{ - Spec: corev1.PodSpec{ - Containers: []corev1.Container{ - { - Name: "runner", - Image: "ghcr.io/actions/runner", - }, - }, - }, - }, - }, - } - + autoscalingRunnerSet := newAutoscalingRunnerSet(autoscalingNS.Name, configSecret.Name) err = k8sClient.Create(ctx, autoscalingRunnerSet) require.NoError(t, err, "failed to create AutoScalingRunnerSet") @@ -170,6 +98,77 @@ func TestAutoscalitRunnerSetReconciler_CreateRunnerScaleSet(t *testing.T) { assert.Len(t, runnerSetList.Items, 1, "Only one EphemeralRunnerSet should be created") } +func TestAutoscalitRunnerSetReconciler_DeleteRunnerScaleSet(t *testing.T) { + t.Parallel() + ctx := context.Background() + autoscalingNS, mgr := createNamespace(t) + configSecret := createDefaultSecret(t, autoscalingNS.Name) + + controller := &AutoscalingRunnerSetReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + Log: logf.Log, + ControllerNamespace: autoscalingNS.Name, + DefaultRunnerScaleSetListenerImage: "ghcr.io/actions/arc", + ActionsClient: fake.NewMultiClient(), + } + err := controller.SetupWithManager(mgr) + require.NoError(t, err, "failed to setup controller") + + autoscalingRunnerSet := newAutoscalingRunnerSet(autoscalingNS.Name, configSecret.Name) + err = k8sClient.Create(ctx, autoscalingRunnerSet) + require.NoError(t, err, "failed to create AutoScalingRunnerSet") + + startManagers(t, mgr) + + // Wait till the listener is created + eventually.Must(t, func(t testing.TB) { + err := k8sClient.Get( + ctx, + client.ObjectKey{ + Name: scaleSetListenerName(autoscalingRunnerSet), + Namespace: autoscalingRunnerSet.Namespace, + }, + new(v1alpha1.AutoscalingListener), + ) + require.NoError(t, err) + }, + eventually.WithTimeout(autoscalingRunnerSetTestTimeout), + eventually.WithInterval(autoscalingRunnerSetTestInterval)) + + // Delete the AutoScalingRunnerSet + err = k8sClient.Delete(ctx, autoscalingRunnerSet) + require.NoError(t, err, "failed to delete AutoScalingRunnerSet") + + // Check if the listener is deleted + eventually.Must(t, func(t testing.TB) { + err := k8sClient.Get(ctx, client.ObjectKey{Name: scaleSetListenerName(autoscalingRunnerSet), Namespace: autoscalingRunnerSet.Namespace}, new(v1alpha1.AutoscalingListener)) + require.NotNil(t, err) + assert.True(t, errors.IsNotFound(err)) + }, + eventually.WithTimeout(autoscalingRunnerSetTestTimeout), + eventually.WithInterval(autoscalingRunnerSetTestInterval)) + + // Check if all the EphemeralRunnerSet is deleted + eventually.Must(t, func(t testing.TB) { + runnerSetList := new(v1alpha1.EphemeralRunnerSetList) + err := k8sClient.List(ctx, runnerSetList, client.InNamespace(autoscalingRunnerSet.Namespace)) + require.NoError(t, err) + assert.Len(t, runnerSetList.Items, 0, "All EphemeralRunnerSet should be deleted") + }, + eventually.WithTimeout(autoscalingRunnerSetTestTimeout), + eventually.WithInterval(autoscalingRunnerSetTestInterval)) + + // Check if the AutoScalingRunnerSet is deleted + eventually.Must(t, func(t testing.TB) { + err := k8sClient.Get(ctx, client.ObjectKey{Name: autoscalingRunnerSet.Name, Namespace: autoscalingRunnerSet.Namespace}, new(v1alpha1.AutoscalingRunnerSet)) + require.NotNil(t, err) + assert.True(t, errors.IsNotFound(err)) + }, + eventually.WithTimeout(autoscalingRunnerSetTestTimeout), + eventually.WithInterval(autoscalingRunnerSetTestInterval)) +} + // var _ = Describe("Test AutoScalingRunnerSet controller", func() { // var ctx context.Context // var mgr ctrl.Manager diff --git a/controllers/actions.github.com/helpers_test.go b/controllers/actions.github.com/helpers_test.go index 973e724c..3098201f 100644 --- a/controllers/actions.github.com/helpers_test.go +++ b/controllers/actions.github.com/helpers_test.go @@ -3,19 +3,46 @@ package actionsgithubcom import ( "context" + "github.com/actions/actions-runner-controller/apis/actions.github.com/v1alpha1" "github.com/onsi/ginkgo/v2" "github.com/stretchr/testify/require" "golang.org/x/sync/errgroup" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/client-go/rest" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/manager" ) const defaultGitHubToken = "gh_token" +func newAutoscalingRunnerSet(namespace, secretName string) *v1alpha1.AutoscalingRunnerSet { + min := 1 + max := 10 + return &v1alpha1.AutoscalingRunnerSet{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-asrs", + Namespace: namespace, + }, + Spec: v1alpha1.AutoscalingRunnerSetSpec{ + GitHubConfigUrl: "https://github.com/owner/repo", + GitHubConfigSecret: secretName, + MaxRunners: &max, + MinRunners: &min, + RunnerGroup: "testgroup", + Template: corev1.PodTemplateSpec{ + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "runner", + Image: "ghcr.io/actions/runner", + }, + }, + }, + }, + }, + } +} + func startManagers(t ginkgo.GinkgoTInterface, first manager.Manager, others ...manager.Manager) { for _, mgr := range append([]manager.Manager{first}, others...) { ctx, cancel := context.WithCancel(context.Background()) @@ -32,16 +59,16 @@ func startManagers(t ginkgo.GinkgoTInterface, first manager.Manager, others ...m } } -func createNamespace(t ginkgo.GinkgoTInterface, client client.Client, cfg *rest.Config) (*corev1.Namespace, manager.Manager) { +func createNamespace(t ginkgo.GinkgoTInterface) (*corev1.Namespace, manager.Manager) { ns := &corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{Name: "testns-autoscaling" + RandStringRunes(5)}, } - err := client.Create(context.Background(), ns) + err := k8sClient.Create(context.Background(), ns) require.NoError(t, err) t.Cleanup(func() { - err := client.Delete(context.Background(), ns) + err := k8sClient.Delete(context.Background(), ns) require.NoError(t, err) }) @@ -54,7 +81,7 @@ func createNamespace(t ginkgo.GinkgoTInterface, client client.Client, cfg *rest. return ns, mgr } -func createDefaultSecret(t ginkgo.GinkgoTInterface, client client.Client, namespace string) *corev1.Secret { +func createDefaultSecret(t ginkgo.GinkgoTInterface, namespace string) *corev1.Secret { secret := &corev1.Secret{ ObjectMeta: metav1.ObjectMeta{ Name: "github-config-secret", @@ -65,7 +92,7 @@ func createDefaultSecret(t ginkgo.GinkgoTInterface, client client.Client, namesp }, } - err := client.Create(context.Background(), secret) + err := k8sClient.Create(context.Background(), secret) require.NoError(t, err) return secret diff --git a/go.mod b/go.mod index 9a858b00..467e8cc1 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,7 @@ require ( github.com/onsi/gomega v1.27.2 github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.14.0 - github.com/rentziass/eventually v0.0.0-20230321190617-a198b1b1faa3 + github.com/rentziass/eventually v0.0.0-20230322104408-065f0b6d63a2 github.com/stretchr/testify v1.8.2 github.com/teambition/rrule-go v1.8.2 go.uber.org/multierr v1.7.0 diff --git a/go.sum b/go.sum index 34cb23a8..4f653192 100644 --- a/go.sum +++ b/go.sum @@ -333,8 +333,8 @@ github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1 github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5mo= github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4= -github.com/rentziass/eventually v0.0.0-20230321190617-a198b1b1faa3 h1:rzJaKZtKN0I3zA9Hq7J93sGaf1K83vik8nmoIHirtQY= -github.com/rentziass/eventually v0.0.0-20230321190617-a198b1b1faa3/go.mod h1:jiSDJFv0sra6DK66duTH5V4kCfqd9OvmFtflPOExNVI= +github.com/rentziass/eventually v0.0.0-20230322104408-065f0b6d63a2 h1:4efabAGkX7mmXAztMNdorD35hzPW9hQxP9GLn6NfgFo= +github.com/rentziass/eventually v0.0.0-20230322104408-065f0b6d63a2/go.mod h1:jiSDJFv0sra6DK66duTH5V4kCfqd9OvmFtflPOExNVI= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=