Skip to content

Commit

Permalink
Add tests (#9)
Browse files Browse the repository at this point in the history
* Add tests

* add provider_test
  • Loading branch information
ZoeySimone authored Aug 26, 2021
1 parent e71fde9 commit 2e3e05a
Show file tree
Hide file tree
Showing 8 changed files with 603 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ bin/
build/
*.tfstate
.terraform/
coverage/go/coverage.out
coverage/go/html/main.html
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ module github.com/hewlettpackard/hpegl-provider-lib
go 1.15

require (
github.com/golang/mock v1.4.4
github.com/hashicorp/terraform-plugin-sdk/v2 v2.5.0
github.com/hpe-hcss/errors v0.0.8
github.com/hpe-hcss/iam-lib v1.12.8
github.com/stretchr/testify v1.6.1
gopkg.in/square/go-jose.v2 v2.5.1
gopkg.in/yaml.v2 v2.4.0
)
50 changes: 50 additions & 0 deletions pkg/mocks/IdentityAPI_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 42 additions & 37 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewProviderFunc(reg []registration.ServiceRegistration, pf ConfigureFunc) p
dataSources := make(map[string]*schema.Resource)
resources := make(map[string]*schema.Resource)
// providerSchema is the Schema for the provider
providerSchema := make(map[string]*schema.Schema)
providerSchema := Schema()
for _, service := range reg {
for k, v := range service.SupportedDataSources() {
// We panic if the data-source name k is repeated in dataSources
Expand Down Expand Up @@ -52,42 +52,6 @@ func NewProviderFunc(reg []registration.ServiceRegistration, pf ConfigureFunc) p
}
}

providerSchema["iam_service_url"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("HPEGL_IAM_SERVICE_URL", "https://client.greenlake.hpe.com/api/iam"),
Description: `The IAM service URL to be used to generate tokens, defaults to production GLC,
can be set by HPEGL_IAM_SERVICE_URL env-var`,
}

providerSchema["api_vended_service_client"] = &schema.Schema{
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("HPEGL_API_VENDED_SERVICE_CLIENT", true),
Description: ``,
}

providerSchema["tenant_id"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("HPEGL_TENANT_ID", ""),
Description: "The tenant-id to be used, can be set by HPEGL_TENANT_ID env-var",
}

providerSchema["user_id"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("HPEGL_USER_ID", ""),
Description: "The user id to be used, can be set by HPEGL_USER_ID env-var",
}

providerSchema["user_secret"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("HPEGL_USER_SECRET", ""),
Description: "The user secret to be used, can be set by HPEGL_USER_SECRET env-var",
}

p := schema.Provider{
Schema: providerSchema,
ResourcesMap: resources,
Expand All @@ -103,6 +67,47 @@ func NewProviderFunc(reg []registration.ServiceRegistration, pf ConfigureFunc) p
}
}

func Schema() map[string]*schema.Schema {
providerSchema := make(map[string]*schema.Schema)
providerSchema["iam_service_url"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("HPEGL_IAM_SERVICE_URL", "https://client.greenlake.hpe.com/api/iam"),
Description: `The IAM service URL to be used to generate tokens, defaults to production GLC,
can be set by HPEGL_IAM_SERVICE_URL env-var`,
}

providerSchema["api_vended_service_client"] = &schema.Schema{
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("HPEGL_API_VENDED_SERVICE_CLIENT", true),
Description: ``,
}

providerSchema["tenant_id"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("HPEGL_TENANT_ID", ""),
Description: "The tenant-id to be used, can be set by HPEGL_TENANT_ID env-var",
}

providerSchema["user_id"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("HPEGL_USER_ID", ""),
Description: "The user id to be used, can be set by HPEGL_USER_ID env-var",
}

providerSchema["user_secret"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("HPEGL_USER_SECRET", ""),
Description: "The user secret to be used, can be set by HPEGL_USER_SECRET env-var",
}

return providerSchema
}

// ServiceRegistrationSlice helper function to return []registration.ServiceRegistration from
// registration.ServiceRegistration input
// For use in service provider repos
Expand Down
170 changes: 170 additions & 0 deletions pkg/provider/provider_test.go
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)()
})
}
}
Loading

0 comments on commit 2e3e05a

Please sign in to comment.