Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for cloud ints #32

Merged
merged 14 commits into from
Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions api/v1/datadog/api_aws_logs_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package datadog_test

import (
"fmt"
"testing"
"time"

"github.com/DataDog/datadog-api-client-go/api/v1/datadog"
"gotest.tools/assert"
)

func generateUniqueAwsLambdaAccounts() (datadog.AwsAccount, datadog.AwsAccountAndLambdaInput, datadog.AwsLogsServicesInput) {
accountId := fmt.Sprintf("go_%09d", time.Now().UnixNano()%1000000000)
var uniqueAwsAccount = datadog.AwsAccount{
AccountId: &accountId,
RoleName: datadog.PtrString("DatadogAWSIntegrationRole"),
AccountSpecificNamespaceRules: &map[string]bool{"opsworks": true},
FilterTags: &[]string{"testTag", "test:Tag2"},
HostTags: &[]string{"filter:one", "filtertwo"},
}

var testLambdaArn = datadog.AwsAccountAndLambdaInput{
AccountId: *uniqueAwsAccount.AccountId,
LambdaArn: "arn:aws:lambda:us-east-1:123456789101:function:GoClientTest",
}

var savedServices = datadog.AwsLogsServicesInput{
AccountId: *uniqueAwsAccount.AccountId,
Services: []string{"s3", "elb", "elbv2", "cloudfront", "redshift", "lambda"},
}

return uniqueAwsAccount, testLambdaArn, savedServices
}

// Test AddAWSLambdaARN and EnableServices endpoints
func TestAddAndSaveAWSLogs(t *testing.T) {
// Setup the Client we'll use to interact with the Test account
teardownTest := setupTest(t)
defer teardownTest(t)
testawsacc, testLambdaAcc, testServices := generateUniqueAwsLambdaAccounts()
defer uninstallAWSIntegration(testawsacc)

// Assert AWS Integration Created with proper fields
TESTAPICLIENT.AWSIntegrationApi.CreateAWSAccount(TESTAUTH, testawsacc)
_, httpResp, err := TESTAPICLIENT.AWSLogsIntegrationApi.AddAWSLambdaARN(TESTAUTH, testLambdaAcc)
if err != nil {
t.Errorf("Error Creating AWS account: %v: %v", testawsacc.AccountId, err)
}
assert.Equal(t, httpResp.StatusCode, 200)

_, httpResp, err = TESTAPICLIENT.AWSLogsIntegrationApi.EnableAWSLogServices(TESTAUTH, testServices)
if err != nil {
t.Errorf("Error enabling log services: %v", err)
}
assert.Equal(t, httpResp.StatusCode, 200)
}

func TestListAWSLogsServices(t *testing.T) {
// Setup the Client we'll use to interact with the Test account
teardownTest := setupTest(t)
defer teardownTest(t)
list_services_output, _, err := TESTAPICLIENT.AWSLogsIntegrationApi.AWSLogsServicesList(TESTAUTH)
if err != nil {
t.Errorf("Error list log services: %v", err)
}

// There are currently 6 supported AWS Logs services as noted in the docs
// https://docs.datadoghq.com/api/?lang=bash#get-list-of-aws-log-ready-services
assert.Check(t, len(list_services_output) >= 6)
}

func TestListAndDeleteAWSLogs(t *testing.T) {
// Setup the Client we'll use to interact with the Test account
teardownTest := setupTest(t)
defer teardownTest(t)
testAWSAcc, testLambdaAcc, testServices := generateUniqueAwsLambdaAccounts()
defer uninstallAWSIntegration(testAWSAcc)
// Create the AWS integration.
TESTAPICLIENT.AWSIntegrationApi.CreateAWSAccount(TESTAUTH, testAWSAcc)

// Add Lambda to Account
add_output, httpresp, err := TESTAPICLIENT.AWSLogsIntegrationApi.AddAWSLambdaARN(TESTAUTH, testLambdaAcc)
if err != nil || httpresp.StatusCode != 200 {
t.Errorf("Error Adding Lambda %v: Status: %v: %v", add_output, httpresp.StatusCode, err)
}

// Enable services for Lambda
TESTAPICLIENT.AWSLogsIntegrationApi.EnableAWSLogServices(TESTAUTH, testServices)

// List AWS Logs integrations before deleting
list_output_1, _, err := TESTAPICLIENT.AWSLogsIntegrationApi.AWSLogsList(TESTAUTH)
if err != nil {
t.Errorf("Error list logs: %v", err)
}
// Iterate over output and list Lambdas
var x = false
for _, Account := range list_output_1 {
if Account.GetAccountId() == *testAWSAcc.AccountId {
if Account.GetLambdas()[0].GetArn() == testLambdaAcc.LambdaArn {
x = true
}
}
}
// Test that variable is true as expected
assert.Equal(t, x, true)

// Delete newly added Lambda
delete_output, httpResp, err := TESTAPICLIENT.AWSLogsIntegrationApi.DeleteAWSLambdaARN(TESTAUTH, testLambdaAcc)
if err != nil || httpResp.StatusCode != 200 {
t.Errorf("Error Deleting Lambda %v: Status: %v: %v", delete_output, httpResp.StatusCode, err)
}
assert.Equal(t, httpResp.StatusCode, 200)

// List AWS logs integrations after deleting
list_output_2, _, _ := TESTAPICLIENT.AWSLogsIntegrationApi.AWSLogsList(TESTAUTH)

var list_of_arns_2 []datadog.AwsLogsListOutputLambdas
x = false
Copy link
Contributor

@jirikuncar jirikuncar Dec 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
x = false
existsAfterDelete = false

for _, Account := range list_output_2 {
if Account.GetAccountId() == *testAWSAcc.AccountId {
list_of_arns_2 = Account.GetLambdas()
}
}
for _, lambda := range list_of_arns_2 {
if lambda.GetArn() == testLambdaAcc.LambdaArn {
x = true
}
}
// Check that ARN no longer exists after delete
assert.Assert(t, x != true)
Copy link
Contributor

@jirikuncar jirikuncar Dec 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.Assert(t, x != true)
assert.Assert(t, !existsAfterDelete)

}
145 changes: 145 additions & 0 deletions api/v1/datadog/api_azure_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package datadog_test

import (
"fmt"
"log"
"testing"
"time"

"github.com/DataDog/datadog-api-client-go/api/v1/datadog"
"gotest.tools/assert"
)

func generateUniqueAzureAccount() (datadog.AzureAccount, datadog.AzureAccount, datadog.AzureAccount) {
tenantName := fmt.Sprintf("go_test-1234-5678-9101-%s", time.Now())
var testAzureAcct = datadog.AzureAccount{
ClientId: datadog.PtrString("testc7f6-1234-5678-9101-3fcbf464test"),
ClientSecret: datadog.PtrString("testingx./Sw*g/Y33t..R1cH+hScMDt"),
TenantName: datadog.PtrString(tenantName),
}

var testUpdateAzureAcct = datadog.AzureAccount{
ClientId: testAzureAcct.ClientId,
ClientSecret: testAzureAcct.ClientSecret,
TenantName: testAzureAcct.TenantName,
NewClientId: datadog.PtrString("testc7f6-1234-5678-9101-3fcbf4update"),
NewTenantName: datadog.PtrString("testc44-1234-5678-9101-cc0073update"),
HostFilters: datadog.PtrString("filter:foo,test:bar"),
}

var testAzureUpdateHostFilters = datadog.AzureAccount{
ClientId: testUpdateAzureAcct.NewClientId,
TenantName: testUpdateAzureAcct.NewTenantName,
HostFilters: datadog.PtrString("test:foo,test:bar"),
}

return testAzureAcct, testUpdateAzureAcct, testAzureUpdateHostFilters
}

func TestAzureCreate(t *testing.T) {
// Setup the Client we'll use to interact with the Test account
teardownTest := setupTest(t)
defer teardownTest(t)
testAzureAcct, _, _ := generateUniqueAzureAccount()
defer uninstallAzureIntegration(testAzureAcct)

_, httpResp, err := TESTAPICLIENT.AzureIntegrationApi.CreateAzureIntegration(TESTAUTH, testAzureAcct)
if err != nil {
t.Errorf("Error Creating Azure intg: %v", err)
}
assert.Equal(t, httpResp.StatusCode, 200)
}

func TestAzureListandDelete(t *testing.T) {
// Setup the Client we'll use to interact with the Test account
teardownTest := setupTest(t)
defer teardownTest(t)
testAzureAcct, _, testAzureUpdateHostFilters := generateUniqueAzureAccount()
defer uninstallAzureIntegration(testAzureAcct)
defer uninstallAzureIntegration(testAzureUpdateHostFilters)

// Setup Azure Account to List
TESTAPICLIENT.AzureIntegrationApi.CreateAzureIntegration(TESTAUTH, testAzureAcct)

azure_list_output, _, err := TESTAPICLIENT.AzureIntegrationApi.ListAzureIntegration(TESTAUTH)
if err != nil {
t.Errorf("Error listing azure intgs: %v", err)
}
var x datadog.AzureAccount
for _, Account := range azure_list_output {
if Account.GetClientId() == *testAzureAcct.ClientId {
x = Account
}
}
assert.Equal(t, x.GetClientId(), *testAzureAcct.ClientId)
assert.Equal(t, x.GetTenantName(), *testAzureAcct.TenantName)

// Assert returned list is greater than or equal to 1
assert.Assert(t, len(azure_list_output) >= 1)

// Test account deletion as well
_, httpResp, err := TESTAPICLIENT.AzureIntegrationApi.DeleteAzureIntegration(TESTAUTH, testAzureAcct)
if httpResp.StatusCode != 200 || err != nil {
t.Errorf("Error uninstalling Azure Account: %v, Another test may have already removed this account.", testAzureAcct)
}
assert.Equal(t, httpResp.StatusCode, 200)
}

func TestUpdateAzureAccount(t *testing.T) {
// Setup the Client we'll use to interact with the Test account
teardownTest := setupTest(t)
defer teardownTest(t)
testAzureAcct, testUpdateAzureAcct, testAzureUpdateHostFilters := generateUniqueAzureAccount()
defer uninstallAzureIntegration(testAzureAcct)

// Setup Azure Account to Update
TESTAPICLIENT.AzureIntegrationApi.CreateAzureIntegration(TESTAUTH, testAzureAcct)

_, httpResp, err := TESTAPICLIENT.AzureIntegrationApi.UpdateAzureIntegration(TESTAUTH, testUpdateAzureAcct)
defer uninstallAzureIntegration(testUpdateAzureAcct)
if err != nil {
t.Errorf("Error Updating Azure Account: %v", err)
}

assert.Equal(t, httpResp.StatusCode, 200)

// List account to ensure update worked.
azure_list_output, _, err := TESTAPICLIENT.AzureIntegrationApi.ListAzureIntegration(TESTAUTH)
if err != nil {
t.Errorf("Error listing Azure intgs: %v", err)
}
var x datadog.AzureAccount
for _, Account := range azure_list_output {
if Account.GetClientId() == *testUpdateAzureAcct.NewClientId {
x = Account
}
}
assert.Equal(t, x.GetClientId(), *testUpdateAzureAcct.NewClientId)
assert.Equal(t, x.GetTenantName(), *testUpdateAzureAcct.NewTenantName)
assert.Equal(t, x.GetHostFilters(), *testUpdateAzureAcct.HostFilters)

// Test update host filters endpoint
_, httpResp, err = TESTAPICLIENT.AzureIntegrationApi.AzureUpdateHostFilters(TESTAUTH, testAzureUpdateHostFilters)
if err != nil {
t.Errorf("Error Updating Azure Host Filters: %v", err)
}
assert.Equal(t, httpResp.StatusCode, 200)
hf_list_output, _, err := TESTAPICLIENT.AzureIntegrationApi.ListAzureIntegration(TESTAUTH)
if err != nil {
t.Errorf("Error listing Azure intgs: %v", err)
}
var y datadog.AzureAccount
for _, Account := range hf_list_output {
if Account.GetClientId() == *testAzureUpdateHostFilters.ClientId {
y = Account
}
}
assert.Equal(t, y.GetHostFilters(), *testAzureUpdateHostFilters.HostFilters)
}

func uninstallAzureIntegration(account datadog.AzureAccount) {
_, httpresp, err := TESTAPICLIENT.AzureIntegrationApi.DeleteAzureIntegration(TESTAUTH, account)
if httpresp.StatusCode != 200 || err != nil {
log.Printf("Error uninstalling Azure Account: %v, Another test may have already removed this account.", account)
}
}
Loading