Skip to content

Commit

Permalink
re-factor to add 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 4072584 commit bfc4f92
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 34 deletions.
21 changes: 21 additions & 0 deletions thousandeyes/acceptance_resources/ftp_server/alerts_enabled.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
data "thousandeyes_agent" "test" {
agent_name = "Vancouver, Canada"
}

resource "thousandeyes_ftp_server" "test" {
password = "test_password"
username = "test_username"
test_name = "Acceptance Test - FTP"
description = "description"
request_type = "Download"
ftp_time_limit = 10
ftp_target_time = 1000
interval = 900
alerts_enabled = true
network_measurements = false
url = "ftp://speedtest.tele2.net/"

agents {
agent_id = data.thousandeyes_agent.test.agent_id
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
data "thousandeyes_agent" "test" {
agent_name = "Vancouver, Canada"
}

resource "thousandeyes_alert_rule" "alert-rule-ftp-test" {
rule_name = "API Team: (${var.environment}) Alert Slack"
alert_type = "HTTP Server"
expression = "((probDetail != \"\"))"
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"
description = "description"
request_type = "Download"
ftp_time_limit = 10
ftp_target_time = 1000
interval = 900
alerts_enabled = false
network_measurements = false
url = "ftp://speedtest.tele2.net/"

agents {
agent_id = data.thousandeyes_agent.test.agent_id
}
}
21 changes: 21 additions & 0 deletions thousandeyes/acceptance_resources/ftp_server/basic.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
data "thousandeyes_agent" "test" {
agent_name = "Vancouver, Canada"
}

resource "thousandeyes_ftp_server" "test" {
password = "test_password"
username = "test_username"
test_name = "Acceptance Test - FTP"
description = "description"
request_type = "Download"
ftp_time_limit = 10
ftp_target_time = 1000
interval = 900
alerts_enabled = false
network_measurements = false
url = "ftp://speedtest.tele2.net/"

agents {
agent_id = data.thousandeyes_agent.test.agent_id
}
}
81 changes: 47 additions & 34 deletions thousandeyes/resource_ftp_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,54 @@ package thousandeyes

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

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

func TestAccThousandEyesFTPServer_basic(t *testing.T) {
testName := "tf-acc-test-ftp-server"
resourceName := "thousandeyes_ftp_server.test"
func TestAccThousandEyesFTPServer(t *testing.T) {
testCases := []struct {
name string
resourceFile string
resourceName string
checkDestroyFunction func(*terraform.State) error
}{
{
name: "basic",
resourceFile: "acceptance_resources/ftp_server/basic.tf",
resourceName: "thousandeyes_ftp_server.test",
checkDestroyFunction: testAccCheckThousandEyesFTPServerDestroy,
},
{
name: "alerts_enabled",
resourceFile: "acceptance_resources/ftp_server/alerts_enabled.tf",
resourceName: "thousandeyes_ftp_server.test",
checkDestroyFunction: testAccCheckThousandEyesFTPServerDestroy,
},
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckThousandEyesFTPServerDestroy,
Steps: testAccCheckThousandEyesFTPServerSteps(testName, resourceName),
})
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: tc.checkDestroyFunction,
Steps: []resource.TestStep{
{
Config: testAccThousandEyesFTPServerConfig(tc.resourceFile),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(tc.resourceName, "password", "test_password"),
resource.TestCheckResourceAttr(tc.resourceName, "username", "test_username"),
// Add more checks based on the resource attributes
),
},
},
})
})
}
}

func testAccCheckThousandEyesFTPServerDestroy(s *terraform.State) error {
Expand All @@ -37,10 +68,10 @@ func testAccCheckThousandEyesFTPServerDestroy(s *terraform.State) error {
return nil
}

func testAccCheckThousandEyesFTPServerSteps(testName, resourceName string) []resource.TestStep {
func testAccCheckThousandEyesFTPServerSteps(testResource, resourceName string) []resource.TestStep {
return []resource.TestStep{
{
Config: testAccThousandEyesFTPServerConfig(testName),
Config: testAccThousandEyesFTPServerConfig(testResource),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "password", "test_password"),
resource.TestCheckResourceAttr(resourceName, "username", "test_username"),
Expand All @@ -50,28 +81,10 @@ func testAccCheckThousandEyesFTPServerSteps(testName, resourceName string) []res
}
}

func testAccThousandEyesFTPServerConfig(testName string) string {
return fmt.Sprintf(`
data "thousandeyes_agent" "test"{
agent_name = "Vancouver, Canada"
}
resource "thousandeyes_ftp_server" "test" {
password = "test_password"
username = "test_username"
test_name = "Acceptance Test - FTP"
description = "description"
request_type = "Download"
ftp_time_limit = 10
ftp_target_time = 1000
interval = 900
alerts_enabled = false
network_measurements = false
url = "ftp://speedtest.tele2.net/"
agents {
agent_id = data.thousandeyes_agent.test.agent_id
}
func testAccThousandEyesFTPServerConfig(testResource string) string {
content, err := os.ReadFile(testResource)
if err != nil {
panic(err)
}
`)
return string(content)
}

0 comments on commit bfc4f92

Please sign in to comment.