-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
603 additions
and
37 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 |
---|---|---|
|
@@ -4,3 +4,5 @@ bin/ | |
build/ | ||
*.tfstate | ||
.terraform/ | ||
coverage/go/coverage.out | ||
coverage/go/html/main.html |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,170 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hewlettpackard/hpegl-provider-lib/pkg/registration" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func testResource() *schema.Resource { | ||
return &schema.Resource{} | ||
} | ||
|
||
type Registration struct { | ||
serviceName string | ||
resources map[string]*schema.Resource | ||
datasources map[string]*schema.Resource | ||
} | ||
|
||
func (r Registration) Name() string { | ||
return r.serviceName | ||
} | ||
|
||
func (r Registration) SupportedDataSources() map[string]*schema.Resource { | ||
return r.datasources | ||
} | ||
|
||
func (r Registration) SupportedResources() map[string]*schema.Resource { | ||
return r.resources | ||
} | ||
|
||
func (r Registration) ProviderSchemaEntry() *schema.Resource { | ||
return &schema.Resource{ | ||
Schema: map[string]*schema.Schema{}, | ||
} | ||
} | ||
|
||
func providerConfigure(p *schema.Provider) schema.ConfigureContextFunc { // nolint staticcheck | ||
return func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) { | ||
return nil, nil | ||
} | ||
} | ||
|
||
func TestNewProviderFunc(t *testing.T) { | ||
t.Parallel() | ||
testcases := []struct { | ||
name string | ||
regs []Registration | ||
panicMsg string | ||
}{ | ||
{ | ||
name: "success", | ||
regs: []Registration{ | ||
{ | ||
serviceName: "test-service", | ||
resources: map[string]*schema.Resource{ | ||
"test-resource": testResource(), | ||
}, | ||
datasources: map[string]*schema.Resource{ | ||
"test-datasource": testResource(), | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "success two services", | ||
regs: []Registration{ | ||
{ | ||
serviceName: "test-service", | ||
resources: map[string]*schema.Resource{ | ||
"test-resource": testResource(), | ||
}, | ||
datasources: map[string]*schema.Resource{ | ||
"test-datasource": testResource(), | ||
}, | ||
}, | ||
{ | ||
serviceName: "test-service2", | ||
resources: map[string]*schema.Resource{ | ||
"test-resource2": testResource(), | ||
}, | ||
datasources: map[string]*schema.Resource{ | ||
"test-datasource2": testResource(), | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "duplicate resource", | ||
regs: []Registration{ | ||
{ | ||
serviceName: "test-service", | ||
resources: map[string]*schema.Resource{ | ||
"test-resource": testResource(), | ||
}, | ||
}, | ||
{ | ||
serviceName: "test-service2", | ||
resources: map[string]*schema.Resource{ | ||
"test-resource": testResource(), | ||
}, | ||
}, | ||
}, | ||
panicMsg: "resource name test-resource is repeated in service test-service2", | ||
}, | ||
{ | ||
name: "duplicate data source", | ||
regs: []Registration{ | ||
{ | ||
serviceName: "test-service", | ||
datasources: map[string]*schema.Resource{ | ||
"test-datasource": testResource(), | ||
}, | ||
}, | ||
{ | ||
serviceName: "test-service2", | ||
datasources: map[string]*schema.Resource{ | ||
"test-datasource": testResource(), | ||
}, | ||
}, | ||
}, | ||
panicMsg: "data-source name test-datasource is repeated in service test-service2", | ||
}, | ||
{ | ||
name: "duplicate service name", | ||
regs: []Registration{ | ||
{ | ||
serviceName: "test-service", | ||
}, | ||
{ | ||
serviceName: "test-service", | ||
}, | ||
}, | ||
panicMsg: "service name test-service is repeated", | ||
}, | ||
} | ||
|
||
for _, testcase := range testcases { | ||
tc := testcase | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
var regs []registration.ServiceRegistration | ||
|
||
if len(tc.regs) == 1 { | ||
regs = ServiceRegistrationSlice(tc.regs[0]) | ||
} else { | ||
regs = make([]registration.ServiceRegistration, len(tc.regs)) | ||
for i, reg := range tc.regs { | ||
regs[i] = reg | ||
} | ||
} | ||
|
||
defer func() { | ||
r := recover() | ||
if r != nil { | ||
if tc.panicMsg != "" { | ||
assert.Equal(t, tc.panicMsg, r) | ||
} else { | ||
assert.Equal(t, nil, r) | ||
} | ||
} | ||
}() | ||
|
||
NewProviderFunc(regs, providerConfigure)() | ||
}) | ||
} | ||
} |
Oops, something went wrong.