Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing baseline provider config #60

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions previewProviderUpgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,23 @@ func PreviewProviderUpgrade(pulumiTest *pulumitest.PulumiTest, providerName stri
test.T().Logf("writing grpc log to %s", grpcLogPath)
grptLog.WriteTo(grpcLogPath)
},
optrun.WithOpts(opttest.NewStackOptions(optnewstack.EnableAutoDestroy())),
optrun.WithCache(filepath.Join(cacheDir, "stack.json")),
optrun.WithOpts(options.BaselineOpts...))
optrun.WithOpts(
opttest.NewStackOptions(optnewstack.EnableAutoDestroy()),
baselineProviderOpt(options, providerName, baselineVersion)),
optrun.WithOpts(options.BaselineOpts...),
)
return previewTest.Preview()
}

func baselineProviderOpt(options optproviderupgrade.PreviewProviderUpgradeOptions, providerName string, baselineVersion string) opttest.Option {
if options.DisableAttach {
return opttest.DownloadProviderVersion(providerName, baselineVersion)
} else {
return opttest.AttachDownloadedPlugin(providerName, baselineVersion)
}
}

func getCacheDir(options optproviderupgrade.PreviewProviderUpgradeOptions, programName string, baselineVersion string) string {
var cacheDir string
for _, pathTemplateElement := range options.CacheDirTemplate {
Expand Down
3 changes: 2 additions & 1 deletion providers/providerInterceptProxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ func NewProviderInterceptProxy(ctx context.Context, downstreamProviderPort Port,
fmt.Sprintf("127.0.0.1:%d", downstreamProviderPort),
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithUnaryInterceptor(rpcutil.OpenTracingClientInterceptor()),
grpc.WithStreamInterceptor(rpcutil.OpenTracingStreamClientInterceptor()))
grpc.WithStreamInterceptor(rpcutil.OpenTracingStreamClientInterceptor()),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(1024*1024*400)))
if err != nil {
return nil, err
}
Expand Down
32 changes: 18 additions & 14 deletions providers/providerInterceptProxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,37 @@ import (
"github.com/pulumi/providertest/pulumitest/opttest"
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/emptypb"
)

func TestProviderInterceptProxy(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
var didAttach, didForward bool
mockFactory := providers.ProviderMockFactory(ctx, providers.ProviderMocks{
Attach: func(ctx context.Context, in *pulumirpc.PluginAttach) (*emptypb.Empty, error) {
didForward = true
return &emptypb.Empty{}, nil
},
Create: func(ctx context.Context, in *pulumirpc.CreateRequest) (*pulumirpc.CreateResponse, error) {
// Return without error to indicate success.
return &pulumirpc.CreateResponse{}, nil
},
})
interceptedFactory := providers.ProviderInterceptFactory(ctx, mockFactory, providers.ProviderInterceptors{
var didAttach bool
// Ensure plugin is downloaded so YAML can look up its schema
_, err := providers.DownloadPluginBinary("azure-native", "2.10.0")
require.NoError(t, err)

interceptedFactory := providers.ProviderInterceptFactory(ctx, providers.DownloadPluginBinaryFactory("azure-native", "2.10.0"), providers.ProviderInterceptors{
Attach: func(ctx context.Context, in *pulumirpc.PluginAttach, client pulumirpc.ResourceProviderClient) (*emptypb.Empty, error) {
didAttach = true
return client.Attach(ctx, in)
},
Configure: func(ctx context.Context, in *pulumirpc.ConfigureRequest, client pulumirpc.ResourceProviderClient) (*pulumirpc.ConfigureResponse, error) {
// Skip checking the real configuration
return &pulumirpc.ConfigureResponse{}, nil
},
Check: func(ctx context.Context, in *pulumirpc.CheckRequest, client pulumirpc.ResourceProviderClient) (*pulumirpc.CheckResponse, error) {
// Skip checking the real configuration
return &pulumirpc.CheckResponse{Inputs: in.News}, nil
},
})
test := pulumitest.NewPulumiTest(t, filepath.Join("..", "pulumitest", "testdata", "yaml_program"), opttest.AttachProvider("random", interceptedFactory))
test := pulumitest.NewPulumiTest(t,
filepath.Join("..", "pulumitest", "testdata", "yaml_azure"),
opttest.AttachProvider("azure-native", interceptedFactory))

test.Preview()
assert.True(t, didAttach, "expected Attach to be called in proxy")
assert.True(t, didForward, "expected Attach to be called in downstream provider")
}
13 changes: 4 additions & 9 deletions providers/providerMock.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ type ProviderMocks struct {
}

// ProviderInterceptFactory creates a new provider factory that can be used to intercept calls to a downstream provider.
func ProviderMockFactory(ctx context.Context, mocks ProviderMocks) ProviderFactory {
func ProviderMockFactory(mocks ProviderMocks) ProviderFactory {
return ResourceProviderFactory(func() (rpc.ResourceProviderServer, error) {
return NewProviderMock(ctx, mocks)
return NewProviderMock(mocks)
})
}

// NewProviderMock creates a new provider proxy that can be used to intercept calls to a downstream provider.
func NewProviderMock(ctx context.Context, mocks ProviderMocks) (rpc.ResourceProviderServer, error) {
func NewProviderMock(mocks ProviderMocks) (rpc.ResourceProviderServer, error) {
return &providerMock{
mocks: mocks,
}, nil
Expand Down Expand Up @@ -92,12 +92,7 @@ func (i *providerMock) Configure(ctx context.Context, in *rpc.ConfigureRequest)
if i.mocks.Configure != nil {
return i.mocks.Configure(ctx, in)
}
return &rpc.ConfigureResponse{
AcceptSecrets: true,
SupportsPreview: true,
AcceptResources: true,
AcceptOutputs: true,
}, nil
return &rpc.ConfigureResponse{}, nil
}

func (i *providerMock) Construct(ctx context.Context, in *rpc.ConstructRequest) (*rpc.ConstructResponse, error) {
Expand Down
71 changes: 71 additions & 0 deletions providers/providerMock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package providers_test

import (
"context"
"path/filepath"
"testing"

"github.com/pulumi/providertest/providers"
"github.com/pulumi/providertest/pulumitest"
"github.com/pulumi/providertest/pulumitest/opttest"
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/structpb"
)

func TestProviderMock(t *testing.T) {
t.Parallel()
source := filepath.Join("..", "pulumitest", "testdata", "python_gcp")

t.Run("defaults", func(t *testing.T) {
test := pulumitest.NewPulumiTest(t, source,
opttest.AttachProvider("gcp",
providers.ProviderMockFactory(providers.ProviderMocks{})))
test.Preview()
})

t.Run("with mocks", func(t *testing.T) {
var attached, configured, checkedConfig, checked, created bool
test := pulumitest.NewPulumiTest(t, source,
opttest.AttachProvider("gcp",
providers.ProviderMockFactory(providers.ProviderMocks{
Attach: func(ctx context.Context, in *pulumirpc.PluginAttach) (*emptypb.Empty, error) {
attached = true
return &emptypb.Empty{}, nil
},
Configure: func(ctx context.Context, in *pulumirpc.ConfigureRequest) (*pulumirpc.ConfigureResponse, error) {
configured = true
return &pulumirpc.ConfigureResponse{}, nil
},
CheckConfig: func(ctx context.Context, in *pulumirpc.CheckRequest) (*pulumirpc.CheckResponse, error) {
checkedConfig = true
return &pulumirpc.CheckResponse{}, nil
},
Check: func(ctx context.Context, in *pulumirpc.CheckRequest) (*pulumirpc.CheckResponse, error) {
checked = true
return &pulumirpc.CheckResponse{Inputs: in.News}, nil
},
Create: func(ctx context.Context, in *pulumirpc.CreateRequest) (*pulumirpc.CreateResponse, error) {
created = true
return &pulumirpc.CreateResponse{Id: "fake-id", Properties: &structpb.Struct{
Fields: map[string]*structpb.Value{
"url": {
Kind: &structpb.Value_StringValue{StringValue: "fake-url"},
},
},
}}, nil
},
Delete: func(ctx context.Context, in *pulumirpc.DeleteRequest) (*emptypb.Empty, error) {
return &emptypb.Empty{}, nil
},
})))
test.Preview()
test.Up()
assert.True(t, attached, "expected Attach to be called in mock")
assert.True(t, configured, "expected Configure to be called in mock")
assert.True(t, checkedConfig, "expected CheckConfig to be called in mock")
assert.True(t, checked, "expected Check to be called in mock")
assert.True(t, created, "expected Create to be called in mock")
})
}
2 changes: 2 additions & 0 deletions pulumitest/testdata/python_gcp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pyc
venv/
3 changes: 3 additions & 0 deletions pulumitest/testdata/python_gcp/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: yaml_gcp
runtime: python
description: A minimal Google Cloud Pulumi YAML program
6 changes: 6 additions & 0 deletions pulumitest/testdata/python_gcp/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pulumi
import pulumi_gcp as gcp

provider = gcp.Provider("provider")
my_bucket = gcp.storage.Bucket("my-bucket", location="US")
pulumi.export("bucketName", my_bucket.url)
2 changes: 2 additions & 0 deletions pulumitest/testdata/python_gcp/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pulumi>=3.0.0,<4.0.0
pulumi-gcp==7.6.0
4 changes: 2 additions & 2 deletions replay/replay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestReplayNormalizesCheckFailureOrder(t *testing.T) {
{Property: "A", Reason: "A-failed"},
}

p, err := providers.NewProviderMock(context.Background(), providers.ProviderMocks{
p, err := providers.NewProviderMock(providers.ProviderMocks{
CheckConfig: func(ctx context.Context, in *pulumirpc.CheckRequest) (*pulumirpc.CheckResponse, error) {
return &pulumirpc.CheckResponse{Failures: failures}, nil
},
Expand Down Expand Up @@ -124,7 +124,7 @@ func TestReplayNormalizesCheckFailureOrder(t *testing.T) {
}

func TestMatchingErrors(t *testing.T) {
p, err := providers.NewProviderMock(context.Background(), providers.ProviderMocks{
p, err := providers.NewProviderMock(providers.ProviderMocks{
Check: func(ctx context.Context, in *pulumirpc.CheckRequest) (*pulumirpc.CheckResponse, error) {
return &pulumirpc.CheckResponse{}, fmt.Errorf("An error has occurred")
},
Expand Down
Loading