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 10 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
122 changes: 122 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,122 @@
package datadog_test

import (
"fmt"
"testing"
"time"

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

var UNIQUEACCOUNTID = fmt.Sprintf("12345%s", time.Now())

var TESTAWSACCLOGS = datadog.AwsAccount{
Copy link
Contributor

Choose a reason for hiding this comment

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

we should create a factory function so each testcase creates it's unique AwsAccount, AwsAccountAndLambdaInput and AwsLogsServicesInput.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good call, I added a quick commit to add this. (talked to @Ricky-Thomas offline)

AccountId: datadog.PtrString(UNIQUEACCOUNTID),
RoleName: datadog.PtrString("DatadogAWSIntegrationRole"),
}

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

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

// 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)
defer uninstallAWSIntegration(TESTAWSACCLOGS)

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

_, httpResp, err = TESTAPICLIENT.AWSLogsIntegrationApi.EnableAWSLogServices(TESTAUTH, TESTSAVESERVICES)
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)
}

// Assert returned list has the expected length of 6
Ricky-Thomas marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(t, len(list_services_output), len(TESTSAVESERVICES.Services))
}

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

// Add Lambda to Account
add_output, httpresp, err := TESTAPICLIENT.AWSLogsIntegrationApi.AddAWSLambdaARN(TESTAUTH, TESTADDLAMBDAARN)
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, TESTSAVESERVICES)

// 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() == UNIQUEACCOUNTID {
if Account.GetLambdas()[0].GetArn() == TESTADDLAMBDAARN.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, TESTADDLAMBDAARN)
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() == UNIQUEACCOUNTID {
list_of_arns_2 = Account.GetLambdas()
}
}
for _, lambda := range list_of_arns_2 {
if lambda.GetArn() == TESTADDLAMBDAARN.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)

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

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

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

var UNIQUETENANTNAME = fmt.Sprintf("testc44-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(UNIQUETENANTNAME),
}

var TESTUPDATEAZUREACC = datadog.AzureAccount{
ClientId: datadog.PtrString("testc7f6-1234-5678-9101-3fcbf464test"),
ClientSecret: datadog.PtrString("testingx./Sw*g/Y33t..R1cH+hScMDt"),
TenantName: datadog.PtrString(UNIQUETENANTNAME),
NewClientId: datadog.PtrString("testc7f6-1234-5678-9101-3fcbf4update"),
NewTenantName: datadog.PtrString("testc44-1234-5678-9101-cc0073update"),
HostFilters: datadog.PtrString("filter:foo,test:bar"),
}

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

func TestAzureCreate(t *testing.T) {
// Setup the Client we'll use to interact with the Test account
teardownTest := setupTest(t)
defer teardownTest(t)
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)
defer uninstallAzureIntegration(TESTAZUREACCT)
defer uninstallAzureIntegration(TESTUPDATEAZUREHOSTFILTERS)

// 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)
Ricky-Thomas marked this conversation as resolved.
Show resolved Hide resolved
}
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)
defer uninstallAzureIntegration(TESTAZUREACCT)

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

_, httpResp, err := TESTAPICLIENT.AzureIntegrationApi.UpdateAzureIntegration(TESTAUTH, TESTUPDATEAZUREACC)
Ricky-Thomas marked this conversation as resolved.
Show resolved Hide resolved
defer uninstallAzureIntegration(TESTUPDATEAZUREACC)
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() == *TESTUPDATEAZUREACC.NewClientId {
x = Account
}
}
assert.Equal(t, x.GetClientId(), *TESTUPDATEAZUREACC.NewClientId)
assert.Equal(t, x.GetTenantName(), *TESTUPDATEAZUREACC.NewTenantName)
assert.Equal(t, x.GetHostFilters(), *TESTUPDATEAZUREACC.HostFilters)

// Test update host filters endpoint
_, httpResp, err = TESTAPICLIENT.AzureIntegrationApi.AzureUpdateHostFilters(TESTAUTH, TESTUPDATEAZUREHOSTFILTERS)
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() == *TESTUPDATEAZUREHOSTFILTERS.ClientId {
y = Account
}
}
assert.Equal(t, y.GetHostFilters(), *TESTUPDATEAZUREHOSTFILTERS.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)
}
}
118 changes: 118 additions & 0 deletions api/v1/datadog/api_gcp_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package datadog_test

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

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

var UNIQUEPROJECTID = fmt.Sprintf("datadog-%s", time.Now())

var TESTGCPACCT = datadog.GcpAccount{
Type: datadog.PtrString("service_account"),
ProjectId: datadog.PtrString(UNIQUEPROJECTID),
PrivateKeyId: datadog.PtrString("fake_private_key_id"),
PrivateKey: datadog.PtrString("fake_key"),
ClientEmail: datadog.PtrString("[email protected]"),
ClientId: datadog.PtrString("123456712345671234567"),
AuthUri: datadog.PtrString("fake_uri"),
TokenUri: datadog.PtrString("fake_uri"),
AuthProviderX509CertUrl: datadog.PtrString("fake_url"),
ClientX509CertUrl: datadog.PtrString("fake_url"),
HostFilters: datadog.PtrString("fake:tag,example:test"),
Automute: datadog.PtrBool(false),
}

var TESTUPDATEGCPACCT = datadog.GcpAccount{
ProjectId: datadog.PtrString(UNIQUEPROJECTID),
ClientEmail: datadog.PtrString("[email protected]"),
HostFilters: datadog.PtrString("fake:update,example:update"),
Automute: datadog.PtrBool(true),
}

func TestGcpCreate(t *testing.T) {
teardownTest := setupTest(t)
defer teardownTest(t)
defer uninstallGcpIntegration(TESTGCPACCT)

_, httpResp, err := TESTAPICLIENT.GCPIntegrationApi.CreateGCPIntegration(TESTAUTH, TESTGCPACCT)
if err != nil {
t.Errorf("Error creating GCP intg: %v", err)
}
assert.Equal(t, httpResp.StatusCode, 200)
}

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

// Setup Gcp Account to List
TESTAPICLIENT.GCPIntegrationApi.CreateGCPIntegration(TESTAUTH, TESTGCPACCT)

gcpListOutput, _, err := TESTAPICLIENT.GCPIntegrationApi.ListGCPIntegration(TESTAUTH)
if err != nil {
t.Errorf("Error listing GCP intg: %v", err)
}
var x datadog.GcpAccount
for _, Account := range gcpListOutput {
if Account.GetProjectId() == UNIQUEPROJECTID {
x = Account
}
}
assert.Equal(t, x.GetClientEmail(), *TESTGCPACCT.ClientEmail)
assert.Equal(t, x.GetHostFilters(), *TESTGCPACCT.HostFilters)

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

// Test account deletion as well
_, httpResp, err := TESTAPICLIENT.GCPIntegrationApi.DeleteGCPIntegration(TESTAUTH, TESTGCPACCT)
if httpResp.StatusCode != 200 || err != nil {
t.Errorf("Error uninstalling GCP Account: %v, Another test may have already removed this account.", TESTAZUREACCT)
Ricky-Thomas marked this conversation as resolved.
Show resolved Hide resolved
}
assert.Equal(t, httpResp.StatusCode, 200)
}

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

// Setup Gcp Account to Update
TESTAPICLIENT.GCPIntegrationApi.CreateGCPIntegration(TESTAUTH, TESTGCPACCT)

_, httpResp, err := TESTAPICLIENT.GCPIntegrationApi.UpdateGCPIntegration(TESTAUTH, TESTUPDATEGCPACCT)
Ricky-Thomas marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
t.Errorf("Error Updating GCP Account: %v", err)
}

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

// List account to ensure update worked.
gcpListOutput, _, err := TESTAPICLIENT.GCPIntegrationApi.ListGCPIntegration(TESTAUTH)
if err != nil {
t.Errorf("Error listing GCP intg: %v", err)
}
var x datadog.GcpAccount
for _, Account := range gcpListOutput {
if Account.GetClientEmail() == *TESTGCPACCT.ClientEmail {
x = Account
}
}
assert.Equal(t, x.GetAutomute(), true)
assert.Equal(t, x.GetHostFilters(), *TESTUPDATEGCPACCT.HostFilters)
}

func uninstallGcpIntegration(account datadog.GcpAccount) {
_, httpresp, err := TESTAPICLIENT.GCPIntegrationApi.DeleteGCPIntegration(TESTAUTH, account)
if httpresp.StatusCode != 200 || err != nil {
log.Printf("Error uninstalling GCP Account: %v, Another test may have already removed this account.", account)
}
}