github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/alert/add_receiver_test.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package alert 21 22 import ( 23 . "github.com/onsi/ginkgo/v2" 24 . "github.com/onsi/gomega" 25 26 corev1 "k8s.io/api/core/v1" 27 "k8s.io/cli-runtime/pkg/genericiooptions" 28 clientfake "k8s.io/client-go/rest/fake" 29 cmdtesting "k8s.io/kubectl/pkg/cmd/testing" 30 31 "github.com/1aal/kubeblocks/pkg/cli/testing" 32 ) 33 34 const ( 35 testNamespace = "test" 36 ) 37 38 var mockConfigmap = func(name string, key string, value string) *corev1.ConfigMap { 39 cm := &corev1.ConfigMap{} 40 cm.Name = name 41 cm.Namespace = testNamespace 42 cm.Data = map[string]string{key: value} 43 return cm 44 } 45 46 var mockBaseOptions = func(s genericiooptions.IOStreams) baseOptions { 47 o := baseOptions{IOStreams: s} 48 alertManagerConfig := ` 49 global: 50 smtp_from: user@kubeblocks.io 51 smtp_smarthost: smtp.feishu.cn:587 52 smtp_auth_username: admin@kubeblocks.io 53 smtp_auth_password: 123456abc 54 smtp_auth_identity: admin@kubeblocks.io 55 receivers: 56 - name: default-receiver 57 - name: receiver-7pb52 58 webhook_configs: 59 - max_alerts: 10 60 url: http://kubeblocks-webhook-adaptor-config.default:5001/api/v1/notify/receiver-7pb52 61 route: 62 group_interval: 30s 63 group_wait: 5s 64 receiver: default-receiver 65 repeat_interval: 10m 66 routes: 67 - continue: true 68 matchers: 69 - app_kubernetes_io_instance=~a|b|c 70 - severity=~info|warning 71 receiver: receiver-7pb52` 72 webhookAdaptorConfig := ` 73 receivers: 74 - name: receiver-7pb52 75 params: 76 url: https://oapi.dingtalk.com/robot/send?access_token=123456 77 type: dingtalk-webhook` 78 alertCM := mockConfigmap(alertConfigmapName, alertConfigFileName, alertManagerConfig) 79 webhookAdaptorCM := mockConfigmap(webhookAdaptorConfigmapName, webhookAdaptorFileName, webhookAdaptorConfig) 80 o.alertConfigMap = alertCM 81 o.webhookConfigMap = webhookAdaptorCM 82 return o 83 } 84 85 var _ = Describe("add receiver", func() { 86 var f *cmdtesting.TestFactory 87 var s genericiooptions.IOStreams 88 89 BeforeEach(func() { 90 f = cmdtesting.NewTestFactory() 91 f.Client = &clientfake.RESTClient{} 92 s, _, _, _ = genericiooptions.NewTestIOStreams() 93 }) 94 95 AfterEach(func() { 96 f.Cleanup() 97 }) 98 99 It("create new add receiver cmd", func() { 100 cmd := newAddReceiverCmd(f, s) 101 Expect(cmd).NotTo(BeNil()) 102 Expect(cmd.HasSubCommands()).Should(BeFalse()) 103 }) 104 105 It("complete", func() { 106 o := baseOptions{IOStreams: s} 107 Expect(o.complete(f)).Should(HaveOccurred()) 108 }) 109 110 It("validate", func() { 111 By("nothing to be input, should fail") 112 o := addReceiverOptions{baseOptions: baseOptions{IOStreams: s}} 113 Expect(o.validate([]string{})).Should(HaveOccurred()) 114 115 By("set email, do not specify the name") 116 o.emails = []string{"user@kubeblocks.io"} 117 o.alertConfigMap = mockConfigmap(alertConfigmapName, alertConfigFileName, "") 118 Expect(o.validate([]string{})).Should(HaveOccurred()) 119 Expect(o.name).ShouldNot(BeEmpty()) 120 121 By("set email, specify the name") 122 Expect(o.validate([]string{"test"})).Should(HaveOccurred()) 123 Expect(o.name).Should(Equal("test")) 124 125 By("set email, set smtp config in configmap") 126 baseOptions := mockBaseOptions(s) 127 o.alertConfigMap = baseOptions.alertConfigMap 128 Expect(o.validate([]string{})).Should(Succeed()) 129 }) 130 131 It("build receiver", func() { 132 o := addReceiverOptions{baseOptions: baseOptions{IOStreams: s}} 133 o.emails = []string{"user@kubeblocks.io", "user1@kubeblocks.io,user2@kubeblocks.io"} 134 o.webhooks = []string{"url=https://oapi.dingtalk.com/robot/send", "url=https://oapi.dingtalk.com/robot/send,url=https://oapi.dingtalk.com/robot/send?"} 135 o.slacks = []string{"api_url=https://foo.com,channel=foo,username=test"} 136 o.webhookConfigMap = mockConfigmap(webhookAdaptorConfigmapName, webhookAdaptorFileName, "") 137 Expect(o.buildReceiver()).Should(Succeed()) 138 Expect(o.receiver).ShouldNot(BeNil()) 139 Expect(o.receiver.EmailConfigs).Should(HaveLen(3)) 140 Expect(o.receiver.WebhookConfigs).Should(HaveLen(2)) 141 Expect(o.receiver.SlackConfigs).Should(HaveLen(1)) 142 }) 143 144 It("build routes", func() { 145 o := addReceiverOptions{baseOptions: baseOptions{IOStreams: s}} 146 o.name = "receiver-test" 147 o.clusters = []string{"cluster1", "cluster2"} 148 o.severities = []string{"critical", "warning"} 149 o.buildRoute() 150 Expect(o.route).ShouldNot(BeNil()) 151 Expect(o.route.Receiver).Should(Equal(o.name)) 152 Expect(o.route.Matchers).Should(HaveLen(2)) 153 Expect(o.route.Matchers[0]).Should(ContainSubstring(routeMatcherClusterKey)) 154 Expect(o.route.Matchers[1]).Should(ContainSubstring(routeMatcherSeverityKey)) 155 }) 156 157 It("run", func() { 158 o := addReceiverOptions{baseOptions: baseOptions{IOStreams: s}} 159 alertCM := mockConfigmap(alertConfigmapName, alertConfigFileName, "") 160 webhookAdaptorCM := mockConfigmap(webhookAdaptorConfigmapName, webhookAdaptorFileName, "") 161 o.baseOptions.alertConfigMap = alertCM 162 o.baseOptions.webhookConfigMap = webhookAdaptorCM 163 o.client = testing.FakeClientSet(alertCM, webhookAdaptorCM) 164 o.name = "receiver-test" 165 Expect(o.addReceiver()).Should(Succeed()) 166 Expect(o.addWebhookReceivers()).Should(Succeed()) 167 }) 168 })