Skip to content

Commit

Permalink
added more acceptance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joaomper-TE committed Dec 12, 2023
1 parent bfc4f92 commit c20d8d6
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ data "thousandeyes_agent" "test" {
resource "thousandeyes_ftp_server" "test" {
password = "test_password"
username = "test_username"
test_name = "Acceptance Test - FTP"
test_name = "Acceptance Test - FTP Alerts Enabled"
description = "description"
request_type = "Download"
ftp_time_limit = 10
ftp_target_time = 1000
interval = 900
alerts_enabled = true
network_measurements = false
bgp_measurements = false
url = "ftp://speedtest.tele2.net/"

agents {
agent_id = data.thousandeyes_agent.test.agent_id
}

alert_rules {
rule_id = 921623 #FTP Default Alert Rule
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ data "thousandeyes_agent" "test" {
}

resource "thousandeyes_alert_rule" "alert-rule-ftp-test" {
rule_name = "API Team: (${var.environment}) Alert Slack"
alert_type = "HTTP Server"
expression = "((probDetail != \"\"))"
rule_name = "ftp server test"
alert_type = "FTP"
expression = "((ftpErrorType != \"None\"))"
rounds_violating_out_of = 1
rounds_violating_required = 1
minimum_sources_pct = 80
notify_on_clear = true
rounds_violating_out_of = 2
rounds_violating_required = 2
rounds_violating_mode = "ANY"
}

resource "thousandeyes_ftp_server" "test" {
password = "test_password"
username = "test_username"
test_name = "Acceptance Test - FTP"
test_name = "Acceptance Test - FTP Multiple Alert Rules"
description = "description"
request_type = "Download"
ftp_time_limit = 10
Expand All @@ -29,4 +27,12 @@ resource "thousandeyes_ftp_server" "test" {
agents {
agent_id = data.thousandeyes_agent.test.agent_id
}

alert_rules {
rule_id = 921623 #FTP Default Alert Rule
}

alert_rules {
rule_id = thousandeyes_alert_rule.alert-rule-ftp-test.id
}
}
26 changes: 26 additions & 0 deletions thousandeyes/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ package thousandeyes
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/thousandeyes/thousandeyes-sdk-go/v2"
"os"
"strconv"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

type ResourceType struct {
Name string
ResourceName string
GetResource func(id int64) (interface{}, error)
}

var testClient *thousandeyes.Client

var providerFactories = map[string]func() (*schema.Provider, error){
Expand Down Expand Up @@ -43,3 +51,21 @@ func testAccPreCheck(t *testing.T) {
os.Exit(1)
}
}

func testAccCheckResourceDestroy(resources []ResourceType, s *terraform.State) error {
for _, resource := range resources {
for _, rs := range s.RootModule().Resources {
if rs.Type == resource.ResourceName {
id, err := strconv.ParseInt(rs.Primary.ID, 10, 64)
if err != nil {
return err
}
_, err = testClient.GetAlertRule(id)
if err == nil {
return fmt.Errorf("%s with id %s still exists", resource.ResourceName, rs.Primary.ID)
}
}
}
}
return nil
}
61 changes: 32 additions & 29 deletions thousandeyes/resource_ftp_server_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package thousandeyes

import (
"fmt"
"os"
"strconv"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand All @@ -21,13 +19,35 @@ func TestAccThousandEyesFTPServer(t *testing.T) {
name: "basic",
resourceFile: "acceptance_resources/ftp_server/basic.tf",
resourceName: "thousandeyes_ftp_server.test",
checkDestroyFunction: testAccCheckThousandEyesFTPServerDestroy,
checkDestroyFunction: testAccCheckDefaultResourceDestroy,
},
{
name: "alerts_enabled",
resourceFile: "acceptance_resources/ftp_server/alerts_enabled.tf",
resourceName: "thousandeyes_ftp_server.test",
checkDestroyFunction: testAccCheckThousandEyesFTPServerDestroy,
checkDestroyFunction: testAccCheckDefaultResourceDestroy,
},
{
name: "alerts_enabled_multiple_alert_rules",
resourceFile: "acceptance_resources/ftp_server/alerts_enabled_multiple_alert_rules.tf",
resourceName: "thousandeyes_ftp_server.test",
checkDestroyFunction: func(state *terraform.State) error {
resourceList := []ResourceType{
{
Name: "FTP Server Test",
ResourceName: "thousandeyes_ftp_server",
GetResource: func(id int64) (interface{}, error) {
return testClient.GetFTPServer(id)
}},
{
Name: "Alert Rules",
ResourceName: "thousandeyes_alert_rule",
GetResource: func(id int64) (interface{}, error) {
return testClient.GetAlertRule(id)
}},
}
return testAccCheckResourceDestroy(resourceList, state)
},
},
}

Expand All @@ -52,33 +72,16 @@ func TestAccThousandEyesFTPServer(t *testing.T) {
}
}

func testAccCheckThousandEyesFTPServerDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type == "thousandeyes_ftp_server" {
id, err := strconv.ParseInt(rs.Primary.ID, 10, 64)
if err != nil {
return err
}
_, err = testClient.GetFTPServer(id)
if err == nil {
return fmt.Errorf("FTPServer %s still exists", rs.Primary.ID)
}
}
}
return nil
}

func testAccCheckThousandEyesFTPServerSteps(testResource, resourceName string) []resource.TestStep {
return []resource.TestStep{
func testAccCheckDefaultResourceDestroy(s *terraform.State) error {
resourceList := []ResourceType{
{
Config: testAccThousandEyesFTPServerConfig(testResource),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "password", "test_password"),
resource.TestCheckResourceAttr(resourceName, "username", "test_username"),
// Add more checks based on the resource attributes
),
},
Name: "FTP Server Test",
ResourceName: "thousandeyes_ftp_server",
GetResource: func(id int64) (interface{}, error) {
return testClient.GetFTPServer(id)
}},
}
return testAccCheckResourceDestroy(resourceList, s)
}

func testAccThousandEyesFTPServerConfig(testResource string) string {
Expand Down

0 comments on commit c20d8d6

Please sign in to comment.