github.com/argoproj/argo-cd/v3@v3.2.1/util/notification/settings/legacy_test.go (about) 1 package settings 2 3 import ( 4 "testing" 5 6 "github.com/argoproj/notifications-engine/pkg/api" 7 "github.com/argoproj/notifications-engine/pkg/services" 8 "github.com/argoproj/notifications-engine/pkg/subscriptions" 9 "github.com/argoproj/notifications-engine/pkg/triggers" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 corev1 "k8s.io/api/core/v1" 13 "k8s.io/apimachinery/pkg/labels" 14 ) 15 16 func TestMergeLegacyConfig_DefaultTriggers(t *testing.T) { 17 cfg := api.Config{ 18 Services: map[string]api.ServiceFactory{}, 19 Triggers: map[string][]triggers.Condition{ 20 "my-trigger1": {{ 21 When: "true", 22 Send: []string{"my-template1"}, 23 }}, 24 "my-trigger2": {{ 25 When: "false", 26 Send: []string{"my-template2"}, 27 }}, 28 }, 29 } 30 context := map[string]string{} 31 configYAML := ` 32 config.yaml: 33 triggers: 34 - name: my-trigger1 35 enabled: true 36 ` 37 err := ApplyLegacyConfig(&cfg, 38 context, 39 &corev1.ConfigMap{Data: map[string]string{"config.yaml": configYAML}}, 40 &corev1.Secret{Data: map[string][]byte{}}, 41 ) 42 require.NoError(t, err) 43 assert.Equal(t, []string{"my-trigger1"}, cfg.DefaultTriggers) 44 } 45 46 func TestMergeLegacyConfig(t *testing.T) { 47 cfg := api.Config{ 48 Templates: map[string]services.Notification{"my-template1": {Message: "foo"}}, 49 Triggers: map[string][]triggers.Condition{ 50 "my-trigger1": {{ 51 When: "true", 52 Send: []string{"my-template1"}, 53 }}, 54 }, 55 Services: map[string]api.ServiceFactory{}, 56 Subscriptions: []subscriptions.DefaultSubscription{{Triggers: []string{"my-trigger1"}}}, 57 } 58 context := map[string]string{"some": "value"} 59 60 configYAML := ` 61 triggers: 62 - name: my-trigger1 63 enabled: true 64 - name: my-trigger2 65 condition: false 66 template: my-template2 67 enabled: true 68 templates: 69 - name: my-template1 70 body: bar 71 - name: my-template2 72 body: foo 73 context: 74 other: value2 75 subscriptions: 76 - triggers: 77 - my-trigger2 78 selector: test=true 79 ` 80 notifiersYAML := ` 81 slack: 82 token: my-token 83 ` 84 err := ApplyLegacyConfig(&cfg, context, 85 &corev1.ConfigMap{Data: map[string]string{"config.yaml": configYAML}}, 86 &corev1.Secret{Data: map[string][]byte{"notifiers.yaml": []byte(notifiersYAML)}}, 87 ) 88 89 require.NoError(t, err) 90 assert.Equal(t, map[string]services.Notification{ 91 "my-template1": {Message: "bar"}, 92 "my-template2": {Message: "foo"}, 93 }, cfg.Templates) 94 95 assert.Equal(t, []triggers.Condition{{ 96 When: "true", 97 Send: []string{"my-template1"}, 98 }}, cfg.Triggers["my-trigger1"]) 99 assert.Equal(t, []triggers.Condition{{ 100 When: "false", 101 Send: []string{"my-template2"}, 102 }}, cfg.Triggers["my-trigger2"]) 103 104 label, err := labels.Parse("test=true") 105 require.NoError(t, err) 106 assert.Equal(t, subscriptions.DefaultSubscriptions([]subscriptions.DefaultSubscription{ 107 {Triggers: []string{"my-trigger2"}, Selector: label}, 108 }), cfg.Subscriptions) 109 assert.NotNil(t, cfg.Services["slack"]) 110 } 111 112 func TestGetDestinations(t *testing.T) { 113 res := GetLegacyDestinations(map[string]string{ 114 "my-trigger.recipients.argocd-notifications.argoproj.io": "slack:my-channel", 115 }, []string{}, nil) 116 assert.Equal(t, services.Destinations{ 117 "my-trigger": []services.Destination{ 118 { 119 Recipient: "my-channel", 120 Service: "slack", 121 }, 122 }, 123 }, res) 124 } 125 126 func TestGetDestinations_DefaultTrigger(t *testing.T) { 127 res := GetLegacyDestinations(map[string]string{ 128 annotationKey: "slack:my-channel", 129 }, []string{"my-trigger"}, nil) 130 assert.Equal(t, services.Destinations{ 131 "my-trigger": []services.Destination{{ 132 Recipient: "my-channel", 133 Service: "slack", 134 }}, 135 }, res) 136 } 137 138 func TestGetDestinations_ServiceDefaultTriggers(t *testing.T) { 139 res := GetLegacyDestinations(map[string]string{ 140 annotationKey: "slack:my-channel", 141 }, []string{}, map[string][]string{ 142 "slack": { 143 "trigger-a", 144 "trigger-b", 145 }, 146 }) 147 assert.Equal(t, services.Destinations{ 148 "trigger-a": []services.Destination{{ 149 Recipient: "my-channel", 150 Service: "slack", 151 }}, 152 "trigger-b": []services.Destination{{ 153 Recipient: "my-channel", 154 Service: "slack", 155 }}, 156 }, res) 157 }