2023-01-12 18:24:11 -05:00
package actionssummerwindnet
2020-02-26 21:23:23 +09:00
import (
"context"
2021-03-05 10:15:39 +09:00
"fmt"
2021-02-18 20:19:08 +09:00
"testing"
2020-03-15 18:08:11 +09:00
"time"
2021-03-05 10:15:39 +09:00
"github.com/google/go-cmp/cmp"
"k8s.io/apimachinery/pkg/runtime"
2020-02-26 21:23:23 +09:00
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"
2022-12-28 01:38:34 +01:00
actionsv1alpha1 "github.com/actions/actions-runner-controller/apis/actions.summerwind.net/v1alpha1"
2020-02-26 21:23:23 +09:00
)
2021-02-18 20:19:08 +09:00
func TestNewRunnerReplicaSet ( t * testing . T ) {
scheme := runtime . NewScheme ()
if err := actionsv1alpha1 . AddToScheme ( scheme ); err != nil {
t . Fatalf ( "%v" , err )
}
r := & RunnerDeploymentReconciler {
CommonRunnerLabels : [] string { "dev" },
Scheme : scheme ,
}
rd := actionsv1alpha1 . RunnerDeployment {
ObjectMeta : metav1 . ObjectMeta {
Name : "example" ,
},
Spec : actionsv1alpha1 . RunnerDeploymentSpec {
2021-03-05 10:15:39 +09:00
Selector : & metav1 . LabelSelector {
MatchLabels : map [ string ] string {
"foo" : "bar" ,
},
},
2021-02-18 20:19:08 +09:00
Template : actionsv1alpha1 . RunnerTemplate {
2021-03-05 10:15:39 +09:00
ObjectMeta : metav1 . ObjectMeta {
Labels : map [ string ] string {
"foo" : "bar" ,
},
},
2021-02-18 20:19:08 +09:00
Spec : actionsv1alpha1 . RunnerSpec {
2021-06-22 17:10:09 +09:00
RunnerConfig : actionsv1alpha1 . RunnerConfig {
Labels : [] string { "project1" },
},
2021-02-18 20:19:08 +09:00
},
},
},
}
rs , err := r . newRunnerReplicaSet ( rd )
if err != nil {
t . Fatalf ( "%v" , err )
}
2021-03-05 10:15:39 +09:00
if val , ok := rs . Labels [ "foo" ]; ok {
if val != "bar" {
t . Errorf ( "foo label does not have bar but %v" , val )
}
} else {
t . Errorf ( "foo label does not exist" )
}
hash1 , ok := rs . Labels [ LabelKeyRunnerTemplateHash ]
if ! ok {
t . Errorf ( "missing runner-template-hash label" )
}
runnerLabel := [] string { "project1" , "dev" }
if d := cmp . Diff ( runnerLabel , rs . Spec . Template . Spec . Labels ); d != "" {
2021-02-18 20:19:08 +09:00
t . Errorf ( "%s" , d )
}
2021-03-05 10:15:39 +09:00
rd2 := rd . DeepCopy ()
rd2 . Spec . Template . Spec . Labels = [] string { "project2" }
rs2 , err := r . newRunnerReplicaSet ( * rd2 )
if err != nil {
t . Fatalf ( "%v" , err )
}
hash2 , ok := rs2 . Labels [ LabelKeyRunnerTemplateHash ]
if ! ok {
t . Errorf ( "missing runner-template-hash label" )
}
if hash1 == hash2 {
t . Errorf (
"runner replica sets from runner deployments with varying labels must have different template hash, but got %s and %s" ,
hash1 , hash2 ,
)
}
rd3 := rd . DeepCopy ()
rd3 . Spec . Template . Labels [ "foo" ] = "baz"
rs3 , err := r . newRunnerReplicaSet ( * rd3 )
if err != nil {
t . Fatalf ( "%v" , err )
}
hash3 , ok := rs3 . Labels [ LabelKeyRunnerTemplateHash ]
if ! ok {
t . Errorf ( "missing runner-template-hash label" )
}
if hash1 == hash3 {
t . Errorf (
"runner replica sets from runner deployments with varying meta labels must have different template hash, but got %s and %s" ,
hash1 , hash3 ,
)
}
2021-02-18 20:19:08 +09:00
}
2020-02-26 21:23:23 +09:00
// SetupDeploymentTest will set up a testing environment.
// This includes:
// * creating a Namespace to be used during the test
// * starting the 'RunnerDeploymentReconciler'
// * stopping the 'RunnerDeploymentReconciler" after the test ends
// Call this function at the start of each of your tests.
2021-06-22 17:10:09 +09:00
func SetupDeploymentTest ( ctx2 context . Context ) * corev1 . Namespace {
var ctx context . Context
var cancel func ()
2020-02-26 21:23:23 +09:00
ns := & corev1 . Namespace {}
BeforeEach ( func () {
2021-06-22 17:10:09 +09:00
ctx , cancel = context . WithCancel ( ctx2 )
2020-02-26 21:23:23 +09:00
* ns = corev1 . Namespace {
ObjectMeta : metav1 . ObjectMeta { Name : "testns-" + randStringRunes ( 5 )},
}
err := k8sClient . Create ( ctx , ns )
Expect ( err ). NotTo ( HaveOccurred (), "failed to create test namespace" )
2021-03-19 16:14:15 +09:00
mgr , err := ctrl . NewManager ( cfg , ctrl . Options {
Namespace : ns . Name ,
})
2020-02-26 21:23:23 +09:00
Expect ( err ). NotTo ( HaveOccurred (), "failed to create manager" )
controller := & RunnerDeploymentReconciler {
Client : mgr . GetClient (),
Scheme : scheme . Scheme ,
Log : logf . Log ,
2020-03-15 18:08:11 +09:00
Recorder : mgr . GetEventRecorderFor ( "runnerreplicaset-controller" ),
2021-02-23 08:05:25 +09:00
Name : "runnerdeployment-" + ns . Name ,
2020-02-26 21:23:23 +09:00
}
err = controller . SetupWithManager ( mgr )
Expect ( err ). NotTo ( HaveOccurred (), "failed to setup controller" )
go func () {
defer GinkgoRecover ()
2021-06-22 17:10:09 +09:00
err := mgr . Start ( ctx )
2020-02-26 21:23:23 +09:00
Expect ( err ). NotTo ( HaveOccurred (), "failed to start manager" )
}()
})
AfterEach ( func () {
2021-06-22 17:10:09 +09:00
defer cancel ()
2020-02-26 21:23:23 +09:00
err := k8sClient . Delete ( ctx , ns )
Expect ( err ). NotTo ( HaveOccurred (), "failed to delete test namespace" )
})
return ns
}
var _ = Context ( "Inside of a new namespace" , func () {
ctx := context . TODO ()
ns := SetupDeploymentTest ( ctx )
Describe ( "when no existing resources exist" , func () {
2020-03-15 18:08:11 +09:00
It ( "should create a new RunnerReplicaSet resource from the specified template, add a another RunnerReplicaSet on template modification, and eventually removes old runnerreplicasets" , func () {
2021-03-05 10:15:39 +09:00
name := "example-runnerdeploy-1"
{
rs := & actionsv1alpha1 . RunnerDeployment {
ObjectMeta : metav1 . ObjectMeta {
Name : name ,
Namespace : ns . Name ,
},
Spec : actionsv1alpha1 . RunnerDeploymentSpec {
Replicas : intPtr ( 1 ),
Selector : & metav1 . LabelSelector {
MatchLabels : map [ string ] string {
"foo" : "bar" ,
},
},
Template : actionsv1alpha1 . RunnerTemplate {
ObjectMeta : metav1 . ObjectMeta {
Labels : map [ string ] string {
"foo" : "bar" ,
},
},
Spec : actionsv1alpha1 . RunnerSpec {
2021-06-22 17:10:09 +09:00
RunnerConfig : actionsv1alpha1 . RunnerConfig {
Repository : "test/valid" ,
Image : "bar" ,
},
RunnerPodSpec : actionsv1alpha1 . RunnerPodSpec {
Env : [] corev1 . EnvVar {
{ Name : "FOO" , Value : "FOOVALUE" },
},
2021-03-05 10:15:39 +09:00
},
},
},
},
}
err := k8sClient . Create ( ctx , rs )
Expect ( err ). NotTo ( HaveOccurred (), "failed to create test RunnerReplicaSet resource" )
runnerSets := actionsv1alpha1 . RunnerReplicaSetList { Items : [] actionsv1alpha1 . RunnerReplicaSet {}}
Eventually (
func () ( int , error ) {
selector , err := metav1 . LabelSelectorAsSelector ( rs . Spec . Selector )
if err != nil {
return 0 , err
}
err = k8sClient . List (
ctx ,
& runnerSets ,
client . InNamespace ( ns . Name ),
client . MatchingLabelsSelector { Selector : selector },
)
if err != nil {
return 0 , err
}
if len ( runnerSets . Items ) != 1 {
return 0 , fmt . Errorf ( "runnerreplicasets is not 1 but %d" , len ( runnerSets . Items ))
}
return * runnerSets . Items [ 0 ]. Spec . Replicas , nil
},
time . Second * 5 , time . Millisecond * 500 ). Should ( BeEquivalentTo ( 1 ))
}
{
// 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
var rd actionsv1alpha1 . RunnerDeployment
Eventually ( func () error {
err := k8sClient . Get ( ctx , types . NamespacedName { Namespace : ns . Name , Name : name }, & rd )
if err != nil {
return fmt . Errorf ( "failed to get test RunnerReplicaSet resource: %v\n" , err )
}
rd . Spec . Replicas = intPtr ( 2 )
return k8sClient . Update ( ctx , & rd )
},
time . Second * 1 , time . Millisecond * 500 ). Should ( BeNil ())
runnerSets := actionsv1alpha1 . RunnerReplicaSetList { Items : [] actionsv1alpha1 . RunnerReplicaSet {}}
Eventually (
func () ( int , error ) {
selector , err := metav1 . LabelSelectorAsSelector ( rd . Spec . Selector )
if err != nil {
return 0 , err
}
err = k8sClient . List (
ctx ,
& runnerSets ,
client . InNamespace ( ns . Name ),
client . MatchingLabelsSelector { Selector : selector },
)
if err != nil {
return 0 , err
}
if len ( runnerSets . Items ) != 1 {
return 0 , fmt . Errorf ( "runnerreplicasets is not 1 but %d" , len ( runnerSets . Items ))
}
return * runnerSets . Items [ 0 ]. Spec . Replicas , nil
},
time . Second * 5 , time . Millisecond * 500 ). Should ( BeEquivalentTo ( 2 ))
}
})
It ( "should create a new RunnerReplicaSet resource from the specified template without labels and selector, add a another RunnerReplicaSet on template modification, and eventually removes old runnerreplicasets" , func () {
name := "example-runnerdeploy-2"
2020-02-26 21:23:23 +09:00
{
rs := & actionsv1alpha1 . RunnerDeployment {
ObjectMeta : metav1 . ObjectMeta {
Name : name ,
Namespace : ns . Name ,
},
Spec : actionsv1alpha1 . RunnerDeploymentSpec {
Replicas : intPtr ( 1 ),
Template : actionsv1alpha1 . RunnerTemplate {
Spec : actionsv1alpha1 . RunnerSpec {
2021-06-22 17:10:09 +09:00
RunnerConfig : actionsv1alpha1 . RunnerConfig {
Repository : "test/valid" ,
Image : "bar" ,
},
RunnerPodSpec : actionsv1alpha1 . RunnerPodSpec {
Env : [] corev1 . EnvVar {
{ Name : "FOO" , Value : "FOOVALUE" },
},
2020-02-26 21:23:23 +09:00
},
},
},
},
}
err := k8sClient . Create ( ctx , rs )
2020-03-10 09:14:11 +09:00
Expect ( err ). NotTo ( HaveOccurred (), "failed to create test RunnerReplicaSet resource" )
2020-02-26 21:23:23 +09:00
2020-03-10 09:14:11 +09:00
runnerSets := actionsv1alpha1 . RunnerReplicaSetList { Items : [] actionsv1alpha1 . RunnerReplicaSet {}}
2020-02-26 21:23:23 +09:00
Eventually (
2021-03-05 10:15:39 +09:00
func () ( int , error ) {
selector , err := metav1 . LabelSelectorAsSelector ( rs . Spec . Selector )
2020-02-26 21:23:23 +09:00
if err != nil {
2021-03-05 10:15:39 +09:00
return 0 , err
2020-02-26 21:23:23 +09:00
}
2021-03-05 10:15:39 +09:00
err = k8sClient . List (
ctx ,
& runnerSets ,
client . InNamespace ( ns . Name ),
client . MatchingLabelsSelector { Selector : selector },
)
2020-02-26 21:23:23 +09:00
if err != nil {
2021-03-05 10:15:39 +09:00
return 0 , err
}
if len ( runnerSets . Items ) != 1 {
return 0 , fmt . Errorf ( "runnerreplicasets is not 1 but %d" , len ( runnerSets . Items ))
2020-02-26 21:23:23 +09:00
}
2021-03-05 10:15:39 +09:00
return * runnerSets . Items [ 0 ]. Spec . Replicas , nil
2020-02-26 21:23:23 +09:00
},
time . Second * 5 , time . Millisecond * 500 ). Should ( BeEquivalentTo ( 1 ))
}
{
2020-03-15 21:55:38 +09:00
// 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
2021-03-05 10:15:39 +09:00
var rd actionsv1alpha1 . RunnerDeployment
2020-02-26 21:23:23 +09:00
Eventually ( func () error {
err := k8sClient . Get ( ctx , types . NamespacedName { Namespace : ns . Name , Name : name }, & rd )
2021-03-05 10:15:39 +09:00
if err != nil {
return fmt . Errorf ( "failed to get test RunnerReplicaSet resource: %v\n" , err )
}
2020-02-26 21:23:23 +09:00
rd . Spec . Replicas = intPtr ( 2 )
return k8sClient . Update ( ctx , & rd )
},
time . Second * 1 , time . Millisecond * 500 ). Should ( BeNil ())
2020-03-10 09:14:11 +09:00
runnerSets := actionsv1alpha1 . RunnerReplicaSetList { Items : [] actionsv1alpha1 . RunnerReplicaSet {}}
2020-02-26 21:23:23 +09:00
Eventually (
2021-03-05 10:15:39 +09:00
func () ( int , error ) {
selector , err := metav1 . LabelSelectorAsSelector ( rd . Spec . Selector )
2020-02-26 21:23:23 +09:00
if err != nil {
2021-03-05 10:15:39 +09:00
return 0 , err
}
err = k8sClient . List (
ctx ,
& runnerSets ,
client . InNamespace ( ns . Name ),
client . MatchingLabelsSelector { Selector : selector },
)
if err != nil {
return 0 , err
}
if len ( runnerSets . Items ) != 1 {
return 0 , fmt . Errorf ( "runnerreplicasets is not 1 but %d" , len ( runnerSets . Items ))
2020-02-26 21:23:23 +09:00
}
2021-03-05 10:15:39 +09:00
return * runnerSets . Items [ 0 ]. Spec . Replicas , nil
2020-02-26 21:23:23 +09:00
},
time . Second * 5 , time . Millisecond * 500 ). Should ( BeEquivalentTo ( 2 ))
}
})
2021-03-05 10:15:39 +09:00
It ( "should adopt RunnerReplicaSet created before 0.18.0 to have Spec.Selector" , func () {
name := "example-runnerdeploy-2"
{
rd := & actionsv1alpha1 . RunnerDeployment {
ObjectMeta : metav1 . ObjectMeta {
Name : name ,
Namespace : ns . Name ,
},
Spec : actionsv1alpha1 . RunnerDeploymentSpec {
Replicas : intPtr ( 1 ),
Template : actionsv1alpha1 . RunnerTemplate {
Spec : actionsv1alpha1 . RunnerSpec {
2021-06-22 17:10:09 +09:00
RunnerConfig : actionsv1alpha1 . RunnerConfig {
Repository : "test/valid" ,
Image : "bar" ,
},
RunnerPodSpec : actionsv1alpha1 . RunnerPodSpec {
Env : [] corev1 . EnvVar {
{ Name : "FOO" , Value : "FOOVALUE" },
},
2021-03-05 10:15:39 +09:00
},
},
},
},
}
createRDErr := k8sClient . Create ( ctx , rd )
Expect ( createRDErr ). NotTo ( HaveOccurred (), "failed to create test RunnerReplicaSet resource" )
Eventually (
func () ( int , error ) {
runnerSets := actionsv1alpha1 . RunnerReplicaSetList { Items : [] actionsv1alpha1 . RunnerReplicaSet {}}
err := k8sClient . List (
ctx ,
& runnerSets ,
client . InNamespace ( ns . Name ),
)
if err != nil {
return 0 , err
}
return len ( runnerSets . Items ), nil
},
time . Second * 1 , time . Millisecond * 500 ). Should ( BeEquivalentTo ( 1 ))
var rs17 * actionsv1alpha1 . RunnerReplicaSet
Consistently (
func () ( * metav1 . LabelSelector , error ) {
runnerSets := actionsv1alpha1 . RunnerReplicaSetList { Items : [] actionsv1alpha1 . RunnerReplicaSet {}}
err := k8sClient . List (
ctx ,
& runnerSets ,
client . InNamespace ( ns . Name ),
)
if err != nil {
return nil , err
}
if len ( runnerSets . Items ) != 1 {
return nil , fmt . Errorf ( "runnerreplicasets is not 1 but %d" , len ( runnerSets . Items ))
}
rs17 = & runnerSets . Items [ 0 ]
return runnerSets . Items [ 0 ]. Spec . Selector , nil
},
time . Second * 1 , time . Millisecond * 500 ). Should ( Not ( BeNil ()))
// We simulate the old, pre 0.18.0 RunnerReplicaSet by updating it.
// I've tried to use controllerutil.Set{Owner,Controller}Reference and k8sClient.Create(rs17)
// but it didn't work due to missing RD UID, where UID is generated on K8s API server on k8sCLient.Create(rd)
rs17 . Spec . Selector = nil
updateRSErr := k8sClient . Update ( ctx , rs17 )
Expect ( updateRSErr ). NotTo ( HaveOccurred ())
Eventually (
func () ( * metav1 . LabelSelector , error ) {
runnerSets := actionsv1alpha1 . RunnerReplicaSetList { Items : [] actionsv1alpha1 . RunnerReplicaSet {}}
err := k8sClient . List (
ctx ,
& runnerSets ,
client . InNamespace ( ns . Name ),
)
if err != nil {
return nil , err
}
if len ( runnerSets . Items ) != 1 {
return nil , fmt . Errorf ( "runnerreplicasets is not 1 but %d" , len ( runnerSets . Items ))
}
return runnerSets . Items [ 0 ]. Spec . Selector , nil
},
time . Second * 1 , time . Millisecond * 500 ). Should ( Not ( BeNil ()))
}
})
2020-02-26 21:23:23 +09:00
})
})