github.com/argoproj-labs/argocd-operator@v0.10.0/controllers/argocd/hooks_test.go (about)

     1  package argocd
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	appsv1 "k8s.io/api/apps/v1"
     9  	v1 "k8s.io/api/rbac/v1"
    10  
    11  	argoproj "github.com/argoproj-labs/argocd-operator/api/v1beta1"
    12  	"github.com/argoproj-labs/argocd-operator/common"
    13  )
    14  
    15  var errMsg = errors.New("this is a test error")
    16  
    17  func testDeploymentHook(cr *argoproj.ArgoCD, v interface{}, s string) error {
    18  	switch o := v.(type) {
    19  	case *appsv1.Deployment:
    20  		var replicas int32 = 3
    21  		o.Spec.Replicas = &replicas
    22  	}
    23  	return nil
    24  }
    25  
    26  func testClusterRoleHook(cr *argoproj.ArgoCD, v interface{}, s string) error {
    27  	switch o := v.(type) {
    28  	case *v1.ClusterRole:
    29  		o.Rules = append(o.Rules, policyRuleForApplicationController()...)
    30  	}
    31  	return nil
    32  }
    33  
    34  func testRoleHook(cr *argoproj.ArgoCD, v interface{}, s string) error {
    35  	switch o := v.(type) {
    36  	case *v1.Role:
    37  		if o.Name == cr.Name+"-"+common.ArgoCDApplicationControllerComponent {
    38  			o.Rules = append(o.Rules, testRules()...)
    39  		}
    40  	}
    41  	return nil
    42  }
    43  
    44  func testErrorHook(cr *argoproj.ArgoCD, v interface{}, s string) error {
    45  	return errMsg
    46  }
    47  
    48  func TestReconcileArgoCD_testDeploymentHook(t *testing.T) {
    49  	defer resetHooks()()
    50  	a := makeTestArgoCD()
    51  
    52  	Register(testDeploymentHook)
    53  
    54  	testDeployment := makeTestDeployment()
    55  
    56  	assert.NoError(t, applyReconcilerHook(a, testDeployment, ""))
    57  	var expectedReplicas int32 = 3
    58  	assert.Equal(t, &expectedReplicas, testDeployment.Spec.Replicas)
    59  }
    60  
    61  func TestReconcileArgoCD_testMultipleHooks(t *testing.T) {
    62  	defer resetHooks()()
    63  	a := makeTestArgoCD()
    64  
    65  	testDeployment := makeTestDeployment()
    66  	testClusterRole := makeTestClusterRole()
    67  
    68  	Register(testDeploymentHook)
    69  	Register(testClusterRoleHook)
    70  
    71  	assert.NoError(t, applyReconcilerHook(a, testDeployment, ""))
    72  	assert.NoError(t, applyReconcilerHook(a, testClusterRole, ""))
    73  
    74  	// Verify if testDeploymentHook is executed successfully
    75  	var expectedReplicas int32 = 3
    76  	assert.Equal(t, &expectedReplicas, testDeployment.Spec.Replicas)
    77  
    78  	// Verify if testClusterRoleHook is executed successfully
    79  	want := append(makeTestPolicyRules(), policyRuleForApplicationController()...)
    80  	assert.Equal(t, want, testClusterRole.Rules)
    81  }
    82  
    83  func TestReconcileArgoCD_hooks_end_upon_error(t *testing.T) {
    84  	defer resetHooks()()
    85  	a := makeTestArgoCD()
    86  	Register(testErrorHook, testClusterRoleHook)
    87  
    88  	testClusterRole := makeTestClusterRole()
    89  
    90  	assert.Error(t, applyReconcilerHook(a, testClusterRole, ""), "this is a test error")
    91  	assert.Equal(t, makeTestPolicyRules(), testClusterRole.Rules)
    92  }
    93  
    94  func resetHooks() func() {
    95  	origDefaultHooksFunc := hooks
    96  
    97  	return func() {
    98  		hooks = origDefaultHooksFunc
    99  	}
   100  }