github.com/argoproj-labs/argocd-operator@v0.10.0/controllers/notificationsconfiguration/configmap_test.go (about) 1 package notificationsconfiguration 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 corev1 "k8s.io/api/core/v1" 9 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 10 "k8s.io/apimachinery/pkg/runtime" 11 "k8s.io/apimachinery/pkg/types" 12 "k8s.io/client-go/kubernetes/scheme" 13 "sigs.k8s.io/controller-runtime/pkg/client" 14 "sigs.k8s.io/controller-runtime/pkg/client/fake" 15 16 "github.com/argoproj-labs/argocd-operator/api/v1alpha1" 17 ) 18 19 type notificationsOpts func(*v1alpha1.NotificationsConfiguration) 20 21 type SchemeOpt func(*runtime.Scheme) error 22 23 func makeTestNotificationsConfiguration(opts ...notificationsOpts) *v1alpha1.NotificationsConfiguration { 24 a := &v1alpha1.NotificationsConfiguration{ 25 ObjectMeta: metav1.ObjectMeta{ 26 Name: "default-notifications-configuration", 27 Namespace: "default", 28 }, 29 } 30 for _, o := range opts { 31 o(a) 32 } 33 return a 34 } 35 36 func makeTestReconcilerScheme(sOpts ...SchemeOpt) *runtime.Scheme { 37 s := scheme.Scheme 38 for _, opt := range sOpts { 39 _ = opt(s) 40 } 41 42 return s 43 } 44 45 func makeTestReconciler(client client.Client, sch *runtime.Scheme) *NotificationsConfigurationReconciler { 46 return &NotificationsConfigurationReconciler{ 47 Client: client, 48 Scheme: sch, 49 } 50 } 51 52 func makeTestReconcilerClient(sch *runtime.Scheme, resObjs, subresObjs []client.Object, runtimeObj []runtime.Object) client.Client { 53 client := fake.NewClientBuilder().WithScheme(sch) 54 if len(resObjs) > 0 { 55 client = client.WithObjects(resObjs...) 56 } 57 if len(subresObjs) > 0 { 58 client = client.WithStatusSubresource(subresObjs...) 59 } 60 if len(runtimeObj) > 0 { 61 client = client.WithRuntimeObjects(runtimeObj...) 62 } 63 return client.Build() 64 } 65 66 func TestReconcileNotifications_CreateConfigMap(t *testing.T) { 67 68 a := makeTestNotificationsConfiguration(func(a *v1alpha1.NotificationsConfiguration) { 69 }) 70 71 resObjs := []client.Object{a} 72 subresObjs := []client.Object{a} 73 runtimeObjs := []runtime.Object{} 74 sch := makeTestReconcilerScheme(v1alpha1.AddToScheme) 75 cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) 76 r := makeTestReconciler(cl, sch) 77 78 a.Spec = v1alpha1.NotificationsConfigurationSpec{ 79 // Add a default template for test 80 Templates: map[string]string{ 81 "template.app-created": `email: 82 subject: Application {{.app.metadata.name}} has been created. 83 message: Application {{.app.metadata.name}} has been created. 84 teams: 85 title: Application {{.app.metadata.name}} has been created.`, 86 }, 87 // Add a default template for test 88 Triggers: map[string]string{ 89 "trigger.on-created": `- description: Application is created. 90 oncePer: app.metadata.name 91 send: 92 - app-created 93 when: "true"`, 94 }, 95 } 96 97 err := r.reconcileNotificationsConfigmap(a) 98 assert.NoError(t, err) 99 100 // Verify if the ConfigMap is created 101 testCM := &corev1.ConfigMap{} 102 assert.NoError(t, r.Client.Get( 103 context.TODO(), 104 types.NamespacedName{ 105 Name: ArgoCDNotificationsConfigMap, 106 Namespace: a.Namespace, 107 }, 108 testCM)) 109 110 // Verify that the configmap has the default template 111 assert.NotEqual(t, testCM.Data["template.app-created"], "") 112 113 // Verify that the configmap has the default trigger 114 assert.NotEqual(t, testCM.Data["trigger.on-created"], "") 115 } 116 117 func TestReconcileNotifications_UpdateConfigMap(t *testing.T) { 118 119 a := makeTestNotificationsConfiguration(func(a *v1alpha1.NotificationsConfiguration) { 120 }) 121 122 resObjs := []client.Object{a} 123 subresObjs := []client.Object{a} 124 runtimeObjs := []runtime.Object{} 125 sch := makeTestReconcilerScheme(v1alpha1.AddToScheme) 126 cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) 127 r := makeTestReconciler(cl, sch) 128 129 a.Spec = v1alpha1.NotificationsConfigurationSpec{ 130 // Add a default template for test 131 Templates: map[string]string{ 132 "template.app-created": `email: 133 subject: Application {{.app.metadata.name}} has been created. 134 message: Application {{.app.metadata.name}} has been created. 135 teams: 136 title: Application {{.app.metadata.name}} has been created.`, 137 }, 138 // Add a default template for test 139 Triggers: map[string]string{ 140 "trigger.on-created": `- description: Application is created. 141 oncePer: app.metadata.name 142 send: 143 - app-created 144 when: "true"`, 145 }, 146 } 147 148 err := r.reconcileNotificationsConfigmap(a) 149 assert.NoError(t, err) 150 151 // Verify if the ConfigMap is created 152 testCM := &corev1.ConfigMap{} 153 assert.NoError(t, r.Client.Get( 154 context.TODO(), 155 types.NamespacedName{ 156 Name: ArgoCDNotificationsConfigMap, 157 Namespace: a.Namespace, 158 }, 159 testCM)) 160 161 // Update the NotificationsConfiguration 162 a.Spec.Triggers["trigger.on-sync-status-test"] = "- when: app.status.sync.status == 'Unknown' \n send: [my-custom-template]" 163 164 err = r.reconcileNotificationsConfigmap(a) 165 assert.NoError(t, err) 166 167 testCM = &corev1.ConfigMap{} 168 assert.NoError(t, r.Client.Get( 169 context.TODO(), 170 types.NamespacedName{ 171 Name: ArgoCDNotificationsConfigMap, 172 Namespace: a.Namespace, 173 }, 174 testCM)) 175 176 // Verify that the updated configuration 177 assert.Equal(t, testCM.Data["trigger.on-sync-status-test"], 178 "- when: app.status.sync.status == 'Unknown' \n send: [my-custom-template]") 179 } 180 181 func TestReconcileNotifications_DeleteConfigMap(t *testing.T) { 182 183 a := makeTestNotificationsConfiguration(func(a *v1alpha1.NotificationsConfiguration) { 184 }) 185 186 resObjs := []client.Object{a} 187 subresObjs := []client.Object{a} 188 runtimeObjs := []runtime.Object{} 189 sch := makeTestReconcilerScheme(v1alpha1.AddToScheme) 190 cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) 191 r := makeTestReconciler(cl, sch) 192 193 // Update the NotificationsConfiguration 194 a.Spec = v1alpha1.NotificationsConfigurationSpec{ 195 Triggers: map[string]string{ 196 "trigger.on-sync-status-test": "- when: app.status.sync.status == 'Unknown' \n send: [my-custom-template]", 197 }, 198 } 199 200 err := r.reconcileNotificationsConfigmap(a) 201 assert.NoError(t, err) 202 203 // Delete the Notifications ConfigMap 204 testCM := &corev1.ConfigMap{ 205 ObjectMeta: metav1.ObjectMeta{ 206 Name: ArgoCDNotificationsConfigMap, 207 Namespace: a.Namespace, 208 }, 209 } 210 assert.NoError(t, r.Client.Delete( 211 context.TODO(), testCM)) 212 213 // Reconcile to check if the ConfigMap is recreated 214 err = r.reconcileNotificationsConfigmap(a) 215 assert.NoError(t, err) 216 217 assert.NoError(t, r.Client.Get( 218 context.TODO(), 219 types.NamespacedName{ 220 Name: ArgoCDNotificationsConfigMap, 221 Namespace: a.Namespace, 222 }, 223 testCM)) 224 225 // Verify if ConfigMap is created with required data 226 assert.Equal(t, testCM.Data["trigger.on-sync-status-test"], 227 "- when: app.status.sync.status == 'Unknown' \n send: [my-custom-template]") 228 }