pulumi/pkg/codegen/convert/plugin_mapper_test.go

571 lines
16 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/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
}
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 (prov *testProvider) GetMapping(key, provider string) ([]byte, string, error) {
return prov.mapping(key, provider)
}
func (prov *testProvider) GetMappings(key string) ([]string, error) {
if prov.mappings == nil {
return nil, nil
}
return prov.mappings(key)
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: workspace.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) {
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: workspace.ResourcePlugin,
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: workspace.ResourcePlugin,
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: workspace.ResourcePlugin,
Version: semverMustParse("1.0.0"),
},
{
Name: "provider",
Kind: workspace.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: workspace.ResourcePlugin,
Version: semverMustParse("1.0.0"),
},
{
Name: "pulumiProviderGcp",
Kind: workspace.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: workspace.ResourcePlugin,
Version: semverMustParse("1.0.0"),
},
{
Name: "pulumiProviderGcp",
Kind: workspace.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: workspace.ResourcePlugin,
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: workspace.ResourcePlugin,
Version: semverMustParse("1.0.0"),
},
{
Name: "pulumiProviderGcp",
Kind: workspace.ResourcePlugin,
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)
}