Skip to content

Commit

Permalink
Add accceptance test generation
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeNeyer committed Mar 9, 2023
1 parent 4a6b400 commit 4cf3df4
Show file tree
Hide file tree
Showing 38 changed files with 3,276 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/resources/netsuite_connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ resource "polytomic_netsuite_connection" "netsuite" {
account_id = "my-account-id"
consumer_key = "my-consumer-key"
consumer_secret = "my-consumer-secret"
token = "token"
token_secret = "token-secret"
}
}
```
Expand Down
2 changes: 2 additions & 0 deletions examples/resources/polytomic_netsuite_connection/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ resource "polytomic_netsuite_connection" "netsuite" {
account_id = "my-account-id"
consumer_key = "my-consumer-key"
consumer_secret = "my-consumer-secret"
token = "token"
token_secret = "token-secret"
}
}

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/hashicorp/terraform-plugin-log v0.8.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.25.0
github.com/mitchellh/mapstructure v1.5.0
github.com/polytomic/polytomic-go v0.0.0-20230308205457-3e0a5ade2f59
github.com/polytomic/polytomic-go v0.0.0-20230309195205-651581fb64dd
github.com/rs/zerolog v1.29.0
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.15.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/polytomic/polytomic-go v0.0.0-20230308205457-3e0a5ade2f59 h1:XJUlgDX6IN5eKYnDmd503lw+8tqYnbJMtqy4kMjJdw0=
github.com/polytomic/polytomic-go v0.0.0-20230308205457-3e0a5ade2f59/go.mod h1:OYSOoZCtLFAW6hCf5YGWGXuoX6RgYAd49QEV7v7QLvo=
github.com/polytomic/polytomic-go v0.0.0-20230309195205-651581fb64dd h1:sWnD3+T2mq/ESqdO83difqnak53qEP3Fay9CYEGYZLw=
github.com/polytomic/polytomic-go v0.0.0-20230309195205-651581fb64dd/go.mod h1:OYSOoZCtLFAW6hCf5YGWGXuoX6RgYAd49QEV7v7QLvo=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/posener/complete v1.2.3 h1:NP0eAhjcjImqslEwo/1hq7gpajME0fTLTezBKDqfXqo=
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
Expand Down
47 changes: 44 additions & 3 deletions provider/gen/connections/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ const (
exportTemplate = "./provider/gen/connections/connections.go.tmpl"

// Resources
connectionResourceTemplate = "./provider/gen/connections/resource.go.tmpl"
exampleResourceTemplate = "./provider/gen/connections/resource.tf.go.tmpl"
exampleResourceOutputPath = "./examples/resources"
connectionResourceTemplate = "./provider/gen/connections/resource.go.tmpl"
connectionResourceTestTemplate = "./provider/gen/connections/resource_test.go.tmpl"
exampleResourceTemplate = "./provider/gen/connections/resource.tf.go.tmpl"
exampleResourceOutputPath = "./examples/resources"

// Datasources
connectionDataSourceTemplate = "./provider/gen/connections/datasource.go.tmpl"
Expand Down Expand Up @@ -76,6 +77,7 @@ type Connection struct {
Config string `yaml:"config"`
Datasource bool `yaml:"datasource"`
Resource bool `yaml:"resource"`
SkipTest bool `yaml:"skip_test"`
}

type Attribute struct {
Expand Down Expand Up @@ -147,6 +149,13 @@ func GenerateConnections() error {
})
}

if !r.SkipTest && r.Resource {
err := writeConnectionTest(r)
if err != nil {
return err
}
}

err = writeConnectionExamples(r)
if err != nil {
return err
Expand Down Expand Up @@ -308,6 +317,38 @@ func writeConnectionDataSource(r Connection) error {
return err
}

func writeConnectionTest(r Connection) error {
tmpl, err := template.New("resource_test.go.tmpl").ParseFiles(connectionResourceTestTemplate)
if err != nil {
return err
}
var buf bytes.Buffer
f, err := os.Create(
filepath.Join(outputPath, fmt.Sprintf("resource_%s_connection_test.go", r.Connection)))
if err != nil {
return err
}
defer f.Close()

err = tmpl.Execute(&buf, Connection{
Name: r.Name,
Connection: strings.Title(r.Connection),
ResourceName: r.Connection,
Attributes: r.Attributes,
})
if err != nil {
return err
}
p, err := format.Source(buf.Bytes())
if err != nil {
return err
}

_, err = f.Write(p)
return err

}

func writeExports(datasources, resources []Importable) error {
tmpl, err := template.New("connections.go.tmpl").ParseFiles(exportTemplate)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions provider/gen/connections/connections.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ connections:
config: polytomic.S3Configuration
resource: true
datasource: true
skip_test: true
attributes:
- name: AwsAccessKeyID
type: string
Expand Down Expand Up @@ -197,6 +198,7 @@ connections:
config: polytomic.AthenaConfiguration
resource: true
datasource: true
skip_test: true
attributes:
- name: AccessKeyID
type: string
Expand Down Expand Up @@ -337,6 +339,7 @@ connections:
config: polytomic.MarketoConfiguration
datasource: true
resource: true
skip_test: true
attributes:
- name: ClientID
type: string
Expand Down Expand Up @@ -396,6 +399,7 @@ connections:
config: polytomic.ChargeBeeConnectionConfiguration
datasource: true
resource: true
skip_test: true
attributes:
- name: Site
type: string
Expand Down Expand Up @@ -629,6 +633,7 @@ connections:
config: polytomic.NetsuiteConnectionConfiguration
datasource: true
resource: true
skip_test: true
attributes:
- name: AccountID
type: string
Expand All @@ -646,10 +651,12 @@ connections:
- name: Token
type: string
required: true
example: token
- name: TokenSecret
type: string
required: true
sensitive: true
example: token-secret
- connection: pipedrive
name: Pipedrive
type: polytomic.PipedriveConnectionType
Expand All @@ -672,6 +679,7 @@ connections:
config: polytomic.RedshiftConnectionConfiguration
datasource: true
resource: true
skip_test: true
attributes:
- name: Hostname
type: string
Expand Down
107 changes: 107 additions & 0 deletions provider/gen/connections/resource_test.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Code generated by Polytomic. DO NOT EDIT.
// edit connections.yaml and re-run go generate

package provider

import (
"context"
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAcc{{ .Connection }}Connection(t *testing.T) {
name := "TestAcc"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Missing configuration
{
Config: testAcc{{ .Connection }}ConnectionResourceNoConfig(name),
ExpectError: regexp.MustCompile("The argument \"configuration\" is required, but no definition was found."),
},
// Empty configuration
{
Config: testAcc{{ .Connection }}ConnectionResourceMissingRequired(name),
ExpectError: regexp.MustCompile("Inappropriate value for attribute \"configuration\":"),
},
{
Config: testAcc{{ .Connection }}ConnectionResource(name),
Check: resource.ComposeTestCheckFunc(
// Check if the resource exists
testAcc{{ .Connection }}ConnectionExists(name),
// Check the name
resource.TestCheckResourceAttr("polytomic_{{ .ResourceName }}_connection.test", "name", name),
),
},
},
})
}

func testAcc{{ .Connection }}ConnectionExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, ok := s.RootModule().Resources["polytomic_{{ .ResourceName }}_connection.test"]
if !ok {
return fmt.Errorf("not found: %s", "polytomic_{{ .ResourceName }}_connection.test")
}

client := testClient()
connections, err := client.Connections().List(context.TODO())
if err != nil {
return err
}
var found bool
for _, connection := range connections {
if connection.Name == name {
found = true
break
}
}

if !found {
return fmt.Errorf("connection %s not found", name)
}

return nil

}
}

func testAcc{{ .Connection }}ConnectionResource(name string) string {
return fmt.Sprintf(`
resource "polytomic_{{ .ResourceName }}_connection" "test" {
name = "%s"
configuration = {
{{- range .Attributes }}
{{- if .Example }}
{{- if eq .Type "string" }}
{{ .AttrName }} = "{{ .Example }}"
{{- else }}
{{ .AttrName }} = {{ .Example }}
{{- end -}}
{{ end }}
{{- end }}
}
}
`, name)
}

func testAcc{{ .Connection }}ConnectionResourceNoConfig(name string) string {
return fmt.Sprintf(`
resource "polytomic_{{ .ResourceName }}_connection" "test" {
name = "%s"
}
`, name)
}

func testAcc{{ .Connection }}ConnectionResourceMissingRequired(name string) string {
return fmt.Sprintf(`
resource "polytomic_{{ .ResourceName }}_connection" "test" {
name = "%s"
configuration = {}
}`, name)
}
99 changes: 99 additions & 0 deletions provider/resource_affinity_connection_test.go

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

Loading

0 comments on commit 4cf3df4

Please sign in to comment.