Skip to content

Commit

Permalink
feat: basic acceptance test
Browse files Browse the repository at this point in the history
  • Loading branch information
thiskevinwang committed Oct 5, 2023
1 parent 4e95920 commit afcb253
Show file tree
Hide file tree
Showing 8 changed files with 335 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Enables acceptance tests
TF_ACC=1

# Secrets used by the acceptance tests
PINECONE_API_KEY=
PINECONE_ENVIRONMENT=
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.tfvars
terraform.tfvars
terraform.tfstate
terraform.tfstate.backup
terraform.tfstate.backup
.env
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ terraform {
provider "pinecone" {}
```

## Testing

```console
cp .env.example .env
go test -v ./...
```

## Appendix

- https://docs.pinecone.io/
Expand Down
37 changes: 33 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,57 @@ go 1.21.1

require (
github.com/hashicorp/terraform-plugin-framework v1.4.0
github.com/hashicorp/terraform-plugin-go v0.19.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-testing v1.5.1
github.com/joho/godotenv v1.5.1
)

require (
github.com/ProtonMail/go-crypto v0.0.0-20230717121422-5aa5874ade95 // indirect
github.com/agext/levenshtein v1.2.2 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-plugin v1.5.1 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/terraform-plugin-go v0.19.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/hc-install v0.6.0 // indirect
github.com/hashicorp/hcl/v2 v2.18.0 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.19.0 // indirect
github.com/hashicorp/terraform-json v0.17.1 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.29.0 // indirect
github.com/hashicorp/terraform-registry-address v0.2.2 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/zclconf/go-cty v1.14.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.13.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
google.golang.org/grpc v1.57.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
Expand Down
164 changes: 158 additions & 6 deletions go.sum

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ func (p *pineconeProvider) Schema(_ context.Context, _ provider.SchemaRequest, r
Attributes: map[string]schema.Attribute{
"apikey": schema.StringAttribute{
Description: "...Or PINECONE_API_KEY",
Optional: false,
Required: true,
Optional: true,
Required: false,
Sensitive: true,
},
"environment": schema.StringAttribute{
Description: "...Or PINECONE_ENVIRONMENT",
Optional: false,
Required: true,
Optional: true,
Required: false,
},
},
}
Expand Down
117 changes: 117 additions & 0 deletions internal/resources/index_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package resources_test

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/thiskevinwang/terraform-provider-pinecone/internal/provider"

"github.com/joho/godotenv"
)

var (
providerConfig = `provider "pinecone" {
# will use PINECONE_API_KEY
# and PINECONE_ENVIRONMENT env vars
}`
// testAccProtoV6ProviderFactories are used to instantiate a provider during
// acceptance testing. The factory function will be invoked for every Terraform
// CLI command executed to create a provider server to which the CLI can
// reattach.
testAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
"pinecone": providerserver.NewProtocol6WithError(provider.New("test")()),
}
)

// setup
func TestMain(m *testing.M) {
fmt.Println(`
#######################
# 🧪 Setting up tests #
#######################
`)

// Load environment variables from a .env file
if err := godotenv.Load("../../.env"); err != nil {
panic(fmt.Sprintf("Error loading ../../.env file: %v", err))
}

// Sanity checks
// fmt.Println(fmt.Sprintf("PINECONE_API_KEY: %s", os.Getenv("PINECONE_API_KEY")))
// fmt.Println(fmt.Sprintf("PINECONE_ENVIRONMENT: %s", os.Getenv("PINECONE_ENVIRONMENT")))

// Run the tests
exitCode := m.Run()

// Exit with the appropriate exit code
os.Exit(exitCode)
}

// Note: this test requires a Pinecone account with a valid API key
// and will create and destroy REAL infrastructure.
func TestAccOrderResource(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Create and Read testing
{
Config: providerConfig + `
resource "pinecone_index" "test" {
name = "acceptance-test"
dimension = 1536
metric = "cosine"
pods = 1
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
// Verify attributes
resource.TestCheckResourceAttr("pinecone_index.test", "dimension", "1536"),
resource.TestCheckResourceAttr("pinecone_index.test", "metric", "cosine"),
resource.TestCheckResourceAttr("pinecone_index.test", "name", "acceptance-test"),
resource.TestCheckResourceAttr("pinecone_index.test", "pods", "1"),
resource.TestCheckResourceAttr("pinecone_index.test", "replicas", "1"),

// Verify dynamic values have any value set in the state.
resource.TestCheckResourceAttrSet("pinecone_index.test", "id"),
),
},
// ImportState testing
// TODO(kevinwang)
// {
// ResourceName: "pinecone_index.test",
// ImportState: true,
// ImportStateVerify: true,
// // The last_updated attribute does not exist in the HashiCups
// // API, therefore there is no value for it during import.
// ImportStateVerifyIgnore: []string{"last_updated"},
// },
// Update and Read testing
// TODO(kevinwang) - update doesn't work yet on the Pinecone free tier
// {
// Config: providerConfig + `

// resource "pinecone_index" "test" {
// name = "acceptance-test"
// dimension = 1536
// metric = "cosine"
// pods = 1
// }
// `,
// Check: resource.ComposeAggregateTestCheckFunc(
// // Verify attributes
// resource.TestCheckResourceAttr("pinecone_index.test", "dimension", "1536"),
// resource.TestCheckResourceAttr("pinecone_index.test", "metric", "cosine"),
// resource.TestCheckResourceAttr("pinecone_index.test", "name", "acceptance-test"),
// resource.TestCheckResourceAttr("pinecone_index.test", "pods", "1"),
// resource.TestCheckResourceAttr("pinecone_index.test", "replicas", "1"),
// ),
// },
// Delete testing automatically occurs in TestCase
},
})
}
9 changes: 8 additions & 1 deletion learning/log.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,11 @@ Do some whimsical diagramming ... it helps alot

Implemented update and destroy.

Next: unit tests?
Next: acceptance tests?

10-05-2023 Flexible — I'm not going to track time anymore

Started working on tests and new-to-me golang module/import/testing isms
https://github.com/hashicorp/terraform-plugin-testing/issues/185

10-05-2023 12:52pm Got a basic CREATE-DESTROY acceptance test working

0 comments on commit afcb253

Please sign in to comment.