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 remaining aws integrations #48

Closed
wants to merge 9 commits into from
Closed
46 changes: 41 additions & 5 deletions api/v1/datadog/api_aws_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestCreateAWSAccount(t *testing.T) {
// Assert AWS Integration Created with proper fields
_, httpresp, err := TESTAPICLIENT.AWSIntegrationApi.CreateAWSAccount(TESTAUTH).AwsAccount(testAwsAccount).Execute()
if err != nil {
t.Errorf("Error creating AWS Account: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
t.Fatalf("Error creating AWS Account: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpresp.StatusCode, 200)
defer uninstallAWSIntegration(testAwsAccount)
Expand All @@ -50,7 +50,7 @@ func TestCreateAWSAccount(t *testing.T) {
RoleName(testAwsAccount.GetRoleName()).
Execute()
if err != nil {
t.Errorf("Error getting AWS Account: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
t.Fatalf("Error getting AWS Account: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpresp.StatusCode, 200)

Expand Down Expand Up @@ -87,7 +87,7 @@ func TestUpdateAWSAccount(t *testing.T) {
RoleName(testAwsAccount.GetRoleName()).
Execute()
if err != nil {
t.Errorf("Error updating AWS Account: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
t.Fatalf("Error updating AWS Account: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpresp.StatusCode, 200)
UPDATEDAWSACCT := datadog.AwsAccount{
Expand All @@ -102,7 +102,7 @@ func TestUpdateAWSAccount(t *testing.T) {
RoleName(UPDATEDAWSACCT.GetRoleName()).
Execute()
if err != nil {
t.Errorf("Error getting AWS Account: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
t.Fatalf("Error getting AWS Account: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpresp.StatusCode, 200)

Expand All @@ -125,7 +125,7 @@ func TestDisableAWSAcct(t *testing.T) {
// Lets first create the account of us to delete
_, httpresp, err := TESTAPICLIENT.AWSIntegrationApi.CreateAWSAccount(TESTAUTH).AwsAccount(testAwsAccount).Execute()
if err != nil {
t.Errorf("Error creating AWS Account: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
t.Fatalf("Error creating AWS Account: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpresp.StatusCode, 200)
defer uninstallAWSIntegration(testAwsAccount)
Expand All @@ -137,6 +137,42 @@ func TestDisableAWSAcct(t *testing.T) {
assert.Equal(t, httpresp.StatusCode, 200, "Error disabling AWS Account: %v", httpresp)
}

func TestGenerateNewExternalId(t *testing.T) {
testAwsAccount := generateUniqueAwsAccount()
// Lets first create the account for us to generate a new id against
_, httpresp, err := TESTAPICLIENT.AWSIntegrationApi.CreateAWSAccount(TESTAUTH).AwsAccount(testAwsAccount).Execute()
if err != nil {
t.Fatalf("Error creating AWS Account: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpresp.StatusCode, 200)

apiResp, httpresp, err := TESTAPICLIENT.AWSIntegrationApi.GenerateNewAWSExternalID(TESTAUTH).AwsAccount(testAwsAccount).Execute()
if err != nil {
t.Fatalf("Error generating new AWS External ID: Response: %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpresp.StatusCode, 200)
assert.Assert(t, apiResp.GetExternalId() != "")
}

func TestListNamespaces(t *testing.T) {
namespaces, httpresp, err := TESTAPICLIENT.AWSIntegrationApi.ListAvailableAWSNamespaces(TESTAUTH).Execute()
if err != nil {
t.Fatalf("Error listing AWS Namespaces: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpresp.StatusCode, 200)
namespacesCheck := make(map[string]bool)
for _, namespace := range namespaces {
namespacesCheck[namespace] = true
}

// Check that a few examples are in the response
// Full list - https://docs.datadoghq.com/api/?lang=bash#list-namespace-rules
assert.Assert(t, namespacesCheck["api_gateway"], true)
assert.Assert(t, namespacesCheck["cloudsearch"], true)
assert.Assert(t, namespacesCheck["directconnect"], true)
assert.Assert(t, namespacesCheck["xray"], true)
}

func uninstallAWSIntegration(account datadog.AwsAccount) {
_, httpresp, err := TESTAPICLIENT.AWSIntegrationApi.DeleteAWSAccount(TESTAUTH).AwsAccount(account).Execute()
if httpresp.StatusCode != 200 || err != nil {
Expand Down
138 changes: 100 additions & 38 deletions api/v1/datadog/api_aws_logs_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"gotest.tools/assert"
)

func generateUniqueAwsLambdaAccounts() (datadog.AwsAccount, datadog.AwsAccountAndLambdaInput, datadog.AwsLogsServicesInput) {
func generateUniqueAwsLambdaAccounts() (datadog.AwsAccount, datadog.AwsAccountAndLambdaRequest, datadog.AwsLogsServicesRequest) {
accountID := fmt.Sprintf("go_%09d", time.Now().UnixNano()%1000000000)
var uniqueAwsAccount = datadog.AwsAccount{
AccountId: &accountID,
Expand All @@ -19,12 +19,12 @@ func generateUniqueAwsLambdaAccounts() (datadog.AwsAccount, datadog.AwsAccountAn
HostTags: &[]string{"filter:one", "filtertwo"},
}

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

var savedServices = datadog.AwsLogsServicesInput{
var savedServices = datadog.AwsLogsServicesRequest{
AccountId: *uniqueAwsAccount.AccountId,
Services: []string{"s3", "elb", "elbv2", "cloudfront", "redshift", "lambda"},
}
Expand All @@ -41,34 +41,34 @@ func TestAddAndSaveAWSLogs(t *testing.T) {
defer uninstallAWSIntegration(testawsacc)

// Assert AWS Integration Created with proper fields
_, httpResp, err := TESTAPICLIENT.AWSIntegrationApi.CreateAWSAccount(TESTAUTH).AwsAccount(testawsacc).Execute()
_, httpresp, err := TESTAPICLIENT.AWSIntegrationApi.CreateAWSAccount(TESTAUTH).AwsAccount(testawsacc).Execute()
if err != nil {
t.Fatalf("Error creating AWS Account: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpResp.StatusCode, 200)
assert.Equal(t, httpresp.StatusCode, 200)

_, httpResp, err = TESTAPICLIENT.AWSLogsIntegrationApi.AddAWSLambdaARN(TESTAUTH).AwsAccountAndLambdaInput(testLambdaAcc).Execute()
_, httpresp, err = TESTAPICLIENT.AWSLogsIntegrationApi.AddAWSLambdaARN(TESTAUTH).AwsAccountAndLambdaRequest(testLambdaAcc).Execute()
if err != nil {
t.Errorf("Error adding lamda ARN: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
t.Fatalf("Error adding lamda ARN: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpResp.StatusCode, 200)
assert.Equal(t, httpresp.StatusCode, 200)

_, httpResp, err = TESTAPICLIENT.AWSLogsIntegrationApi.EnableAWSLogServices(TESTAUTH).AwsLogsServicesInput(testServices).Execute()
_, httpresp, err = TESTAPICLIENT.AWSLogsIntegrationApi.EnableAWSLogServices(TESTAUTH).AwsLogsServicesRequest(testServices).Execute()
if err != nil {
t.Errorf("Error enabling log services: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
t.Fatalf("Error enabling log services: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpResp.StatusCode, 200)
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)
listServicesOutput, httpResp, err := TESTAPICLIENT.AWSLogsIntegrationApi.AWSLogsServicesList(TESTAUTH).Execute()
listServicesOutput, httpresp, err := TESTAPICLIENT.AWSLogsIntegrationApi.AWSLogsServicesList(TESTAUTH).Execute()
if err != nil {
t.Errorf("Error listing log services: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
t.Fatalf("Error listing log services: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpResp.StatusCode, 200)
assert.Equal(t, httpresp.StatusCode, 200)

// 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
Expand All @@ -83,70 +83,132 @@ func TestListAndDeleteAWSLogs(t *testing.T) {
defer uninstallAWSIntegration(testAWSAcc)

// Create the AWS integration.
_, httpResp, err := TESTAPICLIENT.AWSIntegrationApi.CreateAWSAccount(TESTAUTH).AwsAccount(testAWSAcc).Execute()
_, httpresp, err := TESTAPICLIENT.AWSIntegrationApi.CreateAWSAccount(TESTAUTH).AwsAccount(testAWSAcc).Execute()
if err != nil {
t.Fatalf("Error creating AWS Account: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpResp.StatusCode, 200)
assert.Equal(t, httpresp.StatusCode, 200)

// Add Lambda to Account
addOutput, httpresp, err := TESTAPICLIENT.AWSLogsIntegrationApi.AddAWSLambdaARN(TESTAUTH).AwsAccountAndLambdaInput(testLambdaAcc).Execute()
if err != nil || httpresp.StatusCode != 200 {
t.Errorf("Error Adding Lambda %v: Response %s: %v", addOutput, err.(datadog.GenericOpenAPIError).Body(), err)
addOutput, httpresp, err := TESTAPICLIENT.AWSLogsIntegrationApi.AddAWSLambdaARN(TESTAUTH).AwsAccountAndLambdaRequest(testLambdaAcc).Execute()
if err != nil {
t.Fatalf("Error Adding Lambda %v: Response %s: %v", addOutput, err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpResp.StatusCode, 200)
assert.Equal(t, httpresp.StatusCode, 200)

// Enable services for Lambda
_, httpResp, err = TESTAPICLIENT.AWSLogsIntegrationApi.EnableAWSLogServices(TESTAUTH).AwsLogsServicesInput(testServices).Execute()
_, httpresp, err = TESTAPICLIENT.AWSLogsIntegrationApi.EnableAWSLogServices(TESTAUTH).AwsLogsServicesRequest(testServices).Execute()
if err != nil {
t.Fatalf("Error enabling log services: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpResp.StatusCode, 200)
assert.Equal(t, httpresp.StatusCode, 200)

// List AWS Logs integrations before deleting
listOutput1, _, err := TESTAPICLIENT.AWSLogsIntegrationApi.AWSLogsList(TESTAUTH).Execute()
if err != nil {
t.Errorf("Error listing log services: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
t.Fatalf("Error listing log services: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpResp.StatusCode, 200)
assert.Equal(t, httpresp.StatusCode, 200)
// Iterate over output and list Lambdas
var x = false
var accountExists = false
for _, Account := range listOutput1 {
if Account.GetAccountId() == *testAWSAcc.AccountId {
if Account.GetLambdas()[0].GetArn() == testLambdaAcc.LambdaArn {
x = true
accountExists = true
}
}
}
// Test that variable is true as expected
assert.Equal(t, x, true)
assert.Equal(t, accountExists, true)

// Delete newly added Lambda
deleteOutput, httpResp, err := TESTAPICLIENT.AWSLogsIntegrationApi.DeleteAWSLambdaARN(TESTAUTH).AwsAccountAndLambdaInput(testLambdaAcc).Execute()
if err != nil || httpresp.StatusCode != 200 {
t.Errorf("Error deleting Lambda %v: Response %s: %v", deleteOutput, err.(datadog.GenericOpenAPIError).Body(), err)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think Hippo has just changed it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! I went through PR #51 and cleaned up this + one other example I found in the GCP test. Should be all good now.

deleteOutput, httpresp, err := TESTAPICLIENT.AWSLogsIntegrationApi.DeleteAWSLambdaARN(TESTAUTH).AwsAccountAndLambdaRequest(testLambdaAcc).Execute()
if err != nil {
t.Fatalf("Error deleting Lambda %v: Response %s: %v", deleteOutput, err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpResp.StatusCode, 200)
assert.Equal(t, httpresp.StatusCode, 200)

// List AWS logs integrations after deleting
listOutput2, httpResp, err := TESTAPICLIENT.AWSLogsIntegrationApi.AWSLogsList(TESTAUTH).Execute()
listOutput2, httpresp, err := TESTAPICLIENT.AWSLogsIntegrationApi.AWSLogsList(TESTAUTH).Execute()
if err != nil {
t.Errorf("Error listing log services: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
t.Fatalf("Error listing log services: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpResp.StatusCode, 200)
assert.Equal(t, httpresp.StatusCode, 200)

var listOfARNs2 []datadog.AwsLogsListOutputLambdas
x = false
var listOfARNs2 []datadog.AwsLogsListResponseLambdas
var accountExistsAfterDelete = false
for _, Account := range listOutput2 {
if Account.GetAccountId() == *testAWSAcc.AccountId {
listOfARNs2 = Account.GetLambdas()
}
}
for _, lambda := range listOfARNs2 {
if lambda.GetArn() == testLambdaAcc.LambdaArn {
x = true
accountExistsAfterDelete = true
}
}
// Check that ARN no longer exists after delete
assert.Assert(t, x != true)
assert.Assert(t, accountExistsAfterDelete != true)
}

func TestCheckLambdaAsync(t *testing.T) {
// Setup the Client we'll use to interact with the Test account
teardownTest := setupTest(t)
defer teardownTest(t)

testAWSAcc, testLambdaAcc, _ := generateUniqueAwsLambdaAccounts()
defer uninstallAWSIntegration(testAWSAcc)

// Assert AWS Integration Created with proper fields
_, httpresp, err := TESTAPICLIENT.AWSIntegrationApi.CreateAWSAccount(TESTAUTH).AwsAccount(testAWSAcc).Execute()
if err != nil {
t.Fatalf("Error creating AWS Account: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpresp.StatusCode, 200)

status, httpresp, err := TESTAPICLIENT.AWSLogsIntegrationApi.AWSLogsCheckLambdaAsync(TESTAUTH).AwsAccountAndLambdaRequest(testLambdaAcc).Execute()
if err != nil {
t.Fatalf("Error checking the AWS Lambda Response: %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpresp.StatusCode, 200)

assert.Equal(t, len(status.GetErrors()), 0)
assert.Equal(t, status.GetStatus(), "created")

// Give the async call time to finish
time.Sleep(10 * time.Second)

status, httpresp, err = TESTAPICLIENT.AWSLogsIntegrationApi.AWSLogsCheckLambdaAsync(TESTAUTH).AwsAccountAndLambdaRequest(testLambdaAcc).Execute()
if err != nil {
t.Fatalf("Error checking the AWS Lambda Response: %s %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpresp.StatusCode, 200)

assert.Assert(t, status.GetErrors()[0].GetCode() != "")
assert.Assert(t, status.GetErrors()[0].GetMessage() != "")
assert.Equal(t, status.GetStatus(), "error")
}

func TestCheckServicesAsync(t *testing.T) {
// Setup the Client we'll use to interact with the Test account
teardownTest := setupTest(t)
defer teardownTest(t)
testAWSAcc, _, testServices := generateUniqueAwsLambdaAccounts()
defer uninstallAWSIntegration(testAWSAcc)

// Assert AWS Integration Created with proper fields
_, httpresp, err := TESTAPICLIENT.AWSIntegrationApi.CreateAWSAccount(TESTAUTH).AwsAccount(testAWSAcc).Execute()
if err != nil {
t.Fatalf("Error creating AWS Account: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpresp.StatusCode, 200)

status, httpresp, err := TESTAPICLIENT.AWSLogsIntegrationApi.AWSLogsCheckServicesAsync(TESTAUTH).AwsLogsServicesRequest(testServices).Execute()
if err != nil{
t.Fatalf("Error checking the AWS Logs Services Response: %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpresp.StatusCode, 200)

assert.Assert(t, status.GetErrors()[0].GetCode() != "")
assert.Assert(t, status.GetErrors()[0].GetMessage() != "")
}
21 changes: 11 additions & 10 deletions api/v1/datadog/api_azure_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestAzureCreate(t *testing.T) {

_, httpresp, err := TESTAPICLIENT.AzureIntegrationApi.CreateAzureIntegration(TESTAUTH).AzureAccount(testAzureAcct).Execute()
if err != nil {
t.Errorf("Error creating Azure Account: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
t.Fatalf("Error creating Azure Account: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpresp.StatusCode, 200)
}
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestAzureListandDelete(t *testing.T) {
// Test account deletion as well
_, httpresp, err = TESTAPICLIENT.AzureIntegrationApi.DeleteAzureIntegration(TESTAUTH).AzureAccount(testAzureAcct).Execute()
if err != nil {
t.Errorf("Error deleting Azure Account: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
t.Fatalf("Error deleting Azure Account: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpresp.StatusCode, 200)
}
Expand All @@ -104,17 +104,18 @@ func TestUpdateAzureAccount(t *testing.T) {
}
assert.Equal(t, httpresp.StatusCode, 200)

_, httpResp, err := TESTAPICLIENT.AzureIntegrationApi.UpdateAzureIntegration(TESTAUTH).AzureAccount(testUpdateAzureAcct).Execute()
_, httpresp, err = TESTAPICLIENT.AzureIntegrationApi.UpdateAzureIntegration(TESTAUTH).AzureAccount(testUpdateAzureAcct).Execute()
defer uninstallAzureIntegration(testUpdateAzureAcct)
if err != nil {
t.Errorf("Error updating Azure Account: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
t.Fatalf("Error updating Azure Account: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpResp.StatusCode, 200)

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

// List account to ensure update worked.
azureListOutput, _, err := TESTAPICLIENT.AzureIntegrationApi.ListAzureIntegration(TESTAUTH).Execute()
if err != nil {
t.Errorf("Error listing Azure Accounts: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
t.Fatalf("Error listing Azure Accounts: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpresp.StatusCode, 200)
var x datadog.AzureAccount
Expand All @@ -128,14 +129,14 @@ func TestUpdateAzureAccount(t *testing.T) {
assert.Equal(t, x.GetHostFilters(), *testUpdateAzureAcct.HostFilters)

// Test update host filters endpoint
_, httpResp, err = TESTAPICLIENT.AzureIntegrationApi.AzureUpdateHostFilters(TESTAUTH).AzureAccount(testAzureUpdateHostFilters).Execute()
_, httpresp, err = TESTAPICLIENT.AzureIntegrationApi.AzureUpdateHostFilters(TESTAUTH).AzureAccount(testAzureUpdateHostFilters).Execute()
if err != nil {
t.Errorf("Error updating Azure Host Filters: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
t.Fatalf("Error updating Azure Host Filters: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpresp.StatusCode, 200)
HFListOutput, httpResp, err := TESTAPICLIENT.AzureIntegrationApi.ListAzureIntegration(TESTAUTH).Execute()
HFListOutput, httpresp, err := TESTAPICLIENT.AzureIntegrationApi.ListAzureIntegration(TESTAUTH).Execute()
if err != nil {
t.Errorf("Error listing Azure Accounts: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
t.Fatalf("Error listing Azure Accounts: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
}
assert.Equal(t, httpresp.StatusCode, 200)
var y datadog.AzureAccount
Expand Down
Loading