opentofu/builtin/providers/triton/resource_firewall_rule_test.go
James Nugent a0568e544f provider/triton: Move to joyent/triton-go (#13225)
* provider/triton: Move to joyent/triton-go

This commit moves the Triton provider to the new joyent/triton-go
library from gosdc. This has a number of advantages - not least that
requests can be signed using an SSH agent without having to keep
unencrypted key material in memory.

Schema has been maintained for all resources, and several tests have
been added and acceptance tests repaired - in some cases by fixing bugs
in the underlying resources.

After applying this patch, all acceptance tests pass:

```
go generate $(go list ./... | grep -v /terraform/vendor/)
2017/03/30 13:48:33 Generated command/internal_plugin_list.go
TF_ACC=1 go test ./builtin/providers/triton -v  -timeout 120m
=== RUN   TestProvider
--- PASS: TestProvider (0.00s)
=== RUN   TestProvider_impl
--- PASS: TestProvider_impl (0.00s)
=== RUN   TestAccTritonFabric_basic
--- PASS: TestAccTritonFabric_basic (15.11s)
=== RUN   TestAccTritonFirewallRule_basic
--- PASS: TestAccTritonFirewallRule_basic (1.48s)
=== RUN   TestAccTritonFirewallRule_update
--- PASS: TestAccTritonFirewallRule_update (1.55s)
=== RUN   TestAccTritonFirewallRule_enable
--- PASS: TestAccTritonFirewallRule_enable (1.52s)
=== RUN   TestAccTritonKey_basic
--- PASS: TestAccTritonKey_basic (11.76s)
=== RUN   TestAccTritonKey_noKeyName
--- PASS: TestAccTritonKey_noKeyName (11.20s)
=== RUN   TestAccTritonMachine_basic
--- PASS: TestAccTritonMachine_basic (82.19s)
=== RUN   TestAccTritonMachine_dns
--- PASS: TestAccTritonMachine_dns (173.36s)
=== RUN   TestAccTritonMachine_nic
--- PASS: TestAccTritonMachine_nic (167.82s)
=== RUN   TestAccTritonMachine_addNIC
--- PASS: TestAccTritonMachine_addNIC (192.11s)
=== RUN   TestAccTritonMachine_firewall
--- PASS: TestAccTritonMachine_firewall (188.53s)
=== RUN   TestAccTritonMachine_metadata
--- PASS: TestAccTritonMachine_metadata (614.57s)
=== RUN   TestAccTritonVLAN_basic
--- PASS: TestAccTritonVLAN_basic (0.93s)
=== RUN   TestAccTritonVLAN_update
--- PASS: TestAccTritonVLAN_update (1.50s)
PASS
ok  	github.com/hashicorp/terraform/builtin/providers/triton	1463.621s
```

* provider/triton: Update docs for provider config

* deps: Vendor github.com/joyent/triton-go/...

* deps: Remove github.com/joyent/gosdc
2017-03-31 01:25:27 +03:00

161 lines
4.4 KiB
Go

package triton
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/joyent/triton-go"
)
func TestAccTritonFirewallRule_basic(t *testing.T) {
config := testAccTritonFirewallRule_basic
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckTritonFirewallRuleDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckTritonFirewallRuleExists("triton_firewall_rule.test"),
),
},
},
})
}
func TestAccTritonFirewallRule_update(t *testing.T) {
preConfig := testAccTritonFirewallRule_basic
postConfig := testAccTritonFirewallRule_update
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckTritonFirewallRuleDestroy,
Steps: []resource.TestStep{
{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckTritonFirewallRuleExists("triton_firewall_rule.test"),
resource.TestCheckResourceAttr("triton_firewall_rule.test", "rule", "FROM any TO tag \"www\" ALLOW tcp PORT 80"),
resource.TestCheckResourceAttr("triton_firewall_rule.test", "enabled", "false"),
),
},
{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckTritonFirewallRuleExists("triton_firewall_rule.test"),
resource.TestCheckResourceAttr("triton_firewall_rule.test", "rule", "FROM any TO tag \"www\" BLOCK tcp PORT 80"),
resource.TestCheckResourceAttr("triton_firewall_rule.test", "enabled", "true"),
),
},
},
})
}
func TestAccTritonFirewallRule_enable(t *testing.T) {
preConfig := testAccTritonFirewallRule_basic
postConfig := testAccTritonFirewallRule_enable
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckTritonFirewallRuleDestroy,
Steps: []resource.TestStep{
{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckTritonFirewallRuleExists("triton_firewall_rule.test"),
resource.TestCheckResourceAttr("triton_firewall_rule.test", "rule", "FROM any TO tag \"www\" ALLOW tcp PORT 80"),
resource.TestCheckResourceAttr("triton_firewall_rule.test", "enabled", "false"),
),
},
{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckTritonFirewallRuleExists("triton_firewall_rule.test"),
resource.TestCheckResourceAttr("triton_firewall_rule.test", "rule", "FROM any TO tag \"www\" ALLOW tcp PORT 80"),
resource.TestCheckResourceAttr("triton_firewall_rule.test", "enabled", "true"),
),
},
},
})
}
func testCheckTritonFirewallRuleExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
conn := testAccProvider.Meta().(*triton.Client)
resp, err := conn.Firewall().GetFirewallRule(&triton.GetFirewallRuleInput{
ID: rs.Primary.ID,
})
if err != nil && triton.IsResourceNotFound(err) {
return fmt.Errorf("Bad: Check Firewall Rule Exists: %s", err)
} else if err != nil {
return err
}
if resp == nil {
return fmt.Errorf("Bad: Firewall Rule %q does not exist", rs.Primary.ID)
}
return nil
}
}
func testCheckTritonFirewallRuleDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*triton.Client)
for _, rs := range s.RootModule().Resources {
if rs.Type != "triton_firewall_rule" {
continue
}
resp, err := conn.Firewall().GetFirewallRule(&triton.GetFirewallRuleInput{
ID: rs.Primary.ID,
})
if triton.IsResourceNotFound(err) {
return nil
} else if err != nil {
return err
}
if resp != nil {
return fmt.Errorf("Bad: Firewall Rule %q still exists", rs.Primary.ID)
}
}
return nil
}
var testAccTritonFirewallRule_basic = `
resource "triton_firewall_rule" "test" {
rule = "FROM any TO tag \"www\" ALLOW tcp PORT 80"
enabled = false
}
`
var testAccTritonFirewallRule_update = `
resource "triton_firewall_rule" "test" {
rule = "FROM any TO tag \"www\" BLOCK tcp PORT 80"
enabled = true
}
`
var testAccTritonFirewallRule_enable = `
resource "triton_firewall_rule" "test" {
rule = "FROM any TO tag \"www\" ALLOW tcp PORT 80"
enabled = true
}
`