|
1 | 1 | package netbox
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
| 5 | + "fmt" |
4 | 6 | "os"
|
| 7 | + "regexp" |
5 | 8 | "strings"
|
6 | 9 | "testing"
|
7 | 10 |
|
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
8 | 12 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
|
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
9 | 14 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
10 | 15 | )
|
11 | 16 |
|
@@ -33,3 +38,48 @@ func testAccPreCheck(t *testing.T) {
|
33 | 38 | t.Fatal("NETBOX_API_TOKEN must be set for acceptance tests.")
|
34 | 39 | }
|
35 | 40 | }
|
| 41 | + |
| 42 | +func testProviderConfig(plattform string) string { |
| 43 | + return fmt.Sprintf(` |
| 44 | + resource "netbox_platform" "testplatform" { |
| 45 | + name = "%s" |
| 46 | + }`, plattform) |
| 47 | +} |
| 48 | + |
| 49 | +func providerInvalidConfigure() schema.ConfigureContextFunc { |
| 50 | + return func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) { |
| 51 | + var diags diag.Diagnostics |
| 52 | + |
| 53 | + config := &Config{} |
| 54 | + config.ServerURL = "https://fake.netbox.server" |
| 55 | + config.APIToken = "1234567890" |
| 56 | + |
| 57 | + netboxClient, clientError := config.Client() |
| 58 | + if clientError != nil { |
| 59 | + return nil, diag.FromErr(clientError) |
| 60 | + } |
| 61 | + |
| 62 | + return netboxClient, diags |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestAccNetboxProviderConfigure_failure(t *testing.T) { |
| 67 | + |
| 68 | + var testAccInvalidProviders map[string]*schema.Provider |
| 69 | + |
| 70 | + testAccInvalidProvider := Provider() |
| 71 | + testAccInvalidProvider.ConfigureContextFunc = providerInvalidConfigure() |
| 72 | + testAccInvalidProviders = map[string]*schema.Provider{ |
| 73 | + "netbox": testAccInvalidProvider, |
| 74 | + } |
| 75 | + |
| 76 | + resource.Test(t, resource.TestCase{ |
| 77 | + Providers: testAccInvalidProviders, |
| 78 | + Steps: []resource.TestStep{ |
| 79 | + { |
| 80 | + Config: testProviderConfig(acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)), |
| 81 | + ExpectError: regexp.MustCompile("Post \"https://fake.netbox.server/api/dcim/platforms/\": dial tcp: lookup fake.netbox.server: no such host"), |
| 82 | + }, |
| 83 | + }, |
| 84 | + }) |
| 85 | +} |
0 commit comments