1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
|
package integration
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/zorkian/go-datadog-api"
)
/*
PagerDuty Integration
*/
func TestIntegrationPDCreateAndDelete(t *testing.T) {
expected := createTestIntegrationPD(t)
defer cleanUpIntegrationPD(t)
actual, err := client.GetIntegrationPD()
if err != nil {
t.Fatalf("Retrieving a PagerDuty integration failed when it shouldn't: (%s)", err)
}
expectedServiceNames := make([]*string, len(expected.Services))
for _, service := range expected.Services {
expectedServiceNames = append(expectedServiceNames, service.ServiceName)
}
actualServiceNames := make([]*string, len(actual.Services))
for _, service := range actual.Services {
actualServiceNames = append(actualServiceNames, service.ServiceName)
}
assert.Equal(t, expectedServiceNames, actualServiceNames)
}
func TestIntegrationPDUpdate(t *testing.T) {
pdIntegration := createTestIntegrationPD(t)
defer cleanUpIntegrationPD(t)
pdIntegration.Services = append(pdIntegration.Services, datadog.ServicePDRequest{
ServiceName: datadog.String("test-pd-update"),
ServiceKey: datadog.String("test-pd-update-key"),
})
if err := client.UpdateIntegrationPD(pdIntegration); err != nil {
t.Fatalf("Updating a PagerDuty integration failed when it shouldn't: %s", err)
}
actual, err := client.GetIntegrationPD()
if err != nil {
t.Fatalf("Retrieving a PagerDuty integration failed when it shouldn't: %s", err)
}
expectedServiceNames := make([]*string, len(pdIntegration.Services))
for _, service := range pdIntegration.Services {
expectedServiceNames = append(expectedServiceNames, service.ServiceName)
}
actualServiceNames := make([]*string, len(actual.Services))
for _, service := range actual.Services {
actualServiceNames = append(actualServiceNames, service.ServiceName)
}
assert.Equal(t, expectedServiceNames, actualServiceNames)
}
func TestIntegrationPDGet(t *testing.T) {
pdIntegration := createTestIntegrationPD(t)
defer cleanUpIntegrationPD(t)
actual, err := client.GetIntegrationPD()
if err != nil {
t.Fatalf("Retrieving pdIntegration failed when it shouldn't: %s", err)
}
expectedServiceNames := make([]*string, len(pdIntegration.Services))
for _, service := range pdIntegration.Services {
expectedServiceNames = append(expectedServiceNames, service.ServiceName)
}
actualServiceNames := make([]*string, len(actual.Services))
for _, service := range actual.Services {
actualServiceNames = append(actualServiceNames, service.ServiceName)
}
assert.Equal(t, expectedServiceNames, actualServiceNames)
}
func TestIntegrationPDService(t *testing.T) {
// test manipulation of individual service objects in the PD integration
// when the integration is not active, manipulating service objects
// should return 404
so := datadog.ServicePDRequest{
ServiceName: datadog.String("testPDServiceNameIndividual"),
ServiceKey: datadog.String("testPDServiceKeyIndividual"),
}
err := client.CreateIntegrationPDService(&so)
if err == nil {
t.Fatalf("Creating PD integration service object succeeded without active PD integration")
}
_ = createTestIntegrationPD(t)
defer cleanUpIntegrationPD(t)
err = client.CreateIntegrationPDService(&so)
if err != nil {
t.Fatalf("Creating PD integration service object failed when it shouldn't: %s", err)
}
var soRead *datadog.ServicePDRequest
soRead, err = client.GetIntegrationPDService(*so.ServiceName)
if err != nil {
t.Fatalf("Reading PD integration service object failed when it shouldn't: %s", err)
}
// ServiceKey is never returned by API, so we can't test it
assert.Equal(t, *so.ServiceName, *soRead.ServiceName)
so.SetServiceKey("other")
err = client.UpdateIntegrationPDService(&so)
if err != nil {
t.Fatalf("Updating PD integration service object failed when it shouldn't: %s", err)
}
// we can't really test anything here, since only the ServiceKey was changed
err = client.DeleteIntegrationPDService(*so.ServiceName)
if err != nil {
t.Fatalf("Deleting PD integration service object failed when it shouldn't: %s", err)
}
}
func getTestIntegrationPD() *datadog.IntegrationPDRequest {
return &datadog.IntegrationPDRequest{
Services: []datadog.ServicePDRequest{
{
ServiceName: datadog.String("testPDServiceName"),
ServiceKey: datadog.String("testPDServiceKey"),
},
},
Subdomain: datadog.String("testdomain"),
// Datadog will actually validate this value
// so we're leaving it blank for tests
Schedules: []string{},
APIToken: datadog.String("abc123"),
}
}
func createTestIntegrationPD(t *testing.T) *datadog.IntegrationPDRequest {
pdIntegration := getTestIntegrationPD()
err := client.CreateIntegrationPD(pdIntegration)
if err != nil {
t.Fatalf("Creating a PagerDuty integration failed when it shouldn't: %s", err)
}
return pdIntegration
}
func cleanUpIntegrationPD(t *testing.T) {
if err := client.DeleteIntegrationPD(); err != nil {
t.Fatalf("Deleting the PagerDuty integration failed when it shouldn't. Manual cleanup needed. (%s)", err)
}
pdIntegration, err := client.GetIntegrationPD()
if pdIntegration != nil {
t.Fatal("PagerDuty Integration hasn't been deleted when it should have been. Manual cleanup needed.")
}
if err == nil {
t.Fatal("Fetching deleted PagerDuty integration didn't lead to an error.")
}
}
/*
Slack Integration
*/
func TestIntegrationSlackCreateAndDelete(t *testing.T) {
expected := createTestIntegrationSlack(t)
defer cleanUpIntegrationSlack(t)
actual, err := client.GetIntegrationSlack()
if err != nil {
t.Fatalf("Retrieving a Slack integration failed when it shouldn't: (%s)", err)
}
expectedServiceHooksAccounts := make([]*string, len(expected.ServiceHooks))
for _, service := range expected.ServiceHooks {
expectedServiceHooksAccounts = append(expectedServiceHooksAccounts, service.Account)
}
actualServiceHooksAccounts := make([]*string, len(actual.ServiceHooks))
for _, service := range actual.ServiceHooks {
actualServiceHooksAccounts = append(actualServiceHooksAccounts, service.Account)
}
assert.Equal(t, expectedServiceHooksAccounts, actualServiceHooksAccounts)
}
func TestIntegrationSlackUpdate(t *testing.T) {
slackIntegration := createTestIntegrationSlack(t)
defer cleanUpIntegrationSlack(t)
slackIntegration.ServiceHooks = append(slackIntegration.ServiceHooks, datadog.ServiceHookSlackRequest{
Account: datadog.String("Main_Account_2"),
Url: datadog.String("https://hooks.slack.com/services/2/2"),
})
if err := client.UpdateIntegrationSlack(slackIntegration); err != nil {
t.Fatalf("Updating a Slack integration failed when it shouldn't: %s", err)
}
actual, err := client.GetIntegrationSlack()
if err != nil {
t.Fatalf("Retrieving a Slack integration failed when it shouldn't: %s", err)
}
expectedServiceHooksAccounts := make([]*string, len(slackIntegration.ServiceHooks))
for _, service := range slackIntegration.ServiceHooks {
expectedServiceHooksAccounts = append(expectedServiceHooksAccounts, service.Account)
}
actualServiceHooksAccounts := make([]*string, len(actual.ServiceHooks))
for _, service := range actual.ServiceHooks {
actualServiceHooksAccounts = append(actualServiceHooksAccounts, service.Account)
}
assert.Equal(t, expectedServiceHooksAccounts, actualServiceHooksAccounts)
}
func TestIntegrationSlackGet(t *testing.T) {
slackIntegration := createTestIntegrationSlack(t)
defer cleanUpIntegrationSlack(t)
actual, err := client.GetIntegrationSlack()
if err != nil {
t.Fatalf("Retrieving Slack integration failed when it shouldn't: %s", err)
}
expectedServiceHooksAccounts := make([]*string, len(slackIntegration.ServiceHooks))
for _, service := range slackIntegration.ServiceHooks {
expectedServiceHooksAccounts = append(expectedServiceHooksAccounts, service.Account)
}
actualServiceHooksAccounts := make([]*string, len(actual.ServiceHooks))
for _, service := range actual.ServiceHooks {
actualServiceHooksAccounts = append(actualServiceHooksAccounts, service.Account)
}
assert.Equal(t, expectedServiceHooksAccounts, actualServiceHooksAccounts)
}
func getTestIntegrationSlack() *datadog.IntegrationSlackRequest {
return &datadog.IntegrationSlackRequest{
ServiceHooks: []datadog.ServiceHookSlackRequest{
{
Account: datadog.String("Main_Account"),
Url: datadog.String("https://hooks.slack.com/services/1/1"),
},
},
Channels: []datadog.ChannelSlackRequest{
{
ChannelName: datadog.String("#private"),
TransferAllUserComments: datadog.Bool(true),
Account: datadog.String("Main_Account"),
},
},
}
}
func createTestIntegrationSlack(t *testing.T) *datadog.IntegrationSlackRequest {
slackIntegration := getTestIntegrationSlack()
err := client.CreateIntegrationSlack(slackIntegration)
if err != nil {
t.Fatalf("Creating a Slack integration failed when it shouldn't: %s", err)
}
return slackIntegration
}
func cleanUpIntegrationSlack(t *testing.T) {
if err := client.DeleteIntegrationSlack(); err != nil {
t.Fatalf("Deleting the Slack integration failed when it shouldn't. Manual cleanup needed. (%s)", err)
}
slackIntegration, err := client.GetIntegrationSlack()
if slackIntegration != nil {
t.Fatal("Slack Integration hasn't been deleted when it should have been. Manual cleanup needed.")
}
if err == nil {
t.Fatal("Fetching deleted Slack integration didn't lead to an error.")
}
}
/*
Webhook Integration
*/
func TestIntegrationWebhookCreateAndDelete(t *testing.T) {
expected := createTestIntegrationWebhook(t)
defer cleanUpIntegrationWebhook(t)
actual, err := client.GetIntegrationWebhook()
if err != nil {
t.Fatalf("Retrieving a Webhook integration failed when it shouldn't: (%s)", err)
}
expectedWebhooks := make([]*string, len(expected.Webhooks))
for _, wh := range expected.Webhooks {
expectedWebhooks = append(expectedWebhooks, wh.Name)
}
actualWebhooks := make([]*string, len(actual.Webhooks))
for _, wh := range actual.Webhooks {
actualWebhooks = append(actualWebhooks, wh.Name)
}
assert.Equal(t, expectedWebhooks, actualWebhooks)
}
func TestIntegrationWebhookUpdate(t *testing.T) {
webhookIntegration := createTestIntegrationWebhook(t)
defer cleanUpIntegrationWebhook(t)
webhookIntegration.Webhooks = append(webhookIntegration.Webhooks, datadog.Webhook{
Name: datadog.String("Test_Webhook2"),
URL: datadog.String("https://test.url.com/webhook"),
UseCustomPayload: datadog.String("true"),
CustomPayload: datadog.String("custom_payload"),
EncodeAsForm: datadog.String("true"),
Headers: datadog.String("{'Content-Type': 'application/text', 'Authorization': 'token'}"),
})
if err := client.UpdateIntegrationWebhook(webhookIntegration); err != nil {
t.Fatalf("Updating a Webhook integration failed when it shouldn't: %s", err)
}
actual, err := client.GetIntegrationWebhook()
if err != nil {
t.Fatalf("Retrieving a Webhook integration failed when it shouldn't: %s", err)
}
expectedWebhooks := make([]*string, len(webhookIntegration.Webhooks))
for _, wh := range webhookIntegration.Webhooks {
expectedWebhooks = append(expectedWebhooks, wh.Name)
}
actualWebhooks := make([]*string, len(actual.Webhooks))
for _, wh := range actual.Webhooks {
actualWebhooks = append(actualWebhooks, wh.Name)
}
assert.Equal(t, expectedWebhooks, actualWebhooks)
}
func TestIntegrationWebhookGet(t *testing.T) {
webhookIntegration := createTestIntegrationWebhook(t)
defer cleanUpIntegrationWebhook(t)
actual, err := client.GetIntegrationWebhook()
if err != nil {
t.Fatalf("Retrieving Webhook integration failed when it shouldn't: %s", err)
}
expectedWebhooks := make([]*string, len(webhookIntegration.Webhooks))
for _, wh := range webhookIntegration.Webhooks {
expectedWebhooks = append(expectedWebhooks, wh.Name)
}
actualWebhooks := make([]*string, len(actual.Webhooks))
for _, wh := range actual.Webhooks {
actualWebhooks = append(actualWebhooks, wh.Name)
}
assert.Equal(t, expectedWebhooks, actualWebhooks)
}
func getTestIntegrationWebhook() *datadog.IntegrationWebhookRequest {
return &datadog.IntegrationWebhookRequest{
Webhooks: []datadog.Webhook{
{
Name: datadog.String("Test_Webhook1"),
URL: datadog.String("https://test.url.com/webhook"),
UseCustomPayload: datadog.String("true"),
CustomPayload: datadog.String("custom_payload"),
EncodeAsForm: datadog.String("true"),
Headers: datadog.String("{'Content-Type': 'application/text', 'Authorization': 'token'}"),
},
},
}
}
func createTestIntegrationWebhook(t *testing.T) *datadog.IntegrationWebhookRequest {
webhookIntegration := getTestIntegrationWebhook()
err := client.CreateIntegrationWebhook(webhookIntegration)
if err != nil {
t.Fatalf("Creating a Webhook integration failed when it shouldn't: %s", err)
}
return webhookIntegration
}
func cleanUpIntegrationWebhook(t *testing.T) {
if err := client.DeleteIntegrationWebhook(); err != nil {
t.Fatalf("Deleting the Webhook integration failed when it shouldn't. Manual cleanup needed. (%s)", err)
}
webhookIntegration, err := client.GetIntegrationWebhook()
if webhookIntegration != nil {
t.Fatal("Webhook Integration hasn't been deleted when it should have been. Manual cleanup needed.")
}
if err == nil {
t.Fatal("Fetching deleted Webhook integration didn't lead to an error.")
}
}
/*
AWS Integration
*/
func TestIntegrationAWSCreateAndDelete(t *testing.T) {
// Validate creation of AWS Integration
expected := getTestIntegrationAWS()
createAwsIntegrationResponse := createTestIntegrationAWS(t)
defer cleanUpIntegrationAWS(t, getTestIntegrationAWSDeleteRequest())
assert.NotNil(t, createAwsIntegrationResponse.ExternalID, "An external ID should have been returned from Datadog on integration create.")
// Get the AWS Accounts from the AWS Integration
awsAccountsInDatadog, err := client.GetIntegrationAWS()
if err != nil {
t.Fatalf("Retrieving a AWS integration failed when it shouldn't: (%s)", err)
}
// Check the created AWS Account is in the slice
var createdAWSAccount datadog.IntegrationAWSAccount
for _, account := range *awsAccountsInDatadog {
if *account.AccountID == *expected.AccountID {
createdAWSAccount = account
}
}
// Test each property as slices order can change in FilterTags and HostTags
assert.Equal(t, expected.AccountID, createdAWSAccount.AccountID)
assert.Equal(t, expected.RoleName, createdAWSAccount.RoleName)
assert.ElementsMatch(t, expected.FilterTags, createdAWSAccount.FilterTags)
assert.ElementsMatch(t, expected.HostTags, createdAWSAccount.HostTags)
assert.Equal(t, expected.AccountSpecificNamespaceRules, createdAWSAccount.AccountSpecificNamespaceRules)
}
func getTestIntegrationAWS() *datadog.IntegrationAWSAccount {
return &datadog.IntegrationAWSAccount{
AccountID: datadog.String("1111111111111"),
RoleName: datadog.String("GoLangDatadogAWSIntegrationRole"),
FilterTags: []string{
"env:production",
"instance-type:c1.*",
"!region:us-east-1",
},
HostTags: []string{
"account:my_aws_account",
},
AccountSpecificNamespaceRules: map[string]bool{
"auto_scaling": false,
"opsworks": false,
},
}
}
func getTestIntegrationAWSDeleteRequest() *datadog.IntegrationAWSAccountDeleteRequest {
return &datadog.IntegrationAWSAccountDeleteRequest{
AccountID: datadog.String("1111111111111"),
RoleName: datadog.String("GoLangDatadogAWSIntegrationRole"),
}
}
func createTestIntegrationAWS(t *testing.T) *datadog.IntegrationAWSAccountCreateResponse {
awsIntegrationRequest := getTestIntegrationAWS()
result, err := client.CreateIntegrationAWS(awsIntegrationRequest)
if err != nil {
t.Fatalf("Creating a AWS integration failed when it shouldn't: %s", err.Error())
}
return result
}
func cleanUpIntegrationAWS(t *testing.T, awsAccount *datadog.IntegrationAWSAccountDeleteRequest) {
if err := client.DeleteIntegrationAWS(awsAccount); err != nil {
t.Fatalf("Deleting the AWS Account from the AWS integration failed when it shouldn't. Manual cleanup needed. (%s)", err)
}
awsIntegration, err := client.GetIntegrationAWS()
// check the account is no longer in Datadog
for _, account := range *awsIntegration {
if *account.AccountID == *awsAccount.AccountID {
t.Fatal("AWS Account in the AWS Integration hasn't been deleted when it should have been. Manual cleanup needed.")
}
}
if err != nil {
t.Fatalf("Getting AWS accounts from the AWS integration failed when it shouldn't: %s", err)
}
}
/*
Google Cloud Platform Integration
*/
func TestIntegrationGCPCreateAndDelete(t *testing.T) {
expected := createTestIntegrationGCP(t)
defer cleanUpIntegrationGCP(t)
actual, err := client.ListIntegrationGCP()
if err != nil {
t.Fatalf("Retrieving a GCP integration failed when it shouldn't: (%s)", err)
}
assert.Equal(t, 1, len(actual))
assert.Equal(t, expected.ProjectID, actual[0].ProjectID)
assert.Equal(t, expected.ClientEmail, actual[0].ClientEmail)
assert.Equal(t, expected.HostFilters, actual[0].HostFilters)
assert.Equal(t, expected.AutoMute, actual[0].AutoMute)
}
func TestIntegrationGCPUpdate(t *testing.T) {
req := createTestIntegrationGCP(t)
defer cleanUpIntegrationGCP(t)
newHostFilters := datadog.String("name0:value0,name1:value1")
newAutoMute := datadog.Bool(false)
if err := client.UpdateIntegrationGCP(&datadog.IntegrationGCPUpdateRequest{
Type: req.Type,
ProjectID: req.ProjectID,
PrivateKeyID: req.PrivateKeyID,
PrivateKey: req.PrivateKey,
ClientEmail: req.ClientEmail,
ClientID: req.ClientID,
AuthURI: req.AuthURI,
TokenURI: req.TokenURI,
AuthProviderX509CertURL: req.AuthProviderX509CertURL,
ClientX509CertURL: req.ClientX509CertURL,
HostFilters: newHostFilters,
AutoMute: newAutoMute,
}); err != nil {
t.Fatalf("Updating a GCP integration failed when it shouldn't: %s", err)
}
actual, err := client.ListIntegrationGCP()
if err != nil {
t.Fatalf("Retrieving a GCP integration failed when it shouldn't: %s", err)
}
assert.Equal(t, 1, len(actual))
assert.Equal(t, req.ProjectID, actual[0].ProjectID)
assert.Equal(t, req.ClientEmail, actual[0].ClientEmail)
assert.Equal(t, newHostFilters, actual[0].HostFilters)
assert.Equal(t, newAutoMute, actual[0].AutoMute)
}
func getTestIntegrationGCPCreateRequest() *datadog.IntegrationGCPCreateRequest {
return &datadog.IntegrationGCPCreateRequest{
Type: datadog.String("service_account"),
ProjectID: datadog.String("test-project-id"),
PrivateKeyID: datadog.String("1234567890123456789012345678901234567890"),
PrivateKey: datadog.String(""),
ClientEmail: datadog.String("go-datadog-api@test-project-id.iam.gserviceaccount.com"),
ClientID: datadog.String("123456789012345678901"),
AuthURI: datadog.String("https://accounts.google.com/o/oauth2/auth"),
TokenURI: datadog.String("https://oauth2.googleapis.com/token"),
AuthProviderX509CertURL: datadog.String("https://www.googleapis.com/oauth2/v1/certs"),
ClientX509CertURL: datadog.String("https://www.googleapis.com/robot/v1/metadata/x509/go-datadog-api@test-project-id.iam.gserviceaccount.com"),
HostFilters: datadog.String("foo:bar,buzz:lightyear"),
AutoMute: datadog.Bool(true),
}
}
func createTestIntegrationGCP(t *testing.T) *datadog.IntegrationGCPCreateRequest {
req := getTestIntegrationGCPCreateRequest()
err := client.CreateIntegrationGCP(req)
if err != nil {
t.Fatalf("Creating a GCP integration failed when it shouldn't: %s", err)
}
return req
}
func cleanUpIntegrationGCP(t *testing.T) {
if err := client.DeleteIntegrationGCP(&datadog.IntegrationGCPDeleteRequest{
ProjectID: datadog.String("test-project-id"),
ClientEmail: datadog.String("go-datadog-api@test-project-id.iam.gserviceaccount.com"),
}); err != nil {
t.Fatalf("Deleting the GCP integration failed when it shouldn't. Manual cleanup needed. (%s)", err)
}
actual, err := client.ListIntegrationGCP()
if err != nil {
t.Fatalf("Fetching deleted GCP integration didn't lead to an error: %s", err)
}
assert.Equal(t, 0, len(actual))
}
|