github.com/minio/console@v1.4.1/api/admin_notification_endpoints_test.go (about) 1 // This file is part of MinIO Console Server 2 // Copyright (c) 2021 MinIO, Inc. 3 // 4 // This program is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Affero General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // This program is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Affero General Public License for more details. 13 // 14 // You should have received a copy of the GNU Affero General Public License 15 // along with this program. If not, see <http://www.gnu.org/licenses/>. 16 17 package api 18 19 import ( 20 "context" 21 "errors" 22 "reflect" 23 "testing" 24 25 "github.com/go-openapi/swag" 26 27 cfgApi "github.com/minio/console/api/operations/configuration" 28 "github.com/minio/console/models" 29 ) 30 31 func Test_addNotificationEndpoint(t *testing.T) { 32 client := AdminClientMock{} 33 34 type args struct { 35 ctx context.Context 36 client MinioAdmin 37 params *cfgApi.AddNotificationEndpointParams 38 } 39 tests := []struct { 40 name string 41 args args 42 mockSetConfig func(kv string) (restart bool, err error) 43 want *models.SetNotificationEndpointResponse 44 wantErr bool 45 }{ 46 { 47 name: "valid postgres", 48 args: args{ 49 ctx: context.Background(), 50 client: client, 51 params: &cfgApi.AddNotificationEndpointParams{ 52 HTTPRequest: nil, 53 Body: &models.NotificationEndpoint{ 54 AccountID: swag.String("1"), 55 Properties: map[string]string{ 56 "host": "localhost", 57 "user": "user", 58 "password": "passwrd", 59 }, 60 Service: models.NewNofiticationService("postgres"), 61 }, 62 }, 63 }, 64 mockSetConfig: func(_ string) (restart bool, err error) { 65 return false, nil 66 }, 67 want: &models.SetNotificationEndpointResponse{ 68 AccountID: swag.String("1"), 69 Properties: map[string]string{ 70 "host": "localhost", 71 "user": "user", 72 "password": "passwrd", 73 }, 74 Service: models.NewNofiticationService("postgres"), 75 Restart: false, 76 }, 77 wantErr: false, 78 }, 79 { 80 name: "set config returns error", 81 args: args{ 82 ctx: context.Background(), 83 client: client, 84 params: &cfgApi.AddNotificationEndpointParams{ 85 HTTPRequest: nil, 86 Body: &models.NotificationEndpoint{ 87 AccountID: swag.String("1"), 88 Properties: map[string]string{ 89 "host": "localhost", 90 "user": "user", 91 "password": "passwrd", 92 }, 93 Service: models.NewNofiticationService("postgres"), 94 }, 95 }, 96 }, 97 mockSetConfig: func(_ string) (restart bool, err error) { 98 return false, errors.New("error") 99 }, 100 want: nil, 101 wantErr: true, 102 }, 103 { 104 name: "valid mysql", 105 args: args{ 106 ctx: context.Background(), 107 client: client, 108 params: &cfgApi.AddNotificationEndpointParams{ 109 HTTPRequest: nil, 110 Body: &models.NotificationEndpoint{ 111 AccountID: swag.String("1"), 112 Properties: map[string]string{ 113 "host": "localhost", 114 "user": "user", 115 "password": "passwrd", 116 }, 117 Service: models.NewNofiticationService("mysql"), 118 }, 119 }, 120 }, 121 mockSetConfig: func(_ string) (restart bool, err error) { 122 return false, nil 123 }, 124 want: &models.SetNotificationEndpointResponse{ 125 AccountID: swag.String("1"), 126 Properties: map[string]string{ 127 "host": "localhost", 128 "user": "user", 129 "password": "passwrd", 130 }, 131 Service: models.NewNofiticationService("mysql"), 132 Restart: false, 133 }, 134 wantErr: false, 135 }, 136 { 137 name: "valid kafka", 138 args: args{ 139 ctx: context.Background(), 140 client: client, 141 params: &cfgApi.AddNotificationEndpointParams{ 142 HTTPRequest: nil, 143 Body: &models.NotificationEndpoint{ 144 AccountID: swag.String("1"), 145 Properties: map[string]string{ 146 "brokers": "http://localhost:8080/broker1", 147 }, 148 Service: models.NewNofiticationService("kafka"), 149 }, 150 }, 151 }, 152 mockSetConfig: func(_ string) (restart bool, err error) { 153 return false, nil 154 }, 155 want: &models.SetNotificationEndpointResponse{ 156 AccountID: swag.String("1"), 157 Properties: map[string]string{ 158 "brokers": "http://localhost:8080/broker1", 159 }, 160 Service: models.NewNofiticationService("kafka"), 161 Restart: false, 162 }, 163 wantErr: false, 164 }, 165 { 166 name: "valid amqp", 167 args: args{ 168 ctx: context.Background(), 169 client: client, 170 params: &cfgApi.AddNotificationEndpointParams{ 171 HTTPRequest: nil, 172 Body: &models.NotificationEndpoint{ 173 AccountID: swag.String("1"), 174 Properties: map[string]string{ 175 "url": "http://localhost:8080/broker1", 176 }, 177 Service: models.NewNofiticationService("amqp"), 178 }, 179 }, 180 }, 181 mockSetConfig: func(_ string) (restart bool, err error) { 182 return false, nil 183 }, 184 want: &models.SetNotificationEndpointResponse{ 185 AccountID: swag.String("1"), 186 Properties: map[string]string{ 187 "url": "http://localhost:8080/broker1", 188 }, 189 Service: models.NewNofiticationService("amqp"), 190 Restart: false, 191 }, 192 wantErr: false, 193 }, 194 { 195 name: "valid mqtt", 196 args: args{ 197 ctx: context.Background(), 198 client: client, 199 params: &cfgApi.AddNotificationEndpointParams{ 200 HTTPRequest: nil, 201 Body: &models.NotificationEndpoint{ 202 AccountID: swag.String("1"), 203 Properties: map[string]string{ 204 "broker": "http://localhost:8080/broker1", 205 "topic": "minio", 206 }, 207 Service: models.NewNofiticationService("mqtt"), 208 }, 209 }, 210 }, 211 mockSetConfig: func(_ string) (restart bool, err error) { 212 return false, nil 213 }, 214 want: &models.SetNotificationEndpointResponse{ 215 AccountID: swag.String("1"), 216 Properties: map[string]string{ 217 "broker": "http://localhost:8080/broker1", 218 "topic": "minio", 219 }, 220 Service: models.NewNofiticationService("mqtt"), 221 Restart: false, 222 }, 223 wantErr: false, 224 }, 225 { 226 name: "valid elasticsearch", 227 args: args{ 228 ctx: context.Background(), 229 client: client, 230 params: &cfgApi.AddNotificationEndpointParams{ 231 HTTPRequest: nil, 232 Body: &models.NotificationEndpoint{ 233 AccountID: swag.String("1"), 234 Properties: map[string]string{ 235 "url": "http://localhost:8080/broker1", 236 "index": "minio", 237 "format": "namespace", 238 }, 239 Service: models.NewNofiticationService("elasticsearch"), 240 }, 241 }, 242 }, 243 mockSetConfig: func(_ string) (restart bool, err error) { 244 return false, nil 245 }, 246 want: &models.SetNotificationEndpointResponse{ 247 AccountID: swag.String("1"), 248 Properties: map[string]string{ 249 "url": "http://localhost:8080/broker1", 250 "index": "minio", 251 "format": "namespace", 252 }, 253 Service: models.NewNofiticationService("elasticsearch"), 254 Restart: false, 255 }, 256 wantErr: false, 257 }, 258 { 259 name: "valid redis", 260 args: args{ 261 ctx: context.Background(), 262 client: client, 263 params: &cfgApi.AddNotificationEndpointParams{ 264 HTTPRequest: nil, 265 Body: &models.NotificationEndpoint{ 266 AccountID: swag.String("1"), 267 Properties: map[string]string{ 268 "address": "http://localhost:8080/broker1", 269 "key": "minio", 270 "format": "namespace", 271 }, 272 Service: models.NewNofiticationService("redis"), 273 }, 274 }, 275 }, 276 mockSetConfig: func(_ string) (restart bool, err error) { 277 return false, nil 278 }, 279 want: &models.SetNotificationEndpointResponse{ 280 AccountID: swag.String("1"), 281 Properties: map[string]string{ 282 "address": "http://localhost:8080/broker1", 283 "key": "minio", 284 "format": "namespace", 285 }, 286 Service: models.NewNofiticationService("redis"), 287 Restart: false, 288 }, 289 wantErr: false, 290 }, 291 { 292 name: "valid nats", 293 args: args{ 294 ctx: context.Background(), 295 client: client, 296 params: &cfgApi.AddNotificationEndpointParams{ 297 HTTPRequest: nil, 298 Body: &models.NotificationEndpoint{ 299 AccountID: swag.String("1"), 300 Properties: map[string]string{ 301 "address": "http://localhost:8080/broker1", 302 "subject": "minio", 303 }, 304 Service: models.NewNofiticationService("nats"), 305 }, 306 }, 307 }, 308 mockSetConfig: func(_ string) (restart bool, err error) { 309 return false, nil 310 }, 311 want: &models.SetNotificationEndpointResponse{ 312 AccountID: swag.String("1"), 313 Properties: map[string]string{ 314 "address": "http://localhost:8080/broker1", 315 "subject": "minio", 316 }, 317 Service: models.NewNofiticationService("nats"), 318 Restart: false, 319 }, 320 wantErr: false, 321 }, 322 { 323 name: "valid webhook", 324 args: args{ 325 ctx: context.Background(), 326 client: client, 327 params: &cfgApi.AddNotificationEndpointParams{ 328 HTTPRequest: nil, 329 Body: &models.NotificationEndpoint{ 330 AccountID: swag.String("1"), 331 Properties: map[string]string{ 332 "endpoint": "http://localhost:8080/broker1", 333 }, 334 Service: models.NewNofiticationService("webhook"), 335 }, 336 }, 337 }, 338 mockSetConfig: func(_ string) (restart bool, err error) { 339 return false, nil 340 }, 341 want: &models.SetNotificationEndpointResponse{ 342 AccountID: swag.String("1"), 343 Properties: map[string]string{ 344 "endpoint": "http://localhost:8080/broker1", 345 }, 346 Service: models.NewNofiticationService("webhook"), 347 Restart: false, 348 }, 349 wantErr: false, 350 }, 351 { 352 name: "valid nsq", 353 args: args{ 354 ctx: context.Background(), 355 client: client, 356 params: &cfgApi.AddNotificationEndpointParams{ 357 HTTPRequest: nil, 358 Body: &models.NotificationEndpoint{ 359 AccountID: swag.String("1"), 360 Properties: map[string]string{ 361 "nsqd_address": "http://localhost:8080/broker1", 362 "topic": "minio", 363 }, 364 Service: models.NewNofiticationService("nsq"), 365 }, 366 }, 367 }, 368 mockSetConfig: func(_ string) (restart bool, err error) { 369 return false, nil 370 }, 371 want: &models.SetNotificationEndpointResponse{ 372 AccountID: swag.String("1"), 373 Properties: map[string]string{ 374 "nsqd_address": "http://localhost:8080/broker1", 375 "topic": "minio", 376 }, 377 Service: models.NewNofiticationService("nsq"), 378 Restart: false, 379 }, 380 wantErr: false, 381 }, 382 { 383 name: "invalid service", 384 args: args{ 385 ctx: context.Background(), 386 client: client, 387 params: &cfgApi.AddNotificationEndpointParams{ 388 HTTPRequest: nil, 389 Body: &models.NotificationEndpoint{ 390 AccountID: swag.String("1"), 391 Properties: map[string]string{ 392 "host": "localhost", 393 "user": "user", 394 "password": "passwrd", 395 }, 396 Service: models.NewNofiticationService("oorgle"), 397 }, 398 }, 399 }, 400 mockSetConfig: func(_ string) (restart bool, err error) { 401 return false, errors.New("invalid config") 402 }, 403 want: nil, 404 wantErr: true, 405 }, 406 { 407 name: "valid config, restart required", 408 args: args{ 409 ctx: context.Background(), 410 client: client, 411 params: &cfgApi.AddNotificationEndpointParams{ 412 HTTPRequest: nil, 413 Body: &models.NotificationEndpoint{ 414 AccountID: swag.String("1"), 415 Properties: map[string]string{ 416 "host": "localhost", 417 "user": "user", 418 "password": "passwrd", 419 }, 420 Service: models.NewNofiticationService("postgres"), 421 }, 422 }, 423 }, 424 mockSetConfig: func(_ string) (restart bool, err error) { 425 return true, nil 426 }, 427 want: &models.SetNotificationEndpointResponse{ 428 AccountID: swag.String("1"), 429 Properties: map[string]string{ 430 "host": "localhost", 431 "user": "user", 432 "password": "passwrd", 433 }, 434 Service: models.NewNofiticationService("postgres"), 435 Restart: true, 436 }, 437 wantErr: false, 438 }, 439 } 440 for _, tt := range tests { 441 t.Run(tt.name, func(_ *testing.T) { 442 // mock function response from setConfig() 443 minioSetConfigKVMock = tt.mockSetConfig 444 got, err := addNotificationEndpoint(tt.args.ctx, tt.args.client, tt.args.params) 445 if (err != nil) != tt.wantErr { 446 t.Errorf("addNotificationEndpoint() error = %v, wantErr %v", err, tt.wantErr) 447 return 448 } 449 if !reflect.DeepEqual(got, tt.want) { 450 t.Errorf("addNotificationEndpoint() got = %v, want %v", got, tt.want) 451 } 452 }) 453 } 454 }