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 (
|
2023-06-05 20:33:23 +00:00
|
|
|
"context"
|
2023-04-14 17:58:09 +00:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/blang/semver"
|
2024-04-25 17:30:30 +00:00
|
|
|
"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 {
|
2023-10-13 14:12:26 +00:00
|
|
|
plugin.UnimplementedProvider
|
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
|
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) {
|
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
|
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",
|
2024-04-25 17:30:30 +00:00
|
|
|
Kind: apitype.ResourcePlugin,
|
2023-04-14 17:58:09 +00:00
|
|
|
Version: semverMustParse("1.0.0"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testProvider := &testProvider{
|
|
|
|
pkg: tokens.Package("provider"),
|
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)
|
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
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-27 08:47:06 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-05-27 08:47:06 +00:00
|
|
|
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)
|
|
|
|
|
2023-06-05 20:33:23 +00:00
|
|
|
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",
|
2024-04-25 17:30:30 +00:00
|
|
|
Kind: apitype.ResourcePlugin,
|
2023-04-14 17:58:09 +00:00
|
|
|
Version: semverMustParse("1.0.0"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testProvider := &testProvider{
|
|
|
|
pkg: tokens.Package("pulumiProvider"),
|
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)
|
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
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-27 08:47:06 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-05-27 08:47:06 +00:00
|
|
|
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)
|
|
|
|
|
2023-06-05 20:33:23 +00:00
|
|
|
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",
|
2024-04-25 17:30:30 +00:00
|
|
|
Kind: apitype.ResourcePlugin,
|
2023-04-14 17:58:09 +00:00
|
|
|
Version: semverMustParse("1.0.0"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-27 08:47:06 +00:00
|
|
|
t.Run("Available to install", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-04-14 17:58:09 +00:00
|
|
|
|
2023-05-27 08:47:06 +00:00
|
|
|
testProvider := &testProvider{
|
|
|
|
pkg: tokens.Package("yetAnotherProvider"),
|
2023-09-21 11:45:07 +00:00
|
|
|
mapping: func(key, provider string) ([]byte, string, error) {
|
2023-05-27 08:47:06 +00:00
|
|
|
assert.Equal(t, "key", key)
|
2023-09-21 11:45:07 +00:00
|
|
|
assert.Equal(t, "", provider)
|
2023-05-27 08:47:06 +00:00
|
|
|
return []byte("data"), "yetAnotherProvider", nil
|
|
|
|
},
|
|
|
|
}
|
2023-04-14 17:58:09 +00:00
|
|
|
|
2023-05-27 08:47:06 +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)
|
|
|
|
|
2023-06-05 20:33:23 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
data, err := mapper.GetMapping(ctx, "yetAnotherProvider", "")
|
2023-05-27 08:47:06 +00:00
|
|
|
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"),
|
2023-09-21 11:45:07 +00:00
|
|
|
mapping: func(key, provider string) ([]byte, string, error) {
|
2023-05-27 08:47:06 +00:00
|
|
|
assert.Equal(t, "key", key)
|
2023-09-21 11:45:07 +00:00
|
|
|
assert.Equal(t, "", provider)
|
2023-05-27 08:47:06 +00:00
|
|
|
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)
|
|
|
|
|
2023-06-05 20:33:23 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
data, err := mapper.GetMapping(ctx, "yetAnotherProvider", "")
|
2023-05-27 08:47:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []byte{}, data)
|
|
|
|
})
|
2023-04-14 17:58:09 +00:00
|
|
|
}
|
2023-04-14 17:58:09 +00:00
|
|
|
|
|
|
|
func TestPluginMapper_UseMatchingNameFirst(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ws := &testWorkspace{
|
|
|
|
infos: []workspace.PluginInfo{
|
|
|
|
{
|
|
|
|
Name: "otherProvider",
|
2024-04-25 17:30:30 +00:00
|
|
|
Kind: apitype.ResourcePlugin,
|
2023-04-14 17:58:09 +00:00
|
|
|
Version: semverMustParse("1.0.0"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "provider",
|
2024-04-25 17:30:30 +00:00
|
|
|
Kind: apitype.ResourcePlugin,
|
2023-04-14 17:58:09 +00:00
|
|
|
Version: semverMustParse("1.0.0"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testProvider := &testProvider{
|
|
|
|
pkg: tokens.Package("provider"),
|
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)
|
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
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-27 08:47:06 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-05-27 08:47:06 +00:00
|
|
|
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)
|
|
|
|
|
2023-06-05 20:33:23 +00:00
|
|
|
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_MappedNamesDifferFromPulumiName(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ws := &testWorkspace{
|
|
|
|
infos: []workspace.PluginInfo{
|
|
|
|
{
|
|
|
|
Name: "pulumiProviderAws",
|
2024-04-25 17:30:30 +00:00
|
|
|
Kind: apitype.ResourcePlugin,
|
2023-04-14 17:58:09 +00:00
|
|
|
Version: semverMustParse("1.0.0"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "pulumiProviderGcp",
|
2024-04-25 17:30:30 +00:00
|
|
|
Kind: apitype.ResourcePlugin,
|
2023-04-14 17:58:09 +00:00
|
|
|
Version: semverMustParse("1.0.0"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testProviderAws := &testProvider{
|
|
|
|
pkg: tokens.Package("pulumiProviderAws"),
|
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)
|
2023-09-21 11:45:07 +00:00
|
|
|
assert.Equal(t, "", provider)
|
2023-04-14 17:58:09 +00:00
|
|
|
return []byte("dataaws"), "aws", nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testProviderGcp := &testProvider{
|
|
|
|
pkg: tokens.Package("pulumiProviderGcp"),
|
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)
|
2023-09-21 11:45:07 +00:00
|
|
|
assert.Equal(t, "", provider)
|
2023-04-14 17:58:09 +00:00
|
|
|
return []byte("datagcp"), "gcp", nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-27 08:47:06 +00:00
|
|
|
providerFactory := func(pkg tokens.Package, version *semver.Version) (plugin.Provider, error) {
|
2023-04-14 17:58:09 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-05-27 08:47:06 +00:00
|
|
|
installPlugin := func(pkg tokens.Package) *semver.Version {
|
2024-01-25 15:17:38 +00:00
|
|
|
// This will want to install the "gcp" package, but we're calling the pulumi name "pulumiProviderGcp"
|
|
|
|
// for this test.
|
2023-05-27 08:47:06 +00:00
|
|
|
assert.Equal(t, "gcp", 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)
|
|
|
|
|
2023-06-05 20:33:23 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-04-14 17:58:09 +00:00
|
|
|
// Get the mapping for the GCP provider.
|
2023-06-05 20:33:23 +00:00
|
|
|
data, err := mapper.GetMapping(ctx, "gcp", "")
|
2023-04-14 17:58:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []byte("datagcp"), data)
|
|
|
|
|
|
|
|
// Now get the mapping for the AWS provider, it should be cached.
|
2023-06-05 20:33:23 +00:00
|
|
|
data, err = mapper.GetMapping(ctx, "aws", "")
|
2023-04-14 17:58:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []byte("dataaws"), data)
|
|
|
|
}
|
2023-05-27 08:47:06 +00:00
|
|
|
|
|
|
|
func TestPluginMapper_MappedNamesDifferFromPulumiNameWithHint(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ws := &testWorkspace{
|
|
|
|
infos: []workspace.PluginInfo{
|
|
|
|
{
|
|
|
|
Name: "pulumiProviderAws",
|
2024-04-25 17:30:30 +00:00
|
|
|
Kind: apitype.ResourcePlugin,
|
2023-05-27 08:47:06 +00:00
|
|
|
Version: semverMustParse("1.0.0"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "pulumiProviderGcp",
|
2024-04-25 17:30:30 +00:00
|
|
|
Kind: apitype.ResourcePlugin,
|
2023-05-27 08:47:06 +00:00
|
|
|
Version: semverMustParse("1.0.0"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testProvider := &testProvider{
|
|
|
|
pkg: tokens.Package("pulumiProviderGcp"),
|
2023-09-21 11:45:07 +00:00
|
|
|
mapping: func(key, provider string) ([]byte, string, error) {
|
2023-05-27 08:47:06 +00:00
|
|
|
assert.Equal(t, "key", key)
|
2023-09-21 11:45:07 +00:00
|
|
|
assert.Equal(t, "", provider)
|
2023-05-27 08:47:06 +00:00
|
|
|
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)
|
|
|
|
|
2023-06-05 20:33:23 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-05-27 08:47:06 +00:00
|
|
|
// Get the mapping for the GCP provider, telling the mapper that it's pulumi name is "pulumiProviderGcp".
|
2023-06-05 20:33:23 +00:00
|
|
|
data, err := mapper.GetMapping(ctx, "gcp", "pulumiProviderGcp")
|
2023-05-27 08:47:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []byte("datagcp"), data)
|
|
|
|
}
|
2023-06-06 08:42:08 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
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",
|
2024-04-25 17:30:30 +00:00
|
|
|
Kind: apitype.ResourcePlugin,
|
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",
|
2024-04-25 17:30:30 +00:00
|
|
|
Kind: apitype.ResourcePlugin,
|
2023-09-21 11:45:07 +00:00
|
|
|
Version: semverMustParse("1.0.0"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "pulumiProviderGcp",
|
2024-04-25 17:30:30 +00:00
|
|
|
Kind: apitype.ResourcePlugin,
|
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)
|
|
|
|
}
|
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",
|
2024-04-25 17:30:30 +00:00
|
|
|
Kind: apitype.ResourcePlugin,
|
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)
|
|
|
|
}
|