From 1c575b0966020d439a4e84247c92878764d29514 Mon Sep 17 00:00:00 2001 From: Bryce Lampe Date: Fri, 9 May 2025 10:05:35 -0700 Subject: [PATCH] Inject mock more directly --- go.mod | 2 +- go.sum | 4 ++-- provider/internal/image.go | 13 ++++++++----- provider/internal/index.go | 13 ++++++++----- provider/internal/provider.go | 13 +++++-------- provider/internal/provider_test.go | 10 +--------- provider/provider.go | 2 +- 7 files changed, 26 insertions(+), 31 deletions(-) diff --git a/go.mod b/go.mod index 55cdc20..ba64ed0 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/otiai10/copy v1.14.0 github.com/pulumi/providertest v0.3.1 github.com/pulumi/pulumi-dotnet/pulumi-language-dotnet/v3 v3.0.0-20250507122953-af68281fea7f - github.com/pulumi/pulumi-go-provider v1.0.0-rc1.0.20250509163142-82c9935ceef4 + github.com/pulumi/pulumi-go-provider v1.0.0-rc1.0.20250508214503-b09e1ae91a79 github.com/pulumi/pulumi-java/pkg v1.11.0 github.com/pulumi/pulumi-yaml v1.17.0 github.com/pulumi/pulumi/pkg/v3 v3.169.0 diff --git a/go.sum b/go.sum index 4a8ec5d..d8f577b 100644 --- a/go.sum +++ b/go.sum @@ -950,8 +950,8 @@ github.com/pulumi/providertest v0.3.1 h1:vlftr7TZlObh81mL88IhhF0/9ZbLrZZos4NAvR4 github.com/pulumi/providertest v0.3.1/go.mod h1:fFHUP4/9DRyYnHWiRnwcynMtM/a7hHR/QcJfcuZKO3A= github.com/pulumi/pulumi-dotnet/pulumi-language-dotnet/v3 v3.0.0-20250507122953-af68281fea7f h1:2Lwj2Aefzs1nQu5HTePcO4WnynkV+wn9knjvexV0LUE= github.com/pulumi/pulumi-dotnet/pulumi-language-dotnet/v3 v3.0.0-20250507122953-af68281fea7f/go.mod h1:hxBlJXAULwXhJR3hMRoJvWuXjVHCUbDVrLMfkELfVdk= -github.com/pulumi/pulumi-go-provider v1.0.0-rc1.0.20250509163142-82c9935ceef4 h1:JTnT/7lT/sfmmYMzSkiT6VBxY2Fjv2OHITMl/3sLvH4= -github.com/pulumi/pulumi-go-provider v1.0.0-rc1.0.20250509163142-82c9935ceef4/go.mod h1:iBuN17+kkUL0GpXftmBxtwZFWsdyV8W34mGouk2/8bg= +github.com/pulumi/pulumi-go-provider v1.0.0-rc1.0.20250508214503-b09e1ae91a79 h1:BYFzRYAUACLa+vfNBJd1okoJnH0H49q547e2f8kddHw= +github.com/pulumi/pulumi-go-provider v1.0.0-rc1.0.20250508214503-b09e1ae91a79/go.mod h1:nHe4CydeeIK5LlrrRk/fGBB3n+KTpjNWCrph3FGaJ7Q= github.com/pulumi/pulumi-java/pkg v1.11.0 h1:M8C7CKxwBSE/c5RcoGF0sAUSFZywjd7CjmTRQh8nvNk= github.com/pulumi/pulumi-java/pkg v1.11.0/go.mod h1:zoQdTjj488DhUx8dNed6SW1fJnAE4GwGLBVDRpsQVE8= github.com/pulumi/pulumi-yaml v1.17.0 h1:ebzggygqBcQrtmdBUqi28B1L/fAyHCFVIt0dS5re8Oc= diff --git a/provider/internal/image.go b/provider/internal/image.go index e533d73..aefe865 100644 --- a/provider/internal/image.go +++ b/provider/internal/image.go @@ -61,7 +61,9 @@ var _imageExamples string var _migration string // Image is a Docker image build using buildkit. -type Image struct{} +type Image struct { + docker Client +} // Annotate provides a description of the Image resource. func (i *Image) Annotate(a infer.Annotator) { @@ -330,12 +332,13 @@ func (is *ImageState) Annotate(a infer.Annotator) { // client produces a CLI client scoped to this resource and layered on top of // any host-level credentials. func (i *Image) client(ctx context.Context, state ImageState, args ImageArgs) (Client, error) { - cfg := infer.GetConfig[Config](ctx) - - if cli, ok := ctx.Value(_mockClientKey).(Client); ok { - return cli, nil + // Use our mock client, if it's set. + if i.docker != nil { + return i.docker, nil } + cfg := infer.GetConfig[Config](ctx) + // We prefer auth from args, the provider, and state in that order. We // build a slice in reverse order because wrap() will overwrite earlier // entries with later ones. diff --git a/provider/internal/index.go b/provider/internal/index.go index 547d5f9..b0bda76 100644 --- a/provider/internal/index.go +++ b/provider/internal/index.go @@ -46,7 +46,9 @@ var ( var _indexExamples string // Index is an OCI index or manifest list on a remote registry. -type Index struct{} +type Index struct { + docker Client +} // IndexArgs instantiate an Index. type IndexArgs struct { @@ -351,12 +353,13 @@ func (i *Index) client( _ IndexState, args IndexArgs, ) (Client, error) { - cfg := infer.GetConfig[Config](ctx) - - if cli, ok := ctx.Value(_mockClientKey).(Client); ok { - return cli, nil + // Use our mock client, if it's set. + if i.docker != nil { + return i.docker, nil } + cfg := infer.GetConfig[Config](ctx) + // We prefer auth from args, the provider, and state in that order. We // build a slice in reverse order because wrap() will overwrite earlier // entries with later ones. diff --git a/provider/internal/provider.go b/provider/internal/provider.go index ee6eb4d..794d58b 100644 --- a/provider/internal/provider.go +++ b/provider/internal/provider.go @@ -46,9 +46,6 @@ type Config struct { host *host } -// _mockClientKey is used by tests to inject a mock Docker client. -var _mockClientKey any = "mock-client" - // Annotate provides user-facing descriptions and defaults for Config's fields. func (c *Config) Annotate(a infer.Annotator) { a.Describe(&c.Host, "The build daemon's address.") @@ -66,7 +63,7 @@ func (c *Config) Configure(ctx context.Context) error { } // NewBuildxProvider returns a new buildx provider. -func NewBuildxProvider() provider.Provider { +func NewBuildxProvider(mock Client) provider.Provider { prov := infer.Provider( infer.Options{ Metadata: pschema.Metadata{ @@ -114,13 +111,13 @@ func NewBuildxProvider() provider.Provider { }, }, Resources: []infer.InferredResource{ - infer.Resource[*Image](), - infer.Resource[*Index](), + infer.Resource(&Image{docker: mock}), + infer.Resource(&Index{docker: mock}), }, ModuleMap: map[tokens.ModuleName]tokens.ModuleName{ "internal": "index", }, - Config: infer.Config[*Config](), + Config: infer.Config(&Config{}), }, ) @@ -152,7 +149,7 @@ func diffConfigIgnoreInternal( // Schema returns our package specification. func Schema(ctx context.Context, version string) schema.PackageSpec { - p := NewBuildxProvider() + p := NewBuildxProvider(nil) spec, err := provider.GetSchema(ctx, "docker-build", version, p) contract.AssertNoErrorf(err, "missing schema") return spec diff --git a/provider/internal/provider_test.go b/provider/internal/provider_test.go index 52e5c8f..c673ff2 100644 --- a/provider/internal/provider_test.go +++ b/provider/internal/provider_test.go @@ -25,7 +25,6 @@ import ( provider "github.com/pulumi/pulumi-go-provider" "github.com/pulumi/pulumi-go-provider/infer" "github.com/pulumi/pulumi-go-provider/integration" - mwcontext "github.com/pulumi/pulumi-go-provider/middleware/context" "github.com/pulumi/pulumi/sdk/v3/go/common/tokens" ) @@ -79,14 +78,7 @@ func (annotator) SetResourceDeprecationMessage(_ string) {} func newServer(ctx context.Context, t *testing.T, client Client) integration.Server { t.Helper() - p := NewBuildxProvider() - - // Inject a mock client if provided. - if client != nil { - p = mwcontext.Wrap(p, func(ctx context.Context) context.Context { - return context.WithValue(ctx, _mockClientKey, client) - }) - } + p := NewBuildxProvider(client) s, err := integration.NewServer( ctx, diff --git a/provider/provider.go b/provider/provider.go index a5db30e..c01509a 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -34,5 +34,5 @@ func Serve() error { // New creates a new provider. func New(host *provider.HostClient) (rpc.ResourceProviderServer, error) { - return gp.RawServer(Name, Version, internal.NewBuildxProvider())(host) + return gp.RawServer(Name, Version, internal.NewBuildxProvider(nil))(host) }