Skip to content

Commit

Permalink
Add proper github.com/pulumi/pulumi-go-provider/integration tests (#68
Browse files Browse the repository at this point in the history
)

* Add proper `github.com/pulumi/pulumi-go-provider/integration` tests

Fixes #58
Fixes #61
Fixes #63

* Respond to comment
  • Loading branch information
iwahbe authored Aug 31, 2023
1 parent 8392ae5 commit b87db5f
Show file tree
Hide file tree
Showing 11 changed files with 568 additions and 1,252 deletions.
7 changes: 3 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ NUGET_PKG_NAME := Pulumi.Xyz
PROVIDER := pulumi-resource-${PACK}
VERSION ?= $(shell pulumictl get version)
PROVIDER_PATH := provider
VERSION_PATH := ${PROVIDER_PATH}/cmd/main.Version
VERSION_PATH := ${PROVIDER_PATH}.Version

GOPATH := $(shell go env GOPATH)

Expand All @@ -28,7 +28,7 @@ provider_debug::
(cd provider && go build -o $(WORKING_DIR)/bin/${PROVIDER} -gcflags="all=-N -l" -ldflags "-X ${PROJECT}/${VERSION_PATH}=${VERSION}" $(PROJECT)/${PROVIDER_PATH}/cmd/$(PROVIDER))

test_provider::
cd provider/pkg && go test -short -v -count=1 -cover -timeout 2h -parallel ${TESTPARALLELISM} ./...
cd tests && go test -short -v -count=1 -cover -timeout 2h -parallel ${TESTPARALLELISM} ./...

dotnet_sdk:: DOTNET_VERSION := $(shell pulumictl get version --language dotnet)
dotnet_sdk::
Expand Down Expand Up @@ -84,8 +84,7 @@ install:: install_nodejs_sdk install_dotnet_sdk

GO_TEST := go test -v -count=1 -cover -timeout 2h -parallel ${TESTPARALLELISM}

test_all::
cd provider/pkg && $(GO_TEST) ./...
test_all:: test_provider
cd tests/sdk/nodejs && $(GO_TEST) ./...
cd tests/sdk/python && $(GO_TEST) ./...
cd tests/sdk/dotnet && $(GO_TEST) ./...
Expand Down
2 changes: 1 addition & 1 deletion deployment-templates/.goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ builds:
- linux
ldflags:
# The line below MUST align with the module in current provider/go.mod
- -X github.com/your-org-name/pulumi-xyz/provider/pkg/version.Version={{.Tag }}
- -X github.com/your-org-name/pulumi-xyz/provider/Version={{.Tag }}
main: ./cmd/pulumi-resource-xyz/
changelog:
skip: true
Expand Down
70 changes: 4 additions & 66 deletions provider/cmd/pulumi-resource-xyz/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,72 +15,10 @@
package main

import (
"math/rand"
"time"

p "github.com/pulumi/pulumi-go-provider"
"github.com/pulumi/pulumi-go-provider/infer"
)

// Version is initialized by the Go linker to contain the semver of this build.
var Version string

func main() {
p.RunProvider("xyz", Version,
// We tell the provider what resources it needs to support.
// In this case, a single custom resource.
infer.Provider(infer.Options{
Resources: []infer.InferredResource{
infer.Resource[Random, RandomArgs, RandomState](),
},
}))
}

// Each resource has a controlling struct.
// Resource behavior is determined by implementing methods on the controlling struct.
// The `Create` method is mandatory, but other methods are optional.
// - Check: Remap inputs before they are typed.
// - Diff: Change how instances of a resource are compared.
// - Update: Mutate a resource in place.
// - Read: Get the state of a resource from the backing provider.
// - Delete: Custom logic when the resource is deleted.
// - Annotate: Describe fields and set defaults for a resource.
// - WireDependencies: Control how outputs and secrets flows through values.
type Random struct{}

// Each resource has in input struct, defining what arguments it accepts.
type RandomArgs struct {
// Fields projected into Pulumi must be public and hava a `pulumi:"..."` tag.
// The pulumi tag doesn't need to match the field name, but its generally a
// good idea.
Length int `pulumi:"length"`
}

// Each resource has a state, describing the fields that exist on the created resource.
type RandomState struct {
// It is generally a good idea to embed args in outputs, but it isn't strictly necessary.
RandomArgs
// Here we define a required output called result.
Result string `pulumi:"result"`
}

// All resources must implement Create at a minumum.
func (Random) Create(ctx p.Context, name string, input RandomArgs, preview bool) (string, RandomState, error) {
state := RandomState{RandomArgs: input}
if preview {
return name, state, nil
}
state.Result = makeRandom(input.Length)
return name, state, nil
}

func makeRandom(length int) string {
seededRand := rand.New(rand.NewSource(time.Now().UnixNano()))
charset := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
xyz "github.com/pulumi/pulumi-xyz/provider"
)

result := make([]rune, length)
for i := range result {
result[i] = charset[seededRand.Intn(len(charset))]
}
return string(result)
}
// Serve the provider against Pulumi's Provider protocol.
func main() { p.RunProvider(xyz.Name, xyz.Version, xyz.Provider()) }
5 changes: 0 additions & 5 deletions provider/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/acomagu/bufpipe v1.0.4 // indirect
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/cheggaaa/pb v1.0.29 // indirect
Expand All @@ -31,12 +30,10 @@ require (
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/hcl/v2 v2.18.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-ps v1.0.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/natefinch/atomic v1.0.1 // indirect
Expand Down Expand Up @@ -73,12 +70,10 @@ require (
golang.org/x/term v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/tools v0.12.0 // indirect
google.golang.org/genproto v0.0.0-20230803162519-f966b187b2e5 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/grpc v1.57.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/frand v1.4.2 // indirect
sourcegraph.com/sourcegraph/appdash v0.0.0-20211028080628-e2786a622600 // indirect
Expand Down
Loading

0 comments on commit b87db5f

Please sign in to comment.