github.com/1and1/oneandone-cloudserver-sdk-go@v1.4.1/monitoringpolicies_test.go (about) 1 package oneandone 2 3 import ( 4 "fmt" 5 "math/rand" 6 "sync" 7 "testing" 8 "time" 9 ) 10 11 var ( 12 set_mp sync.Once 13 test_mp_name string 14 test_mp_desc string 15 test_mp_mail string 16 test_mp_srv_id string 17 test_mp_srv_name string 18 test_mp *MonitoringPolicy 19 mp_request MonitoringPolicy 20 ) 21 22 const ( 23 port_responding = "RESPONDING" 24 port_not_responding = "NOT_RESPONDING" 25 process_running = "RUNNING" 26 process_not_running = "NOT_RUNNING" 27 mp_email = "test-go-sdk@oneandone.com" 28 ) 29 30 // Helper functions 31 32 func create_monitoring_policy() *MonitoringPolicy { 33 rand.Seed(time.Now().UnixNano()) 34 rint := rand.Intn(999) 35 test_mp_name = fmt.Sprintf("MonitoringPolicy_%d", rint) 36 test_mp_desc = fmt.Sprintf("MonitoringPolicy_%d description", rint) 37 test_mp_mail = "test-go-sdk@oneandone.com" 38 mp_request = MonitoringPolicy{ 39 Name: test_mp_name, 40 Description: test_mp_desc, 41 Email: test_mp_mail, 42 Agent: true, 43 Thresholds: &MonitoringThreshold{ 44 Cpu: &MonitoringLevel{ 45 Warning: &MonitoringValue{ 46 Value: 90, 47 Alert: false, 48 }, 49 Critical: &MonitoringValue{ 50 Value: 95, 51 Alert: false, 52 }, 53 }, 54 Ram: &MonitoringLevel{ 55 Warning: &MonitoringValue{ 56 Value: 90, 57 Alert: false, 58 }, 59 Critical: &MonitoringValue{ 60 Value: 95, 61 Alert: false, 62 }, 63 }, 64 Disk: &MonitoringLevel{ 65 Warning: &MonitoringValue{ 66 Value: 80, 67 Alert: false, 68 }, 69 Critical: &MonitoringValue{ 70 Value: 90, 71 Alert: false, 72 }, 73 }, 74 Transfer: &MonitoringLevel{ 75 Warning: &MonitoringValue{ 76 Value: 1000, 77 Alert: false, 78 }, 79 Critical: &MonitoringValue{ 80 Value: 2000, 81 Alert: false, 82 }, 83 }, 84 InternalPing: &MonitoringLevel{ 85 Warning: &MonitoringValue{ 86 Value: 50, 87 Alert: false, 88 }, 89 Critical: &MonitoringValue{ 90 Value: 100, 91 Alert: true, 92 }, 93 }, 94 }, 95 Ports: []MonitoringPort{ 96 { 97 Protocol: "TCP", 98 Port: 443, 99 AlertIf: port_not_responding, 100 EmailNotification: true, 101 }, 102 }, 103 Processes: []MonitoringProcess{ 104 { 105 Process: "httpdeamon", 106 AlertIf: process_not_running, 107 EmailNotification: false, 108 }, 109 }, 110 } 111 fmt.Printf("Creating new monitoring policy '%s'...\n", test_mp_name) 112 mp_id, mp, err := api.CreateMonitoringPolicy(&mp_request) 113 if err != nil { 114 fmt.Printf("Unable to create a monitoring policy. Error: %s", err.Error()) 115 return nil 116 } 117 if mp_id == "" || mp.Id == "" { 118 fmt.Printf("Unable to create monitoring policy '%s'.", test_mp_name) 119 return nil 120 } 121 api.WaitForState(mp, "ACTIVE", 30, 30) 122 return mp 123 } 124 125 func set_monitoring_policy() { 126 test_mp = create_monitoring_policy() 127 } 128 129 // /monitoring_policies tests 130 131 func TestCreateMonitoringPolicy(t *testing.T) { 132 set_mp.Do(set_monitoring_policy) 133 134 if test_mp == nil { 135 t.Errorf("CreateMonitoringPolicy failed.") 136 } 137 if test_mp.Id == "" { 138 t.Errorf("Missing monitoring policy ID.") 139 } 140 if test_mp.Name != test_mp_name { 141 t.Errorf("Wrong name of the monitoring policy.") 142 } 143 if test_mp.Description != test_mp_desc { 144 t.Errorf("Wrong monitoring policy description.") 145 } 146 if !test_mp.Agent { 147 t.Errorf("Missing monitoring policy agent.") 148 } 149 if test_mp.Email != test_mp_mail { 150 t.Errorf("Wrong email of monitoring policy '%s'.", test_mp.Name) 151 } 152 if test_mp.Thresholds == nil { 153 t.Errorf("Not thresholds found for monitoring policy '%s'.", test_mp.Name) 154 } else { 155 if test_mp.Thresholds.Cpu.Critical.Alert { 156 t.Errorf("Wrong alerting state for CPU critical alert of monitoring policy '%s'.", test_mp.Name) 157 } 158 if test_mp.Thresholds.Cpu.Critical.Value != 95 { 159 t.Errorf("Wrong value for CPU critical alert of monitoring policy '%s'.", test_mp.Name) 160 } 161 if test_mp.Thresholds.Cpu.Warning.Alert { 162 t.Errorf("Wrong alerting state for CPU warning alert of monitoring policy '%s'.", test_mp.Name) 163 } 164 if test_mp.Thresholds.Cpu.Warning.Value != 90 { 165 t.Errorf("Wrong value for CPU warning alert of monitoring policy '%s'.", test_mp.Name) 166 } 167 if test_mp.Thresholds.Ram.Critical.Alert { 168 t.Errorf("Wrong alerting state for RAM critical alert of monitoring policy '%s'.", test_mp.Name) 169 } 170 if test_mp.Thresholds.Ram.Critical.Value != 95 { 171 t.Errorf("Wrong value for RAM critical alert of monitoring policy '%s'.", test_mp.Name) 172 } 173 if test_mp.Thresholds.Ram.Warning.Alert { 174 t.Errorf("Wrong alerting state for RAM warning alert of monitoring policy '%s'.", test_mp.Name) 175 } 176 if test_mp.Thresholds.Ram.Warning.Value != 90 { 177 t.Errorf("Wrong value for RAM warning alert of monitoring policy '%s'.", test_mp.Name) 178 } 179 if test_mp.Thresholds.Disk.Critical.Alert { 180 t.Errorf("Wrong alerting state for disk critical alert of monitoring policy '%s'.", test_mp.Name) 181 } 182 if test_mp.Thresholds.Disk.Critical.Value != 90 { 183 t.Errorf("Wrong value for disk critical alert of monitoring policy '%s'.", test_mp.Name) 184 } 185 if test_mp.Thresholds.Disk.Warning.Alert { 186 t.Errorf("Wrong alerting state for disk warning alert of monitoring policy '%s'.", test_mp.Name) 187 } 188 if test_mp.Thresholds.Disk.Warning.Value != 80 { 189 t.Errorf("Wrong value for disk warning alert of monitoring policy '%s'.", test_mp.Name) 190 } 191 if test_mp.Thresholds.Transfer.Critical.Alert { 192 t.Errorf("Wrong alerting state for transfer critical alert of monitoring policy '%s'.", test_mp.Name) 193 } 194 if test_mp.Thresholds.Transfer.Critical.Value != 2000 { 195 t.Errorf("Wrong value for transfer critical alert of monitoring policy '%s'.", test_mp.Name) 196 } 197 if test_mp.Thresholds.Transfer.Warning.Alert { 198 t.Errorf("Wrong alerting state for transfer warning alert of monitoring policy '%s'.", test_mp.Name) 199 } 200 if test_mp.Thresholds.Transfer.Warning.Value != 1000 { 201 t.Errorf("Wrong value for transfer warning alert of monitoring policy '%s'.", test_mp.Name) 202 } 203 if !test_mp.Thresholds.InternalPing.Critical.Alert { 204 t.Errorf("Wrong alerting state for ping critical alert of monitoring policy '%s'.", test_mp.Name) 205 } 206 if test_mp.Thresholds.InternalPing.Critical.Value != 100 { 207 t.Errorf("Wrong value for ping critical alert of monitoring policy '%s'.", test_mp.Name) 208 } 209 if test_mp.Thresholds.InternalPing.Warning.Alert { 210 t.Errorf("Wrong alerting state for ping warning alert of monitoring policy '%s'.", test_mp.Name) 211 } 212 if test_mp.Thresholds.InternalPing.Warning.Value != 50 { 213 t.Errorf("Wrong value for ping warning alert of monitoring policy '%s'.", test_mp.Name) 214 } 215 } 216 if len(test_mp.Ports) != 1 { 217 t.Errorf("Wrong number of monitoring policy '%s' ports.", test_mp.Name) 218 } else { 219 if test_mp.Ports[0].Protocol != "TCP" { 220 t.Errorf("Wrong monitoring protocol of policy '%s'.", test_mp.Name) 221 } 222 if test_mp.Ports[0].AlertIf != port_not_responding { 223 t.Errorf("Wrong alerting state of monitoring policy '%s' port.", test_mp.Name) 224 } 225 if test_mp.Ports[0].Port != 443 { 226 t.Errorf("Wrong monitoring policy '%s' port number.", test_mp.Name) 227 } 228 if !test_mp.Ports[0].EmailNotification { 229 t.Errorf("Wrong email notification state in monitoring policy '%s' port.", test_mp.Name) 230 } 231 } 232 if len(test_mp.Processes) != 1 { 233 t.Errorf("Wrong number of monitoring policy '%s' processes.", test_mp.Name) 234 } else { 235 if test_mp.Processes[0].Process != "httpdeamon" { 236 t.Errorf("Wrong monitoring policy '%s' process.", test_mp.Name) 237 } 238 if test_mp.Processes[0].AlertIf != process_not_running { 239 t.Errorf("Wrong alerting state of monitoring policy '%s' process.", test_mp.Name) 240 } 241 if test_mp.Processes[0].EmailNotification { 242 t.Errorf("Wrong email notification state in monitoring policy '%s' process.", test_mp.Name) 243 } 244 } 245 } 246 247 func TestGetMonitoringPolicy(t *testing.T) { 248 set_mp.Do(set_monitoring_policy) 249 250 fmt.Printf("Getting monitoring policy '%s'...\n", test_mp.Name) 251 mp, err := api.GetMonitoringPolicy(test_mp.Id) 252 253 if err != nil { 254 t.Errorf("GetMonitoringPolicy failed. Error: " + err.Error()) 255 } 256 if mp.Id != test_mp.Id { 257 t.Errorf("Wrong monitoring policy ID.") 258 } 259 } 260 261 func TestListMonitoringPolicies(t *testing.T) { 262 set_mp.Do(set_monitoring_policy) 263 fmt.Println("Listing all monitoring policies...") 264 265 res, err := api.ListMonitoringPolicies() 266 if err != nil { 267 t.Errorf("ListMonitoringPolicies failed. Error: " + err.Error()) 268 } 269 if len(res) == 0 { 270 t.Errorf("No monitoring policy found.") 271 } 272 273 res, err = api.ListMonitoringPolicies(1, 1, "", "", "id,name,agent") 274 275 if err != nil { 276 t.Errorf("ListMonitoringPolicies with parameter options failed. Error: " + err.Error()) 277 } 278 if len(res) == 0 { 279 t.Errorf("No monitoring policy found.") 280 } 281 if len(res) > 1 { 282 t.Errorf("Wrong number of objects per page.") 283 } 284 if res[0].Id == "" { 285 t.Errorf("Filtering parameters failed.") 286 } 287 if res[0].Name == "" { 288 t.Errorf("Filtering parameters failed.") 289 } 290 if res[0].State != "" { 291 t.Errorf("Filtering parameters failed.") 292 } 293 294 res, err = api.ListMonitoringPolicies(0, 0, "", test_mp.Name, "") 295 if err != nil { 296 t.Errorf("ListMonitoringPolicies with parameter options failed. Error: " + err.Error()) 297 } 298 if len(res) != 1 { 299 t.Errorf("Search parameter failed.") 300 } 301 if res[0].Name != test_mp.Name { 302 t.Errorf("Search parameter failed.") 303 } 304 } 305 306 func TestAttachMonitoringPolicyServers(t *testing.T) { 307 set_mp.Do(set_monitoring_policy) 308 309 fmt.Printf("Attaching servers to monitoring policy '%s'...\n", test_mp.Name) 310 sync_server.Do(func() { deploy_test_server(true) }) 311 312 servers := []string{test_server.Id} 313 mp, err := api.AttachMonitoringPolicyServers(test_mp.Id, servers) 314 315 if err != nil { 316 t.Errorf("AttachMonitoringPolicyServers failed. Error: " + err.Error()) 317 } 318 319 api.WaitForState(mp, "ACTIVE", 30, 60) 320 mp, _ = api.GetMonitoringPolicy(mp.Id) 321 322 if len(mp.Servers) != 1 { 323 t.Errorf("Found no server attached to the monitoring policy.") 324 return 325 } 326 if mp.Servers[0].Id != test_server.Id { 327 t.Errorf("Wrong server IP attached to the monitoring policy.") 328 } 329 test_mp = mp 330 test_mp_srv_id = mp.Servers[0].Id 331 test_mp_srv_name = mp.Servers[0].Name 332 } 333 334 func TestGetMonitoringPolicyServer(t *testing.T) { 335 set_mp.Do(set_monitoring_policy) 336 337 fmt.Printf("Getting server identity attached to monitoring policy '%s'...\n", test_mp.Name) 338 mp_ser, err := api.GetMonitoringPolicyServer(test_mp.Id, test_mp_srv_id) 339 340 if err != nil { 341 t.Errorf("GetMonitoringPolicyServer failed. Error: " + err.Error()) 342 return 343 } 344 if mp_ser.Id != test_mp_srv_id { 345 t.Errorf("Wrong ID of the server attached to monitoring policy '%s'.", test_mp.Name) 346 } 347 if mp_ser.Name != test_mp_srv_name { 348 t.Errorf("Wrong server name attached to monitoring policy '%s'.", test_mp.Name) 349 } 350 } 351 352 func TestListMonitoringPolicyServers(t *testing.T) { 353 set_mp.Do(set_monitoring_policy) 354 sync_server.Do(func() { deploy_test_server(true) }) 355 356 fmt.Printf("Listing servers attached to monitoring policy '%s'...\n", test_mp.Name) 357 mp_srvs, err := api.ListMonitoringPolicyServers(test_mp.Id) 358 359 if err != nil { 360 t.Errorf("ListMonitoringPolicyServers failed. Error: " + err.Error()) 361 return 362 } 363 if len(mp_srvs) == 0 { 364 t.Errorf("Wrong number of servers attached to monitoring policy '%s'.", test_mp.Name) 365 return 366 } 367 if mp_srvs[0].Id != test_server.Id { 368 t.Errorf("Wrong server attached to monitoring policy '%s'.", test_mp.Name) 369 } 370 } 371 372 func TestRemoveMonitoringPolicyServer(t *testing.T) { 373 set_mp.Do(set_monitoring_policy) 374 sync_server.Do(func() { deploy_test_server(true) }) 375 376 fmt.Printf("Removing server attached to monitoring policy '%s'...\n", test_mp.Name) 377 mp, err := api.RemoveMonitoringPolicyServer(test_mp.Id, test_server.Id) 378 379 if err != nil { 380 t.Errorf("RemoveMonitoringPolicyServer failed. Error: " + err.Error()) 381 return 382 } 383 384 api.WaitForState(mp, "ACTIVE", 30, 60) 385 mp, err = api.GetMonitoringPolicy(mp.Id) 386 387 if err != nil { 388 t.Errorf("Removing server from the monitoring policy failed.") 389 } 390 if len(mp.Servers) > 0 { 391 t.Errorf("Server not removed from the monitoring policy.") 392 } 393 } 394 395 func TestGetMonitoringPolicyPort(t *testing.T) { 396 set_mp.Do(set_monitoring_policy) 397 398 fmt.Printf("Getting monitoring policy '%s' port...\n", test_mp.Name) 399 mp_port, err := api.GetMonitoringPolicyPort(test_mp.Id, test_mp.Ports[0].Id) 400 401 if err != nil { 402 t.Errorf("GetMonitoringPolicyPort failed. Error: " + err.Error()) 403 } 404 if mp_port.Id != test_mp.Ports[0].Id { 405 t.Errorf("Wrong port ID.") 406 } 407 if mp_port.Port != test_mp.Ports[0].Port { 408 t.Errorf("Wrong port number in the monitoring policy.") 409 } 410 if mp_port.AlertIf != test_mp.Ports[0].AlertIf { 411 t.Errorf("Wrong alert_if field in the monitoring policy port.") 412 } 413 if mp_port.Protocol != test_mp.Ports[0].Protocol { 414 t.Errorf("Wrong port protocol in the monitoring policy.") 415 } 416 if mp_port.EmailNotification != test_mp.Ports[0].EmailNotification { 417 t.Errorf("Wrong email notification state in the monitoring policy port.") 418 } 419 } 420 421 func TestModifyMonitoringPolicyPort(t *testing.T) { 422 set_mp.Do(set_monitoring_policy) 423 424 fmt.Printf("Modifying monitoring policy '%s' port...\n", test_mp.Name) 425 mp_port, err := api.GetMonitoringPolicyPort(test_mp.Id, test_mp.Ports[0].Id) 426 427 if err != nil { 428 t.Errorf("GetMonitoringPolicyPort failed. Error: " + err.Error()) 429 } 430 431 mp_port.AlertIf = port_responding 432 mp_port.EmailNotification = false 433 434 mp, err := api.ModifyMonitoringPolicyPort(test_mp.Id, mp_port.Id, mp_port) 435 if mp == nil { 436 t.Errorf("ModifyMonitoringPolicyPort failed. Error: " + err.Error()) 437 return 438 } 439 if mp.Ports[0].AlertIf != port_responding { 440 t.Errorf("Unable to modify alert_if field in the monitoring policy port.") 441 } 442 if mp.Ports[0].EmailNotification { 443 t.Errorf("Unable to modify email notification state in the monitoring policy port.") 444 } 445 test_mp = mp 446 } 447 448 func TestAddMonitoringPolicyPorts(t *testing.T) { 449 set_mp.Do(set_monitoring_policy) 450 451 fmt.Printf("Adding ports to monitoring policy '%s'...\n", test_mp.Name) 452 ports := []MonitoringPort{ 453 { 454 Protocol: "TCP", 455 Port: 43215, 456 AlertIf: port_not_responding, 457 EmailNotification: false, 458 }, 459 { 460 Protocol: "UDP", 461 Port: 161, 462 AlertIf: port_responding, 463 EmailNotification: true, 464 }, 465 } 466 mp, err := api.AddMonitoringPolicyPorts(test_mp.Id, ports) 467 468 if err != nil { 469 t.Errorf("AddMonitoringPolicyPorts failed. Error: " + err.Error()) 470 } else { 471 api.WaitForState(mp, "ACTIVE", 60, 60) 472 } 473 mp, _ = api.GetMonitoringPolicy(test_mp.Id) 474 if mp == nil || len(mp.Ports) != 3 { 475 t.Errorf("Unable to add ports to monitoring policy '%s'.\n", test_mp.Name) 476 } 477 } 478 479 func TestListMonitoringPolicyPorts(t *testing.T) { 480 set_mp.Do(set_monitoring_policy) 481 482 fmt.Printf("Listing monitoring policy '%s' ports...\n", test_mp.Name) 483 mp_ports, err := api.ListMonitoringPolicyPorts(test_mp.Id) 484 485 if err != nil { 486 t.Errorf("ListMonitoringPolicyPorts failed. Error: " + err.Error()) 487 } 488 if len(mp_ports) != 3 { 489 t.Errorf("Wrong number of ports found in monitoring policy '%s'.", test_mp.Name) 490 } 491 } 492 493 func TestDeleteMonitoringPort(t *testing.T) { 494 set_mp.Do(set_monitoring_policy) 495 496 mp_ports, _ := api.ListMonitoringPolicyPorts(test_mp.Id) 497 fmt.Printf("Deleting port '%s' from monitoring policy '%s'...\n", mp_ports[0].Id, test_mp.Name) 498 mp, err := api.DeleteMonitoringPolicyPort(test_mp.Id, mp_ports[0].Id) 499 500 if err != nil { 501 t.Errorf("DeleteMonitoringPolicyPort failed. Error: " + err.Error()) 502 } 503 504 api.WaitForState(mp, "ACTIVE", 60, 60) 505 mp, err = api.GetMonitoringPolicy(mp.Id) 506 507 if err != nil { 508 t.Errorf("Deleting port from the monitoring policy failed.") 509 return 510 } 511 if len(mp.Ports) != 2 { 512 t.Errorf("Port not deleted from the monitoring policy.") 513 } 514 for _, port := range mp.Ports { 515 if port.Id == mp_ports[0].Id { 516 t.Errorf("Port not deleted from the monitoring policy.") 517 } 518 } 519 } 520 521 func TestGetMonitoringPolicyProcess(t *testing.T) { 522 set_mp.Do(set_monitoring_policy) 523 524 fmt.Printf("Getting monitoring policy '%s' process...\n", test_mp.Name) 525 mp_process, err := api.GetMonitoringPolicyProcess(test_mp.Id, test_mp.Processes[0].Id) 526 527 if err != nil { 528 t.Errorf("GetMonitoringPolicyProcess failed. Error: " + err.Error()) 529 return 530 } 531 if mp_process.Id != test_mp.Processes[0].Id { 532 t.Errorf("Wrong process ID.") 533 } 534 if mp_process.Process != test_mp.Processes[0].Process { 535 t.Errorf("Wrong process name in the monitoring policy.") 536 } 537 if mp_process.AlertIf != test_mp.Processes[0].AlertIf { 538 t.Errorf("Wrong alert_if field in the monitoring policy process.") 539 } 540 if mp_process.EmailNotification != test_mp.Processes[0].EmailNotification { 541 t.Errorf("Wrong email notification state in the monitoring policy process.") 542 } 543 } 544 545 func TestModifyMonitoringPolicyProcess(t *testing.T) { 546 set_mp.Do(set_monitoring_policy) 547 548 fmt.Printf("Modifying monitoring policy '%s' process...\n", test_mp.Name) 549 mp_process, err := api.GetMonitoringPolicyProcess(test_mp.Id, test_mp.Processes[0].Id) 550 551 if err != nil { 552 t.Errorf("GetMonitoringPolicyProcess failed. Error: " + err.Error()) 553 return 554 } 555 556 mp_process.AlertIf = process_running 557 mp_process.EmailNotification = true 558 559 mp, err := api.ModifyMonitoringPolicyProcess(test_mp.Id, mp_process.Id, mp_process) 560 if err != nil { 561 t.Errorf("ModifyMonitoringPolicyProcess failed. Error: " + err.Error()) 562 return 563 } 564 if mp.Processes[0].AlertIf != process_running { 565 t.Errorf("Unable to modify alert_if field in the monitoring policy process.") 566 } 567 if !mp.Processes[0].EmailNotification { 568 t.Errorf("Unable to modify email notification state in the monitoring policy process.") 569 } 570 test_mp = mp 571 } 572 573 func TestAddMonitoringPolicyProcesses(t *testing.T) { 574 set_mp.Do(set_monitoring_policy) 575 576 fmt.Printf("Adding processes to monitoring policy '%s'...\n", test_mp.Name) 577 processes := []MonitoringProcess{ 578 { 579 Process: "iexplorer", 580 AlertIf: process_not_running, 581 EmailNotification: false, 582 }, 583 { 584 Process: "taskmgr", 585 AlertIf: process_running, 586 EmailNotification: true, 587 }, 588 } 589 mp, err := api.AddMonitoringPolicyProcesses(test_mp.Id, processes) 590 591 if err != nil { 592 t.Errorf("AddMonitoringPolicyProcesses failed. Error: " + err.Error()) 593 } else { 594 api.WaitForState(mp, "ACTIVE", 60, 60) 595 } 596 mp, _ = api.GetMonitoringPolicy(test_mp.Id) 597 if mp == nil || len(mp.Processes) != 3 { 598 t.Errorf("Unable to add processes to monitoring policy '%s'.\n", test_mp.Name) 599 } 600 } 601 602 func TestListMonitoringPolicyProcesses(t *testing.T) { 603 set_mp.Do(set_monitoring_policy) 604 605 fmt.Printf("Listing monitoring policy '%s' processes...\n", test_mp.Name) 606 mp_processes, err := api.ListMonitoringPolicyProcesses(test_mp.Id) 607 608 if err != nil { 609 t.Errorf("ListMonitoringPolicyProcesses failed. Error: " + err.Error()) 610 return 611 } 612 if len(mp_processes) != 3 { 613 t.Errorf("Wrong number of processes found in monitoring policy '%s'.", test_mp.Name) 614 } 615 } 616 617 func TestDeleteMonitoringProcess(t *testing.T) { 618 set_mp.Do(set_monitoring_policy) 619 620 mp_processes, _ := api.ListMonitoringPolicyProcesses(test_mp.Id) 621 if len(mp_processes) == 0 { 622 t.Errorf("No monitoring policy process found.") 623 return 624 } 625 fmt.Printf("Deleting process '%s' from monitoring policy '%s'...\n", mp_processes[0].Id, test_mp.Name) 626 mp, err := api.DeleteMonitoringPolicyProcess(test_mp.Id, mp_processes[0].Id) 627 628 if err != nil { 629 t.Errorf("DeleteMonitoringPolicyProcess failed. Error: " + err.Error()) 630 } 631 632 api.WaitForState(mp, "ACTIVE", 60, 60) 633 mp, err = api.GetMonitoringPolicy(test_mp.Id) 634 635 if err != nil { 636 t.Errorf("Deleting process from the monitoring policy failed.") 637 } 638 if len(mp.Processes) != 2 { 639 t.Errorf("Process not deleted from the monitoring policy.") 640 } 641 for _, process := range mp.Processes { 642 if process.Id == mp_processes[0].Id { 643 t.Errorf("Process not deleted from the monitoring policy.") 644 } 645 } 646 } 647 648 func TestUpdateMonitoringPolicy(t *testing.T) { 649 set_mp.Do(set_monitoring_policy) 650 651 fmt.Printf("Updating monitoring policy '%s'...\n", test_mp.Name) 652 new_name := test_mp.Name + "_updated" 653 new_desc := test_mp.Description + "_updated" 654 new_mail := "test-go-sdk@oneandone.de" 655 mp_request.Name = new_name 656 mp_request.Description = new_desc 657 mp_request.Email = new_mail 658 mp_request.Thresholds.Cpu.Critical.Value = 99 659 mp_request.Thresholds.Transfer.Critical.Alert = true 660 661 mp, err := api.UpdateMonitoringPolicy(test_mp.Id, &mp_request) 662 663 if err != nil { 664 t.Errorf("UpdateMonitoringPolicy failed. Error: " + err.Error()) 665 } else { 666 api.WaitForState(mp, "ACTIVE", 60, 30) 667 mp, _ = api.GetMonitoringPolicy(test_mp.Id) 668 if mp.Name != new_name { 669 t.Errorf("Failed to update monitoring policy name.") 670 } 671 if mp.Description != new_desc { 672 t.Errorf("Failed to update monitoring policy description.") 673 } 674 if mp.Email != new_mail { 675 t.Errorf("Failed to update monitoring policy email.") 676 } 677 if mp.Thresholds.Cpu.Critical.Value != 99 { 678 t.Errorf("Failed to update critical CPU threshold of the monitoring policy.") 679 } 680 if !mp.Thresholds.Transfer.Critical.Alert { 681 t.Errorf("Failed to update alerting state for critical transfer threshold of the monitoring policy.") 682 } 683 } 684 } 685 686 func TestDeleteMonitoringPolicy(t *testing.T) { 687 set_mp.Do(set_monitoring_policy) 688 689 fmt.Printf("Deleting monitoring policy '%s'...\n", test_mp.Name) 690 time.Sleep(time.Second) 691 mp, err := api.DeleteMonitoringPolicy(test_mp.Id) 692 693 if err != nil { 694 t.Errorf("DeleteMonitoringPolicy failed. Error: " + err.Error()) 695 return 696 } 697 api.WaitUntilDeleted(mp) 698 time.Sleep(time.Second) 699 mp, err = api.GetMonitoringPolicy(mp.Id) 700 701 if mp != nil { 702 t.Errorf("Unable to delete the monitoring policy.") 703 } else { 704 test_mp = nil 705 } 706 }