-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add acceptance test for polling source (#72)
* add acceptance test for polling source * add individual acceptance tests for all polling sources * add function to get randomized params and make code dry
- Loading branch information
1 parent
1a3682e
commit 254de94
Showing
8 changed files
with
1,304 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package sumologic | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
) | ||
|
||
func TestAccSumologicCloudFrontSource_create(t *testing.T) { | ||
var cloudFrontSource PollingSource | ||
var collector Collector | ||
cName, cDescription, cCategory := getRandomizedParams() | ||
sName, sDescription, sCategory := getRandomizedParams() | ||
cloudFrontResourceName := "sumologic_cloudfront_source.cloudfront" | ||
testAwsID := os.Getenv("SUMOLOGIC_TEST_AWS_ID") | ||
testAwsKey := os.Getenv("SUMOLOGIC_TEST_AWS_KEY") | ||
testAwsBucket := os.Getenv("SUMOLOGIC_TEST_BUCKET_NAME") | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckCloudFrontSourceDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccSumologicCloudFrontSourceConfig(cName, cDescription, cCategory, sName, sDescription, sCategory, testAwsID, testAwsKey, testAwsBucket), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCloudFrontSourceExists(cloudFrontResourceName, &cloudFrontSource), | ||
testAccCheckCloudFrontSourceValues(&cloudFrontSource, sName, sDescription, sCategory), | ||
testAccCheckCollectorExists("sumologic_collector.test", &collector), | ||
testAccCheckCollectorValues(&collector, cName, cDescription, cCategory, "Etc/UTC", ""), | ||
resource.TestCheckResourceAttrSet(cloudFrontResourceName, "id"), | ||
resource.TestCheckResourceAttr(cloudFrontResourceName, "name", sName), | ||
resource.TestCheckResourceAttr(cloudFrontResourceName, "description", sDescription), | ||
resource.TestCheckResourceAttr(cloudFrontResourceName, "category", sCategory), | ||
resource.TestCheckResourceAttr(cloudFrontResourceName, "content_type", "AwsCloudFrontBucket"), | ||
resource.TestCheckResourceAttr(cloudFrontResourceName, "path.0.type", "S3BucketPathExpression"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
func TestAccSumologicCloudFrontSource_update(t *testing.T) { | ||
var cloudFrontSource PollingSource | ||
cName, cDescription, cCategory := getRandomizedParams() | ||
sName, sDescription, sCategory := getRandomizedParams() | ||
sNameUpdated, sDescriptionUpdated, sCategoryUpdated := getRandomizedParams() | ||
cloudFrontResourceName := "sumologic_cloudfront_source.cloudfront" | ||
testAwsID := os.Getenv("SUMOLOGIC_TEST_AWS_ID") | ||
testAwsKey := os.Getenv("SUMOLOGIC_TEST_AWS_KEY") | ||
testAwsBucket := os.Getenv("SUMOLOGIC_TEST_BUCKET_NAME") | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckHTTPSourceDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccSumologicCloudFrontSourceConfig(cName, cDescription, cCategory, sName, sDescription, sCategory, testAwsID, testAwsKey, testAwsBucket), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCloudFrontSourceExists(cloudFrontResourceName, &cloudFrontSource), | ||
testAccCheckCloudFrontSourceValues(&cloudFrontSource, sName, sDescription, sCategory), | ||
resource.TestCheckResourceAttrSet(cloudFrontResourceName, "id"), | ||
resource.TestCheckResourceAttr(cloudFrontResourceName, "name", sName), | ||
resource.TestCheckResourceAttr(cloudFrontResourceName, "description", sDescription), | ||
resource.TestCheckResourceAttr(cloudFrontResourceName, "category", sCategory), | ||
resource.TestCheckResourceAttr(cloudFrontResourceName, "content_type", "AwsCloudFrontBucket"), | ||
resource.TestCheckResourceAttr(cloudFrontResourceName, "path.0.type", "S3BucketPathExpression"), | ||
), | ||
}, | ||
{ | ||
Config: testAccSumologicCloudFrontSourceConfig(cName, cDescription, cCategory, sNameUpdated, sDescriptionUpdated, sCategoryUpdated, testAwsID, testAwsKey, testAwsBucket), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCloudFrontSourceExists(cloudFrontResourceName, &cloudFrontSource), | ||
testAccCheckCloudFrontSourceValues(&cloudFrontSource, sNameUpdated, sDescriptionUpdated, sCategoryUpdated), | ||
resource.TestCheckResourceAttrSet(cloudFrontResourceName, "id"), | ||
resource.TestCheckResourceAttr(cloudFrontResourceName, "name", sNameUpdated), | ||
resource.TestCheckResourceAttr(cloudFrontResourceName, "description", sDescriptionUpdated), | ||
resource.TestCheckResourceAttr(cloudFrontResourceName, "category", sCategoryUpdated), | ||
resource.TestCheckResourceAttr(cloudFrontResourceName, "content_type", "AwsCloudFrontBucket"), | ||
resource.TestCheckResourceAttr(cloudFrontResourceName, "path.0.type", "S3BucketPathExpression"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
func testAccCheckCloudFrontSourceDestroy(s *terraform.State) error { | ||
client := testAccProvider.Meta().(*Client) | ||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "sumologic_s3_source" && rs.Type != "sumologic_cloudwatch_source" { | ||
continue | ||
} | ||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("HTTP Source destruction check: HTTP Source ID is not set") | ||
} | ||
id, err := strconv.Atoi(rs.Primary.ID) | ||
if err != nil { | ||
return fmt.Errorf("Encountered an error: " + err.Error()) | ||
} | ||
collectorID, err := strconv.Atoi(rs.Primary.Attributes["collector_id"]) | ||
if err != nil { | ||
return fmt.Errorf("Encountered an error: " + err.Error()) | ||
} | ||
s, err := client.GetPollingSource(collectorID, id) | ||
if err != nil { | ||
return fmt.Errorf("Encountered an error: " + err.Error()) | ||
} | ||
if s != nil { | ||
return fmt.Errorf("Polling Source still exists") | ||
} | ||
} | ||
return nil | ||
} | ||
func testAccCheckCloudFrontSourceExists(n string, pollingSource *PollingSource) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("not found: %s", n) | ||
} | ||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("Polling Source ID is not set") | ||
} | ||
id, err := strconv.Atoi(rs.Primary.ID) | ||
if err != nil { | ||
return fmt.Errorf("Polling Source id should be int; got %s", rs.Primary.ID) | ||
} | ||
collectorID, err := strconv.Atoi(rs.Primary.Attributes["collector_id"]) | ||
if err != nil { | ||
return fmt.Errorf("Encountered an error: " + err.Error()) | ||
} | ||
c := testAccProvider.Meta().(*Client) | ||
pollingSourceResp, err := c.GetPollingSource(collectorID, id) | ||
if err != nil { | ||
return err | ||
} | ||
*pollingSource = *pollingSourceResp | ||
return nil | ||
} | ||
} | ||
func testAccCheckCloudFrontSourceValues(pollingSource *PollingSource, name, description, category string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
if pollingSource.Name != name { | ||
return fmt.Errorf("bad name, expected \"%s\", got: %#v", name, pollingSource.Name) | ||
} | ||
if pollingSource.Description != description { | ||
return fmt.Errorf("bad description, expected \"%s\", got: %#v", description, pollingSource.Description) | ||
} | ||
if pollingSource.Category != category { | ||
return fmt.Errorf("bad category, expected \"%s\", got: %#v", category, pollingSource.Category) | ||
} | ||
return nil | ||
} | ||
} | ||
func testAccSumologicCloudFrontSourceConfig(cName, cDescription, cCategory, sName, sDescription, sCategory, testAwsID, testAwsKey, testAwsBucket string) string { | ||
return fmt.Sprintf(` | ||
resource "sumologic_collector" "test" { | ||
name = "%s" | ||
description = "%s" | ||
category = "%s" | ||
} | ||
resource "sumologic_cloudfront_source" "cloudfront" { | ||
name = "%s" | ||
description = "%s" | ||
category = "%s" | ||
content_type = "AwsCloudFrontBucket" | ||
scan_interval = 300000 | ||
paused = false | ||
collector_id = "${sumologic_collector.test.id}" | ||
authentication { | ||
type = "S3BucketAuthentication" | ||
access_key = "%s" | ||
secret_key = "%s" | ||
} | ||
path { | ||
type = "S3BucketPathExpression" | ||
bucket_name = "%s" | ||
path_expression = "*" | ||
} | ||
} | ||
`, cName, cDescription, cCategory, sName, sDescription, sCategory, testAwsID, testAwsKey, testAwsBucket) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package sumologic | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
) | ||
|
||
func TestAccSumologicCloudTrailSource_create(t *testing.T) { | ||
var cloudTrailSource PollingSource | ||
var collector Collector | ||
cName, cDescription, cCategory := getRandomizedParams() | ||
sName, sDescription, sCategory := getRandomizedParams() | ||
cloudTrailResourceName := "sumologic_cloudtrail_source.cloudtrail" | ||
testAwsID := os.Getenv("SUMOLOGIC_TEST_AWS_ID") | ||
testAwsKey := os.Getenv("SUMOLOGIC_TEST_AWS_KEY") | ||
testAwsBucket := os.Getenv("SUMOLOGIC_TEST_BUCKET_NAME") | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckCloudTrailSourceDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccSumologicCloudTrailSourceConfig(cName, cDescription, cCategory, sName, sDescription, sCategory, testAwsID, testAwsKey, testAwsBucket), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCloudTrailSourceExists(cloudTrailResourceName, &cloudTrailSource), | ||
testAccCheckCloudTrailSourceValues(&cloudTrailSource, sName, sDescription, sCategory), | ||
testAccCheckCollectorExists("sumologic_collector.test", &collector), | ||
testAccCheckCollectorValues(&collector, cName, cDescription, cCategory, "Etc/UTC", ""), | ||
resource.TestCheckResourceAttrSet(cloudTrailResourceName, "id"), | ||
resource.TestCheckResourceAttr(cloudTrailResourceName, "name", sName), | ||
resource.TestCheckResourceAttr(cloudTrailResourceName, "description", sDescription), | ||
resource.TestCheckResourceAttr(cloudTrailResourceName, "category", sCategory), | ||
resource.TestCheckResourceAttr(cloudTrailResourceName, "content_type", "AwsCloudTrailBucket"), | ||
resource.TestCheckResourceAttr(cloudTrailResourceName, "path.0.type", "S3BucketPathExpression"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
func TestAccSumologicCloudTrailSource_update(t *testing.T) { | ||
var cloudTrailSource PollingSource | ||
cName, cDescription, cCategory := getRandomizedParams() | ||
sName, sDescription, sCategory := getRandomizedParams() | ||
sNameUpdated, sDescriptionUpdated, sCategoryUpdated := getRandomizedParams() | ||
cloudTrailResourceName := "sumologic_cloudtrail_source.cloudtrail" | ||
testAwsID := os.Getenv("SUMOLOGIC_TEST_AWS_ID") | ||
testAwsKey := os.Getenv("SUMOLOGIC_TEST_AWS_KEY") | ||
testAwsBucket := os.Getenv("SUMOLOGIC_TEST_BUCKET_NAME") | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckHTTPSourceDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccSumologicCloudTrailSourceConfig(cName, cDescription, cCategory, sName, sDescription, sCategory, testAwsID, testAwsKey, testAwsBucket), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCloudTrailSourceExists(cloudTrailResourceName, &cloudTrailSource), | ||
testAccCheckCloudTrailSourceValues(&cloudTrailSource, sName, sDescription, sCategory), | ||
resource.TestCheckResourceAttrSet(cloudTrailResourceName, "id"), | ||
resource.TestCheckResourceAttr(cloudTrailResourceName, "name", sName), | ||
resource.TestCheckResourceAttr(cloudTrailResourceName, "description", sDescription), | ||
resource.TestCheckResourceAttr(cloudTrailResourceName, "category", sCategory), | ||
resource.TestCheckResourceAttr(cloudTrailResourceName, "content_type", "AwsCloudTrailBucket"), | ||
resource.TestCheckResourceAttr(cloudTrailResourceName, "path.0.type", "S3BucketPathExpression"), | ||
), | ||
}, | ||
{ | ||
Config: testAccSumologicCloudTrailSourceConfig(cName, cDescription, cCategory, sNameUpdated, sDescriptionUpdated, sCategoryUpdated, testAwsID, testAwsKey, testAwsBucket), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCloudTrailSourceExists(cloudTrailResourceName, &cloudTrailSource), | ||
testAccCheckCloudTrailSourceValues(&cloudTrailSource, sNameUpdated, sDescriptionUpdated, sCategoryUpdated), | ||
resource.TestCheckResourceAttrSet(cloudTrailResourceName, "id"), | ||
resource.TestCheckResourceAttr(cloudTrailResourceName, "name", sNameUpdated), | ||
resource.TestCheckResourceAttr(cloudTrailResourceName, "description", sDescriptionUpdated), | ||
resource.TestCheckResourceAttr(cloudTrailResourceName, "category", sCategoryUpdated), | ||
resource.TestCheckResourceAttr(cloudTrailResourceName, "content_type", "AwsCloudTrailBucket"), | ||
resource.TestCheckResourceAttr(cloudTrailResourceName, "path.0.type", "S3BucketPathExpression"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
func testAccCheckCloudTrailSourceDestroy(s *terraform.State) error { | ||
client := testAccProvider.Meta().(*Client) | ||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "sumologic_s3_source" && rs.Type != "sumologic_cloudwatch_source" { | ||
continue | ||
} | ||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("HTTP Source destruction check: HTTP Source ID is not set") | ||
} | ||
id, err := strconv.Atoi(rs.Primary.ID) | ||
if err != nil { | ||
return fmt.Errorf("Encountered an error: " + err.Error()) | ||
} | ||
collectorID, err := strconv.Atoi(rs.Primary.Attributes["collector_id"]) | ||
if err != nil { | ||
return fmt.Errorf("Encountered an error: " + err.Error()) | ||
} | ||
s, err := client.GetPollingSource(collectorID, id) | ||
if err != nil { | ||
return fmt.Errorf("Encountered an error: " + err.Error()) | ||
} | ||
if s != nil { | ||
return fmt.Errorf("Polling Source still exists") | ||
} | ||
} | ||
return nil | ||
} | ||
func testAccCheckCloudTrailSourceExists(n string, pollingSource *PollingSource) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("not found: %s", n) | ||
} | ||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("Polling Source ID is not set") | ||
} | ||
id, err := strconv.Atoi(rs.Primary.ID) | ||
if err != nil { | ||
return fmt.Errorf("Polling Source id should be int; got %s", rs.Primary.ID) | ||
} | ||
collectorID, err := strconv.Atoi(rs.Primary.Attributes["collector_id"]) | ||
if err != nil { | ||
return fmt.Errorf("Encountered an error: " + err.Error()) | ||
} | ||
c := testAccProvider.Meta().(*Client) | ||
pollingSourceResp, err := c.GetPollingSource(collectorID, id) | ||
if err != nil { | ||
return err | ||
} | ||
*pollingSource = *pollingSourceResp | ||
return nil | ||
} | ||
} | ||
func testAccCheckCloudTrailSourceValues(pollingSource *PollingSource, name, description, category string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
if pollingSource.Name != name { | ||
return fmt.Errorf("bad name, expected \"%s\", got: %#v", name, pollingSource.Name) | ||
} | ||
if pollingSource.Description != description { | ||
return fmt.Errorf("bad description, expected \"%s\", got: %#v", description, pollingSource.Description) | ||
} | ||
if pollingSource.Category != category { | ||
return fmt.Errorf("bad category, expected \"%s\", got: %#v", category, pollingSource.Category) | ||
} | ||
return nil | ||
} | ||
} | ||
func testAccSumologicCloudTrailSourceConfig(cName, cDescription, cCategory, sName, sDescription, sCategory, testAwsID, testAwsKey, testAwsBucket string) string { | ||
return fmt.Sprintf(` | ||
resource "sumologic_collector" "test" { | ||
name = "%s" | ||
description = "%s" | ||
category = "%s" | ||
} | ||
resource "sumologic_cloudtrail_source" "cloudtrail" { | ||
name = "%s" | ||
description = "%s" | ||
category = "%s" | ||
content_type = "AwsCloudTrailBucket" | ||
scan_interval = 300000 | ||
paused = false | ||
collector_id = "${sumologic_collector.test.id}" | ||
authentication { | ||
type = "S3BucketAuthentication" | ||
access_key = "%s" | ||
secret_key = "%s" | ||
} | ||
path { | ||
type = "S3BucketPathExpression" | ||
bucket_name = "%s" | ||
path_expression = "*" | ||
} | ||
} | ||
`, cName, cDescription, cCategory, sName, sDescription, sCategory, testAwsID, testAwsKey, testAwsBucket) | ||
} |
Oops, something went wrong.