Skip to content

Commit 7711a2e

Browse files
committed
✅ Add Provider Tests
1 parent 53ebcce commit 7711a2e

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

netbox/provider_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package netbox
22

33
import (
4+
"context"
5+
"fmt"
46
"os"
7+
"regexp"
58
"strings"
69
"testing"
710

11+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
812
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
914
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1015
)
1116

@@ -33,3 +38,48 @@ func testAccPreCheck(t *testing.T) {
3338
t.Fatal("NETBOX_API_TOKEN must be set for acceptance tests.")
3439
}
3540
}
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

Comments
 (0)