|
1 | 1 | package settings |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | 4 | "testing" |
6 | 5 |
|
7 | | - "github.com/argoproj/notifications-engine/pkg/api" |
8 | | - "github.com/argoproj/notifications-engine/pkg/services" |
9 | 6 | "github.com/stretchr/testify/assert" |
10 | | - "github.com/stretchr/testify/require" |
11 | 7 | corev1 "k8s.io/api/core/v1" |
12 | | - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
13 | | - "k8s.io/client-go/kubernetes/fake" |
14 | 8 |
|
15 | | - "github.com/argoproj/argo-cd/v3/reposerver/apiclient/mocks" |
| 9 | + "github.com/argoproj/notifications-engine/pkg/api" |
16 | 10 | service "github.com/argoproj/argo-cd/v3/util/notification/argocd" |
17 | 11 | ) |
18 | 12 |
|
19 | | -const ( |
20 | | - testNamespace = "default" |
21 | | - testContextKey = "test-context-key" |
22 | | - testContextKeyValue = "test-context-key-value" |
23 | | -) |
| 13 | +func TestGetFactorySettingsDeferred_ServiceNotInitialized(t *testing.T) { |
| 14 | + var argocdService service.Service // nil service |
24 | 15 |
|
25 | | -func TestInitGetVars(t *testing.T) { |
26 | | - notificationsCm := corev1.ConfigMap{ |
27 | | - ObjectMeta: metav1.ObjectMeta{ |
28 | | - Namespace: testNamespace, |
29 | | - Name: "argocd-notifications-cm", |
30 | | - }, |
31 | | - Data: map[string]string{ |
32 | | - "context": fmt.Sprintf("%s: %s", testContextKey, testContextKeyValue), |
33 | | - "service.webhook.test": "url: https://test.example.com", |
34 | | - "template.app-created": "email:\n subject: Application {{.app.metadata.name}} has been created.\nmessage: Application {{.app.metadata.name}} has been created.\nteams:\n title: Application {{.app.metadata.name}} has been created.\n", |
35 | | - "trigger.on-created": "- description: Application is created.\n oncePer: app.metadata.name\n send:\n - app-created\n when: \"true\"\n", |
36 | | - }, |
37 | | - } |
38 | | - notificationsSecret := corev1.Secret{ |
39 | | - ObjectMeta: metav1.ObjectMeta{ |
40 | | - Name: "argocd-notifications-secret", |
41 | | - Namespace: testNamespace, |
42 | | - }, |
43 | | - Data: map[string][]byte{ |
44 | | - "notification-secret": []byte("secret-value"), |
45 | | - }, |
46 | | - } |
47 | | - kubeclientset := fake.NewClientset(&corev1.ConfigMap{ |
48 | | - ObjectMeta: metav1.ObjectMeta{ |
49 | | - Namespace: testNamespace, |
50 | | - Name: "argocd-notifications-cm", |
51 | | - }, |
52 | | - Data: notificationsCm.Data, |
53 | | - }, |
54 | | - &corev1.Secret{ |
55 | | - ObjectMeta: metav1.ObjectMeta{ |
56 | | - Name: "argocd-notifications-secret", |
57 | | - Namespace: testNamespace, |
58 | | - }, |
59 | | - Data: notificationsSecret.Data, |
60 | | - }) |
61 | | - mockRepoClient := &mocks.Clientset{RepoServerServiceClient: &mocks.RepoServerServiceClient{}} |
62 | | - argocdService, err := service.NewArgoCDService(kubeclientset, testNamespace, mockRepoClient) |
63 | | - require.NoError(t, err) |
64 | | - defer argocdService.Close() |
65 | | - config := api.Config{} |
66 | | - testDestination := services.Destination{ |
67 | | - Service: "webhook", |
68 | | - } |
69 | | - emptyAppData := map[string]any{} |
| 16 | + settings := GetFactorySettingsDeferred( |
| 17 | + func() service.Service { return argocdService }, |
| 18 | + "test-secret", |
| 19 | + "test-configmap", |
| 20 | + false, |
| 21 | + ) |
70 | 22 |
|
71 | | - varsProvider, _ := initGetVars(argocdService, &config, ¬ificationsCm, ¬ificationsSecret) |
| 23 | + cfg := &api.Config{} |
| 24 | + configMap := &corev1.ConfigMap{} |
| 25 | + secret := &corev1.Secret{} |
72 | 26 |
|
73 | | - t.Run("Vars provider serves Application data on app key", func(t *testing.T) { |
74 | | - appData := map[string]any{ |
75 | | - "name": "app-name", |
76 | | - } |
77 | | - result := varsProvider(appData, testDestination) |
78 | | - assert.NotNil(t, t, result["app"]) |
79 | | - assert.Equal(t, result["app"], appData) |
80 | | - }) |
81 | | - t.Run("Vars provider serves notification context data on context key", func(t *testing.T) { |
82 | | - expectedContext := map[string]string{ |
83 | | - testContextKey: testContextKeyValue, |
84 | | - "notificationType": testDestination.Service, |
85 | | - } |
86 | | - result := varsProvider(emptyAppData, testDestination) |
87 | | - assert.NotNil(t, result["context"]) |
88 | | - assert.Equal(t, expectedContext, result["context"]) |
89 | | - }) |
90 | | - t.Run("Vars provider serves notification secrets on secrets key", func(t *testing.T) { |
91 | | - result := varsProvider(emptyAppData, testDestination) |
92 | | - assert.NotNil(t, result["secrets"]) |
93 | | - assert.Equal(t, result["secrets"], notificationsSecret.Data) |
94 | | - }) |
| 27 | + _, err := settings.InitGetVars(cfg, configMap, secret) |
| 28 | + assert.Error(t, err) |
| 29 | + assert.Contains(t, err.Error(), "argocdService is not initialized") |
95 | 30 | } |
0 commit comments