pulumi/pkg/codegen/convert/plugin_mapper_test.go

634 lines
18 KiB
Go
Raw Normal View History

2023-04-14 17:58:09 +00:00
// Copyright 2016-2023, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package convert
import (
"context"
2023-04-14 17:58:09 +00:00
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/blang/semver"
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
2023-04-14 17:58:09 +00:00
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
)
type testWorkspace struct {
infos []workspace.PluginInfo
}
func (ws *testWorkspace) GetPlugins() ([]workspace.PluginInfo, error) {
return ws.infos, nil
}
type testProvider struct {
Send old inputs to Delete (#14051) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes https://github.com/pulumi/pulumi/issues/14115. This was missed as part of https://github.com/pulumi/pulumi/pull/13139. Adds a new configure flag (sends_old_inputs_to_delete) which the engine will now always set to true. If that's set providers can rely on the old inputs being sent to delete, otherwise they'll get nil. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-10-13 14:12:26 +00:00
plugin.UnimplementedProvider
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
pkg tokens.Package
mapping func(key, provider string) ([]byte, string, error)
mappings func(key string) ([]string, error)
2023-04-14 17:58:09 +00:00
}
func (prov *testProvider) Pkg() tokens.Package {
return prov.pkg
}
Normalize plugin.Provider methods to (Context, Request) -> (Response, error) (#16302) Normalize methods on plugin.Provider to the form: ```go Method(context.Context, MethodRequest) (MethodResponse, error) ``` This provides a more consistent and forwards compatible interface for each of our methods. --- I'm motivated to work on this because the bridge maintains a copy of this interface: `ProviderWithContext`. This doubles the pain of dealing with any breaking change and this PR would allow me to remove the extra interface. I'm willing to fix consumers of `plugin.Provider` in `pulumi/pulumi`, but I wanted to make sure that we would be willing to merge this PR if I get it green. <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes # (issue) ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [ ] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-06-07 19:47:49 +00:00
func (prov *testProvider) GetMapping(
_ context.Context, req plugin.GetMappingRequest,
) (plugin.GetMappingResponse, error) {
data, provider, err := prov.mapping(req.Key, req.Provider)
return plugin.GetMappingResponse{
Data: data,
Provider: provider,
}, err
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
}
Normalize plugin.Provider methods to (Context, Request) -> (Response, error) (#16302) Normalize methods on plugin.Provider to the form: ```go Method(context.Context, MethodRequest) (MethodResponse, error) ``` This provides a more consistent and forwards compatible interface for each of our methods. --- I'm motivated to work on this because the bridge maintains a copy of this interface: `ProviderWithContext`. This doubles the pain of dealing with any breaking change and this PR would allow me to remove the extra interface. I'm willing to fix consumers of `plugin.Provider` in `pulumi/pulumi`, but I wanted to make sure that we would be willing to merge this PR if I get it green. <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes # (issue) ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [ ] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-06-07 19:47:49 +00:00
func (prov *testProvider) GetMappings(
_ context.Context, req plugin.GetMappingsRequest,
) (plugin.GetMappingsResponse, error) {
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
if prov.mappings == nil {
Normalize plugin.Provider methods to (Context, Request) -> (Response, error) (#16302) Normalize methods on plugin.Provider to the form: ```go Method(context.Context, MethodRequest) (MethodResponse, error) ``` This provides a more consistent and forwards compatible interface for each of our methods. --- I'm motivated to work on this because the bridge maintains a copy of this interface: `ProviderWithContext`. This doubles the pain of dealing with any breaking change and this PR would allow me to remove the extra interface. I'm willing to fix consumers of `plugin.Provider` in `pulumi/pulumi`, but I wanted to make sure that we would be willing to merge this PR if I get it green. <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes # (issue) ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [ ] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-06-07 19:47:49 +00:00
return plugin.GetMappingsResponse{}, nil
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
}
Normalize plugin.Provider methods to (Context, Request) -> (Response, error) (#16302) Normalize methods on plugin.Provider to the form: ```go Method(context.Context, MethodRequest) (MethodResponse, error) ``` This provides a more consistent and forwards compatible interface for each of our methods. --- I'm motivated to work on this because the bridge maintains a copy of this interface: `ProviderWithContext`. This doubles the pain of dealing with any breaking change and this PR would allow me to remove the extra interface. I'm willing to fix consumers of `plugin.Provider` in `pulumi/pulumi`, but I wanted to make sure that we would be willing to merge this PR if I get it green. <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes # (issue) ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [ ] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-06-07 19:47:49 +00:00
keys, err := prov.mappings(req.Key)
return plugin.GetMappingsResponse{Keys: keys}, err
2023-04-14 17:58:09 +00:00
}
func semverMustParse(s string) *semver.Version {
v := semver.MustParse(s)
return &v
}
func TestPluginMapper_InstalledPluginMatches(t *testing.T) {
t.Parallel()
ws := &testWorkspace{
infos: []workspace.PluginInfo{
{
Name: "provider",
Kind: apitype.ResourcePlugin,
2023-04-14 17:58:09 +00:00
Version: semverMustParse("1.0.0"),
},
},
}
testProvider := &testProvider{
pkg: tokens.Package("provider"),
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
mapping: func(key, provider string) ([]byte, string, error) {
2023-04-14 17:58:09 +00:00
assert.Equal(t, "key", key)
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
assert.Equal(t, "", provider)
2023-04-14 17:58:09 +00:00
return []byte("data"), "provider", nil
},
}
providerFactory := func(pkg tokens.Package, version *semver.Version) (plugin.Provider, error) {
2023-04-14 17:58:09 +00:00
assert.Equal(t, pkg, testProvider.pkg, "unexpected package %s", pkg)
return testProvider, nil
}
installPlugin := func(pkg tokens.Package) *semver.Version {
t.Fatal("should not be called")
return nil
}
mapper, err := NewPluginMapper(ws, providerFactory, "key", nil, installPlugin)
2023-04-14 17:58:09 +00:00
assert.NoError(t, err)
assert.NotNil(t, mapper)
ctx := context.Background()
data, err := mapper.GetMapping(ctx, "provider", "")
2023-04-14 17:58:09 +00:00
assert.NoError(t, err)
assert.Equal(t, []byte("data"), data)
}
func TestPluginMapper_MappedNameDiffersFromPulumiName(t *testing.T) {
t.Parallel()
ws := &testWorkspace{
infos: []workspace.PluginInfo{
{
Name: "pulumiProvider",
Kind: apitype.ResourcePlugin,
2023-04-14 17:58:09 +00:00
Version: semverMustParse("1.0.0"),
},
},
}
testProvider := &testProvider{
pkg: tokens.Package("pulumiProvider"),
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
mapping: func(key, provider string) ([]byte, string, error) {
2023-04-14 17:58:09 +00:00
assert.Equal(t, "key", key)
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
assert.Equal(t, "", provider)
2023-04-14 17:58:09 +00:00
return []byte("data"), "otherProvider", nil
},
}
providerFactory := func(pkg tokens.Package, version *semver.Version) (plugin.Provider, error) {
2023-04-14 17:58:09 +00:00
assert.Equal(t, pkg, testProvider.pkg, "unexpected package %s", pkg)
return testProvider, nil
}
installPlugin := func(pkg tokens.Package) *semver.Version {
// GetMapping will try to install "yetAnotherProvider", but for this test were testing the case where
// that doesn't match and can't be installed, but we should still return the mapping because
// "pulumiProvider" is already installed and will return a mapping for "otherProvider".
assert.Equal(t, "otherProvider", string(pkg))
return nil
}
mapper, err := NewPluginMapper(ws, providerFactory, "key", nil, installPlugin)
2023-04-14 17:58:09 +00:00
assert.NoError(t, err)
assert.NotNil(t, mapper)
ctx := context.Background()
data, err := mapper.GetMapping(ctx, "otherProvider", "")
2023-04-14 17:58:09 +00:00
assert.NoError(t, err)
assert.Equal(t, []byte("data"), data)
}
func TestPluginMapper_NoPluginMatches(t *testing.T) {
t.Parallel()
ws := &testWorkspace{
infos: []workspace.PluginInfo{
{
Name: "pulumiProvider",
Kind: apitype.ResourcePlugin,
2023-04-14 17:58:09 +00:00
Version: semverMustParse("1.0.0"),
},
},
}
t.Run("Available to install", func(t *testing.T) {
t.Parallel()
2023-04-14 17:58:09 +00:00
testProvider := &testProvider{
pkg: tokens.Package("yetAnotherProvider"),
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
mapping: func(key, provider string) ([]byte, string, error) {
assert.Equal(t, "key", key)
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
assert.Equal(t, "", provider)
return []byte("data"), "yetAnotherProvider", nil
},
}
2023-04-14 17:58:09 +00:00
providerFactory := func(pkg tokens.Package, version *semver.Version) (plugin.Provider, error) {
assert.Equal(t, pkg, testProvider.pkg, "unexpected package %s", pkg)
return testProvider, nil
}
installPlugin := func(pkg tokens.Package) *semver.Version {
ver := semver.MustParse("1.0.0")
return &ver
}
mapper, err := NewPluginMapper(ws, providerFactory, "key", nil, installPlugin)
assert.NoError(t, err)
assert.NotNil(t, mapper)
ctx := context.Background()
data, err := mapper.GetMapping(ctx, "yetAnotherProvider", "")
assert.NoError(t, err)
assert.Equal(t, []byte("data"), data)
})
t.Run("Not available to install", func(t *testing.T) {
t.Parallel()
testProvider := &testProvider{
pkg: tokens.Package("pulumiProvider"),
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
mapping: func(key, provider string) ([]byte, string, error) {
assert.Equal(t, "key", key)
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
assert.Equal(t, "", provider)
return []byte("data"), "otherProvider", nil
},
}
providerFactory := func(pkg tokens.Package, version *semver.Version) (plugin.Provider, error) {
assert.Equal(t, pkg, testProvider.pkg, "unexpected package %s", pkg)
return testProvider, nil
}
installPlugin := func(pkg tokens.Package) *semver.Version {
// GetMapping will try to install "yetAnotherProvider", but for this test were testing the case
// where that can't be installed
assert.Equal(t, "yetAnotherProvider", string(pkg))
return nil
}
mapper, err := NewPluginMapper(ws, providerFactory, "key", nil, installPlugin)
assert.NoError(t, err)
assert.NotNil(t, mapper)
ctx := context.Background()
data, err := mapper.GetMapping(ctx, "yetAnotherProvider", "")
assert.NoError(t, err)
assert.Equal(t, []byte{}, data)
})
2023-04-14 17:58:09 +00:00
}
func TestPluginMapper_UseMatchingNameFirst(t *testing.T) {
t.Parallel()
ws := &testWorkspace{
infos: []workspace.PluginInfo{
{
Name: "otherProvider",
Kind: apitype.ResourcePlugin,
Version: semverMustParse("1.0.0"),
},
{
Name: "provider",
Kind: apitype.ResourcePlugin,
Version: semverMustParse("1.0.0"),
},
},
}
testProvider := &testProvider{
pkg: tokens.Package("provider"),
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
mapping: func(key, provider string) ([]byte, string, error) {
assert.Equal(t, "key", key)
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
assert.Equal(t, "", provider)
return []byte("data"), "provider", nil
},
}
providerFactory := func(pkg tokens.Package, version *semver.Version) (plugin.Provider, error) {
assert.Equal(t, pkg, testProvider.pkg, "unexpected package %s", pkg)
return testProvider, nil
}
installPlugin := func(pkg tokens.Package) *semver.Version {
t.Fatal("should not be called")
return nil
}
mapper, err := NewPluginMapper(ws, providerFactory, "key", nil, installPlugin)
assert.NoError(t, err)
assert.NotNil(t, mapper)
ctx := context.Background()
data, err := mapper.GetMapping(ctx, "provider", "")
assert.NoError(t, err)
assert.Equal(t, []byte("data"), data)
}
func TestPluginMapper_MappedNamesDifferFromPulumiName(t *testing.T) {
t.Parallel()
ws := &testWorkspace{
infos: []workspace.PluginInfo{
{
Name: "pulumiProviderAws",
Kind: apitype.ResourcePlugin,
Version: semverMustParse("1.0.0"),
},
{
Name: "pulumiProviderGcp",
Kind: apitype.ResourcePlugin,
Version: semverMustParse("1.0.0"),
},
},
}
testProviderAws := &testProvider{
pkg: tokens.Package("pulumiProviderAws"),
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
mapping: func(key, provider string) ([]byte, string, error) {
assert.Equal(t, "key", key)
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
assert.Equal(t, "", provider)
return []byte("dataaws"), "aws", nil
},
}
testProviderGcp := &testProvider{
pkg: tokens.Package("pulumiProviderGcp"),
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
mapping: func(key, provider string) ([]byte, string, error) {
assert.Equal(t, "key", key)
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
assert.Equal(t, "", provider)
return []byte("datagcp"), "gcp", nil
},
}
providerFactory := func(pkg tokens.Package, version *semver.Version) (plugin.Provider, error) {
if pkg == testProviderAws.pkg {
return testProviderAws, nil
} else if pkg == testProviderGcp.pkg {
return testProviderGcp, nil
}
assert.Fail(t, "unexpected package %s", pkg)
return nil, fmt.Errorf("unexpected package %s", pkg)
}
installPlugin := func(pkg tokens.Package) *semver.Version {
// This will want to install the "gcp" package, but we're calling the pulumi name "pulumiProviderGcp"
// for this test.
assert.Equal(t, "gcp", string(pkg))
return nil
}
mapper, err := NewPluginMapper(ws, providerFactory, "key", nil, installPlugin)
assert.NoError(t, err)
assert.NotNil(t, mapper)
ctx := context.Background()
// Get the mapping for the GCP provider.
data, err := mapper.GetMapping(ctx, "gcp", "")
assert.NoError(t, err)
assert.Equal(t, []byte("datagcp"), data)
// Now get the mapping for the AWS provider, it should be cached.
data, err = mapper.GetMapping(ctx, "aws", "")
assert.NoError(t, err)
assert.Equal(t, []byte("dataaws"), data)
}
func TestPluginMapper_MappedNamesDifferFromPulumiNameWithHint(t *testing.T) {
t.Parallel()
ws := &testWorkspace{
infos: []workspace.PluginInfo{
{
Name: "pulumiProviderAws",
Kind: apitype.ResourcePlugin,
Version: semverMustParse("1.0.0"),
},
{
Name: "pulumiProviderGcp",
Kind: apitype.ResourcePlugin,
Version: semverMustParse("1.0.0"),
},
},
}
testProvider := &testProvider{
pkg: tokens.Package("pulumiProviderGcp"),
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
mapping: func(key, provider string) ([]byte, string, error) {
assert.Equal(t, "key", key)
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
assert.Equal(t, "", provider)
return []byte("datagcp"), "gcp", nil
},
}
providerFactory := func(pkg tokens.Package, version *semver.Version) (plugin.Provider, error) {
assert.Equal(t, pkg, testProvider.pkg, "unexpected package %s", pkg)
return testProvider, nil
}
installPlugin := func(pkg tokens.Package) *semver.Version {
t.Fatal("should not be called")
return nil
}
mapper, err := NewPluginMapper(ws, providerFactory, "key", nil, installPlugin)
assert.NoError(t, err)
assert.NotNil(t, mapper)
ctx := context.Background()
// Get the mapping for the GCP provider, telling the mapper that it's pulumi name is "pulumiProviderGcp".
data, err := mapper.GetMapping(ctx, "gcp", "pulumiProviderGcp")
assert.NoError(t, err)
assert.Equal(t, []byte("datagcp"), data)
}
// Regression test for https://github.com/pulumi/pulumi/issues/13105
func TestPluginMapper_MissingProviderOnlyTriesToInstallOnce(t *testing.T) {
t.Parallel()
ws := &testWorkspace{}
providerFactory := func(pkg tokens.Package, version *semver.Version) (plugin.Provider, error) {
t.Fatal("should not be called")
return nil, nil
}
called := 0
installPlugin := func(pkg tokens.Package) *semver.Version {
called++
assert.Equal(t, "pulumiProviderGcp", string(pkg))
return nil
}
mapper, err := NewPluginMapper(ws, providerFactory, "key", nil, installPlugin)
assert.NoError(t, err)
assert.NotNil(t, mapper)
ctx := context.Background()
// Try to get the mapping for the GCP provider, telling the mapper that it's pulumi name is "pulumiProviderGcp".
data, err := mapper.GetMapping(ctx, "gcp", "pulumiProviderGcp")
assert.NoError(t, err)
assert.Equal(t, []byte{}, data)
// Try and get the mapping again
data, err = mapper.GetMapping(ctx, "gcp", "pulumiProviderGcp")
assert.NoError(t, err)
assert.Equal(t, []byte{}, data)
// Install should have only been called once
assert.Equal(t, 1, called)
}
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
func TestPluginMapper_GetMappingsIsUsed(t *testing.T) {
t.Parallel()
// Test that if the provider supports it that GetMappings is used, and will fetch multiple mappings from the same
// provider.
ws := &testWorkspace{
infos: []workspace.PluginInfo{
{
Name: "pulumiProviderK8s",
Kind: apitype.ResourcePlugin,
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
Version: semverMustParse("1.0.0"),
},
},
}
var mappingCalls []string
testProvider := &testProvider{
pkg: tokens.Package("pulumiProviderK8s"),
mapping: func(key, provider string) ([]byte, string, error) {
mappingCalls = append(mappingCalls, provider)
assert.Equal(t, "key", key)
if provider == "kubernetes" {
return []byte("datakubernetes"), "kubernetes", nil
} else if provider == "helm" {
return []byte("datahelm"), "helm", nil
}
return nil, "", fmt.Errorf("unexpected provider key %s", provider)
},
mappings: func(key string) ([]string, error) {
assert.Equal(t, "key", key)
return []string{"kubernetes", "helm"}, nil
},
}
providerFactory := func(pkg tokens.Package, version *semver.Version) (plugin.Provider, error) {
assert.Equal(t, pkg, testProvider.pkg, "unexpected package %s", pkg)
return testProvider, nil
}
installPlugin := func(pkg tokens.Package) *semver.Version {
assert.Contains(t, []string{"kubernetes", "helm"}, string(pkg))
return nil
}
mapper, err := NewPluginMapper(ws, providerFactory, "key", nil, installPlugin)
assert.NoError(t, err)
assert.NotNil(t, mapper)
ctx := context.Background()
// Get the mapping for the kubernetes provider.
data, err := mapper.GetMapping(ctx, "kubernetes", "")
assert.NoError(t, err)
assert.Equal(t, []byte("datakubernetes"), data)
// This should only have called getMapping once
assert.Equal(t, []string{"kubernetes"}, mappingCalls)
// Now get the mapping for the helm provider.
data, err = mapper.GetMapping(ctx, "helm", "")
assert.NoError(t, err)
assert.Equal(t, []byte("datahelm"), data)
// This should have called getMapping again
assert.Equal(t, []string{"kubernetes", "helm"}, mappingCalls)
}
func TestPluginMapper_GetMappingIsntCalledOnValidMappings(t *testing.T) {
t.Parallel()
// Test that if the provider supports GetMappings that we don't call GetMapping("") on it.
ws := &testWorkspace{
infos: []workspace.PluginInfo{
{
Name: "pulumiProviderAws",
Kind: apitype.ResourcePlugin,
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
Version: semverMustParse("1.0.0"),
},
{
Name: "pulumiProviderGcp",
Kind: apitype.ResourcePlugin,
More efficent mapping lookup (#13975) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Inspired by a comment from Zaid. This allows providers to return what providers they have mapping information for without having to marshal all their mapping data to the engine at the same time, this could save transmitting a lot of data that the engine might not ever need (for example if it's not converting code for that specific provider). It also allows provider to support mulitple mappings. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-21 11:45:07 +00:00
Version: semverMustParse("1.0.0"),
},
},
}
testProviderAws := &testProvider{
pkg: tokens.Package("pulumiProviderAws"),
mapping: func(key, provider string) ([]byte, string, error) {
assert.Equal(t, "key", key)
assert.Equal(t, "aws", provider)
return []byte("dataaws"), "aws", nil
},
mappings: func(key string) ([]string, error) {
assert.Equal(t, "key", key)
return []string{"aws"}, nil
},
}
testProviderGcp := &testProvider{
pkg: tokens.Package("pulumiProviderGcp"),
mapping: func(key, provider string) ([]byte, string, error) {
assert.Equal(t, "key", key)
assert.Equal(t, "", provider)
return []byte("datagcp"), "gcp", nil
},
}
providerFactory := func(pkg tokens.Package, version *semver.Version) (plugin.Provider, error) {
if pkg == testProviderAws.pkg {
return testProviderAws, nil
} else if pkg == testProviderGcp.pkg {
return testProviderGcp, nil
}
assert.Fail(t, "unexpected package %s", pkg)
return nil, fmt.Errorf("unexpected package %s", pkg)
}
installPlugin := func(pkg tokens.Package) *semver.Version {
assert.Contains(t, []string{"aws", "gcp"}, string(pkg))
return nil
}
mapper, err := NewPluginMapper(ws, providerFactory, "key", nil, installPlugin)
assert.NoError(t, err)
assert.NotNil(t, mapper)
ctx := context.Background()
// Get the mapping for the GCP provider.
data, err := mapper.GetMapping(ctx, "gcp", "")
assert.NoError(t, err)
assert.Equal(t, []byte("datagcp"), data)
// Now get the mapping for the AWS provider.
data, err = mapper.GetMapping(ctx, "aws", "")
assert.NoError(t, err)
assert.Equal(t, []byte("dataaws"), data)
}
Fix infinite loop bug in plugin_mapper (#15200) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Found by @GeoffMillerAZ a while back. Just tracked down exactly what caused it. The test added hangs if ran on the current version of the code. Issue was we used to assume that we'd only stop looking for plugins once there weren't any more plugins to look at. But then we added `GetMappings` so we could see which providers a plugin mapped to up-front. So the check needs to actually be stop looking when there's no plugins _or_ when all the plugins left explicitly say they aren't for the current lookup. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-01-23 09:11:33 +00:00
func TestPluginMapper_InfiniteLoopRegression(t *testing.T) {
t.Parallel()
// Test that the mapping loop doesn't end up in an infinite loop in some cases where no mapping is found.
ws := &testWorkspace{
infos: []workspace.PluginInfo{
{
Name: "pulumiProviderAws",
Kind: apitype.ResourcePlugin,
Fix infinite loop bug in plugin_mapper (#15200) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Found by @GeoffMillerAZ a while back. Just tracked down exactly what caused it. The test added hangs if ran on the current version of the code. Issue was we used to assume that we'd only stop looking for plugins once there weren't any more plugins to look at. But then we added `GetMappings` so we could see which providers a plugin mapped to up-front. So the check needs to actually be stop looking when there's no plugins _or_ when all the plugins left explicitly say they aren't for the current lookup. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-01-23 09:11:33 +00:00
Version: semverMustParse("1.0.0"),
},
},
}
testProviderAws := &testProvider{
pkg: tokens.Package("pulumiProviderAws"),
mapping: func(key, provider string) ([]byte, string, error) {
assert.Equal(t, "key", key)
assert.Equal(t, "aws", provider)
return []byte("dataaws"), "aws", nil
},
mappings: func(key string) ([]string, error) {
assert.Equal(t, "key", key)
return []string{"aws"}, nil
},
}
providerFactory := func(pkg tokens.Package, version *semver.Version) (plugin.Provider, error) {
if pkg == testProviderAws.pkg {
return testProviderAws, nil
}
assert.Fail(t, "unexpected package %s", pkg)
return nil, fmt.Errorf("unexpected package %s", pkg)
}
installPlugin := func(pkg tokens.Package) *semver.Version {
assert.Contains(t, []string{"gcp"}, string(pkg))
return nil
}
mapper, err := NewPluginMapper(ws, providerFactory, "key", nil, installPlugin)
assert.NoError(t, err)
assert.NotNil(t, mapper)
ctx := context.Background()
// Get the mapping for the GCP provider, which we don't have a plugin for.
data, err := mapper.GetMapping(ctx, "gcp", "")
assert.NoError(t, err)
assert.Equal(t, []byte{}, data)
}