Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
add tests for providerConfigure function
  • Loading branch information
rrossetti-splunk committed Jun 27, 2025
commit 033fea0854f0a5fc348153c5b25e212968c3ff7f
2 changes: 1 addition & 1 deletion splunk/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
return provider, nil
}

// hasScheme checks if a string has an "://"
// hasScheme checks if a URL has scheme and host
func hasScheme(providerUrl string) bool {
parsed, err := url.Parse(providerUrl)
return err == nil && parsed.Scheme != "" && parsed.Host != ""
Expand Down
54 changes: 54 additions & 0 deletions splunk/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,60 @@ func TestProvider(t *testing.T) {
}
}

func getTestProviderURL(inputData map[string]interface{}, t *testing.T) url.URL {
providerSchema := providerSchema()
// Create a mock *schema.ResourceData
resourceData := schema.TestResourceDataRaw(t, providerSchema, inputData)
// Call the providerConfigure method
providerInterface, err := providerConfigure(resourceData)
if err != nil {
t.Error(err)
}
provider, ok := providerInterface.(*SplunkProvider) // type assertion
if !ok {
t.Error(ok)
}

url := provider.Client.BuildSplunkURL(nil)
return url
}

func TestProviderConfigure(t *testing.T) {
// Define the input data for the ResourceData
inputData := map[string]interface{}{
"url": "localhost",
"timeout": 60,
"insecure_skip_verify": true,
"auth_token": "aa",
}

url := getTestProviderURL(inputData, t)
if got, want := url.Host, "localhost"; got != want {
t.Errorf("hostname invalid, got %s, want %s", got, want)
}

inputData["url"] = "localhost:8089"
url = getTestProviderURL(inputData, t)
if got, want := url.Host, "localhost:8089"; got != want {
t.Errorf("url.Host invalid, got %s, want %s", got, want)
}

inputData["url"] = "https://localhost:8089"
url = getTestProviderURL(inputData, t)
if got, want := url.Host, "localhost:8089"; got != want {
t.Errorf("url.Host invalid, got %s, want %s", got, want)
}
if got, want := url.Scheme, "https"; got != want {
t.Errorf("url.Scheme invalid, got %s, want %s", got, want)
}
// Test URL with Path
inputData["url"] = "https://localhost:8089/test/path"
url = getTestProviderURL(inputData, t)
if got, want := url.Path, "/test/path"; got != want {
t.Errorf("url path invalid, got %s, want %s", got, want)
}
}

func init() {
testAccProvider = Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{
Expand Down
Loading