From 8243fa121cc7d348b52735158c3bf12277b03d10 Mon Sep 17 00:00:00 2001 From: Francesco Renzi Date: Thu, 23 Mar 2023 15:58:50 +0000 Subject: [PATCH] use TestMain --- .../autoscalingrunnerset_controller_test.go | 40 +++++------ .../actions.github.com/controllers_test.go | 69 +++++++++++++++++++ go.mod | 2 +- go.sum | 4 +- 4 files changed, 88 insertions(+), 27 deletions(-) create mode 100644 controllers/actions.github.com/controllers_test.go diff --git a/controllers/actions.github.com/autoscalingrunnerset_controller_test.go b/controllers/actions.github.com/autoscalingrunnerset_controller_test.go index f0d191a7..c8492160 100644 --- a/controllers/actions.github.com/autoscalingrunnerset_controller_test.go +++ b/controllers/actions.github.com/autoscalingrunnerset_controller_test.go @@ -28,6 +28,10 @@ const ( func TestAutoscalitRunnerSetReconciler_CreateRunnerScaleSet(t *testing.T) { t.Parallel() ctx := context.Background() + eventually := eventually.New( + eventually.WithTimeout(autoscalingRunnerSetTestTimeout), + eventually.WithInterval(autoscalingRunnerSetTestInterval), + ) autoscalingNS, mgr := createNamespace(t) configSecret := createDefaultSecret(t, autoscalingNS.Name) @@ -55,9 +59,7 @@ func TestAutoscalitRunnerSetReconciler_CreateRunnerScaleSet(t *testing.T) { require.NoError(t, err) require.Len(t, created.Finalizers, 1) assert.Equal(t, created.Finalizers[0], autoscalingRunnerSetFinalizerName) - }, - eventually.WithTimeout(autoscalingRunnerSetTestTimeout), - eventually.WithInterval(autoscalingRunnerSetTestInterval)) + }) // Check if runner scale set is created on service eventually.Must(t, func(t testing.TB) { @@ -69,9 +71,7 @@ func TestAutoscalitRunnerSetReconciler_CreateRunnerScaleSet(t *testing.T) { require.Contains(t, created.Annotations, runnerScaleSetRunnerGroupNameKey) assert.Equal(t, "testgroup", created.Annotations[runnerScaleSetRunnerGroupNameKey]) - }, - eventually.WithTimeout(autoscalingRunnerSetTestTimeout), - eventually.WithInterval(autoscalingRunnerSetTestInterval)) + }) // Check if ephemeral runner set is created eventually.Must(t, func(t testing.TB) { @@ -79,17 +79,13 @@ func TestAutoscalitRunnerSetReconciler_CreateRunnerScaleSet(t *testing.T) { err := k8sClient.List(ctx, runnerSetList, client.InNamespace(autoscalingRunnerSet.Namespace)) require.NoError(t, err) require.Len(t, runnerSetList.Items, 1) - }, - eventually.WithTimeout(autoscalingRunnerSetTestTimeout), - eventually.WithInterval(autoscalingRunnerSetTestInterval)) + }) // Check if 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)) + }) // Check if status is updated runnerSetList := new(v1alpha1.EphemeralRunnerSetList) @@ -101,6 +97,10 @@ func TestAutoscalitRunnerSetReconciler_CreateRunnerScaleSet(t *testing.T) { func TestAutoscalitRunnerSetReconciler_DeleteRunnerScaleSet(t *testing.T) { t.Parallel() ctx := context.Background() + eventually := eventually.New( + eventually.WithTimeout(autoscalingRunnerSetTestTimeout), + eventually.WithInterval(autoscalingRunnerSetTestInterval), + ) autoscalingNS, mgr := createNamespace(t) configSecret := createDefaultSecret(t, autoscalingNS.Name) @@ -132,9 +132,7 @@ func TestAutoscalitRunnerSetReconciler_DeleteRunnerScaleSet(t *testing.T) { new(v1alpha1.AutoscalingListener), ) require.NoError(t, err) - }, - eventually.WithTimeout(autoscalingRunnerSetTestTimeout), - eventually.WithInterval(autoscalingRunnerSetTestInterval)) + }) // Delete the AutoScalingRunnerSet err = k8sClient.Delete(ctx, autoscalingRunnerSet) @@ -145,9 +143,7 @@ func TestAutoscalitRunnerSetReconciler_DeleteRunnerScaleSet(t *testing.T) { 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) { @@ -155,18 +151,14 @@ func TestAutoscalitRunnerSetReconciler_DeleteRunnerScaleSet(t *testing.T) { 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() { diff --git a/controllers/actions.github.com/controllers_test.go b/controllers/actions.github.com/controllers_test.go new file mode 100644 index 00000000..4e1c68eb --- /dev/null +++ b/controllers/actions.github.com/controllers_test.go @@ -0,0 +1,69 @@ +package actionsgithubcom + +import ( + "io" + "log" + "os" + "path/filepath" + "testing" + + actionsv1alpha1 "github.com/actions/actions-runner-controller/apis/actions.github.com/v1alpha1" + "k8s.io/client-go/kubernetes/scheme" + "k8s.io/client-go/rest" + "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" +) + +var ( + cfg *rest.Config + k8sClient client.Client +) + +func TestMain(m *testing.M) { + 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") + + var err error + cfg, err = testEnv.Start() + if err != nil || cfg == nil { + log.Fatalln(err, "failed to start test environment") + } + + err = actionsv1alpha1.AddToScheme(scheme.Scheme) + if err != nil { + log.Fatalln(err, "failed to add scheme") + } + + // +kubebuilder:scaffold:scheme + + k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) + if err != nil || k8sClient == nil { + log.Fatalln(err, "failed to create client") + } + + // run the tests + code := m.Run() + + err = testEnv.Stop() + if err != nil { + log.Fatalln(err, "failed to stop test environment") + } + + os.Exit(code) +} diff --git a/go.mod b/go.mod index 467e8cc1..1ff2c955 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-20230322104408-065f0b6d63a2 + github.com/rentziass/eventually v0.2.0 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 4f653192..22791108 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-20230322104408-065f0b6d63a2 h1:4efabAGkX7mmXAztMNdorD35hzPW9hQxP9GLn6NfgFo= -github.com/rentziass/eventually v0.0.0-20230322104408-065f0b6d63a2/go.mod h1:jiSDJFv0sra6DK66duTH5V4kCfqd9OvmFtflPOExNVI= +github.com/rentziass/eventually v0.2.0 h1:zC6GB2nRRlARqfngNGUmdKG5Z6cOwyc0BwleF8kcZHY= +github.com/rentziass/eventually v0.2.0/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=