-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from BESTSELLER/acctesting
Acctesting
- Loading branch information
Showing
5 changed files
with
140 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
module github.com/BESTSELLER/terraform-provider-netbox | ||
|
||
go 1.14 | ||
go 1.15 | ||
|
||
require github.com/hashicorp/terraform-plugin-sdk v1.15.0 | ||
require ( | ||
|
||
github.com/hashicorp/terraform-plugin-sdk v1.15.0 | ||
) |
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
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,60 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/BESTSELLER/terraform-provider-netbox/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
) | ||
|
||
var testAccProviders map[string]terraform.ResourceProvider | ||
var testAccProvider *schema.Provider | ||
|
||
func init() { | ||
testAccProvider = Provider().(*schema.Provider) | ||
testAccProviders = map[string]terraform.ResourceProvider{ | ||
"netbox": testAccProvider, | ||
} | ||
} | ||
|
||
func testProvider(t *testing.T) { | ||
if err := Provider().(*schema.Provider).InternalValidate(); err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
} | ||
|
||
func testAccPreCheck(t *testing.T) { | ||
|
||
if v := os.Getenv("NETBOX_ENDPOINT"); v == "" { | ||
t.Fatal("NETBOX_ENDPOINT must be set for acceptance tests") | ||
} | ||
if v := os.Getenv("NETBOX_API_TOKEN"); v == "" { | ||
t.Fatal("NETNOX_API_TOKEN must be set for acceptance tests") | ||
} | ||
|
||
} | ||
|
||
func testAccCheckResourceExists(resource string) resource.TestCheckFunc { | ||
|
||
return func(state *terraform.State) error { | ||
rs, ok := state.RootModule().Resources[resource] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", resource) | ||
} | ||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No Record ID is set") | ||
} | ||
name := rs.Primary.ID | ||
|
||
apiClient := testAccProvider.Meta().(*client.Client) | ||
_, _, err := apiClient.SendRequest("GET", name, nil, 200) | ||
if err != nil { | ||
return fmt.Errorf("error fetching item with resource %s. %s", resource, err) | ||
} | ||
return nil | ||
} | ||
} |
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,60 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/BESTSELLER/terraform-provider-netbox/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
) | ||
|
||
func TestAccRegistryBasic(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckRegistryDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckAvailablePrefix(), | ||
Check: resource.ComposeTestCheckFunc( | ||
|
||
testAccCheckResourceExists("netbox_available_prefix.main"), | ||
resource.TestCheckResourceAttr( | ||
"netbox_available_prefix.main", "description", "bw-testing-terraform"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckRegistryDestroy(s *terraform.State) error { | ||
apiClient := testAccProvider.Meta().(*client.Client) | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "netbox_available_prefix" { | ||
continue | ||
} | ||
|
||
resp, _, err := apiClient.SendRequest("GET", rs.Primary.ID, nil, 404) | ||
if err != nil { | ||
return fmt.Errorf("Resouse was not delete \n %s", resp) | ||
} | ||
|
||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccCheckAvailablePrefix() string { | ||
|
||
return fmt.Sprintf(` | ||
resource "netbox_available_prefix" "main" { | ||
parent_prefix_id = "758" | ||
prefix_length = 26 | ||
description = "acc-testing-terraform" | ||
} | ||
`) | ||
} |